mirror of
https://github.com/movie-web/movie-web.git
synced 2024-12-26 16:11:53 +01:00
some extra checks for the progress saver
This commit is contained in:
parent
1491a117b4
commit
e1edb1cc1f
@ -1,6 +1,7 @@
|
||||
import { useEffect, useRef } from "react";
|
||||
import { useInterval } from "react-use";
|
||||
|
||||
import { playerStatus } from "@/stores/player/slices/source";
|
||||
import { usePlayerStore } from "@/stores/player/store";
|
||||
import { useProgressStore } from "@/stores/progress";
|
||||
|
||||
@ -8,31 +9,36 @@ export function ProgressSaver() {
|
||||
const meta = usePlayerStore((s) => s.meta);
|
||||
const progress = usePlayerStore((s) => s.progress);
|
||||
const updateItem = useProgressStore((s) => s.updateItem);
|
||||
const status = usePlayerStore((s) => s.status);
|
||||
const hasPlayedOnce = usePlayerStore((s) => s.mediaPlaying.hasPlayedOnce);
|
||||
|
||||
const updateItemRef = useRef(updateItem);
|
||||
const dataRef = useRef({
|
||||
updateItem,
|
||||
meta,
|
||||
progress,
|
||||
status,
|
||||
hasPlayedOnce,
|
||||
});
|
||||
useEffect(() => {
|
||||
updateItemRef.current = updateItem;
|
||||
}, [updateItem]);
|
||||
|
||||
const metaRef = useRef(meta);
|
||||
useEffect(() => {
|
||||
metaRef.current = meta;
|
||||
}, [meta]);
|
||||
|
||||
const progressRef = useRef(progress);
|
||||
useEffect(() => {
|
||||
progressRef.current = progress;
|
||||
}, [progress]);
|
||||
dataRef.current.updateItem = updateItem;
|
||||
dataRef.current.meta = meta;
|
||||
dataRef.current.progress = progress;
|
||||
dataRef.current.status = status;
|
||||
dataRef.current.hasPlayedOnce = hasPlayedOnce;
|
||||
}, [updateItem, progress, meta, status, hasPlayedOnce]);
|
||||
|
||||
useInterval(() => {
|
||||
if (updateItemRef.current && metaRef.current && progressRef.current)
|
||||
updateItemRef.current({
|
||||
meta: metaRef.current,
|
||||
progress: {
|
||||
duration: progress.duration,
|
||||
watched: progress.time,
|
||||
},
|
||||
});
|
||||
const d = dataRef.current;
|
||||
if (!d.progress || !d.meta || !d.updateItem) return;
|
||||
if (d.status !== playerStatus.PLAYING) return;
|
||||
if (!hasPlayedOnce) return;
|
||||
d.updateItem({
|
||||
meta: d.meta,
|
||||
progress: {
|
||||
duration: progress.duration,
|
||||
watched: progress.time,
|
||||
},
|
||||
});
|
||||
}, 3000);
|
||||
|
||||
return null;
|
||||
|
Loading…
Reference in New Issue
Block a user