mirror of
https://github.com/jeffvli/feishin.git
synced 2026-06-15 16:04:19 +02:00
fix timestamp restore when song switched before playback start (#2094)
This commit is contained in:
@@ -19,6 +19,8 @@ import {
|
|||||||
import { toast } from '/@/shared/components/toast/toast';
|
import { toast } from '/@/shared/components/toast/toast';
|
||||||
import { PlayerStatus } from '/@/shared/types/types';
|
import { PlayerStatus } from '/@/shared/types/types';
|
||||||
|
|
||||||
|
let startupRestoreSessionHandled = false;
|
||||||
|
|
||||||
export const useQueueRestoreTimestamp = () => {
|
export const useQueueRestoreTimestamp = () => {
|
||||||
const { mediaSeekToTimestamp } = usePlayerActions();
|
const { mediaSeekToTimestamp } = usePlayerActions();
|
||||||
|
|
||||||
@@ -51,28 +53,65 @@ export const useInitialTimestampRestore = () => {
|
|||||||
|
|
||||||
const startupRestoreInitializedRef = useRef(false);
|
const startupRestoreInitializedRef = useRef(false);
|
||||||
const startupSeekArmedRef = useRef<null | number>(null);
|
const startupSeekArmedRef = useRef<null | number>(null);
|
||||||
|
const startupSeekTargetUniqueIdRef = useRef<null | string>(null);
|
||||||
const startupSeekAppliedRef = useRef(false);
|
const startupSeekAppliedRef = useRef(false);
|
||||||
|
|
||||||
const applyStartupSeek = useCallback(() => {
|
const cancelStartupSeek = useCallback(() => {
|
||||||
if (startupSeekAppliedRef.current) {
|
if (startupSeekAppliedRef.current) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const seekTimestamp = startupSeekArmedRef.current;
|
|
||||||
if (!seekTimestamp || seekTimestamp <= 0) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
startupSeekAppliedRef.current = true;
|
startupSeekAppliedRef.current = true;
|
||||||
startupSeekArmedRef.current = null;
|
startupSeekArmedRef.current = null;
|
||||||
|
startupSeekTargetUniqueIdRef.current = null;
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const applyStartupSeek = useCallback(() => {
|
||||||
|
const seekTimestamp = startupSeekArmedRef.current;
|
||||||
|
|
||||||
|
if (startupSeekAppliedRef.current) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!seekTimestamp || seekTimestamp <= 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const targetUniqueId = startupSeekTargetUniqueIdRef.current;
|
||||||
|
const currentUniqueId = usePlayerStore.getState().getQueue().items[
|
||||||
|
usePlayerStore.getState().player.index
|
||||||
|
]?._uniqueId;
|
||||||
|
|
||||||
|
if (targetUniqueId && currentUniqueId !== targetUniqueId) {
|
||||||
|
cancelStartupSeek();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
startupSeekAppliedRef.current = true;
|
||||||
|
startupSeekArmedRef.current = null;
|
||||||
|
startupSeekTargetUniqueIdRef.current = null;
|
||||||
|
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
mediaSeekToTimestamp(seekTimestamp);
|
mediaSeekToTimestamp(seekTimestamp);
|
||||||
}, 100);
|
}, 100);
|
||||||
}, [mediaSeekToTimestamp]);
|
}, [cancelStartupSeek, mediaSeekToTimestamp]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (startupRestoreInitializedRef.current) {
|
const targetUniqueId = startupSeekTargetUniqueIdRef.current;
|
||||||
|
if (
|
||||||
|
!targetUniqueId ||
|
||||||
|
startupSeekAppliedRef.current ||
|
||||||
|
!currentSong ||
|
||||||
|
currentSong._uniqueId === targetUniqueId
|
||||||
|
) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
cancelStartupSeek();
|
||||||
|
}, [cancelStartupSeek, currentSong]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (startupRestoreInitializedRef.current || startupRestoreSessionHandled) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -81,9 +120,11 @@ export const useInitialTimestampRestore = () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
startupRestoreInitializedRef.current = true;
|
startupRestoreInitializedRef.current = true;
|
||||||
|
startupRestoreSessionHandled = true;
|
||||||
|
|
||||||
if (timestamp > 0) {
|
if (timestamp > 0) {
|
||||||
startupSeekArmedRef.current = timestamp;
|
startupSeekArmedRef.current = timestamp;
|
||||||
|
startupSeekTargetUniqueIdRef.current = currentSong._uniqueId;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (playerStatus === PlayerStatus.PLAYING) {
|
if (playerStatus === PlayerStatus.PLAYING) {
|
||||||
|
|||||||
Reference in New Issue
Block a user