node icon indicating copy to clipboard operation
node copied to clipboard

Some info for EventEmitter.removeListener is missing in docs

Open offshore opened this issue 10 years ago • 3 comments

  • Subsystem: events

Description for emitter.on(eventName, listener):

Adds the listener function to the end of the listeners array for the event named eventName. No checks are made to see if the listener has already been added. Multiple calls passing the same combination of eventName and listener will result in the listener being added, and called, multiple times.

That's OK and sufficiently comprehensive (although it doesn't clarify that "add" is "add as LAST", i.e. "push"). However, for description on emitter.removeListener(eventName, listener) nothing said about a case when there are many copies of one listener in list. Does removeListener remove the most recently added listener or the least one? Well, actually it searches for provided listener from the end of list; but despite it may seems obvious, I had to check the events.js source to be sure (master points to 84ebf2b at the time of this post). I suggest that it should be noted in docs.

offshore avatar Apr 22 '16 08:04 offshore

The current docs say, removeListener will remove, at most, one instance of a listener from the listener array. If any single listener has been added multiple times to the listener array for the specified eventName, then removeListener must be called multiple times to remove each instance. and for addListener/on it says, Adds the listener function to the end of the listeners array for the event named eventName.

Whether removeListener adds the most or least recently added copy is an internal implementation detail.

jasnell avatar Apr 22 '16 14:04 jasnell

I'm sure it is not so: otherwise there is a conflict in docs.

The EventListener calls all listeners synchronously in the order in which they were registered. This is important to ensure the proper sequencing of events and to avoid race conditions or logic errors.

Isn't it a guarantee that addListener adds provided listener to the end of internal listeners list? So, if addLister behaves such way -- and this is guaranteed (speaking your words, this is an obvious disclosure of internal implementation details), why removeListener behavior is not qualified?

Well, OK. If one should not rely on removeListener to remove the most recent of handler copies (which I still think is wrong), why THIS is not mentioned?

offshore avatar Apr 22 '16 14:04 offshore

The closed task is convicted to be buried. Well, mkay, I tried my best.

offshore avatar Apr 25 '16 08:04 offshore