From cedec104a142962f365ced1d52699377a9922fb4 Mon Sep 17 00:00:00 2001 From: Yuri Tatishchev Date: Fri, 6 Feb 2026 02:09:37 -0800 Subject: [PATCH] global player pt. 8 preload stuff --- src/lib/components/GlobalPlayer.svelte | 71 +++++++++++++++++++++----- 1 file changed, 58 insertions(+), 13 deletions(-) diff --git a/src/lib/components/GlobalPlayer.svelte b/src/lib/components/GlobalPlayer.svelte index a16fd9d..e503262 100644 --- a/src/lib/components/GlobalPlayer.svelte +++ b/src/lib/components/GlobalPlayer.svelte @@ -26,6 +26,12 @@ let audioEl: HTMLAudioElement | null = null; + // Best-effort preload of the upcoming track's URL + let nextPreloadHref = $state(null); + + // Dedicated preloader element to warm the connection / decode pipeline a bit + let preloadEl = $state(null); + let isPlaying = $state(false); let currentTime = $state(0); let duration = $state(0); @@ -95,6 +101,41 @@ audioEl.volume = snap.volume; } + function computeNextTrackToPreload(): Track | null { + // Prefer to preload the "linear next" item. For shuffle mode, we can still + // best-effort preload the first item in the upcoming order. + if (snap.queue.length === 0) return null; + + if (snap.shuffleEnabled) { + const nextIdx = snap.order[0]; + if (typeof nextIdx === "number") return snap.queue[nextIdx] ?? null; + return null; + } + + if (snap.currentIndex == null) return snap.queue[0] ?? null; + const nextIdx = snap.currentIndex + 1; + return nextIdx >= 0 && nextIdx < snap.queue.length + ? snap.queue[nextIdx] + : null; + } + + function updatePreloadTargets() { + const nextTrack = computeNextTrackToPreload(); + nextPreloadHref = nextTrack?.src ?? null; + + // Also warm via an offscreen