Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

events: add CustomEvent #43514

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -68,6 +68,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 @@ -325,6 +326,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 @@ -979,6 +1023,7 @@ const EventEmitterMixin = (Superclass) => {

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