-
Notifications
You must be signed in to change notification settings - Fork 30.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
doc: update removeListener behaviour #5201
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7b91ac0
doc: update removeListener behaviour
vaibhav93 e7dffb0
doc: fix comment typo in events
vaibhav93 a21be7c
test: use mustCall instead of count variable
vaibhav93 b60d25f
doc: fix typo in events
vaibhav93 9ae5a70
doc: Improve wording for removeListener text
vaibhav93 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -377,6 +377,43 @@ 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. | ||
|
||
Note that once an event has been emitted, all listeners attached to it at the | ||
time of emitting will be called in order. This implies that any `removeListener()` | ||
or `removeAllListeners()` calls *after* emitting and *before* the last listener | ||
finishes execution will not remove them from `emit()` in progress. Subsequent | ||
events will behave as expected. | ||
|
||
```js | ||
const myEmitter = new MyEmitter(); | ||
|
||
var callbackA = () => { | ||
console.log('A'); | ||
myEmitter.removeListener('event', callbackB); | ||
}; | ||
|
||
var callbackB = () => { | ||
console.log('B'); | ||
}; | ||
|
||
myEmitter.on('event', callbackA); | ||
|
||
myEmitter.on('event', callbackB); | ||
|
||
// callbackA removes listener callbackB but it will still be called. | ||
// Interal listener array at time of emit [callbackA, callbackB] | ||
myEmitter.emit('event'); | ||
// Prints: | ||
// A | ||
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. Isn't this backwards? It should be |
||
// B | ||
|
||
// callbackB is now removed. | ||
// Interal listener array [callbackA] | ||
myEmitter.emit('event'); | ||
// Prints: | ||
// A | ||
|
||
``` | ||
|
||
Because listeners are managed using an internal array, calling this will | ||
change the position indices of any listener registered *after* the listener | ||
being removed. This will not impact the order in which listeners are called, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
There is a PR in place that validates code blocks in the docs per eslint rules.
Could you please add
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.
This initialization has been done in the first example of events doc and from there on each example simply uses myEmitter . I was trying to be consistent with the existing doc.
As for the linting of code blocks in docs, I'll do that at once.
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.
Don't worry about the linting of other code blocks, only of the one you are adding in this PR.
Each code block is a separate file in linting world, so yes it was declared above, but to satisfy linting, it needs to be declared in this block as well.
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.
So,
should be added only for the sake of linting that block and then removed before the commit. Or should that piece of code be pushed assuming linted docs would be adopted in the future.
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.
The latter, but hold up. @targos just made a comment about reducing the strictness of the linter.
I'd say ignore what I'm saying right now, if I get these changes landed I will fix this later.