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: Created Jan. 9, 2019, 2:49 p.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 | no next file » | 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld