Browse Source

Revert "events: don't delete the listeners array in removeListener()"

This reverts commit 928ea564d1.

Keeping the original Array instance in-place essentially causes a memory leak
on EventEmitters that use an infinite number of event names (an incrementing
counter, for example), which isn't an unreasonable thing to want to do.

Fixes #3702.
v0.8.7-release
Nathan Rajlich 13 years ago
parent
commit
713b9249e1
  1. 2
      lib/events.js
  2. 9
      test/simple/test-event-emitter-remove-listeners.js

2
lib/events.js

@ -214,6 +214,8 @@ EventEmitter.prototype.removeListener = function(type, listener) {
if (position < 0) return this; if (position < 0) return this;
list.splice(position, 1); list.splice(position, 1);
if (list.length == 0)
delete this._events[type];
} else if (list === listener || } else if (list === listener ||
(list.listener && list.listener === listener)) (list.listener && list.listener === listener))
{ {

9
test/simple/test-event-emitter-remove-listeners.js

@ -42,15 +42,10 @@ function listener3() {
} }
var e1 = new events.EventEmitter(); var e1 = new events.EventEmitter();
var e1listeners = e1.listeners('hello');
e1.on('hello', listener1); e1.on('hello', listener1);
assert.equal(e1listeners.length, 1);
e1.removeListener('hello', listener1); e1.removeListener('hello', listener1);
assert.deepEqual([], e1.listeners('hello')); assert.deepEqual([], e1.listeners('hello'));
// identity check, listeners array should be the same
assert.equal(e1listeners, e1.listeners('hello'));
var e2 = new events.EventEmitter(); var e2 = new events.EventEmitter();
e2.on('hello', listener1); e2.on('hello', listener1);
e2.removeListener('hello', listener2); e2.removeListener('hello', listener2);
@ -59,12 +54,8 @@ assert.deepEqual([listener1], e2.listeners('hello'));
var e3 = new events.EventEmitter(); var e3 = new events.EventEmitter();
e3.on('hello', listener1); e3.on('hello', listener1);
e3.on('hello', listener2); e3.on('hello', listener2);
var e3listeners = e3.listeners('hello');
assert.equal(e3listeners.length, 2)
e3.removeListener('hello', listener1); e3.removeListener('hello', listener1);
assert.equal(e3listeners.length, 1)
assert.deepEqual([listener2], e3.listeners('hello')); assert.deepEqual([listener2], e3.listeners('hello'));
assert.equal(e3listeners, e3.listeners('hello'));

Loading…
Cancel
Save