Skip to content

events: removeListener event should be passed the user's listener function when using once() #5551

@omsmith

Description

@omsmith
  • Version: v4.3.2
  • Platform: Linux osmith-ubuntu-t1700 4.2.0-30-generic #36-Ubuntu SMP Fri Feb 26 00:58:07 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
  • Subsystem: events

tl;dr; I think both of these lines should pass listener.listener || listener
https://github.com/nodejs/node/blob/master/lib/events.js#L309
https://github.com/nodejs/node/blob/master/lib/events.js#L338

When using ee.once(), the user's listener function is wrapped. This is correctly accounted for during addListener when emitting the 'newListener' event, but not during removeListener and the 'removeListener' event.

I can PR later this evening - just opening the issue for the moment.

Test case:

'use strict';

const assert = require('assert');
const EventEmitter = require('events');

const ee = new EventEmitter();

function myListener () {};

let addedType = null;
let addedListener = null;
let removedType = null;
let removedListener = null;

ee.on('removeListener', (type, listener) => {
  removedType = type;
  removedListener = listener;
});
ee.on('newListener', (type, listener) => {
  addedType = type;
  addedListener = listener;
});

ee.once('foo', myListener);

ee.emit('foo');

process.on('exit', () => {
  assert.equal('foo', addedType);
  assert.equal(addedListener, myListener);
  assert.equal('foo', removedType);
  process._rawDebug('next assertion will fail');
  assert.equal(removedListener, myListener);
});
next assertion will fail

assert.js:89
  throw new assert.AssertionError({
  ^
AssertionError: { [Function: g] listener: [Function: myListener] } == [Function: myListener]
    at process.<anonymous> (/home/omsmith/ee-remove-listener-test/index.js:33:9)
    at emitOne (events.js:77:13)
    at process.emit (events.js:169:7)

Metadata

Metadata

Assignees

No one assigned

    Labels

    eventsIssues and PRs related to the events subsystem / EventEmitter.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions