GeezerVison is showing github.com — events: add `CustomEvent` · nodejs/node@e447395
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 |
|---|
@@ -1987,6 +1987,31 @@ added: v14.5.0 | 1987 | 1987 |
| 1988 | 1988 | Removes the `listener` from the list of handlers for event `type`. | 1989 | 1989 |
| | 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 | + | 1990 | 2015 | ### Class: `NodeEventTarget` | 1991 | 2016 |
| 1992 | 2017 | <!-- YAML | @@ -2124,6 +2149,7 @@ to the `EventTarget`. | 2124 | 2149 |
| 2125 | 2150 | [WHATWG-EventTarget]: https://dom.spec.whatwg.org/#interface-eventtarget | 2126 | 2151 | [`--trace-warnings`]: cli.md#--trace-warnings | | 2152 | +[`CustomEvent` Web API]: https://dom.spec.whatwg.org/#customevent | 2127 | 2153 | [`EventTarget` Web API]: https://dom.spec.whatwg.org/#eventtarget | 2128 | 2154 | [`EventTarget` error handling]: #eventtarget-error-handling | 2129 | 2155 | [`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 |
|---|
@@ -67,6 +67,7 @@ const kTrustEvent = Symbol('kTrustEvent'); | 67 | 67 | const { now } = require('internal/perf/utils'); | 68 | 68 |
| 69 | 69 | const kType = Symbol('type'); | | 70 | +const kDetail = Symbol('detail'); | 70 | 71 |
| 71 | 72 | const isTrustedSet = new SafeWeakSet(); | 72 | 73 | const isTrusted = ObjectGetOwnPropertyDescriptor({ | @@ -322,6 +323,49 @@ ObjectDefineProperties( | 322 | 323 | stopPropagation: kEnumerableProperty, | 323 | 324 | }); | 324 | 325 |
| | 326 | +function isCustomEvent(value) { | | 327 | + return isEvent(value) && (value?.[kDetail] !== undefined); | | 328 | +} | | 329 | + | | 330 | +class CustomEvent extends Event { | | 331 | + /** | | 332 | + * @constructor | | 333 | + * @param {string} type | | 334 | + * @param {{ | | 335 | + * bubbles?: boolean, | | 336 | + * cancelable?: boolean, | | 337 | + * composed?: boolean, | | 338 | + * detail?: any, | | 339 | + * }} [options] | | 340 | + */ | | 341 | + constructor(type, options = kEmptyObject) { | | 342 | + if (arguments.length === 0) | | 343 | + throw new ERR_MISSING_ARGS('type'); | | 344 | + super(type, options); | | 345 | + this[kDetail] = options?.detail ?? null; | | 346 | + } | | 347 | + | | 348 | + /** | | 349 | + * @type {any} | | 350 | + */ | | 351 | + get detail() { | | 352 | + if (!isCustomEvent(this)) | | 353 | + throw new ERR_INVALID_THIS('CustomEvent'); | | 354 | + return this[kDetail]; | | 355 | + } | | 356 | +} | | 357 | + | | 358 | +ObjectDefineProperties(CustomEvent.prototype, { | | 359 | + [SymbolToStringTag]: { | | 360 | + __proto__: null, | | 361 | + writable: false, | | 362 | + enumerable: false, | | 363 | + configurable: true, | | 364 | + value: 'CustomEvent', | | 365 | + }, | | 366 | + detail: kEnumerableProperty, | | 367 | +}); | | 368 | + | 325 | 369 | class NodeCustomEvent extends Event { | 326 | 370 | constructor(type, options) { | 327 | 371 | super(type, options); | @@ -984,6 +1028,7 @@ const EventEmitterMixin = (Superclass) => { | 984 | 1028 |
| 985 | 1029 | module.exports = { | 986 | 1030 | Event, | | 1031 | + CustomEvent, | 987 | 1032 | EventEmitterMixin, | 988 | 1033 | EventTarget, | 989 | 1034 | NodeEventTarget, | |
|