Skip to content

refactor: rename event emitter class #2159

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

Merged
merged 2 commits into from
Oct 25, 2023
Merged

Conversation

achingbrain
Copy link
Member

@achingbrain achingbrain commented Oct 13, 2023

We add types to a EventEmitter class which extends the plain js EventTarget.

The name is chosen to give familiarity to node developers but it's not generally a good idea.

This refactor:

  1. Exports a new TypedEventTarget interface which adds types to EventTarget
  2. Renames EventEmitter to TypedEventEmitter to draw a distinction between them
  3. Re-exports TypedEventEmitter as EventEmitter to preserve backwards compatibility

In the future all consuming code should rely on the TypedEventTarget interface while implementing code uses TypedEventEmitter as an implementation of a TypedEventEmitter.

By depending on the interface and not the implementation consuming code is isolated from problems that arise when two versions of @libp2p/interface is in the dependency tree and the event subsystems would otherwise be compatible due to type overlap.

This manifests as an error message about incompatible implementations of the private #listeners field in EventEmitter.

Backwards compatibility is achieved by exporting the TypedEventEmitter class as the older EventEmitter name. This should be removed in a future PR to the v1.0 branch once external modules (e.g. Gossipsub) have been updated to use the new TypedEventEmitter symbol.

Change checklist

  • I have performed a self-review of my own code
  • I have made corresponding changes to the documentation if necessary (this includes comments as well)
  • I have added tests that prove my fix is effective or that my feature works

@achingbrain achingbrain requested a review from a team as a code owner October 13, 2023 17:18
@achingbrain achingbrain marked this pull request as draft October 13, 2023 17:18
@achingbrain achingbrain changed the base branch from release-v1.0 to master October 16, 2023 06:59
@achingbrain achingbrain changed the title refactor!: rename event emitter class refactor: rename event emitter class Oct 16, 2023
@achingbrain achingbrain marked this pull request as ready for review October 24, 2023 16:00
We add types to a EventEmitter class which extends the plain js EventTarget.

The name is chosen to give familiarity to node developers but it's
not generally a good idea.

This refactor:

1. Exports a new TypedEventTarget interface which adds types to EventTarget
2. Renames EventEmitter to TypedEventEmitter to draw a distinction between them

In the future all consuming code should rely on the TypedEventTarget interface
while implemmenting code uses TypedEventEmitter as an implementation of
a TypedEventEmitter.

By depending on the interface and not the implementation consuming code is
isolated from problems that arise when two versions of `@libp2p/interface`
is in the dependency tree and the event subsystems would otherwise be
compatible due to type overlap.

This maifests as an error message about incompatible implementations of
the private `#listeners` field in EventEmitter.
@achingbrain achingbrain force-pushed the refactor/rename-event-emitter branch from c89744a to 7be6dfd Compare October 25, 2023 10:26
@achingbrain achingbrain force-pushed the refactor/rename-event-emitter branch from 56b2e26 to 62d515f Compare October 25, 2023 10:43
@achingbrain achingbrain merged commit b5a808a into master Oct 25, 2023
@achingbrain achingbrain deleted the refactor/rename-event-emitter branch October 25, 2023 11:51
achingbrain added a commit that referenced this pull request Oct 25, 2023
@achingbrain achingbrain restored the refactor/rename-event-emitter branch October 25, 2023 14:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant