import { invoke } from "@tauri-apps/api/core"; import { listen as tauriListen } from "@tauri-apps/api/event"; import type { RpcEventSchema, RpcSchema, } from "@yaakapp-internal/proxy-lib"; type Req = RpcSchema[K][0]; type Res = RpcSchema[K][1]; export async function rpc( cmd: K, payload: Req, ): Promise> { return invoke("rpc", { cmd, payload }) as Promise>; } /** Subscribe to a backend event. Returns an unsubscribe function. */ export function listen( event: K & string, callback: (payload: RpcEventSchema[K]) => void, ): () => void { let unsub: (() => void) | null = null; tauriListen(event, (e) => callback(e.payload)) .then((fn) => { unsub = fn; }) .catch(console.error); return () => unsub?.(); }