GeezerVison is showing github.com — events: add `CustomEvent` · nodejs/node@087adbb
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: doc/api/events.md +26Lines changed: 26 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -1978,6 +1978,31 @@ added: v14.5.0 | 1978 | 1978 |
| 1979 | 1979 | Removes the `listener` from the list of handlers for event `type`. | 1980 | 1980 |
| | 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 | + | 1981 | 2006 | ### Class: `NodeEventTarget` | 1982 | 2007 |
| 1983 | 2008 | <!-- YAML | @@ -2115,6 +2140,7 @@ to the `EventTarget`. | 2115 | 2140 |
| 2116 | 2141 | [WHATWG-EventTarget]: https://dom.spec.whatwg.org/#interface-eventtarget | 2117 | 2142 | [`--trace-warnings`]: cli.md#--trace-warnings | | 2143 | +[`CustomEvent` Web API]: https://dom.spec.whatwg.org/#customevent | 2118 | 2144 | [`EventTarget` Web API]: https://dom.spec.whatwg.org/#eventtarget | 2119 | 2145 | [`EventTarget` error handling]: #eventtarget-error-handling | 2120 | 2146 | [`Event` Web API]: https://dom.spec.whatwg.org/#event | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/event_target.js +45Lines changed: 45 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -76,6 +76,7 @@ const kTimestamp = Symbol('timestamp'); | 76 | 76 | const kBubbles = Symbol('bubbles'); | 77 | 77 | const kComposed = Symbol('composed'); | 78 | 78 | const kPropagationStopped = Symbol('propagationStopped'); | | 79 | +const kDetail = Symbol('detail'); | 79 | 80 |
| 80 | 81 | const isTrustedSet = new SafeWeakSet(); | 81 | 82 | const isTrusted = ObjectGetOwnPropertyDescriptor({ | @@ -327,6 +328,49 @@ ObjectDefineProperties( | 327 | 328 | stopPropagation: kEnumerableProperty, | 328 | 329 | }); | 329 | 330 |
| | 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 | + | 330 | 374 | class NodeCustomEvent extends Event { | 331 | 375 | constructor(type, options) { | 332 | 376 | super(type, options); | @@ -989,6 +1033,7 @@ const EventEmitterMixin = (Superclass) => { | 989 | 1033 |
| 990 | 1034 | module.exports = { | 991 | 1035 | Event, | | 1036 | + CustomEvent, | 992 | 1037 | EventEmitterMixin, | 993 | 1038 | EventTarget, | 994 | 1039 | NodeEventTarget, | |
|