mirror of
https://github.com/gorhill/uBlock.git
synced 2024-10-06 09:37:12 +02:00
code review: use Map instead of Object
This commit is contained in:
parent
2274760275
commit
2d5e3f38f3
@ -719,7 +719,7 @@ chrome.browserAction.onClicked.addListener(function(tab) {
|
|||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
vAPI.messaging = {
|
vAPI.messaging = {
|
||||||
ports: {},
|
ports: new Map(),
|
||||||
listeners: {},
|
listeners: {},
|
||||||
defaultHandler: null,
|
defaultHandler: null,
|
||||||
NOOPFUNC: noopFunc,
|
NOOPFUNC: noopFunc,
|
||||||
@ -736,7 +736,7 @@ vAPI.messaging.listen = function(listenerName, callback) {
|
|||||||
|
|
||||||
vAPI.messaging.onPortMessage = (function() {
|
vAPI.messaging.onPortMessage = (function() {
|
||||||
var messaging = vAPI.messaging,
|
var messaging = vAPI.messaging,
|
||||||
toAuxPending = {};
|
toAuxPending = new Map();
|
||||||
|
|
||||||
var supportsUserStylesheets = vAPI.supportsUserStylesheets;
|
var supportsUserStylesheets = vAPI.supportsUserStylesheets;
|
||||||
|
|
||||||
@ -746,32 +746,34 @@ vAPI.messaging.onPortMessage = (function() {
|
|||||||
this.init(port, request, timeout);
|
this.init(port, request, timeout);
|
||||||
};
|
};
|
||||||
|
|
||||||
CallbackWrapper.prototype.init = function(port, request, timeout) {
|
CallbackWrapper.prototype = {
|
||||||
this.port = port;
|
timerId: null,
|
||||||
this.request = request;
|
init: function(port, request, timeout) {
|
||||||
this.timerId = timeout !== undefined ?
|
this.port = port;
|
||||||
vAPI.setTimeout(this.callback, timeout) :
|
this.request = request;
|
||||||
null;
|
if ( timeout !== undefined ) {
|
||||||
return this;
|
this.timerId = vAPI.setTimeout(this.callback, timeout);
|
||||||
};
|
}
|
||||||
|
return this;
|
||||||
CallbackWrapper.prototype.proxy = function(response) {
|
},
|
||||||
if ( this.timerId !== null ) {
|
proxy: function(response) {
|
||||||
clearTimeout(this.timerId);
|
if ( this.timerId !== null ) {
|
||||||
delete toAuxPending[this.timerId];
|
clearTimeout(this.timerId);
|
||||||
this.timerId = null;
|
toAuxPending.delete(this.timerId);
|
||||||
|
this.timerId = null;
|
||||||
|
}
|
||||||
|
// https://github.com/chrisaljoudi/uBlock/issues/383
|
||||||
|
if ( messaging.ports.has(this.port.name) ) {
|
||||||
|
this.port.postMessage({
|
||||||
|
auxProcessId: this.request.auxProcessId,
|
||||||
|
channelName: this.request.channelName,
|
||||||
|
msg: response !== undefined ? response : null
|
||||||
|
});
|
||||||
|
}
|
||||||
|
// Mark for reuse
|
||||||
|
this.port = this.request = null;
|
||||||
|
callbackWrapperJunkyard.push(this);
|
||||||
}
|
}
|
||||||
// https://github.com/chrisaljoudi/uBlock/issues/383
|
|
||||||
if ( messaging.ports.hasOwnProperty(this.port.name) ) {
|
|
||||||
this.port.postMessage({
|
|
||||||
auxProcessId: this.request.auxProcessId,
|
|
||||||
channelName: this.request.channelName,
|
|
||||||
msg: response !== undefined ? response : null
|
|
||||||
});
|
|
||||||
}
|
|
||||||
// Mark for reuse
|
|
||||||
this.port = this.request = null;
|
|
||||||
callbackWrapperJunkyard.push(this);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
var callbackWrapperJunkyard = [];
|
var callbackWrapperJunkyard = [];
|
||||||
@ -790,13 +792,10 @@ vAPI.messaging.onPortMessage = (function() {
|
|||||||
|
|
||||||
// TODO: This could be an issue with a lot of tabs: easy to address
|
// TODO: This could be an issue with a lot of tabs: easy to address
|
||||||
// with a port name to tab id map.
|
// with a port name to tab id map.
|
||||||
for ( var portName in messaging.ports ) {
|
// When sending to an auxiliary process, the target is always the
|
||||||
if ( messaging.ports.hasOwnProperty(portName) === false ) {
|
// port associated with the root frame.
|
||||||
continue;
|
for ( var entry of messaging.ports ) {
|
||||||
}
|
port = entry[1];
|
||||||
// When sending to an auxiliary process, the target is always the
|
|
||||||
// port associated with the root frame.
|
|
||||||
port = messaging.ports[portName];
|
|
||||||
if ( port.sender.frameId === 0 && port.sender.tab.id === chromiumTabId ) {
|
if ( port.sender.frameId === 0 && port.sender.tab.id === chromiumTabId ) {
|
||||||
portTo = port;
|
portTo = port;
|
||||||
break;
|
break;
|
||||||
@ -816,11 +815,8 @@ vAPI.messaging.onPortMessage = (function() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// As per HTML5, timer id is always an integer, thus suitable to be
|
|
||||||
// used as a key, and which value is safe to use across process
|
|
||||||
// boundaries.
|
|
||||||
if ( wrapper !== undefined ) {
|
if ( wrapper !== undefined ) {
|
||||||
toAuxPending[wrapper.timerId] = wrapper;
|
toAuxPending.set(wrapper.timerId, wrapper);
|
||||||
}
|
}
|
||||||
|
|
||||||
portTo.postMessage({
|
portTo.postMessage({
|
||||||
@ -832,14 +828,10 @@ vAPI.messaging.onPortMessage = (function() {
|
|||||||
|
|
||||||
var toAuxResponse = function(details) {
|
var toAuxResponse = function(details) {
|
||||||
var mainProcessId = details.mainProcessId;
|
var mainProcessId = details.mainProcessId;
|
||||||
if ( mainProcessId === undefined ) {
|
if ( mainProcessId === undefined ) { return; }
|
||||||
return;
|
var wrapper = toAuxPending.get(mainProcessId);
|
||||||
}
|
if ( wrapper === undefined ) { return; }
|
||||||
if ( toAuxPending.hasOwnProperty(mainProcessId) === false ) {
|
toAuxPending.delete(mainProcessId);
|
||||||
return;
|
|
||||||
}
|
|
||||||
var wrapper = toAuxPending[mainProcessId];
|
|
||||||
delete toAuxPending[mainProcessId];
|
|
||||||
wrapper.callback(details.msg);
|
wrapper.callback(details.msg);
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -893,51 +885,50 @@ vAPI.messaging.onPortMessage = (function() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Auxiliary process to main process: prepare response
|
// Auxiliary process to main process: prepare response
|
||||||
var callback = messaging.NOOPFUNC;
|
var callback = this.NOOPFUNC;
|
||||||
if ( request.auxProcessId !== undefined ) {
|
if ( request.auxProcessId !== undefined ) {
|
||||||
callback = callbackWrapperFactory(port, request).callback;
|
callback = callbackWrapperFactory(port, request).callback;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Auxiliary process to main process: specific handler
|
// Auxiliary process to main process: specific handler
|
||||||
var r = messaging.UNHANDLED,
|
var r = this.UNHANDLED,
|
||||||
listener = messaging.listeners[request.channelName];
|
listener = this.listeners[request.channelName];
|
||||||
if ( typeof listener === 'function' ) {
|
if ( typeof listener === 'function' ) {
|
||||||
r = listener(request.msg, port.sender, callback);
|
r = listener(request.msg, port.sender, callback);
|
||||||
}
|
}
|
||||||
if ( r !== messaging.UNHANDLED ) {
|
if ( r !== this.UNHANDLED ) { return; }
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Auxiliary process to main process: default handler
|
// Auxiliary process to main process: default handler
|
||||||
r = messaging.defaultHandler(request.msg, port.sender, callback);
|
r = this.defaultHandler(request.msg, port.sender, callback);
|
||||||
if ( r !== messaging.UNHANDLED ) {
|
if ( r !== this.UNHANDLED ) { return; }
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Auxiliary process to main process: no handler
|
// Auxiliary process to main process: no handler
|
||||||
console.error('uBlock> messaging > unknown request: %o', request);
|
console.error(
|
||||||
|
'vAPI.messaging.onPortMessage > unhandled request: %o',
|
||||||
|
request
|
||||||
|
);
|
||||||
|
|
||||||
// Need to callback anyways in case caller expected an answer, or
|
// Need to callback anyways in case caller expected an answer, or
|
||||||
// else there is a memory leak on caller's side
|
// else there is a memory leak on caller's side
|
||||||
callback();
|
callback();
|
||||||
};
|
}.bind(vAPI.messaging);
|
||||||
})();
|
})();
|
||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
vAPI.messaging.onPortDisconnect = function(port) {
|
vAPI.messaging.onPortDisconnect = function(port) {
|
||||||
port.onDisconnect.removeListener(vAPI.messaging.onPortDisconnect);
|
port.onDisconnect.removeListener(this.onPortDisconnect);
|
||||||
port.onMessage.removeListener(vAPI.messaging.onPortMessage);
|
port.onMessage.removeListener(this.onPortMessage);
|
||||||
delete vAPI.messaging.ports[port.name];
|
this.ports.delete(port.name);
|
||||||
};
|
}.bind(vAPI.messaging);
|
||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
vAPI.messaging.onPortConnect = function(port) {
|
vAPI.messaging.onPortConnect = function(port) {
|
||||||
port.onDisconnect.addListener(vAPI.messaging.onPortDisconnect);
|
port.onDisconnect.addListener(this.onPortDisconnect);
|
||||||
port.onMessage.addListener(vAPI.messaging.onPortMessage);
|
port.onMessage.addListener(this.onPortMessage);
|
||||||
vAPI.messaging.ports[port.name] = port;
|
this.ports.set(port.name, port);
|
||||||
};
|
}.bind(vAPI.messaging);
|
||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
@ -962,12 +953,8 @@ vAPI.messaging.broadcast = function(message) {
|
|||||||
broadcast: true,
|
broadcast: true,
|
||||||
msg: message
|
msg: message
|
||||||
};
|
};
|
||||||
|
for ( var entry of this.ports ) {
|
||||||
for ( var portName in this.ports ) {
|
entry[1].postMessage(messageWrapper);
|
||||||
if ( this.ports.hasOwnProperty(portName) === false ) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
this.ports[portName].postMessage(messageWrapper);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user