feat(web): subscribe to SSE and refresh home/playlists on playlist.system_rebuilt
test-web / test (push) Successful in 39s

The web client only ever SENT events; it had no inbound SSE listener, so a
tab left open across the daily system-playlist rebuild kept showing
yesterday's home + playlist snapshots until a manual reload (the stale-
browse-view bug behind #968). Add useServerEvents(): opens /api/events/stream
while authenticated and, on playlist.system_rebuilt, invalidates the home,
playlists, and system-playlist-status query caches. Deliberately does not
disturb the active playback queue — that self-heals on the failure path.
Issue #968.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-06-20 15:44:05 -04:00
parent 16f76ea707
commit 5a80a1e460
2 changed files with 39 additions and 0 deletions
+37
View File
@@ -0,0 +1,37 @@
import { queryClient } from '$lib/query/client';
import { qk } from '$lib/api/queries';
import { user } from '$lib/auth/store.svelte';
// Inbound server events over SSE (#968). The web client's OUTBOUND half
// (play events) lives in player/events.svelte.ts; this is the inbound
// listener it never had. Today it reacts to `playlist.system_rebuilt` — the
// daily 03:00 rebuild or a manual refresh — by invalidating the home +
// system-playlist query caches so a tab left open across the rebuild stops
// serving yesterday's snapshot (the stale-browse-view bug). Active playback
// self-heals separately on the failure path (store.handleLoadFailure); we
// deliberately do NOT yank a playing queue here — that would interrupt a
// mid-song listen to restart the new mix at track 0.
function onSystemRebuilt(): void {
queryClient.invalidateQueries({ queryKey: qk.home() });
// Prefix match invalidates every kind ('user' | 'system' | 'all').
queryClient.invalidateQueries({ queryKey: ['playlists'] });
queryClient.invalidateQueries({ queryKey: qk.systemPlaylistsStatus() });
}
// Wire once from +layout.svelte's mount. Opens the stream only while
// authenticated and closes it on logout; EventSource auto-reconnects on
// transient network drops.
export function useServerEvents(): void {
$effect(() => {
if (!user.value) return;
// Absent under SSR / jsdom — the listener simply no-ops there.
if (typeof EventSource === 'undefined') return;
const es = new EventSource('/api/events/stream');
es.addEventListener('playlist.system_rebuilt', onSystemRebuilt);
return () => {
es.removeEventListener('playlist.system_rebuilt', onSystemRebuilt);
es.close();
};
});
}
+2
View File
@@ -23,6 +23,7 @@
} from '$lib/player/store.svelte';
import { useMediaSession } from '$lib/player/mediaSession.svelte';
import { useEventsDispatcher } from '$lib/player/events.svelte';
import { useServerEvents } from '$lib/serverEvents.svelte';
import { useGlobalShortcuts } from '$lib/player/shortcuts.svelte';
import { applyMetaThemeColor } from '$lib/theme/applyMetaThemeColor.svelte';
import { audioLoader } from '$lib/player/audioLoader';
@@ -132,6 +133,7 @@
useMediaSession();
useEventsDispatcher();
useServerEvents();
useGlobalShortcuts();
applyMetaThemeColor();
</script>