diff --git a/src/js/static-net-filtering.js b/src/js/static-net-filtering.js index b67213ff1..3bc743248 100644 --- a/src/js/static-net-filtering.js +++ b/src/js/static-net-filtering.js @@ -26,6 +26,7 @@ /******************************************************************************/ import globals from './globals.js'; +import { queueTask, dropTask } from './tasks.js'; import HNTrieContainer from './hntrie.js'; import { sparseBase64 } from './base64-custom.js'; import { BidiTrieContainer } from './biditrie.js'; @@ -3544,7 +3545,7 @@ const FilterContainer = function() { this.selfieVersion = '1'; this.MAX_TOKEN_LENGTH = MAX_TOKEN_LENGTH; - this.optimizeTimerId = undefined; + this.optimizeTaskId = undefined; // As long as CategoryCount is reasonably low, we will use an array to // store buckets using category bits as index. If ever CategoryCount // becomes too large, we can just go back to using a Map. @@ -3590,9 +3591,9 @@ FilterContainer.prototype.reset = function() { filterSequenceWritePtr = FILTER_SEQUENCES_MIN; // Cancel potentially pending optimization run. - if ( this.optimizeTimerId !== undefined ) { - globals.cancelIdleCallback(this.optimizeTimerId); - this.optimizeTimerId = undefined; + if ( this.optimizeTaskId !== undefined ) { + dropTask(this.optimizeTaskId); + this.optimizeTaskId = undefined; } // Runtime registers @@ -3690,20 +3691,20 @@ FilterContainer.prototype.freeze = function() { // Optimizing is not critical for the static network filtering engine to // work properly, so defer this until later to allow for reduced delay to // readiness when no valid selfie is available. - if ( this.optimizeTimerId === undefined ) { - this.optimizeTimerId = globals.requestIdleCallback(( ) => { - this.optimizeTimerId = undefined; + if ( this.optimizeTaskId === undefined ) { + this.optimizeTaskId = queueTask(( ) => { + this.optimizeTaskId = undefined; this.optimize(); - }, { timeout: 5000 }); + }); } }; /******************************************************************************/ FilterContainer.prototype.optimize = function() { - if ( this.optimizeTimerId !== undefined ) { - globals.cancelIdleCallback(this.optimizeTimerId); - this.optimizeTimerId = undefined; + if ( this.optimizeTaskId !== undefined ) { + dropTask(this.optimizeTaskId); + this.optimizeTaskId = undefined; } for ( let bits = 0, n = this.categories.length; bits < n; bits++ ) { diff --git a/src/js/tasks.js b/src/js/tasks.js new file mode 100644 index 000000000..c06010d43 --- /dev/null +++ b/src/js/tasks.js @@ -0,0 +1,42 @@ +/******************************************************************************* + + uBlock Origin - a browser extension to block requests. + Copyright (C) 2014-present Raymond Hill + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see {http://www.gnu.org/licenses/}. + + Home: https://github.com/gorhill/uBlock +*/ + +/* globals requestIdleCallback, cancelIdleCallback */ + +'use strict'; + +/******************************************************************************/ + +export function queueTask(func) { + if ( typeof requestIdleCallback === 'undefined' ) { + return setTimeout(func, 1); + } + + return requestIdleCallback(func, { timeout: 5000 }); +} + +export function dropTask(id) { + if ( typeof cancelIdleCallback === 'undefined' ) { + return clearTimeout(id); + } + + return cancelIdleCallback(id); +} diff --git a/tools/make-nodejs.sh b/tools/make-nodejs.sh index ad6a92f8c..874b2b3a4 100755 --- a/tools/make-nodejs.sh +++ b/tools/make-nodejs.sh @@ -17,6 +17,7 @@ cp src/js/hntrie.js $DES/js cp src/js/static-filtering-parser.js $DES/js cp src/js/static-net-filtering.js $DES/js cp src/js/static-filtering-io.js $DES/js +cp src/js/tasks.js $DES/js cp src/js/text-utils.js $DES/js cp src/js/uri-utils.js $DES/js cp src/js/url-net-filtering.js $DES/js