From 08dc86b9879fba94cbf285a2a6506a001d79df91 Mon Sep 17 00:00:00 2001 From: vicrodh <103399283+vicrodh@users.noreply.github.com> Date: Fri, 6 Feb 2026 21:20:06 -0600 Subject: [PATCH] add homeDataCache store for instant Home navigation Module-level in-memory cache that stores all 11 Home sections' resolved data with TTL (5 min) and genre filter snapshot validation. Follows the same pattern as searchState.ts. --- src/lib/stores/homeDataCache.ts | 97 +++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 src/lib/stores/homeDataCache.ts diff --git a/src/lib/stores/homeDataCache.ts b/src/lib/stores/homeDataCache.ts new file mode 100644 index 000000000..6cc7cbd6a --- /dev/null +++ b/src/lib/stores/homeDataCache.ts @@ -0,0 +1,97 @@ +/** + * Home Data Cache Store + * + * Module-level in-memory cache for HomeView data. + * Prevents re-fetching all 11 sections when navigating back to Home. + * Same pattern as searchState.ts — plain module state, no Svelte stores. + */ + +import type { DisplayTrack, DiscoverPlaylist, DiscoverAlbum, PlaylistTag } from '$lib/types'; + +export interface AlbumCardData { + id: string; + artwork: string; + title: string; + artist: string; + genre: string; + quality?: string; + releaseDate?: string; +} + +export interface ArtistCardData { + id: number; + name: string; + image?: string; + playCount?: number; +} + +export interface HomeCacheData { + // Featured (Qobuz editorial) + newReleases: AlbumCardData[]; + pressAwards: AlbumCardData[]; + mostStreamed: AlbumCardData[]; + qobuzissimes: AlbumCardData[]; + editorPicks: AlbumCardData[]; + + // User-specific (ML) + recentAlbums: AlbumCardData[]; + continueTracks: DisplayTrack[]; + topArtists: ArtistCardData[]; + favoriteAlbums: AlbumCardData[]; + + // Discover + qobuzPlaylists: DiscoverPlaylist[]; + essentialDiscography: DiscoverAlbum[]; + playlistTags: PlaylistTag[]; + + // Metadata + timestamp: number; + genreIds: number[]; // snapshot of genre filter at cache time + scrollTop: number; +} + +const CACHE_TTL_MS = 5 * 60 * 1000; // 5 minutes + +let cache: HomeCacheData | null = null; + +export function getHomeCache(): HomeCacheData | null { + return cache; +} + +export function setHomeCache(data: Omit & { genreIds: number[] }): void { + cache = { + ...data, + timestamp: Date.now(), + scrollTop: cache?.scrollTop ?? 0, + }; +} + +export function clearHomeCache(): void { + cache = null; +} + +export function isHomeCacheValid(currentGenreIds: number[]): boolean { + if (!cache) return false; + + // Check TTL + if (Date.now() - cache.timestamp > CACHE_TTL_MS) { + cache = null; + return false; + } + + // Check genre filter match + const cachedSorted = [...cache.genreIds].sort((a, b) => a - b); + const currentSorted = [...currentGenreIds].sort((a, b) => a - b); + if (cachedSorted.length !== currentSorted.length) return false; + for (let i = 0; i < cachedSorted.length; i++) { + if (cachedSorted[i] !== currentSorted[i]) return false; + } + + return true; +} + +export function updateHomeCacheScrollTop(scrollTop: number): void { + if (cache) { + cache.scrollTop = scrollTop; + } +}