| 50 | } |
| 51 | |
| 52 | function waitForMessage( |
| 53 | messages: ReadonlyArray<Record<string, any>>, |
| 54 | predicate: (m: Record<string, any>) => boolean, |
| 55 | timeoutMs: number, |
| 56 | ): Promise<Record<string, any>> { |
| 57 | return new Promise((resolve, reject) => { |
| 58 | const started = Date.now(); |
| 59 | const tick = () => { |
| 60 | const hit = messages.find(predicate); |
| 61 | if (hit) return resolve(hit); |
| 62 | if (Date.now() - started > timeoutMs) { |
| 63 | return reject(new Error(`Timed out. Messages so far: ${JSON.stringify(messages)}`)); |
| 64 | } |
| 65 | setTimeout(tick, 20); |
| 66 | }; |
| 67 | tick(); |
| 68 | }); |
| 69 | } |
| 70 | |
| 71 | function send(child: ChildProcessWithoutNullStreams, msg: object): void { |
| 72 | child.stdin.write(JSON.stringify(msg) + '\n'); |