()
| 341 | } |
| 342 | |
| 343 | private spawnOne(): void { |
| 344 | if (this.destroyed || this.workers.size >= this.maxSize || !this.healthy) return; |
| 345 | let w: ParsePoolWorker; |
| 346 | try { |
| 347 | w = this.createWorker(); |
| 348 | } catch { |
| 349 | this.totalCrashes++; // counts toward the circuit breaker |
| 350 | return; |
| 351 | } |
| 352 | this.workers.add(w); |
| 353 | this.pending.add(w); |
| 354 | this.parseCounts.set(w, 0); |
| 355 | let settle!: () => void; |
| 356 | const settled = new Promise<void>((resolve) => { settle = resolve; }); |
| 357 | this.loads.set(w, { settled, settle }); |
| 358 | w.on('message', (m) => this.onMessage(w, (m ?? {}) as ParseWorkerMessage)); |
| 359 | w.on('error', (e) => { this.loadSettled(w); this.onWorkerGone(w, `Worker error: ${e?.message ?? 'unknown'}`); }); |
| 360 | w.on('exit', (code) => { this.loadSettled(w); if (code !== 0) this.onWorkerGone(w, `Worker exited with code ${code}`); }); |
| 361 | // Load grammars; the worker replies 'grammars-loaded' and only then is idle. |
| 362 | // Pre-read WASM bytes (when the orchestrator provided them) make this a |
| 363 | // memory load instead of a per-spawn disk read. |
| 364 | w.postMessage({ type: 'load-grammars', languages: this.languages, grammarBuffers: this.grammarBuffers }); |
| 365 | } |
| 366 | |
| 367 | private onMessage(w: ParsePoolWorker, m: ParseWorkerMessage): void { |
| 368 | if (m.type === 'grammars-loaded') { |
no test coverage detected