()
| 316 | } |
| 317 | |
| 318 | private spawnOne(): void { |
| 319 | if (this.destroyed || this.workers.size >= this.maxSize || !this.healthy) return; |
| 320 | let w: ParsePoolWorker; |
| 321 | try { |
| 322 | w = this.createWorker(); |
| 323 | } catch { |
| 324 | this.totalCrashes++; // counts toward the circuit breaker |
| 325 | return; |
| 326 | } |
| 327 | this.workers.add(w); |
| 328 | this.pending.add(w); |
| 329 | this.parseCounts.set(w, 0); |
| 330 | let settle!: () => void; |
| 331 | const settled = new Promise<void>((resolve) => { settle = resolve; }); |
| 332 | this.loads.set(w, { settled, settle }); |
| 333 | w.on('message', (m) => this.onMessage(w, (m ?? {}) as ParseWorkerMessage)); |
| 334 | w.on('error', (e) => { this.loadSettled(w); this.onWorkerGone(w, `Worker error: ${e?.message ?? 'unknown'}`); }); |
| 335 | w.on('exit', (code) => { this.loadSettled(w); if (code !== 0) this.onWorkerGone(w, `Worker exited with code ${code}`); }); |
| 336 | // Load grammars; the worker replies 'grammars-loaded' and only then is idle. |
| 337 | // Pre-read WASM bytes (when the orchestrator provided them) make this a |
| 338 | // memory load instead of a per-spawn disk read. |
| 339 | w.postMessage({ type: 'load-grammars', languages: this.languages, grammarBuffers: this.grammarBuffers }); |
| 340 | } |
| 341 | |
| 342 | private onMessage(w: ParsePoolWorker, m: ParseWorkerMessage): void { |
| 343 | if (m.type === 'grammars-loaded') { |
no test coverage detected