Skip to content

Commit

Permalink
trace_events: use private fields instead of symbols for Tracing
Browse files Browse the repository at this point in the history
PR-URL: #51180
Refs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/Private_properties
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: Trivikram Kamat <[email protected]>
Reviewed-By: Yagiz Nizipli <[email protected]>
  • Loading branch information
Jungku Lee authored Dec 28, 2023
1 parent 9db4bf4 commit 9fe0424
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 16 deletions.
29 changes: 14 additions & 15 deletions lib/trace_events.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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(
Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-util-inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 9fe0424

Please sign in to comment.