feat(rerun): retain live playback and recover isolated viewer resources
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
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())};
|
||||
}
|
||||
Reference in New Issue
Block a user