| Index: lib/messaging.js |
| =================================================================== |
| --- a/lib/messaging.js |
| +++ b/lib/messaging.js |
| @@ -12,16 +12,18 @@ |
| * GNU General Public License for more details. |
| * |
| * You should have received a copy of the GNU General Public License |
| * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| */ |
| "use strict"; |
| +let {EventEmitter} = require("events"); |
| + |
| const MESSAGE_NAME = "AdblockPlus:Message"; |
| const RESPONSE_NAME = "AdblockPlus:Response"; |
| function isPromise(value) |
| { |
| // value instanceof Promise won't work - there can be different Promise |
| // classes (e.g. in different contexts) and there can also be promise-like |
| // classes (e.g. Task). |
| @@ -116,18 +118,18 @@ LightWeightPort.prototype = |
| * Communication port wrapping the message manager API to send and receive |
| * messages. |
| * @param {nsIMessageManager} messageManager |
| * @constructor |
| */ |
| function Port(messageManager) |
| { |
| this._messageManager = messageManager; |
| + this._eventEmitter = new EventEmitter(); |
| - this._callbacks = new Map(); |
| this._responseCallbacks = new Map(); |
| this._responseCallbackCounter = 0; |
| this._handleRequest = this._handleRequest.bind(this); |
| this._handleResponse = this._handleResponse.bind(this); |
| this._messageManager.addMessageListener(MESSAGE_NAME, this._handleRequest); |
| this._messageManager.addMessageListener(RESPONSE_NAME, this._handleResponse); |
| } |
| @@ -203,21 +205,17 @@ Port.prototype = { |
| { |
| this._responseCallbacks.delete(callbackID); |
| callback(processor.value); |
| } |
| }, |
| _dispatch: function(messageName, payload, sender) |
| { |
| - let callbacks = this._callbacks.get(messageName); |
| - if (!callbacks) |
| - return undefined; |
| - |
| - callbacks = callbacks.slice(); |
| + let callbacks = this._eventEmitter.listeners(messageName); |
| let processor = new ResponseProcessor(messageName); |
| for (let callback of callbacks) |
| { |
| try |
| { |
| processor.add(callback(payload, sender)); |
| } |
| catch (e) |
| @@ -241,37 +239,27 @@ Port.prototype = { |
| /** |
| * Adds a handler for the specified message. |
| * @param {string} messageName message that would trigger the callback |
| * @param {Port~messageHandler} callback |
| */ |
| on: function(messageName, callback) |
| { |
| - let callbacks = this._callbacks.get(messageName); |
| - if (callbacks) |
| - callbacks.push(callback); |
| - else |
| - this._callbacks.set(messageName, [callback]); |
| + this._eventEmitter.on(messageName, callback); |
| }, |
| /** |
| * Removes a handler for the specified message. |
| * @param {string} messageName message that would trigger the callback |
| * @param {Port~messageHandler} callback |
| */ |
| off: function(messageName, callback) |
| { |
| - let callbacks = this._callbacks.get(messageName); |
| - if (!callbacks) |
| - return; |
| - |
| - let index = callbacks.indexOf(callback); |
| - if (index >= 0) |
| - callbacks.splice(index, 1); |
| + this._eventEmitter.off(messageName, callback); |
| }, |
| /** |
| * Sends a message. |
| * @param {string} messageName message identifier |
| * @param [payload] data to attach to the message |
| */ |
| emit: function(messageName, payload) |