Browse Source

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.
v0.10.23-release
Jeremy Martin 11 years ago
committed by Ben Noordhuis
parent
commit
71aabedad4
  1. 2
      lib/events.js
  2. 7
      test/simple/test-event-emitter-remove-all-listeners.js

2
lib/events.js

@ -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]);

7
test/simple/test-event-emitter-remove-all-listeners.js

@ -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…
Cancel
Save