From 9fe0424baa862005327dd0e3318425a211632f92 Mon Sep 17 00:00:00 2001 From: Jungku Lee Date: Fri, 29 Dec 2023 08:20:22 +0900 Subject: [PATCH] trace_events: use private fields instead of symbols for `Tracing` PR-URL: https://github.com/nodejs/node/pull/51180 Refs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/Private_properties Reviewed-By: James M Snell Reviewed-By: Luigi Pinca Reviewed-By: Trivikram Kamat Reviewed-By: Yagiz Nizipli --- lib/trace_events.js | 29 ++++++++++++++--------------- test/parallel/test-util-inspect.js | 2 +- 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/lib/trace_events.js b/lib/trace_events.js index 860543e30bc35e..b4b9be242737e2 100644 --- a/lib/trace_events.js +++ b/lib/trace_events.js @@ -3,13 +3,9 @@ const { ArrayPrototypeJoin, SafeSet, - Symbol, } = primordials; const { hasTracing } = internalBinding('config'); -const kHandle = Symbol('handle'); -const kEnabled = Symbol('enabled'); -const kCategories = Symbol('categories'); const kMaxTracingCount = 10; @@ -33,16 +29,19 @@ const { const enabledTracingObjects = new SafeSet(); class Tracing { + #handle; + #categories; + #enabled = false; + constructor(categories) { - this[kHandle] = new CategorySet(categories); - this[kCategories] = categories; - this[kEnabled] = false; + this.#handle = new CategorySet(categories); + this.#categories = categories; } enable() { - if (!this[kEnabled]) { - this[kEnabled] = true; - this[kHandle].enable(); + if (!this.#enabled) { + this.#enabled = true; + this.#handle.enable(); enabledTracingObjects.add(this); if (enabledTracingObjects.size > kMaxTracingCount) { process.emitWarning( @@ -54,19 +53,19 @@ class Tracing { } disable() { - if (this[kEnabled]) { - this[kEnabled] = false; - this[kHandle].disable(); + if (this.#enabled) { + this.#enabled = false; + this.#handle.disable(); enabledTracingObjects.delete(this); } } get enabled() { - return this[kEnabled]; + return this.#enabled; } get categories() { - return ArrayPrototypeJoin(this[kCategories], ','); + return ArrayPrototypeJoin(this.#categories, ','); } [customInspectSymbol](depth, opts) { diff --git a/test/parallel/test-util-inspect.js b/test/parallel/test-util-inspect.js index 4c9e0d683fe7d0..ba5b6f9fe84bb3 100644 --- a/test/parallel/test-util-inspect.js +++ b/test/parallel/test-util-inspect.js @@ -2920,7 +2920,7 @@ assert.strictEqual( try { const trace = require('trace_events').createTracing({ categories: ['fo'] }); const actualDepth0 = util.inspect({ trace }, { depth: 0 }); - assert.strictEqual(actualDepth0, '{ trace: [Tracing] }'); + assert.strictEqual(actualDepth0, '{ trace: Tracing {} }'); const actualDepth1 = util.inspect({ trace }, { depth: 1 }); assert.strictEqual( actualDepth1,