/
vshmidt
/
label-studio
Обзор
Документация
Войти
/
vshmidt
/
label-studio
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
develop
web/libs/editor/src/hooks/useUpdateBuffering.ts
42 строки
1 KB
bmartel
fix: FIT-31: Ensure media buffers are syncable events that keep audio and video in sync (#7633)
21 авг 2025, 12:56
Не верифицирован
21 авг 2025, 12:56
0fd582d
Код
Авторство
О чём код?
import { useCallback, useRef, useEffect } from "react"; import { SYNC_WINDOW } from "../mixins/Syncable"; export const useUpdateBuffering = ( mediaRef: React.RefObject<HTMLMediaElement> | React.MutableRefObject<HTMLMediaElement | undefined>, onBufferingChange: (isBuffering: boolean) => void, ) => { const timeoutRef = useRef<number | null>(null); const bufferingStartedTime = useRef<number | null>(null); const updateBuffering = useCallback(() => { if (timeoutRef.current) { clearTimeout(timeoutRef.current); timeoutRef.current = null; } const mediaEl = mediaRef.current; if (!mediaEl) return; const shouldWaitForSyncWindow = bufferingStartedTime.current !== null && Date.now() - bufferingStartedTime.current < SYNC_WINDOW; const isBuffering = mediaEl.networkState === mediaEl.NETWORK_LOADING || shouldWaitForSyncWindow; if (isBuffering) { onBufferingChange(true); bufferingStartedTime.current = bufferingStartedTime.current ?? Date.now(); timeoutRef.current = window.setTimeout(updateBuffering, 16); } else { onBufferingChange(false); } }, [mediaRef, onBufferingChange]); useEffect(() => { return () => { if (timeoutRef.current) { clearTimeout(timeoutRef.current); } }; }, []); return updateBuffering; };