-
Notifications
You must be signed in to change notification settings - Fork 7.3k
Events doc fixes #8883
Events doc fixes #8883
Changes from all commits
f5620ba
8360257
863e718
ef513cb
60724d1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,7 +33,10 @@ added and `'removeListener'` when a listener is removed. | |
### emitter.addListener(event, listener) | ||
### emitter.on(event, listener) | ||
|
||
Adds a listener to the end of the listeners array for the specified event. | ||
Adds a listener to the end of the listeners array for the specified `event`. | ||
No checks are made to see if the `listener` has already been added. Multiple | ||
calls passing the same combination of `event` and `listener` will result | ||
in the `listener` being added multiple times. | ||
|
||
server.on('connection', function (stream) { | ||
console.log('someone connected!'); | ||
|
@@ -65,6 +68,11 @@ Remove a listener from the listener array for the specified event. | |
// ... | ||
server.removeListener('connection', callback); | ||
|
||
`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 `event`, then `removeListener` must be called | ||
multiple times to remove each instance. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is less obvious. |
||
|
||
Returns emitter, so calls can be chained. | ||
|
||
### emitter.removeAllListeners([event]) | ||
|
@@ -121,8 +129,9 @@ Return the number of listeners for a given event. | |
* `event` {String} The event name | ||
* `listener` {Function} The event handler function | ||
|
||
This event is emitted any time someone adds a new listener. It is unspecified | ||
if `listener` is in the list returned by `emitter.listeners(event)`. | ||
This event is emitted any time someone adds a listener, even if the added | ||
listener was previously already added to the listeners array for the given | ||
`event`. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is borderline misleading... it seems to me to almost suggest that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps...
Truthfully, tho, the best solution would be to simply prevent listeners from being added multiple times. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
So, you can get the no-multiples behaviour from the current API, whereas if it didn't allow multiples, you couldn't get the current behaviour. |
||
|
||
|
||
### Event: 'removeListener' | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this its obvious that when you add a listener it adds a listener. Always. But, it is clear.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, but what wasn't obvious (per the issue I was addressing) is that a single listener can be added multiple times.