Add tasks.js module (#3839)

This commit is contained in:
Manish Jethani 2021-08-19 16:49:20 +05:30 committed by GitHub
parent 4c1c6309b3
commit b19393d8dc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 55 additions and 11 deletions

View File

@ -26,6 +26,7 @@
/******************************************************************************/ /******************************************************************************/
import globals from './globals.js'; import globals from './globals.js';
import { queueTask, dropTask } from './tasks.js';
import HNTrieContainer from './hntrie.js'; import HNTrieContainer from './hntrie.js';
import { sparseBase64 } from './base64-custom.js'; import { sparseBase64 } from './base64-custom.js';
import { BidiTrieContainer } from './biditrie.js'; import { BidiTrieContainer } from './biditrie.js';
@ -3544,7 +3545,7 @@ const FilterContainer = function() {
this.selfieVersion = '1'; this.selfieVersion = '1';
this.MAX_TOKEN_LENGTH = MAX_TOKEN_LENGTH; 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 // As long as CategoryCount is reasonably low, we will use an array to
// store buckets using category bits as index. If ever CategoryCount // store buckets using category bits as index. If ever CategoryCount
// becomes too large, we can just go back to using a Map. // becomes too large, we can just go back to using a Map.
@ -3590,9 +3591,9 @@ FilterContainer.prototype.reset = function() {
filterSequenceWritePtr = FILTER_SEQUENCES_MIN; filterSequenceWritePtr = FILTER_SEQUENCES_MIN;
// Cancel potentially pending optimization run. // Cancel potentially pending optimization run.
if ( this.optimizeTimerId !== undefined ) { if ( this.optimizeTaskId !== undefined ) {
globals.cancelIdleCallback(this.optimizeTimerId); dropTask(this.optimizeTaskId);
this.optimizeTimerId = undefined; this.optimizeTaskId = undefined;
} }
// Runtime registers // Runtime registers
@ -3690,20 +3691,20 @@ FilterContainer.prototype.freeze = function() {
// Optimizing is not critical for the static network filtering engine to // Optimizing is not critical for the static network filtering engine to
// work properly, so defer this until later to allow for reduced delay to // work properly, so defer this until later to allow for reduced delay to
// readiness when no valid selfie is available. // readiness when no valid selfie is available.
if ( this.optimizeTimerId === undefined ) { if ( this.optimizeTaskId === undefined ) {
this.optimizeTimerId = globals.requestIdleCallback(( ) => { this.optimizeTaskId = queueTask(( ) => {
this.optimizeTimerId = undefined; this.optimizeTaskId = undefined;
this.optimize(); this.optimize();
}, { timeout: 5000 }); });
} }
}; };
/******************************************************************************/ /******************************************************************************/
FilterContainer.prototype.optimize = function() { FilterContainer.prototype.optimize = function() {
if ( this.optimizeTimerId !== undefined ) { if ( this.optimizeTaskId !== undefined ) {
globals.cancelIdleCallback(this.optimizeTimerId); dropTask(this.optimizeTaskId);
this.optimizeTimerId = undefined; this.optimizeTaskId = undefined;
} }
for ( let bits = 0, n = this.categories.length; bits < n; bits++ ) { for ( let bits = 0, n = this.categories.length; bits < n; bits++ ) {

42
src/js/tasks.js Normal file
View File

@ -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);
}

View File

@ -17,6 +17,7 @@ cp src/js/hntrie.js $DES/js
cp src/js/static-filtering-parser.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-net-filtering.js $DES/js
cp src/js/static-filtering-io.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/text-utils.js $DES/js
cp src/js/uri-utils.js $DES/js cp src/js/uri-utils.js $DES/js
cp src/js/url-net-filtering.js $DES/js cp src/js/url-net-filtering.js $DES/js