Skip to content

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

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

jasnell
Copy link
Member

@jasnell jasnell commented May 25, 2025

Marking this draft as I'm not 100% sure it's something we want. Looking for feedback and opinions.

This adds new use(...) and useOnce(...) methods to EventEmitter 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.

const ee = new EventEmitter();
{
  using ds = new DisposableStack();
  ds.use(ee.use('foo', () => {});
  ds.use(ee.use('bar', () => {});
  if (something) ds.use(ee.use('baz', () => {});
}
// The event listeners will all be removed automatically when ds is disposed.
// We don't need to separately remember to call removeListener/off to cleanup the event listeners

@jasnell jasnell requested review from mcollina, addaleax and anonrig May 25, 2025 19:01
@nodejs-github-bot
Copy link
Collaborator

Review requested:

  • @nodejs/streams
  • @nodejs/test_runner

@nodejs-github-bot nodejs-github-bot added events Issues and PRs related to the events subsystem / EventEmitter. needs-ci PRs that need a full CI run. stream Issues and PRs related to the stream subsystem. test_runner Issues and PRs related to the test runner subsystem. labels May 25, 2025
{
// Verifiy that the disposable stack removes the handlers
// when the stack is disposed.
// TODO(@jasnell): DisposableStack is not recognized by eslint?
Copy link
Member

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)`
Copy link
Member

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.

Copy link
Member Author

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.

@jasnell
Copy link
Member Author

jasnell commented May 31, 2025

Relevant to advancing on this: #58526

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
events Issues and PRs related to the events subsystem / EventEmitter. needs-ci PRs that need a full CI run. stream Issues and PRs related to the stream subsystem. test_runner Issues and PRs related to the test runner subsystem.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants