Browse Source

events: don't delete the listeners array

The documentation implies that .removeAllListeners() leaves the listeners array
untouched. Make it so.
v0.9.1-release
Ben Noordhuis 13 years ago
parent
commit
78dc13fbf9
  1. 11
      lib/events.js
  2. 10
      test/simple/test-event-emitter-remove-all-listeners.js

11
lib/events.js

@ -203,8 +203,15 @@ EventEmitter.prototype.removeAllListeners = function(type) {
return this; return this;
} }
// does not use listeners(), so no side effect of creating _events[type] var events = this._events && this._events[type];
if (type && this._events && this._events[type]) this._events[type] = null; if (!events) return this;
if (isArray(events)) {
events.splice(0);
} else {
this._events[type] = null;
}
return this; return this;
}; };

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

@ -29,10 +29,14 @@ function listener() {}
var e1 = new events.EventEmitter(); var e1 = new events.EventEmitter();
e1.on('foo', listener); e1.on('foo', listener);
e1.on('bar', listener); e1.on('bar', listener);
var fooListeners = e1.listeners('foo');
var barListeners = e1.listeners('bar');
e1.removeAllListeners('foo'); e1.removeAllListeners('foo');
assert.deepEqual([], e1.listeners('foo')); assert.deepEqual(e1.listeners('foo'), []);
assert.deepEqual([listener], e1.listeners('bar')); assert.deepEqual(e1.listeners('bar'), [listener]);
// identity check, the array should not change
assert.equal(e1.listeners('foo'), fooListeners);
assert.equal(e1.listeners('bar'), barListeners);
var e2 = new events.EventEmitter(); var e2 = new events.EventEmitter();
e2.on('foo', listener); e2.on('foo', listener);

Loading…
Cancel
Save