diff --git a/lib/events.js b/lib/events.js index c77032fe43..682a9280b1 100644 --- a/lib/events.js +++ b/lib/events.js @@ -90,7 +90,7 @@ process.EventEmitter.prototype.removeListener = function (type, listener) { var i = list.indexOf(listener); if (i < 0) return this; list.splice(i, 1); - } else { + } else if (this._events[type] === listener) { this._events[type] = null; } diff --git a/test/simple/test-event-emitter-remove-listeners.js b/test/simple/test-event-emitter-remove-listeners.js new file mode 100644 index 0000000000..87451c47da --- /dev/null +++ b/test/simple/test-event-emitter-remove-listeners.js @@ -0,0 +1,39 @@ +require("../common"); +var events = require('events'); + + +count = 0; + +function listener1 () { + puts('listener1'); + count++; +} + +function listener2 () { + puts('listener2'); + count++; +} + +function listener3 () { + puts('listener3'); + count++; +} + +e1 = new events.EventEmitter(); +e1.addListener("hello", listener1); +e1.removeListener("hello", listener1); +assert.deepEqual([], e1.listeners('hello')); + +e2 = new events.EventEmitter(); +e2.addListener("hello", listener1); +e2.removeListener("hello", listener2); +assert.deepEqual([listener1], e2.listeners('hello')); + +e3 = new events.EventEmitter(); +e3.addListener("hello", listener1); +e3.addListener("hello", listener2); +e3.removeListener("hello", listener1); +assert.deepEqual([listener2], e3.listeners('hello')); + + +