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

Side by Side Diff: lib/events.js

Issue 29729594: Noissue - Use Map to store EventEmitter listeners (Closed)
Patch Set: Created March 21, 2018, 1:49 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 | lib/filterNotifier.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-present eyeo GmbH 3 * Copyright (C) 2006-present 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 /** 20 /**
21 * Registers and emits named events. 21 * Registers and emits named events.
22 * 22 *
23 * @constructor 23 * @constructor
24 */ 24 */
25 exports.EventEmitter = function() 25 exports.EventEmitter = function()
26 { 26 {
27 this._listeners = Object.create(null); 27 this._listeners = new Map();
28 }; 28 };
29 29
30 exports.EventEmitter.prototype = { 30 exports.EventEmitter.prototype = {
31 /** 31 /**
32 * Adds a listener for the specified event name. 32 * Adds a listener for the specified event name.
33 * 33 *
34 * @param {string} name 34 * @param {string} name
35 * @param {function} listener 35 * @param {function} listener
36 */ 36 */
37 on(name, listener) 37 on(name, listener)
38 { 38 {
39 if (name in this._listeners) 39 if (this._listeners.has(name))
Manish Jethani 2018/04/01 08:44:16 It's usually not worth calling Map.has if you're g
kzar 2018/04/03 17:27:55 Done.
40 this._listeners[name].push(listener); 40 this._listeners.get(name).push(listener);
41 else 41 else
42 this._listeners[name] = [listener]; 42 this._listeners.set(name, [listener]);
43 }, 43 },
44 44
45 /** 45 /**
46 * Removes a listener for the specified event name. 46 * Removes a listener for the specified event name.
47 * 47 *
48 * @param {string} name 48 * @param {string} name
49 * @param {function} listener 49 * @param {function} listener
50 */ 50 */
51 off(name, listener) 51 off(name, listener)
52 { 52 {
53 let listeners = this._listeners[name]; 53 let listeners = this._listeners.get(name);
Manish Jethani 2018/04/01 08:44:16 While we're at it, perhaps it would be a good idea
kzar 2018/04/03 17:27:55 Good idea, but I'd rather not since this would cha
Sebastian Noack 2018/04/03 20:42:34 What change of behavior are you concerned about? M
Manish Jethani 2018/04/04 06:54:49 Dave has a point that this would be more likely to
54 if (listeners) 54 if (listeners)
55 { 55 {
56 let idx = listeners.indexOf(listener); 56 let idx = listeners.indexOf(listener);
57 if (idx != -1) 57 if (idx != -1)
58 listeners.splice(idx, 1); 58 listeners.splice(idx, 1);
59 } 59 }
60 }, 60 },
61 61
62 /** 62 /**
63 * Adds a one time listener and returns a promise that 63 * Adds a one time listener and returns a promise that
(...skipping 16 matching lines...) Expand all
80 }, 80 },
81 81
82 /** 82 /**
83 * Returns a copy of the array of listeners for the specified event. 83 * Returns a copy of the array of listeners for the specified event.
84 * 84 *
85 * @param {string} name 85 * @param {string} name
86 * @return {function[]} 86 * @return {function[]}
87 */ 87 */
88 listeners(name) 88 listeners(name)
89 { 89 {
90 let listeners = this._listeners[name]; 90 let listeners = this._listeners.get(name);
91 return listeners ? listeners.slice() : []; 91 return listeners ? listeners.slice() : [];
92 }, 92 },
93 93
94 /** 94 /**
95 * Calls all previously added listeners for the given event name. 95 * Calls all previously added listeners for the given event name.
96 * 96 *
97 * @param {string} name 97 * @param {string} name
98 * @param {...*} [arg] 98 * @param {...*} [arg]
99 */ 99 */
100 emit(name, ...args) 100 emit(name, ...args)
101 { 101 {
102 let listeners = this.listeners(name); 102 let listeners = this.listeners(name);
103 for (let listener of listeners) 103 for (let listener of listeners)
104 listener(...args); 104 listener(...args);
105 } 105 }
106 }; 106 };
OLDNEW
« no previous file with comments | « no previous file | lib/filterNotifier.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld