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 |