diff options
author | Joyee Cheung <joyeec9h3@gmail.com> | 2022-02-17 18:15:01 +0300 |
---|---|---|
committer | Joyee Cheung <joyeec9h3@gmail.com> | 2022-03-23 15:07:55 +0300 |
commit | ea0668a27e2d89dd7924d577b6bcf3f87ed3d986 (patch) | |
tree | 9910cb89d911351f0a67b0a676acdbc38b77ddb3 /lib | |
parent | eb7b89c8291f057cb9c3a903b4e4a67ec95bfd7d (diff) |
lib: use class fields in Event and EventTarget
https://bugs.chromium.org/p/v8/issues/detail?id=10704 is already
fixed, so switch back to class fields instead of using symbol
properties.
PR-URL: https://github.com/nodejs/node/pull/42361
Refs: https://github.com/nodejs/node/commit/b1c3909bd766327a569c2e4279a4670454f3f9db
Reviewed-By: Michaƫl Zasso <targos@protonmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Mestery <mestery@protonmail.com>
Reviewed-By: Darshan Sen <raisinten@gmail.com>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/internal/event_target.js | 48 |
1 files changed, 22 insertions, 26 deletions
diff --git a/lib/internal/event_target.js b/lib/internal/event_target.js index 7c539db2eb3..0f9a896396f 100644 --- a/lib/internal/event_target.js +++ b/lib/internal/event_target.js @@ -63,16 +63,7 @@ const kTrustEvent = Symbol('kTrustEvent'); const { now } = require('internal/perf/utils'); -// TODO(joyeecheung): V8 snapshot does not support instance member -// initializers for now: -// https://bugs.chromium.org/p/v8/issues/detail?id=10704 const kType = Symbol('type'); -const kDefaultPrevented = Symbol('defaultPrevented'); -const kCancelable = Symbol('cancelable'); -const kTimestamp = Symbol('timestamp'); -const kBubbles = Symbol('bubbles'); -const kComposed = Symbol('composed'); -const kPropagationStopped = Symbol('propagationStopped'); const isTrustedSet = new SafeWeakSet(); const isTrusted = ObjectGetOwnPropertyDescriptor({ @@ -86,6 +77,13 @@ function isEvent(value) { } class Event { + #cancelable = false; + #bubbles = false; + #composed = false; + #defaultPrevented = false; + #timestamp = now(); + #propagationStopped = false; + /** * @param {string} type * @param {{ @@ -101,13 +99,11 @@ class Event { allowArray: true, allowFunction: true, nullable: true, }); const { cancelable, bubbles, composed } = { ...options }; - this[kCancelable] = !!cancelable; - this[kBubbles] = !!bubbles; - this[kComposed] = !!composed; + this.#cancelable = !!cancelable; + this.#bubbles = !!bubbles; + this.#composed = !!composed; + this[kType] = `${type}`; - this[kDefaultPrevented] = false; - this[kTimestamp] = now(); - this[kPropagationStopped] = false; if (options?.[kTrustEvent]) { isTrustedSet.add(this); } @@ -135,9 +131,9 @@ class Event { return `${name} ${inspect({ type: this[kType], - defaultPrevented: this[kDefaultPrevented], - cancelable: this[kCancelable], - timeStamp: this[kTimestamp], + defaultPrevented: this.#defaultPrevented, + cancelable: this.#cancelable, + timeStamp: this.#timestamp, }, opts)}`; } @@ -150,7 +146,7 @@ class Event { preventDefault() { if (!isEvent(this)) throw new ERR_INVALID_THIS('Event'); - this[kDefaultPrevented] = true; + this.#defaultPrevented = true; } /** @@ -195,7 +191,7 @@ class Event { get cancelable() { if (!isEvent(this)) throw new ERR_INVALID_THIS('Event'); - return this[kCancelable]; + return this.#cancelable; } /** @@ -204,7 +200,7 @@ class Event { get defaultPrevented() { if (!isEvent(this)) throw new ERR_INVALID_THIS('Event'); - return this[kCancelable] && this[kDefaultPrevented]; + return this.#cancelable && this.#defaultPrevented; } /** @@ -213,7 +209,7 @@ class Event { get timeStamp() { if (!isEvent(this)) throw new ERR_INVALID_THIS('Event'); - return this[kTimestamp]; + return this.#timestamp; } @@ -244,7 +240,7 @@ class Event { get bubbles() { if (!isEvent(this)) throw new ERR_INVALID_THIS('Event'); - return this[kBubbles]; + return this.#bubbles; } /** @@ -253,7 +249,7 @@ class Event { get composed() { if (!isEvent(this)) throw new ERR_INVALID_THIS('Event'); - return this[kComposed]; + return this.#composed; } /** @@ -271,7 +267,7 @@ class Event { get cancelBubble() { if (!isEvent(this)) throw new ERR_INVALID_THIS('Event'); - return this[kPropagationStopped]; + return this.#propagationStopped; } /** @@ -288,7 +284,7 @@ class Event { stopPropagation() { if (!isEvent(this)) throw new ERR_INVALID_THIS('Event'); - this[kPropagationStopped] = true; + this.#propagationStopped = true; } static NONE = 0; |