use Alarms API since js timers are limited to 596 hours

fixes #388
This commit is contained in:
tophf 2018-05-14 08:46:22 +03:00
parent d149ab662c
commit 82b797738a
2 changed files with 15 additions and 3 deletions

View File

@ -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();

View File

@ -16,6 +16,7 @@
"webNavigation",
"contextMenus",
"storage",
"alarms",
"<all_urls>"
],
"background": {