1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-07-05 11:37:01 +02:00

Fix bad eslint rule + minor code review

`webext.js` module needs to be explicitly imported.

Added time-based heuristic to decide when a webpage loses
communication with background process.
This commit is contained in:
Raymond Hill 2024-03-22 14:46:57 -04:00
parent d368747235
commit 5d60df4b1b
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
3 changed files with 15 additions and 13 deletions

View File

@ -22,5 +22,4 @@ rules:
globals: globals:
browser: readonly browser: readonly
chrome: readonly chrome: readonly
webext: readonly
vAPI: readonly vAPI: readonly

View File

@ -81,6 +81,7 @@ vAPI.messaging = {
portTimerDelay: 10000, portTimerDelay: 10000,
msgIdGenerator: 1, msgIdGenerator: 1,
pending: new Map(), pending: new Map(),
waitStartTime: 0,
shuttingDown: false, shuttingDown: false,
shutdown: function() { shutdown: function() {
@ -111,16 +112,11 @@ vAPI.messaging = {
// revisited to isolate the picker dialog DOM from the page DOM. // revisited to isolate the picker dialog DOM from the page DOM.
messageListener: function(details) { messageListener: function(details) {
if ( typeof details !== 'object' || details === null ) { return; } if ( typeof details !== 'object' || details === null ) { return; }
if ( details.msgId === undefined ) { return; }
// Response to specific message previously sent const resolver = this.pending.get(details.msgId);
if ( details.msgId !== undefined ) { if ( resolver === undefined ) { return; }
const resolver = this.pending.get(details.msgId); this.pending.delete(details.msgId);
if ( resolver !== undefined ) { resolver(details.msg);
this.pending.delete(details.msgId);
resolver(details.msg);
return;
}
}
}, },
messageListenerBound: null, messageListenerBound: null,
@ -199,13 +195,18 @@ vAPI.messaging = {
// the main process is no longer reachable: memory leaks and bad // the main process is no longer reachable: memory leaks and bad
// performance become a risk -- especially for long-lived, dynamic // performance become a risk -- especially for long-lived, dynamic
// pages. Guard against this. // pages. Guard against this.
if ( this.pending.size > 1000 ) { if ( this.pending.size > 64 ) {
vAPI.shutdown.exec(); if ( (Date.now() - this.waitStartTime) > 60000 ) {
vAPI.shutdown.exec();
}
} }
const port = this.getPort(); const port = this.getPort();
if ( port === null ) { if ( port === null ) {
return Promise.resolve(); return Promise.resolve();
} }
if ( this.pending.size === 0 ) {
this.waitStartTime = Date.now();
}
const msgId = this.msgIdGenerator++; const msgId = this.msgIdGenerator++;
const promise = new Promise(resolve => { const promise = new Promise(resolve => {
this.pending.set(msgId, resolve); this.pending.set(msgId, resolve);

View File

@ -19,6 +19,8 @@
Home: https://github.com/gorhill/uBlock Home: https://github.com/gorhill/uBlock
*/ */
import webext from './webext.js';
/******************************************************************************/ /******************************************************************************/
// Broadcast a message to all uBO contexts // Broadcast a message to all uBO contexts