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

Side by Side Diff: lib/events.js

Issue 29855595: Issue 6741 - Use ES2015 classes in lib/events.js (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Created Aug. 14, 2018, 11:30 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 | no next file » | 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 * @class
24 */ 24 */
25 exports.EventEmitter = function() 25 exports.EventEmitter = class EventEmitter
26 { 26 {
27 this._listeners = new Map(); 27 constructor()
28 }; 28 {
29 this._listeners = new Map();
30 }
29 31
30 exports.EventEmitter.prototype = {
31 /** 32 /**
32 * Adds a listener for the specified event name. 33 * adds a listener for the specified event name.
33 * 34 *
34 * @param {string} name 35 * @param {string} name
35 * @param {function} listener 36 * @param {function} listener
36 */ 37 */
37 on(name, listener) 38 on(name, listener)
38 { 39 {
39 let listeners = this._listeners.get(name); 40 let listeners = this._listeners.get(name);
40 if (listeners) 41 if (listeners)
41 listeners.push(listener); 42 listeners.push(listener);
42 else 43 else
43 this._listeners.set(name, [listener]); 44 this._listeners.set(name, [listener]);
44 }, 45 }
45
46 /** 46 /**
47 * Removes a listener for the specified event name. 47 * Removes a listener for the specified event name.
48 * 48 *
49 * @param {string} name 49 * @param {string} name
50 * @param {function} listener 50 * @param {function} listener
51 */ 51 */
52 off(name, listener) 52 off(name, listener)
53 { 53 {
54 let listeners = this._listeners.get(name); 54 let listeners = this._listeners.get(name);
55 if (listeners) 55 if (listeners)
56 { 56 {
57 let idx = listeners.indexOf(listener); 57 let idx = listeners.indexOf(listener);
58 if (idx != -1) 58 if (idx != -1)
59 listeners.splice(idx, 1); 59 listeners.splice(idx, 1);
60 } 60 }
61 }, 61 }
62
63 /** 62 /**
64 * Adds a one time listener and returns a promise that 63 * Adds a one time listener and returns a promise that
65 * is resolved the next time the specified event is emitted. 64 * is resolved the next time the specified event is emitted.
66 * @param {string} name 65 * @param {string} name
67 * @return {Promise} 66 * @return {Promise}
68 */ 67 */
69 once(name) 68 once(name)
70 { 69 {
71 return new Promise(resolve => 70 return new Promise(resolve =>
72 { 71 {
73 let listener = () => 72 let listener = () =>
74 { 73 {
75 this.off(name, listener); 74 this.off(name, listener);
76 resolve(); 75 resolve();
77 }; 76 };
78 77
79 this.on(name, listener); 78 this.on(name, listener);
80 }); 79 });
81 }, 80 }
82
83 /** 81 /**
84 * Returns a copy of the array of listeners for the specified event. 82 * Returns a copy of the array of listeners for the specified event.
85 * 83 *
86 * @param {string} name 84 * @param {string} name
87 * @return {function[]} 85 * @return {function[]}
88 */ 86 */
89 listeners(name) 87 listeners(name)
90 { 88 {
91 let listeners = this._listeners.get(name); 89 let listeners = this._listeners.get(name);
92 return listeners ? listeners.slice() : []; 90 return listeners ? listeners.slice() : [];
93 }, 91 }
94
95 /** 92 /**
96 * Calls all previously added listeners for the given event name. 93 * Calls all previously added listeners for the given event name.
97 * 94 *
98 * @param {string} name 95 * @param {string} name
99 * @param {...*} [arg] 96 * @param {...*} [arg]
100 */ 97 */
101 emit(name, ...args) 98 emit(name, ...args)
102 { 99 {
103 let listeners = this.listeners(name); 100 let listeners = this.listeners(name);
104 for (let listener of listeners) 101 for (let listener of listeners)
105 listener(...args); 102 listener(...args);
106 } 103 }
107 }; 104 };
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld