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

Side by Side Diff: lib/messaging.js

Issue 29338534: Issue 3826 - Filter preference change events (Closed)
Patch Set: Fixed typo in comment Created March 18, 2016, 6:15 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 | « lib/events.js ('k') | lib/prefs.js » ('j') | 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 /** @module messaging */ 18 /** @module messaging */
19 19
20 "use strict"; 20 "use strict";
21 21
22 let {EventEmitter} = require("events");
23
22 /** 24 /**
23 * Communication port wrapping ext.onMessage to receive messages. 25 * Communication port wrapping ext.onMessage to receive messages.
24 * 26 *
25 * @constructor 27 * @constructor
26 */ 28 */
27 function Port() 29 function Port()
28 { 30 {
29 this._callbacks = Object.create(null); 31 this._eventEmitter = new EventEmitter();
30 ext.onMessage.addListener(this._onMessage.bind(this)); 32 ext.onMessage.addListener(this._onMessage.bind(this));
31 }; 33 };
32 34
33 Port.prototype = { 35 Port.prototype = {
34 _onMessage: function(message, sender, sendResponse) 36 _onMessage: function(message, sender, sendResponse)
35 { 37 {
36 let async = false; 38 let async = false;
37 let callbacks = this._callbacks[message.type] || []; 39 let callbacks = this._eventEmitter._callbacks[message.type] || [];
38 40
39 for (let callback of callbacks) 41 for (let callback of callbacks)
40 { 42 {
41 let response = callback(message, sender); 43 let response = callback(message, sender);
42 44
43 if (response instanceof Promise) 45 if (response instanceof Promise)
44 { 46 {
45 response.then( 47 response.then(
46 sendResponse, 48 sendResponse,
47 reason => { 49 reason => {
(...skipping 25 matching lines...) Expand all
73 75
74 /** 76 /**
75 * Adds a callback for the specified message. 77 * Adds a callback for the specified message.
76 * 78 *
77 * The return value of the callback (if not undefined) is sent as response. 79 * The return value of the callback (if not undefined) is sent as response.
78 * @param {string} name 80 * @param {string} name
79 * @param {Port~messageCallback} callback 81 * @param {Port~messageCallback} callback
80 */ 82 */
81 on: function(name, callback) 83 on: function(name, callback)
82 { 84 {
83 if (name in this._callbacks) 85 this._eventEmitter.on(name, callback);
84 this._callbacks[name].push(callback);
85 else
86 this._callbacks[name] = [callback];
87 }, 86 },
88 87
89 /** 88 /**
90 * Removes a callback for the specified message. 89 * Removes a callback for the specified message.
91 * 90 *
92 * @param {string} name 91 * @param {string} name
93 * @param {Port~messageCallback} callback 92 * @param {Port~messageCallback} callback
94 */ 93 */
95 off: function(name, callback) 94 off: function(name, callback)
96 { 95 {
97 let callbacks = this._callbacks[name]; 96 this._eventEmitter.off(name, callback);
98 if (callbacks)
99 {
100 let idx = callbacks.indexOf(callback);
101 if (idx != -1)
102 callbacks.splice(idx, 1);
103 }
104 } 97 }
105 }; 98 };
106 99
107 /** 100 /**
108 * The default port to receive messages. 101 * The default port to receive messages.
109 * 102 *
110 * @type {Port} 103 * @type {Port}
111 */ 104 */
112 exports.port = new Port(); 105 exports.port = new Port();
OLDNEW
« no previous file with comments | « lib/events.js ('k') | lib/prefs.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld