Skip to content
This repository

Fix #3425: removeAllListeners should delete array #3431

Closed
wants to merge 1 commit into from

3 participants

Reid Burke Nathan Rajlich Isaac Z. Schlueter
Reid Burke
reid commented

When removeAllListeners is called, the listeners array is deleted to maintain compatibility with v0.6.

This request fixes issue #3425 by reverting 78dc13f. In addition to the revert, I've updated the tests to match the reverted behavior.

Reid Burke reid Fix #3425: removeAllListeners should delete array
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
2fd00b8
Nathan Rajlich
Collaborator

See #3425 (comment) for my alternate proposal for a solution.

Reid Burke
reid commented

@isaacs This needs an API doc clarification, but I haven't yet come up with something that doesn't refer to EventEmitter's internals.

Reid Burke reid commented on the diff
lib/events.js
@@ -228,15 +228,8 @@ EventEmitter.prototype.removeAllListeners = function(type) {
228 228 return this;
229 229 }
230 230
231   - var events = this._events && this._events[type];
232   - if (!events) return this;
233   -
234   - if (isArray(events)) {
235   - events.splice(0);
236   - } else {
237   - this._events[type] = null;
238   - }
239   -
  231 + // does not use listeners(), so no side effect of creating _events[type]
2
Reid Burke
reid added a note

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

Nathan Rajlich Collaborator
TooTallNate added a note

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Isaac Z. Schlueter
Collaborator
isaacs commented

Landed, along with an alternate fix for the thing that this was fixing, and clarification in the docs. Thanks, everyone :)

Isaac Z. Schlueter isaacs closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Jun 13, 2012
Reid Burke reid Fix #3425: removeAllListeners should delete array
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
2fd00b8
This page is out of date. Refresh to see the latest.
11 lib/events.js
@@ -228,15 +228,8 @@ EventEmitter.prototype.removeAllListeners = function(type) {
228 228 return this;
229 229 }
230 230
231   - var events = this._events && this._events[type];
232   - if (!events) return this;
233   -
234   - if (isArray(events)) {
235   - events.splice(0);
236   - } else {
237   - this._events[type] = null;
238   - }
239   -
  231 + // does not use listeners(), so no side effect of creating _events[type]
  232 + if (type && this._events && this._events[type]) this._events[type] = null;
240 233 return this;
241 234 };
242 235
13 test/simple/test-event-emitter-remove-all-listeners.js
@@ -39,10 +39,15 @@ e1.removeAllListeners('baz');
39 39 assert.deepEqual(e1.listeners('foo'), [listener]);
40 40 assert.deepEqual(e1.listeners('bar'), []);
41 41 assert.deepEqual(e1.listeners('baz'), []);
42   -// identity check, the array should not change
43   -assert.equal(e1.listeners('foo'), fooListeners);
44   -assert.equal(e1.listeners('bar'), barListeners);
45   -assert.equal(e1.listeners('baz'), bazListeners);
  42 +// after calling removeAllListeners,
  43 +// the old listeners array should stay unchanged
  44 +assert.deepEqual(fooListeners, [listener]);
  45 +assert.deepEqual(barListeners, [listener]);
  46 +assert.deepEqual(bazListeners, [listener, listener]);
  47 +// after calling removeAllListeners,
  48 +// new listeners arrays are different from the old
  49 +assert.notEqual(e1.listeners('bar'), barListeners);
  50 +assert.notEqual(e1.listeners('baz'), bazListeners);
46 51
47 52 var e2 = new events.EventEmitter();
48 53 e2.on('foo', listener);

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.