| 1 | import { useCallback, useSyncExternalStore } from "react"; |
| 2 | import type { ControllerLiveStore } from "./useController"; |
| 3 | |
| 4 | /** Keep streaming telemetry on the live subscription, off the app state tree. */ |
| 5 | export function useLiveTurnMetrics(liveStore: ControllerLiveStore | undefined, tabId: string | undefined) { |
| 6 | const subscribe = useCallback( |
| 7 | (cb: () => void) => liveStore?.subscribe(tabId, cb) ?? (() => {}), |
| 8 | [liveStore, tabId], |
| 9 | ); |
| 10 | const liveOutput = useSyncExternalStore(subscribe, () => liveStore?.getSnapshot(tabId)); |
| 11 | const liveModelActiveAt = useSyncExternalStore(subscribe, () => liveStore?.getModelActiveAt?.(tabId)); |
| 12 | const liveRateOutputQuarters = useSyncExternalStore(subscribe, () => liveStore?.getRateOutputQuarters?.(tabId)); |
| 13 | return { liveOutput, liveModelActiveAt, liveRateOutputQuarters }; |
| 14 | } |
| 15 |