import {useEffect,useRef,useState} from 'react'; import type {LiveRerunViewer} from './rerunHost'; import {emptyLivePlayback,liveRerunPlayback} from './liveRerunPlayback'; export function useLiveRerunPlayback(viewer:LiveRerunViewer|null,acquisition:string){ const controller=useRef|null>(null); const [state,setState]=useState(emptyLivePlayback); useEffect(()=>{ setState(emptyLivePlayback); if(!viewer)return; const current=liveRerunPlayback(viewer);controller.current=current; const poll=()=>{try{setState(current.read());}catch{/* Native recording may be between frames. */}}; poll();const timer=setInterval(poll,250); return()=>{clearInterval(timer);if(controller.current===current)controller.current=null;}; },[viewer,acquisition]); const update=(action:(value:NonNullable)=>typeof state)=>{ const current=controller.current;if(!current)return; try{setState(action(current));}catch{/* Teardown does not own acquisition control. */} }; return {state,seek:(time:number)=>update(c=>c.seek(time)),setPlaying:(playing:boolean)=>update(c=>c.setPlaying(playing)),jumpToLive:()=>update(c=>c.jumpToLive())}; }