Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
Fix #3425: removeAllListeners should delete array
Browse files Browse the repository at this point in the history
When removeAllListeners is called, the listeners array
is deleted to maintain compatibility with v0.6.

Reverts "events: don't delete the listeners array"

This reverts commit 78dc13f.

Conflicts:

	test/simple/test-event-emitter-remove-all-listeners.js
  • Loading branch information
reid authored and isaacs committed Jun 15, 2012
1 parent 13400e3 commit c9a1b5d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
11 changes: 2 additions & 9 deletions lib/events.js
Expand Up @@ -228,15 +228,8 @@ EventEmitter.prototype.removeAllListeners = function(type) {
return this;
}

var events = this._events && this._events[type];
if (!events) return this;

if (isArray(events)) {
events.splice(0);
} else {
this._events[type] = null;
}

// does not use listeners(), so no side effect of creating _events[type]
if (type && this._events && this._events[type]) this._events[type] = null;
return this;
};

Expand Down
13 changes: 9 additions & 4 deletions test/simple/test-event-emitter-remove-all-listeners.js
Expand Up @@ -39,10 +39,15 @@ e1.removeAllListeners('baz');
assert.deepEqual(e1.listeners('foo'), [listener]);
assert.deepEqual(e1.listeners('bar'), []);
assert.deepEqual(e1.listeners('baz'), []);
// identity check, the array should not change
assert.equal(e1.listeners('foo'), fooListeners);
assert.equal(e1.listeners('bar'), barListeners);
assert.equal(e1.listeners('baz'), bazListeners);
// after calling removeAllListeners,
// the old listeners array should stay unchanged
assert.deepEqual(fooListeners, [listener]);
assert.deepEqual(barListeners, [listener]);
assert.deepEqual(bazListeners, [listener, listener]);
// after calling removeAllListeners,
// new listeners arrays are different from the old
assert.notEqual(e1.listeners('bar'), barListeners);
assert.notEqual(e1.listeners('baz'), bazListeners);

var e2 = new events.EventEmitter();
e2.on('foo', listener);
Expand Down

0 comments on commit c9a1b5d

Please sign in to comment.