-
Notifications
You must be signed in to change notification settings - Fork 29.6k
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
Support CustomEvent #40678
Comments
Replaces the node `EventEmitter` with the pure-js `EventTarget` class. All events are now instances of [Event](https://developer.mozilla.org/en-US/docs/Web/API/Event). For typing [CustomEvent](https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent) can be used which gives us a typed `.detail` field. Tediously `EventTarget` itself [doesn't support typed events](microsoft/TypeScript#28357) (yet?) and node.js [doesn't support](nodejs/node#40678) `CustomEvent` globally so bit of trickery was required but hopefully this can be removed in future: ```js import { EventEmitter, CustomEvent } from '@libp2p/interfaces' interface EventMap { 'foo': CustomEvent<number> } class MyEmitter extends EventEmitter<EventMap> { // ... other code here } const emitter = new MyEmitter() emitter.addEventListener('foo', (evt) => { const n = evt.detail // n is a 'number' }) ```
There has been no activity on this feature request for 5 months and it is unlikely to be implemented. It will be closed 6 months after the last non-automated comment. For more information on how the project manages feature requests, please consult the feature request management document. |
Yet another polyfill proposal that:
class CustomEvent extends Event {
#detail;
constructor(type, options) {
super(type, options);
this.#detail = options?.detail ?? null;
}
get detail() {
return this.#detail;
}
} |
For anyone wanting to work on this, the steps to take are:
W.r.t. (1): diff --git a/test/wpt/status/dom/events.json b/test/wpt/status/dom/events.json
new file mode 100644
index 00000000000..0967ef424bc
--- /dev/null
+++ b/test/wpt/status/dom/events.json
@@ -0,0 +1 @@
+{}
diff --git a/test/wpt/test-events.js b/test/wpt/test-events.js
new file mode 100644
index 00000000000..5040d56d6a2
--- /dev/null
+++ b/test/wpt/test-events.js
@@ -0,0 +1,7 @@
+'use strict';
+require('../common');
+const { WPTRunner } = require('../common/wpt');
+
+const runner = new WPTRunner('dom/events');
+
+runner.runJsTests(); Where dom/events is https://github.com/web-platform-tests/wpt/tree/master/dom/events |
PR-URL: nodejs#43151 Refs: nodejs#40678 Reviewed-By: Masashi Hirano <shisama07@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: James M Snell <jasnell@gmail.com>
Is this issue still open? It looks like someone has opened a PR on it. |
@F3n67u We have a bit more. It needs to be exposed as global. Working on it. :-) |
This implements the Web API `CustomEvent` in `internal/event_target`. Signed-off-by: Daeyeon Jeong daeyeon.dev@gmail.com PR-URL: nodejs#43514 Refs: nodejs#40678 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Signed-off-by: Daeyeon Jeong daeyeon.dev@gmail.com PR-URL: nodejs#43885 Fixes: nodejs#40678 Refs: nodejs#43514 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Darshan Sen <raisinten@gmail.com> Reviewed-By: Mestery <mestery@protonmail.com>
This implements the Web API `CustomEvent` in `internal/event_target`. Signed-off-by: Daeyeon Jeong daeyeon.dev@gmail.com PR-URL: nodejs#43514 Refs: nodejs#40678 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Signed-off-by: Daeyeon Jeong daeyeon.dev@gmail.com PR-URL: nodejs#43885 Fixes: nodejs#40678 Refs: nodejs#43514 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Darshan Sen <raisinten@gmail.com> Reviewed-By: Mestery <mestery@protonmail.com>
Signed-off-by: Daeyeon Jeong daeyeon.dev@gmail.com PR-URL: #43885 Backport-PR-URL: #44082 Fixes: #40678 Refs: #43514 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Darshan Sen <raisinten@gmail.com> Reviewed-By: Mestery <mestery@protonmail.com>
This implements the Web API `CustomEvent` in `internal/event_target`. Signed-off-by: Daeyeon Jeong daeyeon.dev@gmail.com PR-URL: nodejs#43514 Refs: nodejs#40678 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Signed-off-by: Daeyeon Jeong daeyeon.dev@gmail.com PR-URL: nodejs#43885 Fixes: nodejs#40678 Refs: nodejs#43514 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Darshan Sen <raisinten@gmail.com> Reviewed-By: Mestery <mestery@protonmail.com>
PR-URL: nodejs/node#43151 Refs: nodejs/node#40678 Reviewed-By: Masashi Hirano <shisama07@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: James M Snell <jasnell@gmail.com>
This implements the Web API `CustomEvent` in `internal/event_target`. Signed-off-by: Daeyeon Jeong daeyeon.dev@gmail.com PR-URL: nodejs/node#43514 Backport-PR-URL: nodejs/node#44082 Refs: nodejs/node#40678 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Signed-off-by: Daeyeon Jeong daeyeon.dev@gmail.com PR-URL: nodejs/node#43885 Backport-PR-URL: nodejs/node#44082 Fixes: nodejs/node#40678 Refs: nodejs/node#43514 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Darshan Sen <raisinten@gmail.com> Reviewed-By: Mestery <mestery@protonmail.com>
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.
Is your feature request related to a problem? Please describe.
When trying to mimic browser APIs surrounding
EventTarget
, we frequently useCustomEvent
which is currently unavailable in Node.It's useful in that it allows us to dispatch events through an
EventTarget
and include a custom data payload.In a some way, this is the closest we have to a standardised
Observable
API (specificallySubject
) and it's very useful.Describe the solution you'd like
CustomEvent
existsDescribe alternatives you've considered
Writing a polyfill
The text was updated successfully, but these errors were encountered: