Some info for EventEmitter.removeListener is missing in docs
- Subsystem: events
Description for emitter.on(eventName, listener):
Adds the
listenerfunction to the end of the listeners array for the event namedeventName. No checks are made to see if thelistenerhas already been added. Multiple calls passing the same combination ofeventNameandlistenerwill result in thelistenerbeing 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.
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.
I'm sure it is not so: otherwise there is a conflict in docs.
The
EventListenercalls 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?
The closed task is convicted to be buried. Well, mkay, I tried my best.