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

Unified Diff: lib/events.js

Issue 29977555: Issue 7202 - Add hasListeners method to EventEmitter (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Remove checks from on() and off() Created Jan. 10, 2019, 4 a.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | test/filterNotifier.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/events.js
===================================================================
--- a/lib/events.js
+++ b/lib/events.js
@@ -48,19 +48,28 @@
* @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);
+ if (listeners.length > 1)
+ {
+ let idx = listeners.indexOf(listener);
+ if (idx != -1)
+ listeners.splice(idx, 1);
+ }
+ else if (listeners[0] === listener)
+ {
+ // We must use strict equality above for compatibility with
+ // Array.prototype.indexOf
+ this._listeners.delete(name);
+ }
}
}
/**
* Adds a one time listener and returns a promise that
* is resolved the next time the specified event is emitted.
*
* @param {string} name
@@ -83,27 +92,43 @@
/**
* Returns a copy of the array of listeners for the specified event.
*
* @param {string} name
* @returns {Array.<function>}
*/
listeners(name)
{
- let listeners = this._listeners.get(name);
+ let listeners = this._listeners.size > 0 ? this._listeners.get(name) : null;
return listeners ? listeners.slice() : [];
}
/**
+ * Checks whether there are any listeners for the specified event.
+ *
+ * @param {string} [name] The name of the event. If omitted, checks whether
+ * there are any listeners for any event.
+ * @returns {boolean}
+ */
+ hasListeners(name)
+ {
+ return this._listeners.size > 0 &&
+ (typeof name == "undefined" || this._listeners.has(name));
+ }
+
+ /**
* Calls all previously added listeners for the given event name.
*
* @param {string} name
* @param {...*} [args]
*/
emit(name, ...args)
{
- let listeners = this.listeners(name);
- for (let listener of listeners)
- listener(...args);
+ let listeners = this._listeners.size > 0 ? this._listeners.get(name) : null;
+ if (listeners)
+ {
+ for (let listener of listeners.slice())
+ listener(...args);
+ }
}
}
exports.EventEmitter = EventEmitter;
« no previous file with comments | « no previous file | test/filterNotifier.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld