fix: abort preload if next track is unavailable
Part-of: <https://dev.funkwhale.audio/funkwhale/funkwhale/-/merge_requests/2346>
This commit is contained in:
parent
b21edbb64d
commit
536fa25f0e
|
@ -122,7 +122,7 @@ export const useTracks = createGlobalState(() => {
|
||||||
}, 3000, { immediate: false })
|
}, 3000, { immediate: false })
|
||||||
|
|
||||||
// Preload next track
|
// Preload next track
|
||||||
const { start: startPreloadTimeout } = useTimeoutFn(async (index) => {
|
const { start: startPreloadTimeout, stop: abortPreload } = useTimeoutFn(async (index) => {
|
||||||
const { queue } = useQueue()
|
const { queue } = useQueue()
|
||||||
const sound = await createSound(queue.value[index as number])
|
const sound = await createSound(queue.value[index as number])
|
||||||
await sound.preload()
|
await sound.preload()
|
||||||
|
@ -167,15 +167,15 @@ export const useTracks = createGlobalState(() => {
|
||||||
|
|
||||||
let lastTrack: QueueTrack
|
let lastTrack: QueueTrack
|
||||||
watchEffect(async () => {
|
watchEffect(async () => {
|
||||||
|
abortPreload()
|
||||||
|
|
||||||
if (!hasNext.value) return
|
if (!hasNext.value) return
|
||||||
|
|
||||||
const nextTrack = queue.value[currentIndex.value + 1]
|
const nextTrack = queue.value[currentIndex.value + 1]
|
||||||
if (lastTrack === nextTrack) return
|
if (nextTrack && lastTrack === nextTrack) return
|
||||||
lastTrack = nextTrack
|
lastTrack = nextTrack
|
||||||
|
|
||||||
// NOTE: Preload next track
|
// NOTE: Preload next track
|
||||||
// Calling this function clears previous timeout and starts a new one.
|
|
||||||
// Since this watchEffect fires whenever currentIndex / nextTrack changes, it will automatically cleanup previous preload.
|
|
||||||
// @ts-expect-error vueuse is wrongly typed: https://github.com/vueuse/vueuse/issues/2691
|
// @ts-expect-error vueuse is wrongly typed: https://github.com/vueuse/vueuse/issues/2691
|
||||||
startPreloadTimeout(currentIndex.value + 1)
|
startPreloadTimeout(currentIndex.value + 1)
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in New Issue