When video is playing, save position intermittently

This commit is contained in:
David Granado 2022-03-02 10:13:46 -06:00 committed by infinite-persistence
parent c4ae797add
commit 38f00be231
2 changed files with 30 additions and 0 deletions

View file

@ -27,9 +27,11 @@ import { getAllIds } from 'util/buildHomepage';
import type { HomepageCat } from 'util/buildHomepage';
import debounce from 'util/debounce';
import { formatLbryUrlForWeb, generateListSearchUrlParams } from 'util/url';
import useInterval from 'effects/use-interval';
// const PLAY_TIMEOUT_ERROR = 'play_timeout_error';
// const PLAY_TIMEOUT_LIMIT = 2000;
const PLAY_POSITION_SAVE_INTERVAL = 1500;
type Props = {
position: number,
@ -137,6 +139,7 @@ function VideoViewer(props: Props) {
const [videoNode, setVideoNode] = useState();
const [localAutoplayNext, setLocalAutoplayNext] = useState(autoplayNext);
const isFirstRender = React.useRef(true);
const playerRef = React.useRef(null);
useEffect(() => {
if (isFirstRender.current) {
@ -146,6 +149,12 @@ function VideoViewer(props: Props) {
toggleAutoplayNext();
}, [localAutoplayNext]);
useInterval(() => {
if (playerRef.current && isPlaying) {
handlePosition(playerRef.current);
}
}, PLAY_POSITION_SAVE_INTERVAL);
const updateVolumeState = React.useCallback(
debounce((volume, muted) => {
changeVolume(volume);
@ -420,6 +429,8 @@ function VideoViewer(props: Props) {
if (position) {
player.currentTime(position);
}
playerRef.current = player;
}, playerReadyDependencyList); // eslint-disable-line
return (

View file

@ -0,0 +1,19 @@
import { useRef, useEffect } from 'react';
export default function useInterval(callback, delay) {
const savedCallback = useRef();
useEffect(() => {
savedCallback.current = callback;
}, [callback]);
useEffect(() => {
const tick = () => {
savedCallback.current();
};
if (delay !== null) {
let id = setInterval(tick, delay);
return () => clearInterval(id);
}
}, [delay]);
}