Skip to content

Commit

Permalink
events: add CustomEvent
Browse files Browse the repository at this point in the history
This implements the Web API `CustomEvent` in `internal/event_target`.

Signed-off-by: Daeyeon Jeong [email protected]

PR-URL: nodejs#43514
Refs: nodejs#40678
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Benjamin Gruenbaum <[email protected]>
  • Loading branch information
daeyeon authored and Fyko committed Sep 15, 2022
1 parent 0fc3bda commit f69faac
Show file tree
Hide file tree
Showing 5 changed files with 428 additions and 3 deletions.
26 changes: 26 additions & 0 deletions doc/api/events.md
Original file line number Diff line number Diff line change
Expand Up @@ -1987,6 +1987,31 @@ added: v14.5.0

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

### Class: `CustomEvent`

<!-- YAML
added: REPLACEME
-->

> Stability: 1 - Experimental.
* Extends: {Event}

The `CustomEvent` object is an adaptation of the [`CustomEvent` Web API][].
Instances are created internally by Node.js.

#### `event.detail`

<!-- YAML
added: REPLACEME
-->

> Stability: 1 - Experimental.
* Type: {any} Returns custom data passed when initializing.

Read-only.

### Class: `NodeEventTarget`

<!-- YAML
Expand Down Expand Up @@ -2124,6 +2149,7 @@ to the `EventTarget`.

[WHATWG-EventTarget]: https://dom.spec.whatwg.org/#interface-eventtarget
[`--trace-warnings`]: cli.md#--trace-warnings
[`CustomEvent` Web API]: https://dom.spec.whatwg.org/#customevent
[`EventTarget` Web API]: https://dom.spec.whatwg.org/#eventtarget
[`EventTarget` error handling]: #eventtarget-error-handling
[`Event` Web API]: https://dom.spec.whatwg.org/#event
Expand Down
45 changes: 45 additions & 0 deletions lib/internal/event_target.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ const kTrustEvent = Symbol('kTrustEvent');
const { now } = require('internal/perf/utils');

const kType = Symbol('type');
const kDetail = Symbol('detail');

const isTrustedSet = new SafeWeakSet();
const isTrusted = ObjectGetOwnPropertyDescriptor({
Expand Down Expand Up @@ -322,6 +323,49 @@ ObjectDefineProperties(
stopPropagation: kEnumerableProperty,
});

function isCustomEvent(value) {
return isEvent(value) && (value?.[kDetail] !== undefined);
}

class CustomEvent extends Event {
/**
* @constructor
* @param {string} type
* @param {{
* bubbles?: boolean,
* cancelable?: boolean,
* composed?: boolean,
* detail?: any,
* }} [options]
*/
constructor(type, options = kEmptyObject) {
if (arguments.length === 0)
throw new ERR_MISSING_ARGS('type');
super(type, options);
this[kDetail] = options?.detail ?? null;
}

/**
* @type {any}
*/
get detail() {
if (!isCustomEvent(this))
throw new ERR_INVALID_THIS('CustomEvent');
return this[kDetail];
}
}

ObjectDefineProperties(CustomEvent.prototype, {
[SymbolToStringTag]: {
__proto__: null,
writable: false,
enumerable: false,
configurable: true,
value: 'CustomEvent',
},
detail: kEnumerableProperty,
});

class NodeCustomEvent extends Event {
constructor(type, options) {
super(type, options);
Expand Down Expand Up @@ -984,6 +1028,7 @@ const EventEmitterMixin = (Superclass) => {

module.exports = {
Event,
CustomEvent,
EventEmitterMixin,
EventTarget,
NodeEventTarget,
Expand Down
Loading

0 comments on commit f69faac

Please sign in to comment.