mirror of https://github.com/nodejs/node.git
events: fix TypeError in removeAllListeners
Check that `listeners` is actually an array before trying to manipulate it because it won't be if no regular event listeners have been registered yet but there are 'removeListener' event listeners.pull/37258/head
parent
a34bbaf31b
commit
71aabedad4
|
@ -263,7 +263,7 @@ EventEmitter.prototype.removeAllListeners = function(type) {
|
|||
|
||||
if (typeof listeners === 'function') {
|
||||
this.removeListener(type, listeners);
|
||||
} else {
|
||||
} else if (Array.isArray(listeners)) {
|
||||
// LIFO order
|
||||
while (listeners.length)
|
||||
this.removeListener(type, listeners[listeners.length - 1]);
|
||||
|
|
|
@ -71,3 +71,10 @@ e2.removeAllListeners();
|
|||
console.error(e2);
|
||||
assert.deepEqual([], e2.listeners('foo'));
|
||||
assert.deepEqual([], e2.listeners('bar'));
|
||||
|
||||
var e3 = new events.EventEmitter();
|
||||
e3.on('removeListener', listener);
|
||||
// check for regression where removeAllListeners throws when
|
||||
// there exists a removeListener listener, but there exists
|
||||
// no listeners for the provided event type
|
||||
assert.doesNotThrow(e3.removeAllListeners.bind(e3, 'foo'));
|
||||
|
|
Loading…
Reference in New Issue