mirror of
https://github.com/gorhill/uBlock.git
synced 2024-11-10 01:02:08 +01: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:
parent
d368747235
commit
5d60df4b1b
3 changed files with 15 additions and 13 deletions
|
@ -22,5 +22,4 @@ rules:
|
|||
globals:
|
||||
browser: readonly
|
||||
chrome: readonly
|
||||
webext: readonly
|
||||
vAPI: readonly
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -19,6 +19,8 @@
|
|||
Home: https://github.com/gorhill/uBlock
|
||||
*/
|
||||
|
||||
import webext from './webext.js';
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
// Broadcast a message to all uBO contexts
|
||||
|
|
Loading…
Reference in a new issue