OLD | NEW |
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 |
(...skipping 16 matching lines...) Expand all Loading... |
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 /** |
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: function(name, listener) | 37 on(name, listener) |
38 { | 38 { |
39 if (name in this._listeners) | 39 if (name in this._listeners) |
40 this._listeners[name].push(listener); | 40 this._listeners[name].push(listener); |
41 else | 41 else |
42 this._listeners[name] = [listener]; | 42 this._listeners[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: function(name, listener) | 51 off(name, listener) |
52 { | 52 { |
53 let listeners = this._listeners[name]; | 53 let listeners = this._listeners[name]; |
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 |
64 * is resolved the next time the specified event is emitted. | 64 * is resolved the next time the specified event is emitted. |
65 * | 65 * @param {string} name |
66 * @return {Promise} | 66 * @return {Promise} |
67 */ | 67 */ |
68 once: function(name) | 68 once(name) |
69 { | 69 { |
70 return new Promise(resolve => | 70 return new Promise(resolve => |
71 { | 71 { |
72 let listener = () => | 72 let listener = () => |
73 { | 73 { |
74 this.off(name, listener); | 74 this.off(name, listener); |
75 resolve(); | 75 resolve(); |
76 }; | 76 }; |
77 | 77 |
78 this.on(name, listener); | 78 this.on(name, listener); |
79 }); | 79 }); |
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: function(name) | 88 listeners(name) |
89 { | 89 { |
90 let listeners = this._listeners[name]; | 90 let listeners = this._listeners[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: function(name) | 100 emit(name, ...args) |
101 { | 101 { |
102 let args = []; | |
103 for (let i = 1; i < arguments.length; i++) | |
104 args.push(arguments[i]); | |
105 | |
106 let listeners = this.listeners(name); | 102 let listeners = this.listeners(name); |
107 for (let listener of listeners) | 103 for (let listener of listeners) |
108 listener.apply(null, args); | 104 listener(...args); |
109 } | 105 } |
110 }; | 106 }; |
OLD | NEW |