Skip to content

Commit aea7fe5

Browse files
anonrigUlisesGascon
authored andcommitted
inspector: use private fields instead of symbols
PR-URL: #50776 Reviewed-By: Vinícius Lourenço Claro Cardoso <contact@viniciusl.com.br> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
1 parent a9d2909 commit aea7fe5

File tree

1 file changed

+24
-33
lines changed

1 file changed

+24
-33
lines changed

lib/inspector.js

+24-33
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ const {
44
JSONParse,
55
JSONStringify,
66
SafeMap,
7-
Symbol,
87
SymbolDispose,
98
} = primordials;
109

@@ -45,28 +44,19 @@ const {
4544
console,
4645
} = internalBinding('inspector');
4746

48-
const connectionSymbol = Symbol('connectionProperty');
49-
const messageCallbacksSymbol = Symbol('messageCallbacks');
50-
const nextIdSymbol = Symbol('nextId');
51-
const onMessageSymbol = Symbol('onMessage');
52-
5347
class Session extends EventEmitter {
54-
constructor() {
55-
super();
56-
this[connectionSymbol] = null;
57-
this[nextIdSymbol] = 1;
58-
this[messageCallbacksSymbol] = new SafeMap();
59-
}
48+
#connection = null;
49+
#nextId = 1;
50+
#messageCallbacks = new SafeMap();
6051

6152
/**
6253
* Connects the session to the inspector back-end.
6354
* @returns {void}
6455
*/
6556
connect() {
66-
if (this[connectionSymbol])
57+
if (this.#connection)
6758
throw new ERR_INSPECTOR_ALREADY_CONNECTED('The inspector session');
68-
this[connectionSymbol] =
69-
new Connection((message) => this[onMessageSymbol](message));
59+
this.#connection = new Connection((message) => this.#onMessage(message));
7060
}
7161

7262
/**
@@ -77,23 +67,24 @@ class Session extends EventEmitter {
7767
connectToMainThread() {
7868
if (isMainThread)
7969
throw new ERR_INSPECTOR_NOT_WORKER();
80-
if (this[connectionSymbol])
70+
if (this.#connection)
8171
throw new ERR_INSPECTOR_ALREADY_CONNECTED('The inspector session');
82-
this[connectionSymbol] =
72+
this.#connection =
8373
new MainThreadConnection(
84-
(message) => queueMicrotask(() => this[onMessageSymbol](message)));
74+
(message) => queueMicrotask(() => this.#onMessage(message)));
8575
}
8676

87-
[onMessageSymbol](message) {
77+
#onMessage(message) {
8878
const parsed = JSONParse(message);
8979
try {
9080
if (parsed.id) {
91-
const callback = this[messageCallbacksSymbol].get(parsed.id);
92-
this[messageCallbacksSymbol].delete(parsed.id);
81+
const callback = this.#messageCallbacks.get(parsed.id);
82+
this.#messageCallbacks.delete(parsed.id);
9383
if (callback) {
9484
if (parsed.error) {
95-
return callback(new ERR_INSPECTOR_COMMAND(parsed.error.code,
96-
parsed.error.message));
85+
return callback(
86+
new ERR_INSPECTOR_COMMAND(parsed.error.code, parsed.error.message),
87+
);
9788
}
9889

9990
callback(null, parsed.result);
@@ -127,18 +118,18 @@ class Session extends EventEmitter {
127118
validateFunction(callback, 'callback');
128119
}
129120

130-
if (!this[connectionSymbol]) {
121+
if (!this.#connection) {
131122
throw new ERR_INSPECTOR_NOT_CONNECTED();
132123
}
133-
const id = this[nextIdSymbol]++;
124+
const id = this.#nextId++;
134125
const message = { id, method };
135126
if (params) {
136127
message.params = params;
137128
}
138129
if (callback) {
139-
this[messageCallbacksSymbol].set(id, callback);
130+
this.#messageCallbacks.set(id, callback);
140131
}
141-
this[connectionSymbol].dispatch(JSONStringify(message));
132+
this.#connection.dispatch(JSONStringify(message));
142133
}
143134

144135
/**
@@ -148,16 +139,16 @@ class Session extends EventEmitter {
148139
* @returns {void}
149140
*/
150141
disconnect() {
151-
if (!this[connectionSymbol])
142+
if (!this.#connection)
152143
return;
153-
this[connectionSymbol].disconnect();
154-
this[connectionSymbol] = null;
155-
const remainingCallbacks = this[messageCallbacksSymbol].values();
144+
this.#connection.disconnect();
145+
this.#connection = null;
146+
const remainingCallbacks = this.#messageCallbacks.values();
156147
for (const callback of remainingCallbacks) {
157148
process.nextTick(callback, new ERR_INSPECTOR_CLOSED());
158149
}
159-
this[messageCallbacksSymbol].clear();
160-
this[nextIdSymbol] = 1;
150+
this.#messageCallbacks.clear();
151+
this.#nextId = 1;
161152
}
162153
}
163154

0 commit comments

Comments
 (0)