feat: SWR cache for Sidebar playlists
Sidebar now uses stale-while-revalidate for playlists, settings, and local track counts. On mount, cached data is shown immediately while fresh data loads in the background. - Create sidebarDataCache.ts with sessionStorage persistence - Sidebar restores from cache on mount (fresh/stale), loads in background - Cache cleared only on real playlist changes (create/delete/track changes) - Sort order, play counts, and UI state do NOT invalidate the cache - Coordinated loadSidebarData() replaces three independent load calls
This commit is contained in:
1 parent
4aa32d388b
commit
ee7ea4f164
2 files changed
+146
-7
No files matched your search
@@ -38,6 +38,12 @@
|
||||
getActiveMenuId,
|
||||
MENU_INACTIVITY_TIMEOUT
|
||||
} from '$lib/stores/floatingMenuStore';
|
||||
import {
|
||||
getSidebarCache,
|
||||
getSidebarCacheStatus,
|
||||
setSidebarCache,
|
||||
clearSidebarCache
|
||||
} from '$lib/stores/sidebarDataCache';
|
||||
|
||||
interface Playlist {
|
||||
id: number;
|
||||
@@ -430,9 +436,9 @@
|
||||
}
|
||||
|
||||
export function refreshPlaylists() {
|
||||
clearSidebarCache();
|
||||
playlistTooltipCache.clear();
|
||||
loadUserPlaylists();
|
||||
loadPlaylistSettings();
|
||||
loadSidebarData();
|
||||
}
|
||||
|
||||
export function refreshPlaylistSettings() {
|
||||
@@ -445,9 +451,9 @@
|
||||
|
||||
// Call this when tracks are added/removed from a playlist
|
||||
export function onPlaylistTracksChanged(playlistId: number) {
|
||||
clearSidebarCache();
|
||||
invalidatePlaylistTooltip(playlistId);
|
||||
loadUserPlaylists();
|
||||
loadLocalTrackCounts();
|
||||
loadSidebarData();
|
||||
}
|
||||
|
||||
// Focus and clear the search input (for keybinding)
|
||||
@@ -927,15 +933,34 @@
|
||||
}
|
||||
}
|
||||
|
||||
async function loadSidebarData() {
|
||||
await Promise.all([
|
||||
loadUserPlaylists(),
|
||||
loadPlaylistSettings(),
|
||||
loadLocalTrackCounts()
|
||||
]);
|
||||
saveSidebarToCache();
|
||||
}
|
||||
|
||||
onMount(() => {
|
||||
loadSortPreference();
|
||||
loadUserPlaylists();
|
||||
loadPlaylistSettings();
|
||||
loadLocalTrackCounts();
|
||||
loadFolders(); // Load playlist folders
|
||||
loadFavoritesPreferences(); // Load favorites tab order
|
||||
loadSidebarCollapseState(); // Load collapse states
|
||||
|
||||
// SWR: try cache first for playlists/settings/counts
|
||||
const cacheStatus = getSidebarCacheStatus();
|
||||
if (cacheStatus === 'fresh') {
|
||||
restoreFromSidebarCache();
|
||||
} else if (cacheStatus === 'stale') {
|
||||
restoreFromSidebarCache();
|
||||
// Background refresh
|
||||
loadSidebarData();
|
||||
} else {
|
||||
// Empty: normal load with loading indicator
|
||||
loadSidebarData();
|
||||
}
|
||||
|
||||
// Subscribe to offline state changes
|
||||
const unsubscribeOffline = subscribeOffline(() => {
|
||||
offlineStatus = getOfflineStatus();
|
||||
@@ -1015,6 +1040,42 @@
|
||||
}
|
||||
}
|
||||
|
||||
function saveSidebarToCache() {
|
||||
if (isOffline) return; // Don't cache offline data
|
||||
const settingsArray = Array.from(playlistSettings.values());
|
||||
const countsRecord: Record<string, number> = {};
|
||||
for (const [id, count] of localTrackCounts) {
|
||||
countsRecord[String(id)] = count;
|
||||
}
|
||||
setSidebarCache({
|
||||
playlists: userPlaylists,
|
||||
playlistSettings: settingsArray,
|
||||
localTrackCounts: countsRecord,
|
||||
});
|
||||
}
|
||||
|
||||
function restoreFromSidebarCache(): boolean {
|
||||
const cached = getSidebarCache();
|
||||
if (!cached) return false;
|
||||
|
||||
userPlaylists = cached.playlists as Playlist[];
|
||||
|
||||
const settingsMap = new Map<number, PlaylistSettings>();
|
||||
for (const s of cached.playlistSettings as PlaylistSettings[]) {
|
||||
settingsMap.set(s.qobuz_playlist_id, s);
|
||||
}
|
||||
playlistSettings = settingsMap;
|
||||
|
||||
const countsMap = new Map<number, number>();
|
||||
for (const [id, count] of Object.entries(cached.localTrackCounts)) {
|
||||
countsMap.set(Number(id), count);
|
||||
}
|
||||
localTrackCounts = countsMap;
|
||||
|
||||
playlistsLoading = false;
|
||||
return true;
|
||||
}
|
||||
|
||||
async function loadUserPlaylists() {
|
||||
playlistsLoading = true;
|
||||
try {
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
/**
|
||||
* Sidebar Data Cache Store (SWR)
|
||||
*
|
||||
* Module-level cache for sidebar data (playlists, settings, local track counts).
|
||||
* Uses sessionStorage for persistence across navigation.
|
||||
*
|
||||
* Cache invalidation policy:
|
||||
* - Invalidate on real playlist changes: create, delete, track add/remove
|
||||
* - Do NOT invalidate on: sort order (local), play counts, UI state
|
||||
*/
|
||||
|
||||
const SESSION_KEY = 'qbz-sidebar-cache';
|
||||
|
||||
export interface SidebarCacheData {
|
||||
playlists: unknown[];
|
||||
playlistSettings: unknown[];
|
||||
localTrackCounts: Record<string, number>;
|
||||
timestamp: number;
|
||||
}
|
||||
|
||||
const FRESH_TTL_MS = 5 * 60 * 1000; // 5 minutes
|
||||
const MAX_TTL_MS = 60 * 60 * 1000; // 60 minutes
|
||||
|
||||
export type SidebarCacheStatus = 'fresh' | 'stale' | 'empty';
|
||||
|
||||
let cache: SidebarCacheData | null = null;
|
||||
|
||||
function restoreFromSession(): void {
|
||||
try {
|
||||
const stored = sessionStorage.getItem(SESSION_KEY);
|
||||
if (stored) {
|
||||
cache = JSON.parse(stored);
|
||||
}
|
||||
} catch {
|
||||
// sessionStorage not available or corrupted
|
||||
}
|
||||
}
|
||||
|
||||
function persistToSession(): void {
|
||||
if (!cache) {
|
||||
try { sessionStorage.removeItem(SESSION_KEY); } catch {}
|
||||
return;
|
||||
}
|
||||
try {
|
||||
sessionStorage.setItem(SESSION_KEY, JSON.stringify(cache));
|
||||
} catch {
|
||||
// sessionStorage full or not available
|
||||
}
|
||||
}
|
||||
|
||||
// Initialize from sessionStorage
|
||||
restoreFromSession();
|
||||
|
||||
export function getSidebarCacheStatus(): SidebarCacheStatus {
|
||||
if (!cache) return 'empty';
|
||||
|
||||
const age = Date.now() - cache.timestamp;
|
||||
if (age > MAX_TTL_MS) return 'empty';
|
||||
if (age <= FRESH_TTL_MS) return 'fresh';
|
||||
return 'stale';
|
||||
}
|
||||
|
||||
export function getSidebarCache(): SidebarCacheData | null {
|
||||
return cache;
|
||||
}
|
||||
|
||||
export function setSidebarCache(data: Omit<SidebarCacheData, 'timestamp'>): void {
|
||||
cache = {
|
||||
...data,
|
||||
timestamp: Date.now(),
|
||||
};
|
||||
persistToSession();
|
||||
}
|
||||
|
||||
export function clearSidebarCache(): void {
|
||||
cache = null;
|
||||
persistToSession();
|
||||
}
|
||||
Reference in new issue
Block a user