diff --git a/lib/events.js b/lib/events.js index 7e219a14a4..c2b604f892 100644 --- a/lib/events.js +++ b/lib/events.js @@ -214,6 +214,8 @@ EventEmitter.prototype.removeListener = function(type, listener) { if (position < 0) return this; list.splice(position, 1); + if (list.length == 0) + delete this._events[type]; } else if (list === listener || (list.listener && list.listener === listener)) { diff --git a/test/simple/test-event-emitter-remove-listeners.js b/test/simple/test-event-emitter-remove-listeners.js index 33ee3df564..70526668c4 100644 --- a/test/simple/test-event-emitter-remove-listeners.js +++ b/test/simple/test-event-emitter-remove-listeners.js @@ -42,15 +42,10 @@ function listener3() { } var e1 = new events.EventEmitter(); -var e1listeners = e1.listeners('hello'); e1.on('hello', listener1); -assert.equal(e1listeners.length, 1); e1.removeListener('hello', listener1); assert.deepEqual([], e1.listeners('hello')); -// identity check, listeners array should be the same -assert.equal(e1listeners, e1.listeners('hello')); - var e2 = new events.EventEmitter(); e2.on('hello', listener1); e2.removeListener('hello', listener2); @@ -59,12 +54,8 @@ assert.deepEqual([listener1], e2.listeners('hello')); var e3 = new events.EventEmitter(); e3.on('hello', listener1); e3.on('hello', listener2); -var e3listeners = e3.listeners('hello'); -assert.equal(e3listeners.length, 2) e3.removeListener('hello', listener1); -assert.equal(e3listeners.length, 1) assert.deepEqual([listener2], e3.listeners('hello')); -assert.equal(e3listeners, e3.listeners('hello'));