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

Delta Between Two Patch Sets: lib/events.js

Issue 29338534: Issue 3826 - Filter preference change events (Closed)
Left Patch Set: Fixed typo in comment Created March 18, 2016, 6:15 p.m.
Right 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:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « dependencies ('k') | lib/messaging.js » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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 events */ 18 /** @module events */
19 19
20 "use strict"; 20 "use strict";
21 21
22 /** 22 /**
23 * Registers and emits names events. 23 * Registers and emits names events.
24 * 24 *
25 * @constructor 25 * @constructor
26 */ 26 */
27 exports.EventEmitter = function() 27 exports.EventEmitter = function()
28 { 28 {
29 this._callbacks = Object.create(null); 29 this._listeners = Object.create(null);
30 }; 30 };
31 31
32 exports.EventEmitter.prototype = { 32 exports.EventEmitter.prototype = {
33 /** 33 /**
34 * Adds a callback for the specified event name. 34 * Adds a listener for the specified event name.
35 * 35 *
36 * @param {string} name 36 * @param {string} name
37 * @param {function} callback 37 * @param {function} listener
38 */ 38 */
39 on: function(name, callback) 39 on: function(name, listener)
40 { 40 {
41 if (name in this._callbacks) 41 if (name in this._listeners)
42 this._callbacks[name].push(callback); 42 this._listeners[name].push(listener);
43 else 43 else
44 this._callbacks[name] = [callback]; 44 this._listeners[name] = [listener];
45 }, 45 },
46 46
47 /** 47 /**
48 * Removes a callback for the specified event name. 48 * Removes a listener for the specified event name.
49 * 49 *
50 * @param {string} name 50 * @param {string} name
51 * @param {function} callback 51 * @param {function} listener
52 */ 52 */
53 off: function(name, callback) 53 off: function(name, listener)
54 { 54 {
55 let callbacks = this._callbacks[name]; 55 let listeners = this._listeners[name];
56 if (callbacks) 56 if (listeners)
57 { 57 {
58 let idx = callbacks.indexOf(callback); 58 let idx = listeners.indexOf(listener);
59 if (idx != -1) 59 if (idx != -1)
60 callbacks.splice(idx, 1); 60 listeners.splice(idx, 1);
61 } 61 }
62 }, 62 },
63 63
64 /** 64 /**
65 * Calls all previously added callbacks for the given event name. 65 * Calls all previously added listeners for the given event name.
66 * 66 *
67 * @param {string} name 67 * @param {string} name
68 * @param {...*} [arg] 68 * @param {...*} [arg]
69 */ 69 */
70 emit: function(name) 70 emit: function(name)
71 { 71 {
72 let callbacks = this._callbacks[name]; 72 let listeners = this._listeners[name];
73 if (callbacks) 73 if (listeners)
74 { 74 {
75 for (let callback of callbacks) 75 let args = [];
Sebastian Noack 2016/03/23 11:37:22 Array.prototype.slice.call(arguments, 1), or passi
kzar 2016/03/23 11:56:01 Acknowledged.
76 callbacks.apply(null, Array.prototype.slice.call(arguments, 1)); 76 for (let i = 1; i < arguments.length; i++)
77 args.push(arguments[i]);
78
79 let currentListeners = listeners.slice();
Sebastian Noack 2016/03/23 11:37:22 This is necessary to avoid side effects when a lis
kzar 2016/03/23 11:56:01 Acknowledged.
80 for (let listener of currentListeners)
81 listener.apply(null, args);
77 } 82 }
78 } 83 }
79 }; 84 };
Sebastian Noack 2016/03/23 11:37:22 Moreover, I think "listener" is a more appropriate
LEFTRIGHT

Powered by Google App Engine
This is Rietveld