Skip to content

Commit 6e8cc4b

Browse files
committed
events: add CustomEvent
This implements the Web API `CustomEvent` in `internal/event_target`. Signed-off-by: Daeyeon Jeong [email protected]
1 parent 6ac55fa commit 6e8cc4b

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
@@ -68,6 +68,7 @@ const kTrustEvent = Symbol('kTrustEvent');
6868
const { now } = require('internal/perf/utils');
6969

7070
const kType = Symbol('type');
71+
const kDetail = Symbol('type');
7172

7273
const isTrustedSet = new SafeWeakSet();
7374
const isTrusted = ObjectGetOwnPropertyDescriptor({
@@ -325,6 +326,49 @@ ObjectDefineProperties(
325326
stopPropagation: kEnumerableProperty,
326327
});
327328

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

9801024
module.exports = {
9811025
Event,
1026+
CustomEvent,
9821027
EventEmitterMixin,
9831028
EventTarget,
9841029
NodeEventTarget,

0 commit comments

Comments
 (0)