1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-07-08 12:57:57 +02:00

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`
This commit is contained in:
Raymond Hill 2019-09-17 17:43:52 -04:00
parent 60c2381518
commit ed99d52bc4
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
2 changed files with 19 additions and 20 deletions

View File

@ -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.

View File

@ -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;
},