| LEFT | RIGHT |
| 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 * | |
| 23 * @class | |
| 24 */ | 22 */ |
| 25 exports.EventEmitter = class EventEmitter | 23 class EventEmitter |
| 26 { | 24 { |
| 27 constructor() | 25 constructor() |
| 28 { | 26 { |
| 29 this._listeners = new Map(); | 27 this._listeners = new Map(); |
| 30 } | 28 } |
| 31 | 29 |
| 32 /** | 30 /** |
| 33 * adds a listener for the specified event name. | 31 * Adds a listener for the specified event name. |
| 34 * | 32 * |
| 35 * @param {string} name | 33 * @param {string} name |
| 36 * @param {function} listener | 34 * @param {function} listener |
| 37 */ | 35 */ |
| 38 on(name, listener) | 36 on(name, listener) |
| 39 { | 37 { |
| 40 let listeners = this._listeners.get(name); | 38 let listeners = this._listeners.get(name); |
| 41 if (listeners) | 39 if (listeners) |
| 42 listeners.push(listener); | 40 listeners.push(listener); |
| 43 else | 41 else |
| 44 this._listeners.set(name, [listener]); | 42 this._listeners.set(name, [listener]); |
| 45 } | 43 } |
| 44 |
| 46 /** | 45 /** |
| 47 * Removes a listener for the specified event name. | 46 * Removes a listener for the specified event name. |
| 48 * | 47 * |
| 49 * @param {string} name | 48 * @param {string} name |
| 50 * @param {function} listener | 49 * @param {function} listener |
| 51 */ | 50 */ |
| 52 off(name, listener) | 51 off(name, listener) |
| 53 { | 52 { |
| 54 let listeners = this._listeners.get(name); | 53 let listeners = this._listeners.get(name); |
| 55 if (listeners) | 54 if (listeners) |
| 56 { | 55 { |
| 57 let idx = listeners.indexOf(listener); | 56 let idx = listeners.indexOf(listener); |
| 58 if (idx != -1) | 57 if (idx != -1) |
| 59 listeners.splice(idx, 1); | 58 listeners.splice(idx, 1); |
| 60 } | 59 } |
| 61 } | 60 } |
| 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 * @param {string} name |
| 66 * @return {Promise} | 67 * @returns {Promise} |
| 67 */ | 68 */ |
| 68 once(name) | 69 once(name) |
| 69 { | 70 { |
| 70 return new Promise(resolve => | 71 return new Promise(resolve => |
| 71 { | 72 { |
| 72 let listener = () => | 73 let listener = () => |
| 73 { | 74 { |
| 74 this.off(name, listener); | 75 this.off(name, listener); |
| 75 resolve(); | 76 resolve(); |
| 76 }; | 77 }; |
| 77 | 78 |
| 78 this.on(name, listener); | 79 this.on(name, listener); |
| 79 }); | 80 }); |
| 80 } | 81 } |
| 82 |
| 81 /** | 83 /** |
| 82 * Returns a copy of the array of listeners for the specified event. | 84 * Returns a copy of the array of listeners for the specified event. |
| 83 * | 85 * |
| 84 * @param {string} name | 86 * @param {string} name |
| 85 * @return {function[]} | 87 * @returns {Array.<function>} |
| 86 */ | 88 */ |
| 87 listeners(name) | 89 listeners(name) |
| 88 { | 90 { |
| 89 let listeners = this._listeners.get(name); | 91 let listeners = this._listeners.get(name); |
| 90 return listeners ? listeners.slice() : []; | 92 return listeners ? listeners.slice() : []; |
| 91 } | 93 } |
| 94 |
| 92 /** | 95 /** |
| 93 * Calls all previously added listeners for the given event name. | 96 * Calls all previously added listeners for the given event name. |
| 94 * | 97 * |
| 95 * @param {string} name | 98 * @param {string} name |
| 96 * @param {...*} [arg] | 99 * @param {...*} [args] |
| 97 */ | 100 */ |
| 98 emit(name, ...args) | 101 emit(name, ...args) |
| 99 { | 102 { |
| 100 let listeners = this.listeners(name); | 103 let listeners = this.listeners(name); |
| 101 for (let listener of listeners) | 104 for (let listener of listeners) |
| 102 listener(...args); | 105 listener(...args); |
| 103 } | 106 } |
| 104 }; | 107 } |
| 108 |
| 109 exports.EventEmitter = EventEmitter; |
| LEFT | RIGHT |