(msg: unknown)
| 60 | if (this.alive) this.msgCb?.({ type: 'result', id, result }); |
| 61 | } |
| 62 | postMessage(msg: unknown): void { |
| 63 | const m = msg as CallMsg | { type: 'close' } | { type: 'trim' }; |
| 64 | if (m?.type === 'trim') { |
| 65 | this.trims++; |
| 66 | if (!this.holdsTrimReport && this.alive) this.emitMessage({ type: 'trimmed', heldBytes: this.heldBytes }); |
| 67 | return; |
| 68 | } |
| 69 | if (m?.type === 'close') { |
| 70 | // Like the real worker: close up and exit by itself, with code 0. |
| 71 | if (this.ignoresClose) return; |
| 72 | this.alive = false; |
| 73 | setTimeout(() => { this.exitCode = 0; this.exitCb?.(0); }, 0); |
| 74 | return; |
| 75 | } |
| 76 | if (!m || m.type !== 'call') return; |
| 77 | const action = this.behavior(m); |
| 78 | if ('crash' in action) { |
| 79 | this.alive = false; |
| 80 | setTimeout(() => this.exitCb?.(13), 0); // simulate a crash exit |
| 81 | return; |
| 82 | } |
| 83 | if ('hang' in action) return; // never reply |
| 84 | if ('wait' in action) { void action.wait.then((r) => this.reply(m.id, r)); return; } |
| 85 | setTimeout(() => this.reply(m.id, action.result), 0); |
| 86 | } |
| 87 | terminate(): Promise<number> { this.alive = false; this.terminated = true; return Promise.resolve(0); } |
| 88 | } |
| 89 |
nothing calls this directly
no test coverage detected