8000
We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
CustomEvent
1 parent 140d6af commit e447395Copy full SHA for e447395
doc/api/events.md
@@ -1987,6 +1987,31 @@ added: v14.5.0
1987
1988
Removes the `listener` from the list of handlers for event `type`.
1989
1990
+### Class: `CustomEvent`
1991
+
1992
+<!-- YAML
1993
+added: REPLACEME
1994
+-->
1995
1996
+> Stability: 1 - Experimental.
1997
1998
+* Extends: {Event}
1999
2000
+The `CustomEvent` object is an adaptation of the [`CustomEvent` Web API][].
2001
+Instances are created internally by Node.js.
2002
2003
+#### `event.detail`
2004
2005
2006
2007
2008
2009
2010
2011
+* Type: {any} Returns custom data passed when initializing.
2012
2013
+Read-only.
2014
2015
### Class: `NodeEventTarget`
2016
2017
<!-- YAML
@@ -2124,6 +2149,7 @@ to the `EventTarget`.
2124
2149
2125
2150
[WHATWG-EventTarget]: https://dom.spec.whatwg.org/#interface-eventtarget
2126
2151
[`--trace-warnings`]: cli.md#--trace-warnings
2152
+[`CustomEvent` Web API]: https://dom.spec.whatwg.org/#customevent
2127
2153
[`EventTarget` Web API]: https://dom.spec.whatwg.org/#eventtarget
2128
2154
[`EventTarget` error handling]: #eventtarget-error-handling
2129
2155
[`Event` Web API]: https://dom.spec.whatwg.org/#event
lib/internal/event_target.js
@@ -67,6 +67,7 @@ const kTrustEvent = Symbol('kTrustEvent');
67
const { now } = require('internal/perf/utils');
68
69
const kType = Symbol('type');
70
+const kDetail = Symbol('detail');
71
72
const isTrustedSet = new SafeWeakSet();
73
const isTrusted = ObjectGetOwnPropertyDescriptor({
@@ -322,6 +323,49 @@ ObjectDefineProperties(
322
323
stopPropagation: kEnumerableProperty,
324
});
325
326
+function isCustomEvent(value) {
327
+ return isEvent(value) && (value?.[kDetail] !== undefined);
328
+}
329
330
+class CustomEvent extends Event {
331
+ /**
332
+ * @constructor
333
+ * @param {string} type
334
+ * @param {{
335
+ * bubbles?: boolean,
336
+ * cancelable?: boolean,
337
+ * composed?: boolean,
338
+ * detail?: any,
339
+ * }} [options]
340
+ */
341
+ constructor(type, options = kEmptyObject) {
342
+ if (arguments.length === 0)
343
+ throw new ERR_MISSING_ARGS('type');
344
+ super(type, options);
345
+ this[kDetail] = options?.detail ?? null;
346
+ }
347
348
349
+ * @type {any}
350
351
+ get detail() {
352
+ if (!isCustomEvent(this))
353
+ throw new ERR_INVALID_THIS('CustomEvent');
354
+ return this[kDetail];
355
356
357
358
+ObjectDefineProperties(CustomEvent.prototype, {
359
+ [SymbolToStringTag]: {
360
+ __proto__: null,
361
+ writable: false,
362
+ enumerable: false,
363
+ configurable: true,
364
+ value: 'CustomEvent',
365
+ },
366
+ detail: kEnumerableProperty,
367
+});
368
369
class NodeCustomEvent extends Event {
370
constructor(type, options) {
371
super(type, options);
@@ -984,6 +1028,7 @@ const EventEmitterMixin = (Superclass) => {
984
1028
985
1029
module.exports = {
986
1030
Event,
1031
+ CustomEvent,
987
1032
EventEmitterMixin,
988
1033
EventTarget,
989
1034
NodeEventTarget,