mirror of
https://gitea.toothfairyai.com/ToothFairyAI/tf_code.git
synced 2026-04-01 14:52:25 +00:00
disable server unless explicitly opted in (#7529)
This commit is contained in:
@@ -13,11 +13,16 @@ export namespace Rpc {
|
||||
}
|
||||
}
|
||||
|
||||
export function emit(event: string, data: unknown) {
|
||||
postMessage(JSON.stringify({ type: "rpc.event", event, data }))
|
||||
}
|
||||
|
||||
export function client<T extends Definition>(target: {
|
||||
postMessage: (data: string) => void | null
|
||||
onmessage: ((this: Worker, ev: MessageEvent<any>) => any) | null
|
||||
}) {
|
||||
const pending = new Map<number, (result: any) => void>()
|
||||
const listeners = new Map<string, Set<(data: any) => void>>()
|
||||
let id = 0
|
||||
target.onmessage = async (evt) => {
|
||||
const parsed = JSON.parse(evt.data)
|
||||
@@ -28,6 +33,14 @@ export namespace Rpc {
|
||||
pending.delete(parsed.id)
|
||||
}
|
||||
}
|
||||
if (parsed.type === "rpc.event") {
|
||||
const handlers = listeners.get(parsed.event)
|
||||
if (handlers) {
|
||||
for (const handler of handlers) {
|
||||
handler(parsed.data)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return {
|
||||
call<Method extends keyof T>(method: Method, input: Parameters<T[Method]>[0]): Promise<ReturnType<T[Method]>> {
|
||||
@@ -37,6 +50,17 @@ export namespace Rpc {
|
||||
target.postMessage(JSON.stringify({ type: "rpc.request", method, input, id: requestId }))
|
||||
})
|
||||
},
|
||||
on<Data>(event: string, handler: (data: Data) => void) {
|
||||
let handlers = listeners.get(event)
|
||||
if (!handlers) {
|
||||
handlers = new Set()
|
||||
listeners.set(event, handlers)
|
||||
}
|
||||
handlers.add(handler)
|
||||
return () => {
|
||||
handlers!.delete(handler)
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user