Got models and event system working

This commit is contained in:
Gregory Schier
2026-03-08 15:18:31 -07:00
parent 7382287bef
commit 0a616eb5e2
13 changed files with 181 additions and 52 deletions

View File

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