Skip to content

Commit f5cdc4c

Browse files
committed
doc: clarify add/removeListener semantics
Clarify that adding or removing a listener is not idempotent. RE: nodejs#8853 PR: nodejs#8911 PR-URL: nodejs#8911 Signed-off-by: Timothy J Fontaine <[email protected]>
1 parent a9aa5b2 commit f5cdc4c

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

doc/api/events.markdown

+13-5
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ added and `'removeListener'` when a listener is removed.
3333
### emitter.addListener(event, listener)
3434
### emitter.on(event, listener)
3535

36-
Adds a listener to the end of the listeners array for the specified event.
36+
Adds a listener to the end of the listeners array for the specified `event`.
37+
No checks are made to see if the `listener` has already been added. Multiple
38+
calls passing the same combination of `event` and `listener` will result in the
39+
`listener` being added multiple times.
3740

3841
server.on('connection', function (stream) {
3942
console.log('someone connected!');
@@ -65,6 +68,11 @@ Remove a listener from the listener array for the specified event.
6568
// ...
6669
server.removeListener('connection', callback);
6770

71+
`removeListener` will remove, at most, one instance of a listener from the
72+
listener array. If any single listener has been added multiple times to the
73+
listener array for the specified `event`, then `removeListener` must be called
74+
multiple times to remove each instance.
75+
6876
Returns emitter, so calls can be chained.
6977

7078
### emitter.removeAllListeners([event])
@@ -110,14 +118,14 @@ Return the number of listeners for a given event.
110118
* `event` {String} The event name
111119
* `listener` {Function} The event handler function
112120

113-
This event is emitted any time someone adds a new listener. It is unspecified
114-
if `listener` is in the list returned by `emitter.listeners(event)`.
121+
This event is emitted any time a listener is added. When this event is triggered,
122+
the listener may not yet have been added to the array of listeners for the `event`.
115123

116124

117125
### Event: 'removeListener'
118126

119127
* `event` {String} The event name
120128
* `listener` {Function} The event handler function
121129

122-
This event is emitted any time someone removes a listener. It is unspecified
123-
if `listener` is in the list returned by `emitter.listeners(event)`.
130+
This event is emitted any time someone removes a listener. When this event is triggered,
131+
the listener may not yet have been removed from the array of listeners for the `event`.

0 commit comments

Comments
 (0)