From 63d0f124ba46d966514eca18d44b355fd4940441 Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Mon, 10 Apr 2023 10:10:19 -0400 Subject: [PATCH] Replace requestIdleCallback() with new vAPI.defer utility Related commit: - https://github.com/gorhill/uBlock/commit/91f9795023de45dcee17567d3faef8c65335e2e0 --- platform/common/vapi-common.js | 10 +++++----- src/js/static-ext-filtering-db.js | 25 ++++++++----------------- 2 files changed, 13 insertions(+), 22 deletions(-) diff --git a/platform/common/vapi-common.js b/platform/common/vapi-common.js index e5c09ad2b..8c46dbef2 100644 --- a/platform/common/vapi-common.js +++ b/platform/common/vapi-common.js @@ -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; diff --git a/src/js/static-ext-filtering-db.js b/src/js/static-ext-filtering-db.js index 010465c71..07638b96d 100644 --- a/src/js/static-ext-filtering-db.js +++ b/src/js/static-ext-filtering-db.js @@ -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