1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-11-23 19:03:02 +01:00

code review: use Map instead of Object

This commit is contained in:
gorhill 2017-10-25 11:27:16 -04:00
parent 2274760275
commit 2d5e3f38f3
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2

View File

@ -719,7 +719,7 @@ chrome.browserAction.onClicked.addListener(function(tab) {
/******************************************************************************/
vAPI.messaging = {
ports: {},
ports: new Map(),
listeners: {},
defaultHandler: null,
NOOPFUNC: noopFunc,
@ -736,7 +736,7 @@ vAPI.messaging.listen = function(listenerName, callback) {
vAPI.messaging.onPortMessage = (function() {
var messaging = vAPI.messaging,
toAuxPending = {};
toAuxPending = new Map();
var supportsUserStylesheets = vAPI.supportsUserStylesheets;
@ -746,32 +746,34 @@ vAPI.messaging.onPortMessage = (function() {
this.init(port, request, timeout);
};
CallbackWrapper.prototype.init = function(port, request, timeout) {
this.port = port;
this.request = request;
this.timerId = timeout !== undefined ?
vAPI.setTimeout(this.callback, timeout) :
null;
return this;
};
CallbackWrapper.prototype.proxy = function(response) {
if ( this.timerId !== null ) {
clearTimeout(this.timerId);
delete toAuxPending[this.timerId];
this.timerId = null;
CallbackWrapper.prototype = {
timerId: null,
init: function(port, request, timeout) {
this.port = port;
this.request = request;
if ( timeout !== undefined ) {
this.timerId = vAPI.setTimeout(this.callback, timeout);
}
return this;
},
proxy: function(response) {
if ( this.timerId !== null ) {
clearTimeout(this.timerId);
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 = [];
@ -790,13 +792,10 @@ vAPI.messaging.onPortMessage = (function() {
// TODO: This could be an issue with a lot of tabs: easy to address
// with a port name to tab id map.
for ( var portName in messaging.ports ) {
if ( messaging.ports.hasOwnProperty(portName) === false ) {
continue;
}
// When sending to an auxiliary process, the target is always the
// port associated with the root frame.
port = messaging.ports[portName];
// When sending to an auxiliary process, the target is always the
// port associated with the root frame.
for ( var entry of messaging.ports ) {
port = entry[1];
if ( port.sender.frameId === 0 && port.sender.tab.id === chromiumTabId ) {
portTo = port;
break;
@ -816,11 +815,8 @@ vAPI.messaging.onPortMessage = (function() {
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 ) {
toAuxPending[wrapper.timerId] = wrapper;
toAuxPending.set(wrapper.timerId, wrapper);
}
portTo.postMessage({
@ -832,14 +828,10 @@ vAPI.messaging.onPortMessage = (function() {
var toAuxResponse = function(details) {
var mainProcessId = details.mainProcessId;
if ( mainProcessId === undefined ) {
return;
}
if ( toAuxPending.hasOwnProperty(mainProcessId) === false ) {
return;
}
var wrapper = toAuxPending[mainProcessId];
delete toAuxPending[mainProcessId];
if ( mainProcessId === undefined ) { return; }
var wrapper = toAuxPending.get(mainProcessId);
if ( wrapper === undefined ) { return; }
toAuxPending.delete(mainProcessId);
wrapper.callback(details.msg);
};
@ -893,51 +885,50 @@ vAPI.messaging.onPortMessage = (function() {
}
// Auxiliary process to main process: prepare response
var callback = messaging.NOOPFUNC;
var callback = this.NOOPFUNC;
if ( request.auxProcessId !== undefined ) {
callback = callbackWrapperFactory(port, request).callback;
}
// Auxiliary process to main process: specific handler
var r = messaging.UNHANDLED,
listener = messaging.listeners[request.channelName];
var r = this.UNHANDLED,
listener = this.listeners[request.channelName];
if ( typeof listener === 'function' ) {
r = listener(request.msg, port.sender, callback);
}
if ( r !== messaging.UNHANDLED ) {
return;
}
if ( r !== this.UNHANDLED ) { return; }
// Auxiliary process to main process: default handler
r = messaging.defaultHandler(request.msg, port.sender, callback);
if ( r !== messaging.UNHANDLED ) {
return;
}
r = this.defaultHandler(request.msg, port.sender, callback);
if ( r !== this.UNHANDLED ) { return; }
// 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
// else there is a memory leak on caller's side
callback();
};
}.bind(vAPI.messaging);
})();
/******************************************************************************/
vAPI.messaging.onPortDisconnect = function(port) {
port.onDisconnect.removeListener(vAPI.messaging.onPortDisconnect);
port.onMessage.removeListener(vAPI.messaging.onPortMessage);
delete vAPI.messaging.ports[port.name];
};
port.onDisconnect.removeListener(this.onPortDisconnect);
port.onMessage.removeListener(this.onPortMessage);
this.ports.delete(port.name);
}.bind(vAPI.messaging);
/******************************************************************************/
vAPI.messaging.onPortConnect = function(port) {
port.onDisconnect.addListener(vAPI.messaging.onPortDisconnect);
port.onMessage.addListener(vAPI.messaging.onPortMessage);
vAPI.messaging.ports[port.name] = port;
};
port.onDisconnect.addListener(this.onPortDisconnect);
port.onMessage.addListener(this.onPortMessage);
this.ports.set(port.name, port);
}.bind(vAPI.messaging);
/******************************************************************************/
@ -962,12 +953,8 @@ vAPI.messaging.broadcast = function(message) {
broadcast: true,
msg: message
};
for ( var portName in this.ports ) {
if ( this.ports.hasOwnProperty(portName) === false ) {
continue;
}
this.ports[portName].postMessage(messageWrapper);
for ( var entry of this.ports ) {
entry[1].postMessage(messageWrapper);
}
};