From 536fa25f0e5eb9e74726e6d79acea97670c0b8f9 Mon Sep 17 00:00:00 2001 From: Kasper Seweryn Date: Sun, 29 Jan 2023 10:41:59 +0100 Subject: [PATCH] fix: abort preload if next track is unavailable Part-of: --- front/src/composables/audio/tracks.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/front/src/composables/audio/tracks.ts b/front/src/composables/audio/tracks.ts index 13fc845db..7c24f5560 100644 --- a/front/src/composables/audio/tracks.ts +++ b/front/src/composables/audio/tracks.ts @@ -122,7 +122,7 @@ export const useTracks = createGlobalState(() => { }, 3000, { immediate: false }) // Preload next track - const { start: startPreloadTimeout } = useTimeoutFn(async (index) => { + const { start: startPreloadTimeout, stop: abortPreload } = useTimeoutFn(async (index) => { const { queue } = useQueue() const sound = await createSound(queue.value[index as number]) await sound.preload() @@ -167,15 +167,15 @@ export const useTracks = createGlobalState(() => { let lastTrack: QueueTrack watchEffect(async () => { + abortPreload() + if (!hasNext.value) return const nextTrack = queue.value[currentIndex.value + 1] - if (lastTrack === nextTrack) return + if (nextTrack && lastTrack === nextTrack) return lastTrack = nextTrack // 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 startPreloadTimeout(currentIndex.value + 1) })