-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
events: Add use/useOnce methods to EventEmitter #58453
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
base: main
Are you sure you want to change the base?
Conversation
Review requested:
|
{ | ||
// Verifiy that the disposable stack removes the handlers | ||
// when the stack is disposed. | ||
// TODO(@jasnell): DisposableStack is not recognized by eslint? |
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.
Opened #58454 to address this.
@@ -1085,6 +1085,54 @@ newListeners[0](); | |||
emitter.emit('log'); | |||
``` | |||
|
|||
### `emitter.use(eventName, listener)` |
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.
A lot of different things can extend EventEmitter
class. The name use()
is too generic and might also collide with existing userspace code, I think useEventListener()
(or addDisposableEventListener()
, onUse()
, scopedOn()
, etc., it's quite bikesheddable...) would be better.
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.
Yeah I kind of picked use
here intentionally because of that ;-) ... wanted to prompt a discussion about it.
Relevant to advancing on this: #58526 |
Marking this draft as I'm not 100% sure it's something we want. Looking for feedback and opinions.
This adds new
use(...)
anduseOnce(...)
methods toEventEmitter
that returns a disposable object that will unregister the event listeners when disposed, along with two changes that use the new apis to demonstrate how it can be used to simplify some cleanup.