mirror of
https://github.com/markuryy/shark.git
synced 2025-12-12 19:51:01 +00:00
feat(spotify): library caching
This commit is contained in:
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user