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

Delta Between Two Patch Sets: lib/events.js

Issue 29338969: Issue 3862 - Add EventEmitter class and implement in the FilterNotifier (Closed)
Left Patch Set: Created March 23, 2016, 2:34 p.m.
Right Patch Set: Use arrow functions Created March 23, 2016, 5:06 p.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 | « no previous file | lib/filterNotifier.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 "use strict"; 18 "use strict";
19 19
20 /** 20 /**
21 * Registers and emits names events. 21 * Registers and emits named events.
Wladimir Palant 2016/03/23 16:01:07 names => named
Sebastian Noack 2016/03/23 16:23:43 Done.
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 = Object.create(null);
28 }; 28 };
29 29
30 exports.EventEmitter.prototype = { 30 exports.EventEmitter.prototype = {
31 /** 31 /**
(...skipping 28 matching lines...) Expand all
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
64 * is resolved the next time the specified event is emitted. 64 * is resolved the next time the specified event is emitted.
65 * 65 *
66 * @return {Promise} 66 * @return {Promise}
67 */ 67 */
68 once: function(name) 68 once: function(name)
69 { 69 {
70 return new Promise(function(resolve) 70 return new Promise(resolve =>
71 { 71 {
72 let listener = function() 72 let listener = () =>
Wladimir Palant 2016/03/23 16:01:07 What about arguments? For this to be usable these
Sebastian Noack 2016/03/23 16:23:43 I thought about passing arguments, but passing in
73 { 73 {
74 this.off(name, listener); 74 this.off(name, listener);
75 resolve(); 75 resolve();
76 }.bind(this); 76 };
77 77
78 this.on(name, listener); 78 this.on(name, listener);
79 }.bind(this)); 79 });
Wladimir Palant 2016/03/23 16:01:07 Use an arrow function instead of bind(this)?
Sebastian Noack 2016/03/23 16:23:43 I agree that automatically bound arrow functions w
Wladimir Palant 2016/03/23 16:34:09 It does: https://hg.adblockplus.org/jshydra/file/t
Sebastian Noack 2016/03/23 17:07:16 Awesome! I didn't realize because it only does if
80 }, 80 },
81 81
82 /** 82 /**
83 * Calls all previously added listeners for the given event name. 83 * Calls all previously added listeners for the given event name.
84 * 84 *
85 * @param {string} name 85 * @param {string} name
86 * @param {...*} [arg] 86 * @param {...*} [arg]
87 */ 87 */
88 emit: function(name) 88 emit: function(name)
Wladimir Palant 2016/03/23 16:01:07 Heh, I can see where you got this from but naming
Sebastian Noack 2016/03/23 16:23:43 Well, that is just the regular node.js name for th
89 { 89 {
90 let listeners = this._listeners[name]; 90 let listeners = this._listeners[name];
91 if (listeners) 91 if (listeners)
92 { 92 {
93 let args = []; 93 let args = [];
94 for (let i = 1; i < arguments.length; i++) 94 for (let i = 1; i < arguments.length; i++)
95 args.push(arguments[i]); 95 args.push(arguments[i]);
Wladimir Palant 2016/03/23 16:01:07 Just use Array functions: let args = [].slice.c
Sebastian Noack 2016/03/23 16:23:44 Passing the arguments object to a different functi
Wladimir Palant 2016/03/23 16:34:09 And rest parameters aren't supported before Chrome
96 96
97 let currentListeners = listeners.slice(); 97 let currentListeners = listeners.slice();
98 for (let listener of currentListeners) 98 for (let listener of currentListeners)
99 listener.apply(null, args); 99 listener.apply(null, args);
Wladimir Palant 2016/03/23 16:01:07 Catch errors here so that remaining listeners are
Sebastian Noack 2016/03/23 16:23:43 We didn't do that before for FilterStorage, Prefs,
100 } 100 }
101 } 101 }
102 }; 102 };
LEFTRIGHT

Powered by Google App Engine
This is Rietveld