mirror of
https://github.com/gorhill/uBlock.git
synced 2024-11-10 01:02:08 +01:00
Start using browser.alarms instead of setTimeout() where applicable
Related documentation: https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Background_scripts#convert_to_non-persistent
This commit is contained in:
parent
66c70cf746
commit
bec6cad2c0
5 changed files with 65 additions and 25 deletions
|
@ -82,6 +82,7 @@
|
|||
"open_in_tab": true
|
||||
},
|
||||
"permissions": [
|
||||
"alarms",
|
||||
"contextMenus",
|
||||
"privacy",
|
||||
"storage",
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -90,6 +90,7 @@
|
|||
"open_in_tab": true
|
||||
},
|
||||
"permissions": [
|
||||
"alarms",
|
||||
"dns",
|
||||
"menus",
|
||||
"privacy",
|
||||
|
|
|
@ -78,6 +78,7 @@
|
|||
"name": "uBlock Origin",
|
||||
"options_page": "dashboard.html",
|
||||
"permissions": [
|
||||
"alarms",
|
||||
"contextMenus",
|
||||
"privacy",
|
||||
"storage",
|
||||
|
|
|
@ -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 };
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
|
Loading…
Reference in a new issue