From 82b797738a5221a1d81aa1e6f9c94624ff0429c4 Mon Sep 17 00:00:00 2001 From: tophf Date: Mon, 14 May 2018 08:46:22 +0300 Subject: [PATCH] use Alarms API since js timers are limited to 596 hours fixes #388 --- background/update.js | 17 ++++++++++++++--- manifest.json | 1 + 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/background/update.js b/background/update.js index 29b8359f..633a33d3 100644 --- a/background/update.js +++ b/background/update.js @@ -23,6 +23,9 @@ global API_METHODS ERROR_VERSION: 'error: version is older than installed style', }; + const ALARM_NAME = 'scheduledUpdate'; + const MIN_INTERVAL_MS = 60e3; + let lastUpdateTime = parseInt(localStorage.lastUpdateTime) || Date.now(); let checkingAll = false; let logQueue = []; @@ -207,14 +210,22 @@ global API_METHODS function schedule() { const interval = prefs.get('updateInterval') * 60 * 60 * 1000; - if (interval) { + if (interval > 0) { const elapsed = Math.max(0, Date.now() - lastUpdateTime); - debounce(checkAllStyles, Math.max(10e3, interval - elapsed)); + chrome.alarms.create(ALARM_NAME, { + when: Date.now() + Math.max(MIN_INTERVAL_MS, interval - elapsed), + }); + chrome.alarms.onAlarm.addListener(onAlarm); } else { - debounce.unregister(checkAllStyles); + chrome.alarms.clear(ALARM_NAME, ignoreChromeError); + chrome.alarms.onAlarm.removeListener(onAlarm); } } + function onAlarm({name}) { + if (name === ALARM_NAME) checkAllStyles(); + } + function resetInterval() { localStorage.lastUpdateTime = lastUpdateTime = Date.now(); schedule(); diff --git a/manifest.json b/manifest.json index 75981db0..2ede9c09 100644 --- a/manifest.json +++ b/manifest.json @@ -16,6 +16,7 @@ "webNavigation", "contextMenus", "storage", + "alarms", "" ], "background": {