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

Side by Side Diff: lib/events.js

Issue 29338969: Issue 3862 - Add EventEmitter class and implement in the FilterNotifier (Closed)
Patch Set: Created March 23, 2016, 2:34 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') | lib/filterNotifier.js » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2016 Eyeo GmbH
4 *
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
7 * published by the Free Software Foundation.
8 *
9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
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/>.
16 */
17
18 "use strict";
19
20 /**
21 * Registers and emits names events.
Wladimir Palant 2016/03/23 16:01:07 names => named
Sebastian Noack 2016/03/23 16:23:43 Done.
22 *
23 * @constructor
24 */
25 exports.EventEmitter = function()
26 {
27 this._listeners = Object.create(null);
28 };
29
30 exports.EventEmitter.prototype = {
31 /**
32 * Adds a listener for the specified event name.
33 *
34 * @param {string} name
35 * @param {function} listener
36 */
37 on: function(name, listener)
38 {
39 if (name in this._listeners)
40 this._listeners[name].push(listener);
41 else
42 this._listeners[name] = [listener];
43 },
44
45 /**
46 * Removes a listener for the specified event name.
47 *
48 * @param {string} name
49 * @param {function} listener
50 */
51 off: function(name, listener)
52 {
53 let listeners = this._listeners[name];
54 if (listeners)
55 {
56 let idx = listeners.indexOf(listener);
57 if (idx != -1)
58 listeners.splice(idx, 1);
59 }
60 },
61
62 /**
63 * Adds a one time listener and returns a promise that
64 * is resolved the next time the specified event is emitted.
65 *
66 * @return {Promise}
67 */
68 once: function(name)
69 {
70 return new Promise(function(resolve)
71 {
72 let listener = function()
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 {
74 this.off(name, listener);
75 resolve();
76 }.bind(this);
77
78 this.on(name, listener);
79 }.bind(this));
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 },
81
82 /**
83 * Calls all previously added listeners for the given event name.
84 *
85 * @param {string} name
86 * @param {...*} [arg]
87 */
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 {
90 let listeners = this._listeners[name];
91 if (listeners)
92 {
93 let args = [];
94 for (let i = 1; i < arguments.length; 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
97 let currentListeners = listeners.slice();
98 for (let listener of currentListeners)
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 }
101 }
102 };
OLDNEW
« no previous file with comments | « no previous file | lib/filterNotifier.js » ('j') | lib/filterNotifier.js » ('J')

Powered by Google App Engine
This is Rietveld