|
4 | 4 | JSONParse, |
5 | 5 | JSONStringify, |
6 | 6 | SafeMap, |
7 | | - Symbol, |
8 | 7 | SymbolDispose, |
9 | 8 | } = primordials; |
10 | 9 |
|
@@ -45,28 +44,19 @@ const { |
45 | 44 | console, |
46 | 45 | } = internalBinding('inspector'); |
47 | 46 |
|
48 | | -const connectionSymbol = Symbol('connectionProperty'); |
49 | | -const messageCallbacksSymbol = Symbol('messageCallbacks'); |
50 | | -const nextIdSymbol = Symbol('nextId'); |
51 | | -const onMessageSymbol = Symbol('onMessage'); |
52 | | - |
53 | 47 | 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(); |
60 | 51 |
|
61 | 52 | /** |
62 | 53 | * Connects the session to the inspector back-end. |
63 | 54 | * @returns {void} |
64 | 55 | */ |
65 | 56 | connect() { |
66 | | - if (this[connectionSymbol]) |
| 57 | + if (this.#connection) |
67 | 58 | 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)); |
70 | 60 | } |
71 | 61 |
|
72 | 62 | /** |
@@ -77,23 +67,24 @@ class Session extends EventEmitter { |
77 | 67 | connectToMainThread() { |
78 | 68 | if (isMainThread) |
79 | 69 | throw new ERR_INSPECTOR_NOT_WORKER(); |
80 | | - if (this[connectionSymbol]) |
| 70 | + if (this.#connection) |
81 | 71 | throw new ERR_INSPECTOR_ALREADY_CONNECTED('The inspector session'); |
82 | | - this[connectionSymbol] = |
| 72 | + this.#connection = |
83 | 73 | new MainThreadConnection( |
84 | | - (message) => queueMicrotask(() => this[onMessageSymbol](message))); |
| 74 | + (message) => queueMicrotask(() => this.#onMessage(message))); |
85 | 75 | } |
86 | 76 |
|
87 | | - [onMessageSymbol](message) { |
| 77 | + #onMessage(message) { |
88 | 78 | const parsed = JSONParse(message); |
89 | 79 | try { |
90 | 80 | 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); |
93 | 83 | if (callback) { |
94 | 84 | 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 | + ); |
97 | 88 | } |
98 | 89 |
|
99 | 90 | callback(null, parsed.result); |
@@ -127,18 +118,18 @@ class Session extends EventEmitter { |
127 | 118 | validateFunction(callback, 'callback'); |
128 | 119 | } |
129 | 120 |
|
130 | | - if (!this[connectionSymbol]) { |
| 121 | + if (!this.#connection) { |
131 | 122 | throw new ERR_INSPECTOR_NOT_CONNECTED(); |
132 | 123 | } |
133 | | - const id = this[nextIdSymbol]++; |
| 124 | + const id = this.#nextId++; |
134 | 125 | const message = { id, method }; |
135 | 126 | if (params) { |
136 | 127 | message.params = params; |
137 | 128 | } |
138 | 129 | if (callback) { |
139 | | - this[messageCallbacksSymbol].set(id, callback); |
| 130 | + this.#messageCallbacks.set(id, callback); |
140 | 131 | } |
141 | | - this[connectionSymbol].dispatch(JSONStringify(message)); |
| 132 | + this.#connection.dispatch(JSONStringify(message)); |
142 | 133 | } |
143 | 134 |
|
144 | 135 | /** |
@@ -148,16 +139,16 @@ class Session extends EventEmitter { |
148 | 139 | * @returns {void} |
149 | 140 | */ |
150 | 141 | disconnect() { |
151 | | - if (!this[connectionSymbol]) |
| 142 | + if (!this.#connection) |
152 | 143 | 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(); |
156 | 147 | for (const callback of remainingCallbacks) { |
157 | 148 | process.nextTick(callback, new ERR_INSPECTOR_CLOSED()); |
158 | 149 | } |
159 | | - this[messageCallbacksSymbol].clear(); |
160 | | - this[nextIdSymbol] = 1; |
| 150 | + this.#messageCallbacks.clear(); |
| 151 | + this.#nextId = 1; |
161 | 152 | } |
162 | 153 | } |
163 | 154 |
|
|