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 some issues Created March 23, 2016, 11:32 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« lib/events.js ('K') | « 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 this._onMessage = this._onMessage.bind(this); 32 this._onMessage = this._onMessage.bind(this);
31 ext.onMessage.addListener(this._onMessage); 33 ext.onMessage.addListener(this._onMessage);
32 }; 34 };
33 35
34 Port.prototype = { 36 Port.prototype = {
35 _onMessage: function(message, sender, sendResponse) 37 _onMessage: function(message, sender, sendResponse)
36 { 38 {
37 let async = false; 39 let async = false;
38 let callbacks = this._callbacks[message.type]; 40 let callbacks = this._eventEmitter._listeners[message.type];
39 41
40 if (callbacks) 42 if (callbacks)
41 { 43 {
42 for (let callback of callbacks) 44 for (let callback of callbacks)
43 { 45 {
44 let response = callback(message, sender); 46 let response = callback(message, sender);
45 47
46 if (response && typeof response.then == "function") 48 if (response && typeof response.then == "function")
47 { 49 {
48 response.then( 50 response.then(
(...skipping 28 matching lines...) Expand all
77 79
78 /** 80 /**
79 * Adds a callback for the specified message. 81 * Adds a callback for the specified message.
80 * 82 *
81 * The return value of the callback (if not undefined) is sent as response. 83 * The return value of the callback (if not undefined) is sent as response.
82 * @param {string} name 84 * @param {string} name
83 * @param {Port~messageCallback} callback 85 * @param {Port~messageCallback} callback
84 */ 86 */
85 on: function(name, callback) 87 on: function(name, callback)
86 { 88 {
87 if (name in this._callbacks) 89 this._eventEmitter.on(name, callback);
88 this._callbacks[name].push(callback);
89 else
90 this._callbacks[name] = [callback];
91 }, 90 },
92 91
93 /** 92 /**
94 * Removes a callback for the specified message. 93 * Removes a callback for the specified message.
95 * 94 *
96 * @param {string} name 95 * @param {string} name
97 * @param {Port~messageCallback} callback 96 * @param {Port~messageCallback} callback
98 */ 97 */
99 off: function(name, callback) 98 off: function(name, callback)
100 { 99 {
101 let callbacks = this._callbacks[name]; 100 this._eventEmitter.off(name, callback);
102 if (callbacks)
103 {
104 let idx = callbacks.indexOf(callback);
105 if (idx != -1)
106 callbacks.splice(idx, 1);
107 }
108 }, 101 },
109 102
110 /** 103 /**
111 * Disables the port and makes it stop listening to incoming messages. 104 * Disables the port and makes it stop listening to incoming messages.
112 */ 105 */
113 disconnect: function() 106 disconnect: function()
114 { 107 {
115 ext.onMessage.removeListener(this._onMessage); 108 ext.onMessage.removeListener(this._onMessage);
116 } 109 }
117 }; 110 };
(...skipping 14 matching lines...) Expand all
132 exports.getPort = function(window) 125 exports.getPort = function(window)
133 { 126 {
134 let port = new Port(); 127 let port = new Port();
135 window.addEventListener("unload", () => 128 window.addEventListener("unload", () =>
136 { 129 {
137 port.disconnect(); 130 port.disconnect();
138 }); 131 });
139 return port; 132 return port;
140 }; 133 };
141 134
OLDNEW
« lib/events.js ('K') | « lib/events.js ('k') | lib/prefs.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld