From ed99d52bc464f9248799e8f6c9f0e0f561d60d12 Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Tue, 17 Sep 2019 17:43:52 -0400 Subject: [PATCH] Simplify slightly messaging code - No need to hold onto channel name and message data while waiting for response to resolve - Use more representative `msgId` instead of `auxProcessId` --- platform/chromium/vapi-background.js | 25 ++++++++++++------------- platform/chromium/vapi-client.js | 14 +++++++------- 2 files changed, 19 insertions(+), 20 deletions(-) diff --git a/platform/chromium/vapi-background.js b/platform/chromium/vapi-background.js index 3da3f27b5..9441622c7 100644 --- a/platform/chromium/vapi-background.js +++ b/platform/chromium/vapi-background.js @@ -1004,44 +1004,43 @@ vAPI.messaging = { // Use a wrapper to avoid closure and to allow reuse. CallbackWrapper: class { - constructor(messaging, port, request) { + constructor(messaging, port, msgId) { this.messaging = messaging; this.callback = this.proxy.bind(this); // bind once - this.init(port, request); + this.init(port, msgId); } - init(port, request) { + init(port, msgId) { this.port = port; - this.request = request; + this.msgId = msgId; return this; } proxy(response) { // https://github.com/chrisaljoudi/uBlock/issues/383 if ( this.messaging.ports.has(this.port.name) ) { this.port.postMessage({ - auxProcessId: this.request.auxProcessId, - channelName: this.request.channelName, - msg: response !== undefined ? response : null + msgId: this.msgId, + msg: response !== undefined ? response : null, }); } // Store for reuse - this.port = this.request = null; + this.port = null; this.messaging.callbackWrapperJunkyard.push(this); } }, callbackWrapperJunkyard: [], - callbackWrapperFactory: function(port, request) { + callbackWrapperFactory: function(port, msgId) { return this.callbackWrapperJunkyard.length !== 0 - ? this.callbackWrapperJunkyard.pop().init(port, request) - : new this.CallbackWrapper(this, port, request); + ? this.callbackWrapperJunkyard.pop().init(port, msgId) + : new this.CallbackWrapper(this, port, msgId); }, onPortMessage: function(request, port) { // prepare response let callback = this.NOOPFUNC; - if ( request.auxProcessId !== undefined ) { - callback = this.callbackWrapperFactory(port, request).callback; + if ( request.msgId !== undefined ) { + callback = this.callbackWrapperFactory(port, request.msgId).callback; } // Content process to main process: framework handler. diff --git a/platform/chromium/vapi-client.js b/platform/chromium/vapi-client.js index d2a772fad..1b22f4d99 100644 --- a/platform/chromium/vapi-client.js +++ b/platform/chromium/vapi-client.js @@ -80,7 +80,7 @@ vAPI.messaging = { channels: new Map(), connections: new Map(), pending: new Map(), - auxProcessId: 1, + msgIdGenerator: 1, shuttingDown: false, Connection: function(handler, details) { @@ -136,10 +136,10 @@ vAPI.messaging = { } // Response to specific message previously sent - if ( details.auxProcessId ) { - const resolver = this.pending.get(details.auxProcessId); + if ( details.msgId !== undefined ) { + const resolver = this.pending.get(details.msgId); if ( resolver !== undefined ) { - this.pending.delete(details.auxProcessId); + this.pending.delete(details.msgId); resolver(details.msg); return; } @@ -274,11 +274,11 @@ vAPI.messaging = { if ( port === null ) { return Promise.resolve(); } - const auxProcessId = this.auxProcessId++; + const msgId = this.msgIdGenerator++; const promise = new Promise(resolve => { - this.pending.set(auxProcessId, resolve); + this.pending.set(msgId, resolve); }); - port.postMessage({ channelName, auxProcessId, msg }); + port.postMessage({ channelName, msgId, msg }); return promise; },