mirror of
https://github.com/legop3/MultiRoombaRover.git
synced 2026-11-14 18:58:49 -05:00
29 lines
1.0 KiB
JavaScript
29 lines
1.0 KiB
JavaScript
import { useCallback, useEffect, useRef, useState } from 'react';
|
|
import { RESTART_DELAY_MS } from '../lib/whepPlayback.js';
|
|
|
|
export default function useWhepRestart(enabled = true) {
|
|
const timer = useRef(null);
|
|
const enabledRef = useRef(enabled);
|
|
const [restartToken, setRestartToken] = useState(0);
|
|
const cancelRestart = useCallback(() => {
|
|
clearTimeout(timer.current);
|
|
timer.current = null;
|
|
}, []);
|
|
useEffect(() => {
|
|
enabledRef.current = enabled;
|
|
if (!enabled) cancelRestart();
|
|
}, [enabled, cancelRestart]);
|
|
const scheduleRestart = useCallback(() => {
|
|
if (!enabledRef.current) return;
|
|
cancelRestart();
|
|
// Re-negotiate instead of reusing the failed PeerConnection. Callers that
|
|
// own session requests also use this token to obtain fresh authorization.
|
|
timer.current = setTimeout(() => {
|
|
timer.current = null;
|
|
setRestartToken(Date.now());
|
|
}, RESTART_DELAY_MS);
|
|
}, [cancelRestart]);
|
|
useEffect(() => cancelRestart, [cancelRestart]);
|
|
return { restartToken, scheduleRestart, cancelRestart };
|
|
}
|