| 1 | import { useEffect, useSyncExternalStore } from "react"; |
| 2 | import { app } from "./bridge"; |
| 3 | import { runtimeStateStore, selectRuntime, selectRuntimeSession } from "./runtimeStateStore"; |
| 4 | import type { SessionIdentity } from "./sessionIdentity"; |
| 5 | |
| 6 | export function useRuntimeSession(tabId: string | undefined, identity: SessionIdentity | string | undefined) { |
| 7 | const snapshot = useSyncExternalStore(runtimeStateStore.subscribe, runtimeStateStore.getSnapshot); |
| 8 | const failed = useSyncExternalStore(runtimeStateStore.subscribe, runtimeStateStore.getFailed); |
| 9 | const session = selectRuntimeSession(snapshot, tabId, identity); |
| 10 | return selectRuntime(session, failed); |
| 11 | } |
| 12 | |
| 13 | export function useRuntimeStateSync() { |
| 14 | useEffect(() => { |
| 15 | if (!app.GetRuntimeStateSnapshot) return; |
| 16 | let disposed = false; |
| 17 | let stop: (() => void) | undefined; |
| 18 | void import("./runtimeStateSync").then(({ startAppRuntimeStateSync }) => { |
| 19 | if (!disposed) stop = startAppRuntimeStateSync(); |
| 20 | }); |
| 21 | return () => { disposed = true; stop?.(); }; |
| 22 | }, []); |
| 23 | } |
| 24 |