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

Fix #3425: removeAllListeners should delete array #3431

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 2 additions & 9 deletions lib/events.js
Expand Up @@ -228,15 +228,8 @@ EventEmitter.prototype.removeAllListeners = function(type) {
return this; return this;
} }


var events = this._events && this._events[type]; // does not use listeners(), so no side effect of creating _events[type]
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TooTallNate The reverted behavior explicitly tries to avoid calling listeners() to avoid creating _events[type]. Is that still needed?. Regarding your proposal in #3425 (comment)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I personally think that's overkill. I don't consider removeAllListeners() to be a hot code path enough to justify that, but others would need to weigh in on that as well.

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

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

return this; 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('foo'), [listener]);
assert.deepEqual(e1.listeners('bar'), []); assert.deepEqual(e1.listeners('bar'), []);
assert.deepEqual(e1.listeners('baz'), []); assert.deepEqual(e1.listeners('baz'), []);
// identity check, the array should not change // after calling removeAllListeners,
assert.equal(e1.listeners('foo'), fooListeners); // the old listeners array should stay unchanged
assert.equal(e1.listeners('bar'), barListeners); assert.deepEqual(fooListeners, [listener]);
assert.equal(e1.listeners('baz'), bazListeners); 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(); var e2 = new events.EventEmitter();
e2.on('foo', listener); e2.on('foo', listener);
Expand Down