| 41 | if (this.alive) this.msgCb?.({ type: 'parse-result', id, result, parseMs }); |
| 42 | } |
| 43 | postMessage(msg: unknown): void { |
| 44 | const m = msg as { type: string } & Partial<ParseMsg>; |
| 45 | if (m.type === 'load-grammars') { |
| 46 | setTimeout(() => { if (this.alive) this.msgCb?.({ type: 'grammars-loaded' }); }, 0); |
| 47 | return; |
| 48 | } |
| 49 | if (m.type !== 'parse') return; |
| 50 | const action = this.behavior(m as ParseMsg); |
| 51 | if ('crash' in action) { |
| 52 | this.alive = false; |
| 53 | setTimeout(() => this.exitCb?.(1), 0); // simulate a WASM-OOM exit(1) |
| 54 | return; |
| 55 | } |
| 56 | if ('hang' in action) return; // never reply → timeout path |
| 57 | if ('wait' in action) { void action.wait.then((r) => this.reply(m.id!, r)); return; } |
| 58 | setTimeout(() => this.reply(m.id!, action.result, action.parseMs), 0); |
| 59 | } |
| 60 | terminate(): Promise<number> { this.alive = false; this.onTerminate?.(); return Promise.resolve(0); } |
| 61 | } |
| 62 | |