From c37fdacb348fa8f735f0c01fdfb0eefe6152dd34 Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Thu, 17 Feb 2022 23:15:40 +0800 Subject: [PATCH] lib: use class fields in observe.js MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Reviewed-By: Benjamin Gruenbaum Reviewed-By: Luigi Pinca Reviewed-By: Antoine du Hamel Reviewed-By: Mestery Reviewed-By: Darshan Sen --- lib/internal/perf/observe.js | 75 +++++++++++++++++------------------- 1 file changed, 36 insertions(+), 39 deletions(-) diff --git a/lib/internal/perf/observe.js b/lib/internal/perf/observe.js index 2f9506ed28e3eb..ca9e68a96258ab 100644 --- a/lib/internal/perf/observe.js +++ b/lib/internal/perf/observe.js @@ -61,13 +61,9 @@ const { const { inspect } = require('util'); -const kBuffer = Symbol('kBuffer'); -const kCallback = Symbol('kCallback'); const kDispatch = Symbol('kDispatch'); -const kEntryTypes = Symbol('kEntryTypes'); const kMaybeBuffer = Symbol('kMaybeBuffer'); const kDeprecatedFields = Symbol('kDeprecatedFields'); -const kType = Symbol('kType'); const kDeprecationMessage = 'Custom PerformanceEntry accessors are deprecated. ' + @@ -151,20 +147,22 @@ function maybeIncrementObserverCount(type) { } class PerformanceObserverEntryList { + #buffer = []; + constructor(entries) { - this[kBuffer] = ArrayPrototypeSort(entries, (first, second) => { + this.#buffer = ArrayPrototypeSort(entries, (first, second) => { return first.startTime - second.startTime; }); } getEntries() { - return ArrayPrototypeSlice(this[kBuffer]); + return ArrayPrototypeSlice(this.#buffer); } getEntriesByType(type) { type = `${type}`; return ArrayPrototypeFilter( - this[kBuffer], + this.#buffer, (entry) => entry.entryType === type); } @@ -172,11 +170,11 @@ class PerformanceObserverEntryList { name = `${name}`; if (type != null /** not nullish */) { return ArrayPrototypeFilter( - this[kBuffer], + this.#buffer, (entry) => entry.name === name && entry.entryType === type); } return ArrayPrototypeFilter( - this[kBuffer], + this.#buffer, (entry) => entry.name === name); } @@ -188,20 +186,19 @@ class PerformanceObserverEntryList { depth: options.depth == null ? null : options.depth - 1 }; - return `PerformanceObserverEntryList ${inspect(this[kBuffer], opts)}`; + return `PerformanceObserverEntryList ${inspect(this.#buffer, opts)}`; } } class PerformanceObserver { + #buffer = []; + #entryTypes = new SafeSet(); + #type; + #callback; + constructor(callback) { - // TODO(joyeecheung): V8 snapshot does not support instance member - // initializers for now: - // https://bugs.chromium.org/p/v8/issues/detail?id=10704 - this[kBuffer] = []; - this[kEntryTypes] = new SafeSet(); - this[kType] = undefined; validateFunction(callback, 'callback'); - this[kCallback] = callback; + this.#callback = callback; } observe(options = {}) { @@ -219,10 +216,10 @@ class PerformanceObserver { 'options.entryTypes can not set with ' + 'options.type together'); - switch (this[kType]) { + switch (this.#type) { case undefined: - if (entryTypes !== undefined) this[kType] = kTypeMultiple; - if (type !== undefined) this[kType] = kTypeSingle; + if (entryTypes !== undefined) this.#type = kTypeMultiple; + if (type !== undefined) this.#type = kTypeSingle; break; case kTypeSingle: if (entryTypes !== undefined) @@ -238,53 +235,53 @@ class PerformanceObserver { break; } - if (this[kType] === kTypeMultiple) { + if (this.#type === kTypeMultiple) { if (!ArrayIsArray(entryTypes)) { throw new ERR_INVALID_ARG_TYPE( 'options.entryTypes', 'string[]', entryTypes); } - maybeDecrementObserverCounts(this[kEntryTypes]); - this[kEntryTypes].clear(); + maybeDecrementObserverCounts(this.#entryTypes); + this.#entryTypes.clear(); for (let n = 0; n < entryTypes.length; n++) { if (ArrayPrototypeIncludes(kSupportedEntryTypes, entryTypes[n])) { - this[kEntryTypes].add(entryTypes[n]); + this.#entryTypes.add(entryTypes[n]); maybeIncrementObserverCount(entryTypes[n]); } } } else { if (!ArrayPrototypeIncludes(kSupportedEntryTypes, type)) return; - this[kEntryTypes].add(type); + this.#entryTypes.add(type); maybeIncrementObserverCount(type); if (buffered) { const entries = filterBufferMapByNameAndType(undefined, type); - ArrayPrototypePushApply(this[kBuffer], entries); + ArrayPrototypePushApply(this.#buffer, entries); kPending.add(this); if (kPending.size) queuePending(); } } - if (this[kEntryTypes].size) + if (this.#entryTypes.size) kObservers.add(this); else this.disconnect(); } disconnect() { - maybeDecrementObserverCounts(this[kEntryTypes]); + maybeDecrementObserverCounts(this.#entryTypes); kObservers.delete(this); kPending.delete(this); - this[kBuffer] = []; - this[kEntryTypes].clear(); - this[kType] = undefined; + this.#buffer = []; + this.#entryTypes.clear(); + this.#type = undefined; } takeRecords() { - const list = this[kBuffer]; - this[kBuffer] = []; + const list = this.#buffer; + this.#buffer = []; return list; } @@ -293,17 +290,17 @@ class PerformanceObserver { } [kMaybeBuffer](entry) { - if (!this[kEntryTypes].has(entry.entryType)) + if (!this.#entryTypes.has(entry.entryType)) return; - ArrayPrototypePush(this[kBuffer], entry); + ArrayPrototypePush(this.#buffer, entry); kPending.add(this); if (kPending.size) queuePending(); } [kDispatch]() { - this[kCallback](new PerformanceObserverEntryList(this.takeRecords()), - this); + this.#callback(new PerformanceObserverEntryList(this.takeRecords()), + this); } [kInspect](depth, options) { @@ -317,8 +314,8 @@ class PerformanceObserver { return `PerformanceObserver ${inspect({ connected: kObservers.has(this), pending: kPending.has(this), - entryTypes: ArrayFrom(this[kEntryTypes]), - buffer: this[kBuffer], + entryTypes: ArrayFrom(this.#entryTypes), + buffer: this.#buffer, }, opts)}`; } }