MCPcopy
hub / github.com/wavetermdev/waveterm / rpcResponseGenerator

Function rpcResponseGenerator

frontend/app/store/wshrpcutil-base.ts:16–72  ·  view source on GitHub ↗
(
    openRpcs: Map<string, ClientRpcEntry>,
    command: string,
    reqid: string,
    timeout: number
)

Source from the content-addressed store, hash-verified

14}
15
16async 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

Callers 1

sendRpcCommandFunction · 0.85

Calls 4

sendRpcCancelFunction · 0.85
pushMethod · 0.80
setMethod · 0.80
deleteMethod · 0.80

Tested by

no test coverage detected