1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-07-08 12:57:57 +02:00

Replace requestIdleCallback() with new vAPI.defer utility

Related commit:
- 91f9795023
This commit is contained in:
Raymond Hill 2023-04-10 10:10:19 -04:00
parent 3b14fd915a
commit 63d0f124ba
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
2 changed files with 13 additions and 22 deletions

View File

@ -81,17 +81,17 @@ vAPI.defer = {
this.onraf(...args); this.onraf(...args);
} }
} }
onidle(delay, ...args) { onidle(delay, options, ...args) {
if ( this.timer !== null ) { return; } if ( this.timer !== null ) { return; }
const delayInMs = vAPI.defer.normalizeDelay(delay); const delayInMs = vAPI.defer.normalizeDelay(delay);
if ( delayInMs !== 0 ) { if ( delayInMs !== 0 ) {
this.type = 0; this.type = 0;
this.timer = vAPI.setTimeout(( ) => { this.timer = vAPI.setTimeout(( ) => {
this.timer = null; this.timer = null;
this.onric(...args); this.onric(options, ...args);
}, delayInMs); }, delayInMs);
} else { } else {
this.onric(...args); this.onric(options, ...args);
} }
} }
off() { off() {
@ -119,13 +119,13 @@ vAPI.defer = {
this.callback(...args); this.callback(...args);
}); });
} }
onric(...args) { onric(options, ...args) {
if ( this.timer !== null ) { return; } if ( this.timer !== null ) { return; }
this.type = 2; this.type = 2;
this.timer = self.requestIdleCallback(deadline => { this.timer = self.requestIdleCallback(deadline => {
this.timer = null; this.timer = null;
this.callback(deadline, ...args); this.callback(deadline, ...args);
}); }, options);
} }
ongoing() { ongoing() {
return this.timer !== null; return this.timer !== null;

View File

@ -26,7 +26,6 @@
const StaticExtFilteringHostnameDB = class { const StaticExtFilteringHostnameDB = class {
constructor(nBits, selfie = undefined) { constructor(nBits, selfie = undefined) {
this.nBits = nBits; this.nBits = nBits;
this.timer = undefined;
this.strToIdMap = new Map(); this.strToIdMap = new Map();
this.hostnameToSlotIdMap = new Map(); this.hostnameToSlotIdMap = new Map();
this.regexToSlotIdMap = new Map(); this.regexToSlotIdMap = new Map();
@ -39,6 +38,9 @@ const StaticExtFilteringHostnameDB = class {
if ( selfie !== undefined ) { if ( selfie !== undefined ) {
this.fromSelfie(selfie); this.fromSelfie(selfie);
} }
this.cleanupTimer = vAPI.defer.create(( ) => {
this.strToIdMap.clear();
});
} }
store(hn, bits, s) { store(hn, bits, s) {
@ -48,7 +50,7 @@ const StaticExtFilteringHostnameDB = class {
iStr = this.strSlots.length; iStr = this.strSlots.length;
this.strSlots.push(s); this.strSlots.push(s);
this.strToIdMap.set(s, iStr); this.strToIdMap.set(s, iStr);
if ( this.timer === undefined ) { if ( this.cleanupTimer.ongoing() === false ) {
this.collectGarbage(true); this.collectGarbage(true);
} }
} }
@ -85,22 +87,11 @@ const StaticExtFilteringHostnameDB = class {
} }
collectGarbage(later = false) { collectGarbage(later = false) {
if ( later === false ) { if ( later ) {
if ( this.timer !== undefined ) { return this.cleanupTimer.onidle(5000, { timeout: 5000 });
self.cancelIdleCallback(this.timer);
this.timer = undefined;
}
this.strToIdMap.clear();
return;
} }
if ( this.timer !== undefined ) { return; } this.cleanupTimer.off();
this.timer = self.requestIdleCallback( this.strToIdMap.clear();
( ) => {
this.timer = undefined;
this.strToIdMap.clear();
},
{ timeout: 5000 }
);
} }
// modifiers = 0: all items // modifiers = 0: all items