mirror of https://github.com/gorhill/uBlock.git
eliminate overhead of creating two funcs/closure for each port connection
This commit is contained in:
parent
338bf59507
commit
91276bebfb
|
@ -219,6 +219,7 @@ vAPI.messaging = {
|
|||
ports: {},
|
||||
listeners: {},
|
||||
defaultHandler: null,
|
||||
NOOPFUNC: function(){},
|
||||
UNHANDLED: 'vAPI.messaging.notHandled'
|
||||
};
|
||||
|
||||
|
@ -230,21 +231,17 @@ vAPI.messaging.listen = function(listenerName, callback) {
|
|||
|
||||
/******************************************************************************/
|
||||
|
||||
vAPI.messaging.onConnect = function(port) {
|
||||
var onMessage = function(request) {
|
||||
var callback = function(response) {
|
||||
if ( vAPI.lastError() || response === undefined ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( request.requestId ) {
|
||||
vAPI.messaging.onPortMessage = function(request, port) {
|
||||
var callback = vAPI.messaging.NOOPFUNC;
|
||||
if ( request.requestId !== undefined ) {
|
||||
callback = function(response) {
|
||||
port.postMessage({
|
||||
requestId: request.requestId,
|
||||
portName: request.portName,
|
||||
msg: response
|
||||
msg: response !== undefined ? response : null
|
||||
});
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
// Specific handler
|
||||
var r;
|
||||
|
@ -265,14 +262,19 @@ vAPI.messaging.onConnect = function(port) {
|
|||
console.error('µBlock> messaging > unknown request: %o', request);
|
||||
};
|
||||
|
||||
var onDisconnect = function(port) {
|
||||
port.onDisconnect.removeListener(onDisconnect);
|
||||
port.onMessage.removeListener(onMessage);
|
||||
/******************************************************************************/
|
||||
|
||||
vAPI.messaging.onDisconnect = function(port) {
|
||||
port.onDisconnect.removeListener(vAPI.messaging.onDisconnect);
|
||||
port.onMessage.removeListener(vAPI.messaging.onPortMessage);
|
||||
delete vAPI.messaging.ports[port.name];
|
||||
};
|
||||
|
||||
port.onDisconnect.addListener(onDisconnect);
|
||||
port.onMessage.addListener(onMessage);
|
||||
/******************************************************************************/
|
||||
|
||||
vAPI.messaging.onConnect = function(port) {
|
||||
port.onDisconnect.addListener(vAPI.messaging.onDisconnect);
|
||||
port.onMessage.addListener(vAPI.messaging.onPortMessage);
|
||||
vAPI.messaging.ports[port.name] = port;
|
||||
};
|
||||
|
||||
|
@ -286,7 +288,7 @@ vAPI.messaging.setup = function(defaultHandler) {
|
|||
|
||||
if ( typeof defaultHandler !== 'function' ) {
|
||||
defaultHandler = function(){ return null; };
|
||||
};
|
||||
}
|
||||
this.defaultHandler = defaultHandler;
|
||||
|
||||
chrome.runtime.onConnect.addListener(this.onConnect);
|
||||
|
|
|
@ -26,13 +26,17 @@
|
|||
/******************************************************************************/
|
||||
/******************************************************************************/
|
||||
|
||||
// Message router and default handler
|
||||
// Default handler
|
||||
|
||||
vAPI.messaging.setup(function(request, sender, callback) {
|
||||
(function() {
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
var onMessage = function(request, sender, callback) {
|
||||
var µb = µBlock;
|
||||
|
||||
switch ( request.what ) {
|
||||
// Async
|
||||
switch ( request.what ) {
|
||||
case 'getAssetContent':
|
||||
µb.assets.getLocal(request.url, callback);
|
||||
return;
|
||||
|
@ -41,14 +45,21 @@ vAPI.messaging.setup(function(request, sender, callback) {
|
|||
µb.loadUbiquitousWhitelists();
|
||||
return;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
// Sync
|
||||
var response;
|
||||
|
||||
switch ( request.what ) {
|
||||
case 'contextMenuEvent':
|
||||
µb.contextMenuClientX = request.clientX;
|
||||
µb.contextMenuClientY = request.clientY;
|
||||
break;
|
||||
|
||||
case 'getUserSettings':
|
||||
callback(µb.userSettings);
|
||||
response = µb.userSettings;
|
||||
break;
|
||||
|
||||
case 'gotoURL':
|
||||
|
@ -60,13 +71,21 @@ vAPI.messaging.setup(function(request, sender, callback) {
|
|||
break;
|
||||
|
||||
case 'userSettings':
|
||||
callback(µb.changeUserSettings(request.name, request.value));
|
||||
response = µb.changeUserSettings(request.name, request.value);
|
||||
break;
|
||||
|
||||
default:
|
||||
return vAPI.messaging.UNHANDLED;
|
||||
}
|
||||
});
|
||||
|
||||
callback(response);
|
||||
};
|
||||
|
||||
vAPI.messaging.setup(onMessage);
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
})();
|
||||
|
||||
/******************************************************************************/
|
||||
/******************************************************************************/
|
||||
|
|
Loading…
Reference in New Issue