Skip to content

Commit 2c92a1f

Browse files
DavidCai1111Fishrock123
authored andcommitted
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 9f23cb2 commit 2c92a1f

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
@@ -308,7 +308,7 @@ EventEmitter.prototype.prependOnceListener =
308308
// emits a 'removeListener' event iff the listener was removed
309309
EventEmitter.prototype.removeListener =
310310
function removeListener(type, listener) {
311-
var list, events, position, i;
311+
var list, events, position, i, originalListener;
312312

313313
if (typeof listener !== 'function')
314314
throw new TypeError('"listener" argument must be a function');
@@ -327,14 +327,15 @@ EventEmitter.prototype.removeListener =
327327
else {
328328
delete events[type];
329329
if (events.removeListener)
330-
this.emit('removeListener', type, listener);
330+
this.emit('removeListener', type, list.listener || listener);
331331
}
332332
} else if (typeof list !== 'function') {
333333
position = -1;
334334

335335
for (i = list.length; i-- > 0;) {
336336
if (list[i] === listener ||
337337
(list[i].listener && list[i].listener === listener)) {
338+
originalListener = list[i].listener;
338339
position = i;
339340
break;
340341
}
@@ -356,7 +357,7 @@ EventEmitter.prototype.removeListener =
356357
}
357358

358359
if (events.removeListener)
359-
this.emit('removeListener', type, listener);
360+
this.emit('removeListener', type, originalListener || listener);
360361
}
361362

362363
return this;

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

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

103103
// Interal listener array [listener3]
104104
e6.emit('hello');
105+
106+
const e7 = new events.EventEmitter();
107+
108+
const listener5 = () => {};
109+
110+
e7.once('hello', listener5);
111+
e7.on('removeListener', common.mustCall((eventName, listener) => {
112+
assert.strictEqual(eventName, 'hello');
113+
assert.strictEqual(listener, listener5);
114+
}));
115+
e7.emit('hello');

0 commit comments

Comments
 (0)