Skip to content

Commit ea0668a

Browse files
committed
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: #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>
1 parent eb7b89c commit ea0668a

File tree

1 file changed

+22
-26
lines changed

1 file changed

+22
-26
lines changed

lib/internal/event_target.js

+22-26
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,7 @@ const kTrustEvent = Symbol('kTrustEvent');
6363

6464
const { now } = require('internal/perf/utils');
6565

66-
// TODO(joyeecheung): V8 snapshot does not support instance member
67-
// initializers for now:
68-
// https://bugs.chromium.org/p/v8/issues/detail?id=10704
6966
const kType = Symbol('type');
70-
const kDefaultPrevented = Symbol('defaultPrevented');
71-
const kCancelable = Symbol('cancelable');
72-
const kTimestamp = Symbol('timestamp');
73-
const kBubbles = Symbol('bubbles');
74-
const kComposed = Symbol('composed');
75-
const kPropagationStopped = Symbol('propagationStopped');
7667

7768
const isTrustedSet = new SafeWeakSet();
7869
const isTrusted = ObjectGetOwnPropertyDescriptor({
@@ -86,6 +77,13 @@ function isEvent(value) {
8677
}
8778

8879
class Event {
80+
#cancelable = false;
81+
#bubbles = false;
82+
#composed = false;
83+
#defaultPrevented = false;
84+
#timestamp = now();
85+
#propagationStopped = false;
86+
8987
/**
9088
* @param {string} type
9189
* @param {{
@@ -101,13 +99,11 @@ class Event {
10199
allowArray: true, allowFunction: true, nullable: true,
102100
});
103101
const { cancelable, bubbles, composed } = { ...options };
104-
this[kCancelable] = !!cancelable;
105-
this[kBubbles] = !!bubbles;
106-
this[kComposed] = !!composed;
102+
this.#cancelable = !!cancelable;
103+
this.#bubbles = !!bubbles;
104+
this.#composed = !!composed;
105+
107106
this[kType] = `${type}`;
108-
this[kDefaultPrevented] = false;
109-
this[kTimestamp] = now();
110-
this[kPropagationStopped] = false;
111107
if (options?.[kTrustEvent]) {
112108
isTrustedSet.add(this);
113109
}
@@ -135,9 +131,9 @@ class Event {
135131

136132
return `${name} ${inspect({
137133
type: this[kType],
138-
defaultPrevented: this[kDefaultPrevented],
139-
cancelable: this[kCancelable],
140-
timeStamp: this[kTimestamp],
134+
defaultPrevented: this.#defaultPrevented,
135+
cancelable: this.#cancelable,
136+
timeStamp: this.#timestamp,
141137
}, opts)}`;
142138
}
143139

@@ -150,7 +146,7 @@ class Event {
150146
preventDefault() {
151147
if (!isEvent(this))
152148
throw new ERR_INVALID_THIS('Event');
153-
this[kDefaultPrevented] = true;
149+
this.#defaultPrevented = true;
154150
}
155151

156152
/**
@@ -195,7 +191,7 @@ class Event {
195191
get cancelable() {
196192
if (!isEvent(this))
197193
throw new ERR_INVALID_THIS('Event');
198-
return this[kCancelable];
194+
return this.#cancelable;
199195
}
200196

201197
/**
@@ -204,7 +200,7 @@ class Event {
204200
get defaultPrevented() {
205201
if (!isEvent(this))
206202
throw new ERR_INVALID_THIS('Event');
207-
return this[kCancelable] && this[kDefaultPrevented];
203+
return this.#cancelable && this.#defaultPrevented;
208204
}
209205

210206
/**
@@ -213,7 +209,7 @@ class Event {
213209
get timeStamp() {
214210
if (!isEvent(this))
215211
throw new ERR_INVALID_THIS('Event');
216-
return this[kTimestamp];
212+
return this.#timestamp;
217213
}
218214

219215

@@ -244,7 +240,7 @@ class Event {
244240
get bubbles() {
245241
if (!isEvent(this))
246242
throw new ERR_INVALID_THIS('Event');
247-
return this[kBubbles];
243+
return this.#bubbles;
248244
}
249245

250246
/**
@@ -253,7 +249,7 @@ class Event {
253249
get composed() {
254250
if (!isEvent(this))
255251
throw new ERR_INVALID_THIS('Event');
256-
return this[kComposed];
252+
return this.#composed;
257253
}
258254

259255
/**
@@ -271,7 +267,7 @@ class Event {
271267
get cancelBubble() {
272268
if (!isEvent(this))
273269
throw new ERR_INVALID_THIS('Event');
274-
return this[kPropagationStopped];
270+
return this.#propagationStopped;
275271
}
276272

277273
/**
@@ -288,7 +284,7 @@ class Event {
288284
stopPropagation() {
289285
if (!isEvent(this))
290286
throw new ERR_INVALID_THIS('Event');
291-
this[kPropagationStopped] = true;
287+
this.#propagationStopped = true;
292288
}
293289

294290
static NONE = 0;

0 commit comments

Comments
 (0)