Skip to content

Commit

Permalink
lib: use class fields in Event and EventTarget
Browse files Browse the repository at this point in the history
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: #42361
Refs: b1c3909
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>
  • Loading branch information
joyeecheung committed Mar 23, 2022
1 parent eb7b89c commit ea0668a
Showing 1 changed file with 22 additions and 26 deletions.
48 changes: 22 additions & 26 deletions lib/internal/event_target.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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 {{
Expand All @@ -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);
}
Expand Down Expand Up @@ -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)}`;
}

Expand All @@ -150,7 +146,7 @@ class Event {
preventDefault() {
if (!isEvent(this))
throw new ERR_INVALID_THIS('Event');
this[kDefaultPrevented] = true;
this.#defaultPrevented = true;
}

/**
Expand Down Expand Up @@ -195,7 +191,7 @@ class Event {
get cancelable() {
if (!isEvent(this))
throw new ERR_INVALID_THIS('Event');
return this[kCancelable];
return this.#cancelable;
}

/**
Expand All @@ -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;
}

/**
Expand All @@ -213,7 +209,7 @@ class Event {
get timeStamp() {
if (!isEvent(this))
throw new ERR_INVALID_THIS('Event');
return this[kTimestamp];
return this.#timestamp;
}


Expand Down Expand Up @@ -244,7 +240,7 @@ class Event {
get bubbles() {
if (!isEvent(this))
throw new ERR_INVALID_THIS('Event');
return this[kBubbles];
return this.#bubbles;
}

/**
Expand All @@ -253,7 +249,7 @@ class Event {
get composed() {
if (!isEvent(this))
throw new ERR_INVALID_THIS('Event');
return this[kComposed];
return this.#composed;
}

/**
Expand All @@ -271,7 +267,7 @@ class Event {
get cancelBubble() {
if (!isEvent(this))
throw new ERR_INVALID_THIS('Event');
return this[kPropagationStopped];
return this.#propagationStopped;
}

/**
Expand All @@ -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;
Expand Down

0 comments on commit ea0668a

Please sign in to comment.