eliminate overhead of creating two funcs/closure for each port connection

pull/2/head
Raymond Hill 10 years ago committed by gorhill
parent 0a0c011960
commit 789bfe09f6

@ -219,6 +219,7 @@ vAPI.messaging = {
ports: {}, ports: {},
listeners: {}, listeners: {},
defaultHandler: null, defaultHandler: null,
NOOPFUNC: function(){},
UNHANDLED: 'vAPI.messaging.notHandled' UNHANDLED: 'vAPI.messaging.notHandled'
}; };
@ -230,49 +231,50 @@ vAPI.messaging.listen = function(listenerName, callback) {
/******************************************************************************/ /******************************************************************************/
vAPI.messaging.onConnect = function(port) { vAPI.messaging.onPortMessage = function(request, port) {
var onMessage = function(request) { var callback = vAPI.messaging.NOOPFUNC;
var callback = function(response) { if ( request.requestId !== undefined ) {
if ( vAPI.lastError() || response === undefined ) { callback = function(response) {
return; port.postMessage({
} requestId: request.requestId,
portName: request.portName,
if ( request.requestId ) { msg: response !== undefined ? response : null
port.postMessage({ });
requestId: request.requestId,
portName: request.portName,
msg: response
});
}
}; };
}
// Specific handler // Specific handler
var r; var r;
var listener = vAPI.messaging.listeners[request.portName]; var listener = vAPI.messaging.listeners[request.portName];
if ( typeof listener === 'function' ) { if ( typeof listener === 'function' ) {
r = listener(request.msg, port.sender, callback); r = listener(request.msg, port.sender, callback);
} }
if ( r !== vAPI.messaging.UNHANDLED ) { if ( r !== vAPI.messaging.UNHANDLED ) {
return; return;
} }
// Default handler // Default handler
r = vAPI.messaging.defaultHandler(request.msg, port.sender, callback); r = vAPI.messaging.defaultHandler(request.msg, port.sender, callback);
if ( r !== vAPI.messaging.UNHANDLED ) { if ( r !== vAPI.messaging.UNHANDLED ) {
return; return;
} }
console.error('µBlock> messaging > unknown request: %o', request); console.error('µBlock> messaging > unknown request: %o', request);
}; };
var onDisconnect = function(port) { /******************************************************************************/
port.onDisconnect.removeListener(onDisconnect);
port.onMessage.removeListener(onMessage);
delete vAPI.messaging.ports[port.name];
};
port.onDisconnect.addListener(onDisconnect); vAPI.messaging.onDisconnect = function(port) {
port.onMessage.addListener(onMessage); port.onDisconnect.removeListener(vAPI.messaging.onDisconnect);
port.onMessage.removeListener(vAPI.messaging.onPortMessage);
delete vAPI.messaging.ports[port.name];
};
/******************************************************************************/
vAPI.messaging.onConnect = function(port) {
port.onDisconnect.addListener(vAPI.messaging.onDisconnect);
port.onMessage.addListener(vAPI.messaging.onPortMessage);
vAPI.messaging.ports[port.name] = port; vAPI.messaging.ports[port.name] = port;
}; };
@ -286,7 +288,7 @@ vAPI.messaging.setup = function(defaultHandler) {
if ( typeof defaultHandler !== 'function' ) { if ( typeof defaultHandler !== 'function' ) {
defaultHandler = function(){ return null; }; defaultHandler = function(){ return null; };
}; }
this.defaultHandler = defaultHandler; this.defaultHandler = defaultHandler;
chrome.runtime.onConnect.addListener(this.onConnect); chrome.runtime.onConnect.addListener(this.onConnect);

Loading…
Cancel
Save