Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

EventEmitter.removeListener removes only one listener #5211

Closed
CrabDude opened this issue Apr 3, 2013 · 2 comments
Closed

EventEmitter.removeListener removes only one listener #5211

CrabDude opened this issue Apr 3, 2013 · 2 comments
Labels

Comments

@CrabDude
Copy link

CrabDude commented Apr 3, 2013

This is different from a lot of other libraries (jQuery) and removeEventListener in the DOM where the same listener can be added multiple times, and removal removes all handlers for for the same event/listener combo.

It seems like the only way to determine if all events of the same type/handler are removed, is to listen for removeListener and keep calling removeListener until it's no longer emitted, or to manually iterate through the listener array checking for listener.

Example of issue:

function foo() {}

process.on('foo', foo)
process.on('foo', foo)
process.emit('foo') // foo called twice

process.removeListener('foo', foo)
process.emit('foo') // foo still called

Is this by design? It would cause memory leaks if someone was not familiar with this issue and was extending and creating a a long lasting EventEmitter.

@bnoordhuis
Copy link
Member

Is this by design?

Maybe not originally but it's pretty much codified now. I don't want to change the behavior either; EventEmitter#removeListener() gets called a lot and is already quite expensive. Doing mandatory full scans over the listeners array would make that a lot worse.

@isaacs
Copy link

isaacs commented Apr 4, 2013

The event subsystem is locked at this point. I'd be willing to accept refactoring changes to improve performance, but any interface changes should be considered final as of 0.10.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants