Skip to content

Commit

Permalink
Start using browser.alarms instead of setTimeout() where applicable
Browse files Browse the repository at this point in the history
  • Loading branch information
gorhill committed Apr 8, 2023
1 parent 66c70cf commit bec6cad
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 25 deletions.
1 change: 1 addition & 0 deletions platform/chromium/manifest.json
Expand Up @@ -82,6 +82,7 @@
"open_in_tab": true
},
"permissions": [
"alarms",
"contextMenus",
"privacy",
"storage",
Expand Down
55 changes: 55 additions & 0 deletions platform/common/vapi-background.js
Expand Up @@ -95,6 +95,61 @@ vAPI.storage = webext.storage.local;
/******************************************************************************/
/******************************************************************************/

vAPI.alarms = {
create(callback) {
this.uniqueIdGenerator += 1;
const name = this.uniqueIdGenerator.toString(36);
const client = new this.Client(name, callback);
this.clientMap.set(name, client);
return client;
},
Client: class {
constructor(name, callback) {
this.name = name;
this.callback = callback;
}
on(delay) {
const delayInMinutes = this.normalizeDelay(delay);
browser.alarms.get(this.name, alarm => {
if ( alarm ) { return; }
return browser.alarms.create(this.name, { delayInMinutes });
});
}
offon(delay) {
const delayInMinutes = this.normalizeDelay(delay);
return browser.alarms.create(this.name, { delayInMinutes });
}
off() {
return browser.alarms.clear(this.name);
}
normalizeDelay(delay) {
let delayInMinutes = 0;
if ( typeof delay === 'number' ) {
delayInMinutes = delay;
} else if ( typeof delay === 'object' ) {
if ( delay.sec !== undefined ) {
delayInMinutes = delay.sec / 60;
} else if ( delay.ms !== undefined ) {
delayInMinutes = delay.ms / 60000;
}
}
return Math.max(delayInMinutes, 1);
}
},
onAlarm(alarm) {
const client = this.clientMap.get(alarm.name);
if ( client === undefined ) { return; }
client.callback(alarm);
},
clientMap: new Map(),
uniqueIdGenerator: 1000000,
};

browser.alarms.onAlarm.addListener(alarm => vAPI.alarms.onAlarm(alarm));

/******************************************************************************/
/******************************************************************************/

// https://github.com/gorhill/uMatrix/issues/234
// https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/privacy/network

Expand Down
1 change: 1 addition & 0 deletions platform/firefox/manifest.json
Expand Up @@ -90,6 +90,7 @@
"open_in_tab": true
},
"permissions": [
"alarms",
"dns",
"menus",
"privacy",
Expand Down
1 change: 1 addition & 0 deletions platform/opera/manifest.json
Expand Up @@ -78,6 +78,7 @@
"name": "uBlock Origin",
"options_page": "dashboard.html",
"permissions": [
"alarms",
"contextMenus",
"privacy",
"storage",
Expand Down
32 changes: 7 additions & 25 deletions src/js/storage.js
Expand Up @@ -1276,35 +1276,17 @@ self.addEventListener('hiddenSettingsChanged', ( ) => {
return false;
};

let createTimer;
let destroyTimer;

const destroy = function() {
io.remove(/^selfie\//);
µb.selfieIsInvalid = true;
createTimer = vAPI.setTimeout(( ) => {
createTimer = undefined;
create();
}, µb.hiddenSettings.selfieAfter * 60000);
};

const destroyAsync = function() {
if ( destroyTimer !== undefined ) { return; }
if ( createTimer !== undefined ) {
clearTimeout(createTimer);
createTimer = undefined;
if ( µb.selfieIsInvalid === false ) {
io.remove(/^selfie\//);
µb.selfieIsInvalid = true;
}
destroyTimer = vAPI.setTimeout(
( ) => {
destroyTimer = undefined;
destroy();
},
1019
);
µb.selfieIsInvalid = true;
createAlarm.offon(µb.hiddenSettings.selfieAfter);
};

µb.selfieManager = { load, destroy: destroyAsync };
const createAlarm = vAPI.alarms.create(create);

µb.selfieManager = { load, destroy };
}

/******************************************************************************/
Expand Down

0 comments on commit bec6cad

Please sign in to comment.