1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-07-05 11:37:01 +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);
}
}
onidle(delay, ...args) {
onidle(delay, options, ...args) {
if ( this.timer !== null ) { return; }
const delayInMs = vAPI.defer.normalizeDelay(delay);
if ( delayInMs !== 0 ) {
this.type = 0;
this.timer = vAPI.setTimeout(( ) => {
this.timer = null;
this.onric(...args);
this.onric(options, ...args);
}, delayInMs);
} else {
this.onric(...args);
this.onric(options, ...args);
}
}
off() {
@ -119,13 +119,13 @@ vAPI.defer = {
this.callback(...args);
});
}
onric(...args) {
onric(options, ...args) {
if ( this.timer !== null ) { return; }
this.type = 2;
this.timer = self.requestIdleCallback(deadline => {
this.timer = null;
this.callback(deadline, ...args);
});
}, options);
}
ongoing() {
return this.timer !== null;

View File

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