1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-10-06 09:37:12 +02:00

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 { 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++ ) {

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-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