From f6f0b387eab735c49e0987020f5673950d5c9f91 Mon Sep 17 00:00:00 2001 From: DavidCai Date: Fri, 29 Apr 2016 10:47:06 +0800 Subject: [PATCH] events: pass the original listener added by once When removing a `once` listener, the listener being passed to the `removeListener` callback is the wrapper. This unwraps the listener so that `removeListener` is passed the actual listener. PR-URL: https://github.com/nodejs/node/pull/6394 Reviewed-By: Colin Ihrig Reviewed-By: James M Snell --- lib/events.js | 7 ++++--- test/parallel/test-event-emitter-remove-listeners.js | 11 +++++++++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/lib/events.js b/lib/events.js index 6cb6900267b2ec..3cc16d20a32d22 100644 --- a/lib/events.js +++ b/lib/events.js @@ -270,7 +270,7 @@ EventEmitter.prototype.once = function once(type, listener) { // emits a 'removeListener' event iff the listener was removed EventEmitter.prototype.removeListener = function removeListener(type, listener) { - var list, events, position, i; + var list, events, position, i, originalListener; if (typeof listener !== 'function') throw new TypeError('listener must be a function'); @@ -289,7 +289,7 @@ EventEmitter.prototype.removeListener = else { delete events[type]; if (events.removeListener) - this.emit('removeListener', type, listener); + this.emit('removeListener', type, list.listener || listener); } } else if (typeof list !== 'function') { position = -1; @@ -297,6 +297,7 @@ EventEmitter.prototype.removeListener = for (i = list.length; i-- > 0;) { if (list[i] === listener || (list[i].listener && list[i].listener === listener)) { + originalListener = list[i].listener; position = i; break; } @@ -318,7 +319,7 @@ EventEmitter.prototype.removeListener = } if (events.removeListener) - this.emit('removeListener', type, listener); + this.emit('removeListener', type, originalListener || listener); } return this; diff --git a/test/parallel/test-event-emitter-remove-listeners.js b/test/parallel/test-event-emitter-remove-listeners.js index 95b39f4555b903..0d2ce551d3fcf9 100644 --- a/test/parallel/test-event-emitter-remove-listeners.js +++ b/test/parallel/test-event-emitter-remove-listeners.js @@ -98,3 +98,14 @@ e6.emit('hello'); // Interal listener array [listener3] e6.emit('hello'); + +const e7 = new events.EventEmitter(); + +const listener5 = () => {}; + +e7.once('hello', listener5); +e7.on('removeListener', common.mustCall((eventName, listener) => { + assert.strictEqual(eventName, 'hello'); + assert.strictEqual(listener, listener5); +})); +e7.emit('hello');