MCPcopy Create free account
hub / github.com/arctic-cli/interface / client

Function client

packages/arctic/src/util/rpc.ts:16–41  ·  view source on GitHub ↗
(target: {
    postMessage: (data: string) => void | null
    onmessage: ((this: Worker, ev: MessageEvent<any>) => any) | null
  })

Source from the content-addressed store, hash-verified

14 }
15
16 export function client<T extends Definition>(target: {
17 postMessage: (data: string) => void | null
18 onmessage: ((this: Worker, ev: MessageEvent<any>) => any) | null
19 }) {
20 const pending = new Map<number, (result: any) => void>()
21 let id = 0
22 target.onmessage = async (evt) => {
23 const parsed = JSON.parse(evt.data)
24 if (parsed.type === "rpc.result") {
25 const resolve = pending.get(parsed.id)
26 if (resolve) {
27 resolve(parsed.result)
28 pending.delete(parsed.id)
29 }
30 }
31 }
32 return {
33 call<Method extends keyof T>(method: Method, input: Parameters<T[Method]>[0]): Promise<ReturnType<T[Method]>> {
34 const requestId = id++
35 return new Promise((resolve) => {
36 pending.set(requestId, resolve)
37 target.postMessage(JSON.stringify({ type: "rpc.request", method, input, id: requestId }))
38 })
39 },
40 }
41 }
42}

Callers

nothing calls this directly

Calls 3

resolveFunction · 0.85
getMethod · 0.45
deleteMethod · 0.45

Tested by

no test coverage detected