22 lines
1.2 KiB
TypeScript
22 lines
1.2 KiB
TypeScript
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<ReturnType<typeof liveRerunPlayback>|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 controller.current>)=>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())};
|
|
}
|