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