Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: lib/messaging.js

Issue 29345627: Issue 4129 - Reuse EventEmitter class in messaging module (Closed)
Patch Set: Created June 7, 2016, 4 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2016 Eyeo GmbH 3 * Copyright (C) 2006-2016 Eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 "use strict"; 18 "use strict";
19 19
20 let {EventEmitter} = require("events");
21
20 const MESSAGE_NAME = "AdblockPlus:Message"; 22 const MESSAGE_NAME = "AdblockPlus:Message";
21 const RESPONSE_NAME = "AdblockPlus:Response"; 23 const RESPONSE_NAME = "AdblockPlus:Response";
22 24
23 function isPromise(value) 25 function isPromise(value)
24 { 26 {
25 // value instanceof Promise won't work - there can be different Promise 27 // value instanceof Promise won't work - there can be different Promise
26 // classes (e.g. in different contexts) and there can also be promise-like 28 // classes (e.g. in different contexts) and there can also be promise-like
27 // classes (e.g. Task). 29 // classes (e.g. Task).
28 return (value && typeof value.then == "function"); 30 return (value && typeof value.then == "function");
29 } 31 }
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
114 116
115 /** 117 /**
116 * Communication port wrapping the message manager API to send and receive 118 * Communication port wrapping the message manager API to send and receive
117 * messages. 119 * messages.
118 * @param {nsIMessageManager} messageManager 120 * @param {nsIMessageManager} messageManager
119 * @constructor 121 * @constructor
120 */ 122 */
121 function Port(messageManager) 123 function Port(messageManager)
122 { 124 {
123 this._messageManager = messageManager; 125 this._messageManager = messageManager;
126 this._eventEmitter = new EventEmitter();
124 127
125 this._callbacks = new Map();
126 this._responseCallbacks = new Map(); 128 this._responseCallbacks = new Map();
127 this._responseCallbackCounter = 0; 129 this._responseCallbackCounter = 0;
128 130
129 this._handleRequest = this._handleRequest.bind(this); 131 this._handleRequest = this._handleRequest.bind(this);
130 this._handleResponse = this._handleResponse.bind(this); 132 this._handleResponse = this._handleResponse.bind(this);
131 this._messageManager.addMessageListener(MESSAGE_NAME, this._handleRequest); 133 this._messageManager.addMessageListener(MESSAGE_NAME, this._handleRequest);
132 this._messageManager.addMessageListener(RESPONSE_NAME, this._handleResponse); 134 this._messageManager.addMessageListener(RESPONSE_NAME, this._handleResponse);
133 } 135 }
134 Port.prototype = { 136 Port.prototype = {
135 /** 137 /**
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
201 callbackData[2] = --expectedResponses; 203 callbackData[2] = --expectedResponses;
202 if (expectedResponses <= 0) 204 if (expectedResponses <= 0)
203 { 205 {
204 this._responseCallbacks.delete(callbackID); 206 this._responseCallbacks.delete(callbackID);
205 callback(processor.value); 207 callback(processor.value);
206 } 208 }
207 }, 209 },
208 210
209 _dispatch: function(messageName, payload, sender) 211 _dispatch: function(messageName, payload, sender)
210 { 212 {
211 let callbacks = this._callbacks.get(messageName); 213 let callbacks = this._eventEmitter.listeners(messageName);
212 if (!callbacks)
213 return undefined;
214
215 callbacks = callbacks.slice();
216 let processor = new ResponseProcessor(messageName); 214 let processor = new ResponseProcessor(messageName);
217 for (let callback of callbacks) 215 for (let callback of callbacks)
218 { 216 {
219 try 217 try
220 { 218 {
221 processor.add(callback(payload, sender)); 219 processor.add(callback(payload, sender));
222 } 220 }
223 catch (e) 221 catch (e)
224 { 222 {
225 Cu.reportError(e); 223 Cu.reportError(e);
(...skipping 13 matching lines...) Expand all
239 * response). 237 * response).
240 */ 238 */
241 239
242 /** 240 /**
243 * Adds a handler for the specified message. 241 * Adds a handler for the specified message.
244 * @param {string} messageName message that would trigger the callback 242 * @param {string} messageName message that would trigger the callback
245 * @param {Port~messageHandler} callback 243 * @param {Port~messageHandler} callback
246 */ 244 */
247 on: function(messageName, callback) 245 on: function(messageName, callback)
248 { 246 {
249 let callbacks = this._callbacks.get(messageName); 247 this._eventEmitter.on(messageName, callback);
250 if (callbacks)
251 callbacks.push(callback);
252 else
253 this._callbacks.set(messageName, [callback]);
254 }, 248 },
255 249
256 /** 250 /**
257 * Removes a handler for the specified message. 251 * Removes a handler for the specified message.
258 * @param {string} messageName message that would trigger the callback 252 * @param {string} messageName message that would trigger the callback
259 * @param {Port~messageHandler} callback 253 * @param {Port~messageHandler} callback
260 */ 254 */
261 off: function(messageName, callback) 255 off: function(messageName, callback)
262 { 256 {
263 let callbacks = this._callbacks.get(messageName); 257 this._eventEmitter.off(messageName, callback);
264 if (!callbacks)
265 return;
266
267 let index = callbacks.indexOf(callback);
268 if (index >= 0)
269 callbacks.splice(index, 1);
270 }, 258 },
271 259
272 /** 260 /**
273 * Sends a message. 261 * Sends a message.
274 * @param {string} messageName message identifier 262 * @param {string} messageName message identifier
275 * @param [payload] data to attach to the message 263 * @param [payload] data to attach to the message
276 */ 264 */
277 emit: function(messageName, payload) 265 emit: function(messageName, payload)
278 { 266 {
279 sendMessage(this._messageManager, messageName, payload, undefined); 267 sendMessage(this._messageManager, messageName, payload, undefined);
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
319 catch (e) 307 catch (e)
320 { 308 {
321 // Parent 309 // Parent
322 messageManager = Cc["@mozilla.org/parentprocessmessagemanager;1"] 310 messageManager = Cc["@mozilla.org/parentprocessmessagemanager;1"]
323 .getService(Ci.nsIMessageListenerManager); 311 .getService(Ci.nsIMessageListenerManager);
324 } 312 }
325 313
326 let port = new Port(messageManager); 314 let port = new Port(messageManager);
327 onShutdown.add(() => port.disconnect()); 315 onShutdown.add(() => port.disconnect());
328 exports.port = port; 316 exports.port = port;
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld