(
openRpcs: Map<string, ClientRpcEntry>,
command: string,
reqid: string,
timeout: number
)
| 14 | } |
| 15 | |
| 16 | async function* rpcResponseGenerator( |
| 17 | openRpcs: Map<string, ClientRpcEntry>, |
| 18 | command: string, |
| 19 | reqid: string, |
| 20 | timeout: number |
| 21 | ): AsyncGenerator<any, void, boolean> { |
| 22 | const msgQueue: RpcMessage[] = []; |
| 23 | let signalFn: () => void; |
| 24 | let signalPromise = new Promise<void>((resolve) => (signalFn = resolve)); |
| 25 | let timeoutId: NodeJS.Timeout = null; |
| 26 | if (timeout > 0) { |
| 27 | timeoutId = setTimeout(() => { |
| 28 | msgQueue.push({ resid: reqid, error: "EC-TIME: timeout waiting for response" }); |
| 29 | signalFn(); |
| 30 | }, timeout); |
| 31 | } |
| 32 | const msgFn = (msg: RpcMessage) => { |
| 33 | msgQueue.push(msg); |
| 34 | signalFn(); |
| 35 | // reset signal promise |
| 36 | signalPromise = new Promise<void>((resolve) => (signalFn = resolve)); |
| 37 | }; |
| 38 | openRpcs.set(reqid, { |
| 39 | reqId: reqid, |
| 40 | startTs: Date.now(), |
| 41 | command: command, |
| 42 | msgFn: msgFn, |
| 43 | }); |
| 44 | yield null; |
| 45 | try { |
| 46 | while (true) { |
| 47 | while (msgQueue.length > 0) { |
| 48 | const msg = msgQueue.shift()!; |
| 49 | if (msg.error != null) { |
| 50 | throw new Error(msg.error); |
| 51 | } |
| 52 | if (!msg.cont && msg.data == null) { |
| 53 | return; |
| 54 | } |
| 55 | const shouldTerminate = yield msg.data; |
| 56 | if (shouldTerminate) { |
| 57 | sendRpcCancel(reqid); |
| 58 | return; |
| 59 | } |
| 60 | if (!msg.cont) { |
| 61 | return; |
| 62 | } |
| 63 | } |
| 64 | await signalPromise; |
| 65 | } |
| 66 | } finally { |
| 67 | openRpcs.delete(reqid); |
| 68 | if (timeoutId != null) { |
| 69 | clearTimeout(timeoutId); |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 |
no test coverage detected