Skip to content

Commit f6f0b38

Browse files
DavidCai1111Myles Borins
authored and
Myles Borins
committed
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: #6394 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 96bdfae commit f6f0b38

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

lib/events.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ EventEmitter.prototype.once = function once(type, listener) {
270270
// emits a 'removeListener' event iff the listener was removed
271271
EventEmitter.prototype.removeListener =
272272
function removeListener(type, listener) {
273-
var list, events, position, i;
273+
var list, events, position, i, originalListener;
274274

275275
if (typeof listener !== 'function')
276276
throw new TypeError('listener must be a function');
@@ -289,14 +289,15 @@ EventEmitter.prototype.removeListener =
289289
else {
290290
delete events[type];
291291
if (events.removeListener)
292-
this.emit('removeListener', type, listener);
292+
this.emit('removeListener', type, list.listener || listener);
293293
}
294294
} else if (typeof list !== 'function') {
295295
position = -1;
296296

297297
for (i = list.length; i-- > 0;) {
298298
if (list[i] === listener ||
299299
(list[i].listener && list[i].listener === listener)) {
300+
originalListener = list[i].listener;
300301
position = i;
301302
break;
302303
}
@@ -318,7 +319,7 @@ EventEmitter.prototype.removeListener =
318319
}
319320

320321
if (events.removeListener)
321-
this.emit('removeListener', type, listener);
322+
this.emit('removeListener', type, originalListener || listener);
322323
}
323324

324325
return this;

test/parallel/test-event-emitter-remove-listeners.js

+11
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,14 @@ e6.emit('hello');
9898

9999
// Interal listener array [listener3]
100100
e6.emit('hello');
101+
102+
const e7 = new events.EventEmitter();
103+
104+
const listener5 = () => {};
105+
106+
e7.once('hello', listener5);
107+
e7.on('removeListener', common.mustCall((eventName, listener) => {
108+
assert.strictEqual(eventName, 'hello');
109+
assert.strictEqual(listener, listener5);
110+
}));
111+
e7.emit('hello');

0 commit comments

Comments
 (0)