From abe67de13899d1e007c1739b1c72acb9d9a7782d Mon Sep 17 00:00:00 2001 From: wvffle Date: Mon, 28 Nov 2022 22:16:50 +0000 Subject: [PATCH] Fix radio race condition --- front/src/composables/audio/queue.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/front/src/composables/audio/queue.ts b/front/src/composables/audio/queue.ts index bea8df7e2..8c64205a0 100644 --- a/front/src/composables/audio/queue.ts +++ b/front/src/composables/audio/queue.ts @@ -315,11 +315,22 @@ export const useQueue = createGlobalState(() => { if (localStorage.queue) { (async () => { const { queue: { currentIndex: index, tracks: oldTracks } } = JSON.parse(localStorage.queue) as { queue: { currentIndex: number, tracks: Track[] } } + + const oldRadios = localStorage.radios + if (oldTracks.length !== 0) { tracks.value.length = 0 await enqueue(...oldTracks) } + // NOTE: There is a race condition between clearing queue and adding new tracks that resets the radio. + // We need to reset the radio to the old state + try { + const radios = JSON.parse(oldRadios) + store.commit('radios/current', radios.radios.current) + store.commit('radios/running', radios.radios.running) + } catch (err) {} + currentIndex.value = index delete localStorage.queue })().catch((error) => console.error('Could not successfully migrate between queue versions', error))