UNPKG

@masa-dev/react-signage

Version:

This is a react library for signage.

31 lines (30 loc) 1.09 kB
import { MAX_RETRY_SPAN, MIN_RETRY_SPAN } from "../../../../consts"; import { useDebug } from "../../../debug/useDebug"; import { useEffect, useRef } from "react"; export function useVideoError(params) { const { ref } = params; const { debugMessage } = useDebug(); const retrySpanRef = useRef(1000); const retryTimerRef = useRef(undefined); useEffect(() => { return () => clearTimeout(retryTimerRef.current); }, []); const handleVideoError = () => { if (!ref.current?.src) return; debugMessage({ message: 'video error. retrying...', severity: 'error' }); clearTimeout(retryTimerRef.current); retryTimerRef.current = setTimeout(() => { ref.current?.load(); ref.current?.play(); upSpan(); }, retrySpanRef.current); }; const upSpan = () => { retrySpanRef.current = Math.min(retrySpanRef.current * 1.2, MAX_RETRY_SPAN); }; const resetSpan = () => { retrySpanRef.current = MIN_RETRY_SPAN; }; return { handleVideoError, resetSpan }; }