From 5d60df4b1bcdc813701eaee36a29e2434b823651 Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Fri, 22 Mar 2024 14:46:57 -0400 Subject: [PATCH] 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. --- .eslintrc.yml | 1 - platform/common/vapi-client.js | 25 +++++++++++++------------ src/js/broadcast.js | 2 ++ 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/.eslintrc.yml b/.eslintrc.yml index 52b8b83fc..e3329a78f 100644 --- a/.eslintrc.yml +++ b/.eslintrc.yml @@ -22,5 +22,4 @@ rules: globals: browser: readonly chrome: readonly - webext: readonly vAPI: readonly diff --git a/platform/common/vapi-client.js b/platform/common/vapi-client.js index ca125b584..a6d46c625 100644 --- a/platform/common/vapi-client.js +++ b/platform/common/vapi-client.js @@ -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); diff --git a/src/js/broadcast.js b/src/js/broadcast.js index 964465b17..61d647f06 100644 --- a/src/js/broadcast.js +++ b/src/js/broadcast.js @@ -19,6 +19,8 @@ Home: https://github.com/gorhill/uBlock */ +import webext from './webext.js'; + /******************************************************************************/ // Broadcast a message to all uBO contexts