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:
browser: readonly
chrome: readonly
webext: readonly
vAPI: readonly

View file

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

View file

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