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 daeyeon.dev@gmail.com

PR-URL: nodejs/node#43514
Backport-PR-URL: nodejs/node#44082
Refs: nodejs/node#40678
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
  • Loading branch information
daeyeon authored and guangwong committed Oct 10, 2022
1 parent d0610ac commit f2a8e51
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 @@ -1978,6 +1978,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 @@ -2115,6 +2140,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 @@ -76,6 +76,7 @@ const kTimestamp = Symbol('timestamp');
const kBubbles = Symbol('bubbles');
const kComposed = Symbol('composed');
const kPropagationStopped = Symbol('propagationStopped');
const kDetail = Symbol('detail');

const isTrustedSet = new SafeWeakSet();
const isTrusted = ObjectGetOwnPropertyDescriptor({
Expand Down Expand Up @@ -327,6 +328,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 @@ -989,6 +1033,7 @@ const EventEmitterMixin = (Superclass) => {

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

0 comments on commit f2a8e51

Please sign in to comment.