feat(spotify): library caching

This commit is contained in:
2025-10-16 11:27:08 -04:00
parent e19c25e94b
commit 1bffafad44
7 changed files with 1570 additions and 69 deletions

View File

@@ -23,6 +23,7 @@ export interface SpotifyAuthState {
// User data
user: SpotifyUser | null;
loggedIn: boolean;
cacheTimestamp: number | null; // Unix timestamp in seconds
}
// Initialize the store with spotify.json
@@ -36,7 +37,8 @@ const defaultState: SpotifyAuthState = {
refreshToken: null,
expiresAt: null,
user: null,
loggedIn: false
loggedIn: false,
cacheTimestamp: null
};
// Create a writable store for reactive UI updates
@@ -50,6 +52,7 @@ export async function loadSpotifyAuth(): Promise<void> {
const refreshToken = await store.get<string>('refreshToken');
const expiresAt = await store.get<number>('expiresAt');
const user = await store.get<SpotifyUser>('user');
const cacheTimestamp = await store.get<number>('cacheTimestamp');
spotifyAuth.set({
clientId: clientId ?? null,
@@ -58,7 +61,8 @@ export async function loadSpotifyAuth(): Promise<void> {
refreshToken: refreshToken ?? null,
expiresAt: expiresAt ?? null,
user: user ?? null,
loggedIn: !!(accessToken && user)
loggedIn: !!(accessToken && user),
cacheTimestamp: cacheTimestamp ?? null
});
}
@@ -129,5 +133,16 @@ export function isTokenExpired(expiresAt: number | null): boolean {
return Date.now() >= (expiresAt - bufferTime);
}
// Save cache timestamp
export async function saveCacheTimestamp(timestamp: number): Promise<void> {
await store.set('cacheTimestamp', timestamp);
await store.save();
spotifyAuth.update(s => ({
...s,
cacheTimestamp: timestamp
}));
}
// Initialize on module load
loadSpotifyAuth();