Skip to content

Commit e447395

Browse files
daeyeondanielleadams
authored andcommitted
events: add CustomEvent
This implements the Web API `CustomEvent` in `internal/event_target`. Signed-off-by: Daeyeon Jeong [email protected] PR-URL: #43514 Refs: #40678 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]>
1 parent 140d6af commit e447395

File tree

5 files changed

+428
-3
lines changed

5 files changed

+428
-3
lines changed

doc/api/events.md

+26
Original file line numberDiff line numberDiff line change
@@ -1987,6 +1987,31 @@ added: v14.5.0
19871987

19881988
Removes the `listener` from the list of handlers for event `type`.
19891989

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+
<!-- YAML
2006+
added: REPLACEME
2007+
-->
2008+
2009+
> Stability: 1 - Experimental.
2010+
2011+
* Type: {any} Returns custom data passed when initializing.
2012+
2013+
Read-only.
2014+
19902015
### Class: `NodeEventTarget`
19912016

19922017
<!-- YAML
@@ -2124,6 +2149,7 @@ to the `EventTarget`.
21242149

21252150
[WHATWG-EventTarget]: https://dom.spec.whatwg.org/#interface-eventtarget
21262151
[`--trace-warnings`]: cli.md#--trace-warnings
2152+
[`CustomEvent` Web API]: https://dom.spec.whatwg.org/#customevent
21272153
[`EventTarget` Web API]: https://dom.spec.whatwg.org/#eventtarget
21282154
[`EventTarget` error handling]: #eventtarget-error-handling
21292155
[`Event` Web API]: https://dom.spec.whatwg.org/#event

lib/internal/event_target.js

+45
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ const kTrustEvent = Symbol('kTrustEvent');
6767
const { now } = require('internal/perf/utils');
6868

6969
const kType = Symbol('type');
70+
const kDetail = Symbol('detail');
7071

7172
const isTrustedSet = new SafeWeakSet();
7273
const isTrusted = ObjectGetOwnPropertyDescriptor({
@@ -322,6 +323,49 @@ ObjectDefineProperties(
322323
stopPropagation: kEnumerableProperty,
323324
});
324325

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+
325369
class NodeCustomEvent extends Event {
326370
constructor(type, options) {
327371
super(type, options);
@@ -984,6 +1028,7 @@ const EventEmitterMixin = (Superclass) => {
9841028

9851029
module.exports = {
9861030
Event,
1031+
CustomEvent,
9871032
EventEmitterMixin,
9881033
EventTarget,
9891034
NodeEventTarget,

0 commit comments

Comments
 (0)