Index: lib/events.js |
=================================================================== |
--- a/lib/events.js |
+++ b/lib/events.js |
@@ -14,94 +14,96 @@ |
* You should have received a copy of the GNU General Public License |
* along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
*/ |
"use strict"; |
/** |
* Registers and emits named events. |
- * |
- * @constructor |
*/ |
-exports.EventEmitter = function() |
+class EventEmitter |
{ |
- this._listeners = new Map(); |
-}; |
+ constructor() |
+ { |
+ this._listeners = new Map(); |
+ } |
-exports.EventEmitter.prototype = { |
/** |
* Adds a listener for the specified event name. |
* |
* @param {string} name |
* @param {function} listener |
*/ |
on(name, listener) |
{ |
let listeners = this._listeners.get(name); |
if (listeners) |
listeners.push(listener); |
else |
this._listeners.set(name, [listener]); |
- }, |
+ } |
/** |
* Removes a listener for the specified event name. |
* |
* @param {string} name |
* @param {function} listener |
*/ |
off(name, listener) |
{ |
let listeners = this._listeners.get(name); |
if (listeners) |
{ |
let idx = listeners.indexOf(listener); |
if (idx != -1) |
listeners.splice(idx, 1); |
} |
- }, |
+ } |
/** |
* Adds a one time listener and returns a promise that |
* is resolved the next time the specified event is emitted. |
+ * |
* @param {string} name |
- * @return {Promise} |
+ * @returns {Promise} |
*/ |
once(name) |
{ |
return new Promise(resolve => |
{ |
let listener = () => |
{ |
this.off(name, listener); |
resolve(); |
}; |
this.on(name, listener); |
}); |
- }, |
+ } |
/** |
* Returns a copy of the array of listeners for the specified event. |
* |
* @param {string} name |
- * @return {function[]} |
+ * @returns {Array.<function>} |
*/ |
listeners(name) |
{ |
let listeners = this._listeners.get(name); |
return listeners ? listeners.slice() : []; |
- }, |
+ } |
/** |
* Calls all previously added listeners for the given event name. |
* |
* @param {string} name |
- * @param {...*} [arg] |
+ * @param {...*} [args] |
*/ |
emit(name, ...args) |
{ |
let listeners = this.listeners(name); |
for (let listener of listeners) |
listener(...args); |
} |
-}; |
+} |
+ |
+exports.EventEmitter = EventEmitter; |