Skip to content

Commit 087adbb

Browse files
daeyeontargos
authored andcommitted
events: add CustomEvent
This implements the Web API `CustomEvent` in `internal/event_target`. Signed-off-by: Daeyeon Jeong daeyeon.dev@gmail.com PR-URL: #43514 Backport-PR-URL: #44082 Refs: #40678 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
1 parent a8c2418 commit 087adbb

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
@@ -1978,6 +1978,31 @@ added: v14.5.0
19781978

19791979
Removes the `listener` from the list of handlers for event `type`.
19801980

1981+
### Class: `CustomEvent`
1982+
1983+
<!-- YAML
1984+
added: REPLACEME
1985+
-->
1986+
1987+
> Stability: 1 - Experimental.
1988+
1989+
* Extends: {Event}
1990+
1991+
The `CustomEvent` object is an adaptation of the [`CustomEvent` Web API][].
1992+
Instances are created internally by Node.js.
1993+
1994+
#### `event.detail`
1995+
1996+
<!-- YAML
1997+
added: REPLACEME
1998+
-->
1999+
2000+
> Stability: 1 - Experimental.
2001+
2002+
* Type: {any} Returns custom data passed when initializing.
2003+
2004+
Read-only.
2005+
19812006
### Class: `NodeEventTarget`
19822007

19832008
<!-- YAML
@@ -2115,6 +2140,7 @@ to the `EventTarget`.
21152140

21162141
[WHATWG-EventTarget]: https://dom.spec.whatwg.org/#interface-eventtarget
21172142
[`--trace-warnings`]: cli.md#--trace-warnings
2143+
[`CustomEvent` Web API]: https://dom.spec.whatwg.org/#customevent
21182144
[`EventTarget` Web API]: https://dom.spec.whatwg.org/#eventtarget
21192145
[`EventTarget` error handling]: #eventtarget-error-handling
21202146
[`Event` Web API]: https://dom.spec.whatwg.org/#event

lib/internal/event_target.js

+45
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ const kTimestamp = Symbol('timestamp');
7676
const kBubbles = Symbol('bubbles');
7777
const kComposed = Symbol('composed');
7878
const kPropagationStopped = Symbol('propagationStopped');
79+
const kDetail = Symbol('detail');
7980

8081
const isTrustedSet = new SafeWeakSet();
8182
const isTrusted = ObjectGetOwnPropertyDescriptor({
@@ -327,6 +328,49 @@ ObjectDefineProperties(
327328
stopPropagation: kEnumerableProperty,
328329
});
329330

331+
function isCustomEvent(value) {
332+
return isEvent(value) && (value?.[kDetail] !== undefined);
333+
}
334+
335+
class CustomEvent extends Event {
336+
/**
337+
* @constructor
338+
* @param {string} type
339+
* @param {{
340+
* bubbles?: boolean,
341+
* cancelable?: boolean,
342+
* composed?: boolean,
343+
* detail?: any,
344+
* }} [options]
345+
*/
346+
constructor(type, options = kEmptyObject) {
347+
if (arguments.length === 0)
348+
throw new ERR_MISSING_ARGS('type');
349+
super(type, options);
350+
this[kDetail] = options?.detail ?? null;
351+
}
352+
353+
/**
354+
* @type {any}
355+
*/
356+
get detail() {
357+
if (!isCustomEvent(this))
358+
throw new ERR_INVALID_THIS('CustomEvent');
359+
return this[kDetail];
360+
}
361+
}
362+
363+
ObjectDefineProperties(CustomEvent.prototype, {
364+
[SymbolToStringTag]: {
365+
__proto__: null,
366+
writable: false,
367+
enumerable: false,
368+
configurable: true,
369+
value: 'CustomEvent',
370+
},
371+
detail: kEnumerableProperty,
372+
});
373+
330374
class NodeCustomEvent extends Event {
331375
constructor(type, options) {
332376
super(type, options);
@@ -989,6 +1033,7 @@ const EventEmitterMixin = (Superclass) => {
9891033

9901034
module.exports = {
9911035
Event,
1036+
CustomEvent,
9921037
EventEmitterMixin,
9931038
EventTarget,
9941039
NodeEventTarget,

0 commit comments

Comments
 (0)