Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

events: pass the original listener added by EventEmitter#once to the removeListener handler #6394

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions lib/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ EventEmitter.prototype.prependOnceListener =
// 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" argument must be a function');
Expand All @@ -327,14 +327,15 @@ 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;

for (i = list.length; i-- > 0;) {
if (list[i] === listener ||
(list[i].listener && list[i].listener === listener)) {
originalListener = list[i].listener;
position = i;
break;
}
Expand All @@ -356,7 +357,7 @@ EventEmitter.prototype.removeListener =
}

if (events.removeListener)
this.emit('removeListener', type, listener);
this.emit('removeListener', type, originalListener || listener);
}

return this;
Expand Down
11 changes: 11 additions & 0 deletions test/parallel/test-event-emitter-remove-listeners.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,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');