Skip to content

Commit

Permalink
fix: remove CustomEvent polyfill (#2652)
Browse files Browse the repository at this point in the history
The EventTarget implementation of libp2p contained workarounds for incomplete runtime support for the feature. One of the workarounds was specifically marked to be removed once CustomEvent was implemented in NodeJS and the [upstream ticket](nodejs/node#40678) was closed. This has happened two years ago. The implementation of the standard event in Node18 and the subsequent removal of the experimental flag in Node19 broke the current workaround and causes tsc to error. Some of these were suppressed with @ts-ignore, others were not. This fix closes #2420.

The fix removes the workaround as instructed by the source code and restores compatibility with recent versions of Node types.
  • Loading branch information
EliasOenal authored Aug 9, 2024
1 parent fd4aea3 commit 0edbfe7
Showing 1 changed file with 1 addition and 19 deletions.
20 changes: 1 addition & 19 deletions packages/interface/src/event-target.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,22 +105,4 @@ export class TypedEventEmitter<EventMap extends Record<string, any>> extends Eve
}
}

/**
* CustomEvent is a standard event but it's not supported by node.
*
* Remove this when https://github.com/nodejs/node/issues/40678 is closed.
*
* Ref: https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent
*/
class CustomEventPolyfill<T = any> extends Event {
/** Returns any custom data event was created with. Typically used for synthetic events. */
public detail: T

constructor (message: string, data?: EventInit & { detail: T }) {
super(message, data)
// @ts-expect-error could be undefined
this.detail = data?.detail
}
}

export const CustomEvent = globalThis.CustomEvent ?? CustomEventPolyfill
export const CustomEvent = globalThis.CustomEvent

0 comments on commit 0edbfe7

Please sign in to comment.