From bfcd30efcf4df28fd53831fa73f907cf973e5d47 Mon Sep 17 00:00:00 2001 From: Ximi1970 Date: Fri, 3 Apr 2020 17:45:20 +0200 Subject: [PATCH] use new poll timing --- webext/background.js | 36 +++++++++++++++++++++++++++++++++++- webext/js/defaults.js | 18 ++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/webext/background.js b/webext/background.js index 6feec47..adc14ad 100644 --- a/webext/background.js +++ b/webext/background.js @@ -1,6 +1,11 @@ var SysTrayX = { debugAccounts: false, + pollTiming: { + pollStartupDelay: "5", + pollInterval: "5" + }, + platformInfo: undefined }; @@ -13,6 +18,8 @@ SysTrayX.Messaging = { init: function() { // Get the accounts from the storage SysTrayX.Messaging.getAccounts(); + + // Lookout for storage changes browser.storage.onChanged.addListener(SysTrayX.Messaging.storageChanged); // Send the window title to app @@ -22,7 +29,11 @@ SysTrayX.Messaging = { SysTrayX.Messaging.sendPreferences(); // this.unReadMessages(this.unreadFiltersTest).then(this.unreadCb); - window.setInterval(SysTrayX.Messaging.pollAccounts, 1000); + // window.setInterval(SysTrayX.Messaging.pollAccounts, 1000); + window.setTimeout( + SysTrayX.Messaging.pollAccounts, + SysTrayX.pollTiming.pollStartupDelay * 1000 + ); // Try to catch the window state browser.windows.onFocusChanged.addListener(SysTrayX.Window.focusChanged); @@ -35,6 +46,20 @@ SysTrayX.Messaging = { // Get the new preferences SysTrayX.Messaging.getAccounts(); + if ("pollStartupDelay" in changes && changes["pollStartupDelay"].newValue) { + SysTrayX.pollTiming = { + ...SysTrayX.pollTiming, + pollStartupDelay: changes["pollStartupDelay"].newValue + }; + } + + if ("pollInterval" in changes && changes["pollInterval"].newValue) { + SysTrayX.pollTiming = { + ...SysTrayX.pollTiming, + pollInterval: changes["pollInterval"].newValue + }; + } + if ("addonprefchanged" in changes && changes["addonprefchanged"].newValue) { // // Send new preferences to the app @@ -73,6 +98,12 @@ SysTrayX.Messaging = { SysTrayX.Messaging.unreadCb ); } + + // Next round... + window.setTimeout( + SysTrayX.Messaging.pollAccounts, + SysTrayX.pollTiming.pollInterval * 1000 + ); }, // @@ -315,6 +346,9 @@ async function start() { }); } + // Get the poll timing + SysTrayX.pollTiming = await getPollTiming(); + // Set platform SysTrayX.platformInfo = await browser.runtime .getPlatformInfo() diff --git a/webext/js/defaults.js b/webext/js/defaults.js index 0d1313e..b79aa90 100644 --- a/webext/js/defaults.js +++ b/webext/js/defaults.js @@ -67,3 +67,21 @@ async function getStartupState() { const getState = browser.storage.sync.get("startMinimized"); return await getState.then(getStartupState, onStartupStateError); } + +// +// Get poll timing +// +async function getPollTiming() { + function getDelayAndInterval(result) { + return { pollStartupDelay: result.pollStartupDelay || "5", pollInterval: result.pollInterval || "5" }; + } + + function onDelayAndIntervalError() { + return { pollStartupDelay: "5", pollInterval: "5" }; + } + + const getTiming = browser.storage.sync.get([ + "pollStartupDelay", + "pollInterval"]); + return await getTiming.then(getDelayAndInterval, onDelayAndIntervalError); +}