Skip to content
Closed
Changes from 1 commit
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
Prev Previous commit
Next Next commit
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.
  • Loading branch information
joyeecheung committed Mar 16, 2022
commit 3e7e7ed6e4a5390ccc467e9233f71dad690c2c35
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