feat(favorites): implement local SQLite persistence for favorites
This ensures favorites sync properly across all views (album detail, continue listening, etc.) by using local persistence as the source of truth for UI, with API calls for actual operations. Backend (Rust): - Add favorites_cache.rs module with SQLite tables: - favorite_tracks (track_id INTEGER PRIMARY KEY) - favorite_albums (album_id TEXT PRIMARY KEY) - favorite_artists (artist_id INTEGER PRIMARY KEY) - Add Tauri commands for cache operations: - get_cached_favorite_* - read from local cache - cache_favorite_* / uncache_favorite_* - add/remove single items - sync_cached_favorite_* - bulk sync from API data - clear_favorites_cache - for logout Frontend: - Rewrite favoritesStore.ts to use persistence: - loadFavorites(): Load from cache first, then sync from API - toggleFavorite(): API call first → update cache on success - No optimistic updates - UI shows loading state until confirmed - Rewrite albumFavoritesStore.ts with same pattern - Create artistFavoritesStore.ts for artist favorites - Update +page.svelte to load all favorites on login - Update FavoritesView to sync cache when loading from API Sync strategy: - On login: Fetch from Qobuz API → sync to local cache - On toggle: API call → on success → update local cache → notify UI - FavoritesView reads from API and syncs local cache - All other views read from local cache (instant UI)
This commit is contained in:
1 parent
defd2f0fe1
commit
4c606e029e
8 files changed
+929
-72
No files matched your search
@@ -2,13 +2,19 @@
|
||||
* Album Favorites Store
|
||||
*
|
||||
* Centralized store for tracking favorite albums.
|
||||
* Uses local SQLite persistence for instant UI updates.
|
||||
*
|
||||
* Sync strategy:
|
||||
* - On login: Fetch from Qobuz API → sync to local cache → populate store
|
||||
* - On toggle: API call first → on success → update local cache → notify UI
|
||||
* - UI reads from in-memory store (backed by local cache)
|
||||
*/
|
||||
|
||||
import { invoke } from '@tauri-apps/api/core';
|
||||
import { logRecoEvent } from '$lib/services/recoService';
|
||||
|
||||
let favoriteAlbumIds = new Set<string>();
|
||||
let togglingAlbumIds = new Set<string>(); // Album IDs currently being toggled (API in progress)
|
||||
let togglingAlbumIds = new Set<string>(); // Album IDs currently being toggled
|
||||
let isLoaded = false;
|
||||
let isLoading = false;
|
||||
const listeners = new Set<() => void>();
|
||||
@@ -40,14 +46,50 @@ export function isFavoritesLoaded(): boolean {
|
||||
return isLoaded;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load album favorites from local cache first, then sync from API
|
||||
*/
|
||||
export async function loadAlbumFavorites(): Promise<void> {
|
||||
if (isLoading || isLoaded) return;
|
||||
isLoading = true;
|
||||
if (isLoading) return;
|
||||
|
||||
isLoading = true;
|
||||
try {
|
||||
// Step 1: Load from local cache for instant UI
|
||||
try {
|
||||
const cachedIds = await invoke<string[]>('get_cached_favorite_albums');
|
||||
if (cachedIds.length > 0) {
|
||||
favoriteAlbumIds = new Set(cachedIds);
|
||||
isLoaded = true;
|
||||
notifyListeners();
|
||||
console.log(`[AlbumFavorites] Loaded ${cachedIds.length} albums from local cache`);
|
||||
}
|
||||
} catch (cacheErr) {
|
||||
console.debug('[AlbumFavorites] No local cache available:', cacheErr);
|
||||
}
|
||||
|
||||
// Step 2: Fetch from API and sync to local cache
|
||||
await syncFromApi();
|
||||
|
||||
} catch (err) {
|
||||
console.error('[AlbumFavorites] Failed to load favorites:', err);
|
||||
if (!isLoaded) {
|
||||
favoriteAlbumIds = new Set();
|
||||
isLoaded = true;
|
||||
notifyListeners();
|
||||
}
|
||||
} finally {
|
||||
isLoading = false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sync album favorites from Qobuz API to local cache
|
||||
*/
|
||||
export async function syncFromApi(): Promise<void> {
|
||||
try {
|
||||
const allAlbumIds: string[] = [];
|
||||
let offset = 0;
|
||||
const limit = 500;
|
||||
const collected: string[] = [];
|
||||
|
||||
while (true) {
|
||||
const result = await invoke<{ albums?: { items: Array<{ id: string }>; total?: number } }>('get_favorites', {
|
||||
@@ -59,25 +101,33 @@ export async function loadAlbumFavorites(): Promise<void> {
|
||||
const items = result.albums?.items ?? [];
|
||||
if (!items.length) break;
|
||||
|
||||
collected.push(...items.map(item => item.id));
|
||||
allAlbumIds.push(...items.map(item => item.id));
|
||||
offset += items.length;
|
||||
|
||||
if (items.length < limit) break;
|
||||
if (result.albums?.total && offset >= result.albums.total) break;
|
||||
}
|
||||
|
||||
favoriteAlbumIds = new Set(collected);
|
||||
// Sync to local cache
|
||||
await invoke('sync_cached_favorite_albums', { albumIds: allAlbumIds });
|
||||
|
||||
// Update in-memory store
|
||||
favoriteAlbumIds = new Set(allAlbumIds);
|
||||
isLoaded = true;
|
||||
notifyListeners();
|
||||
|
||||
console.log(`[AlbumFavorites] Synced ${allAlbumIds.length} albums from API`);
|
||||
} catch (err) {
|
||||
console.error('Failed to load album favorites:', err);
|
||||
} finally {
|
||||
isLoading = false;
|
||||
console.error('[AlbumFavorites] Failed to sync from API:', err);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Toggle album favorite status
|
||||
* API call first → on success → update local cache → notify UI
|
||||
*/
|
||||
export async function toggleAlbumFavorite(albumId: string): Promise<boolean> {
|
||||
// Prevent double-toggling while API call is in progress
|
||||
if (togglingAlbumIds.has(albumId)) {
|
||||
return favoriteAlbumIds.has(albumId);
|
||||
}
|
||||
@@ -85,18 +135,15 @@ export async function toggleAlbumFavorite(albumId: string): Promise<boolean> {
|
||||
const wasFavorite = favoriteAlbumIds.has(albumId);
|
||||
const newState = !wasFavorite;
|
||||
|
||||
// Optimistic update + mark as toggling
|
||||
if (newState) {
|
||||
favoriteAlbumIds.add(albumId);
|
||||
} else {
|
||||
favoriteAlbumIds.delete(albumId);
|
||||
}
|
||||
// Mark as toggling
|
||||
togglingAlbumIds.add(albumId);
|
||||
notifyListeners();
|
||||
|
||||
try {
|
||||
if (newState) {
|
||||
await invoke('add_favorite', { favType: 'album', itemId: albumId });
|
||||
await invoke('cache_favorite_album', { albumId });
|
||||
favoriteAlbumIds.add(albumId);
|
||||
void logRecoEvent({
|
||||
eventType: 'favorite',
|
||||
itemType: 'album',
|
||||
@@ -104,26 +151,37 @@ export async function toggleAlbumFavorite(albumId: string): Promise<boolean> {
|
||||
});
|
||||
} else {
|
||||
await invoke('remove_favorite', { favType: 'album', itemId: albumId });
|
||||
await invoke('uncache_favorite_album', { albumId });
|
||||
favoriteAlbumIds.delete(albumId);
|
||||
}
|
||||
return newState;
|
||||
} catch (err) {
|
||||
if (newState) {
|
||||
favoriteAlbumIds.delete(albumId);
|
||||
} else {
|
||||
favoriteAlbumIds.add(albumId);
|
||||
}
|
||||
console.error('Failed to toggle album favorite:', err);
|
||||
console.error('[AlbumFavorites] Failed to toggle favorite:', err);
|
||||
return wasFavorite;
|
||||
} finally {
|
||||
// Always clear toggling state
|
||||
togglingAlbumIds.delete(albumId);
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sync local cache from an array of album IDs
|
||||
* Used by FavoritesView after fetching from API
|
||||
*/
|
||||
export async function syncCache(albumIds: string[]): Promise<void> {
|
||||
try {
|
||||
await invoke('sync_cached_favorite_albums', { albumIds });
|
||||
favoriteAlbumIds = new Set(albumIds);
|
||||
notifyListeners();
|
||||
} catch (err) {
|
||||
console.error('[AlbumFavorites] Failed to sync cache:', err);
|
||||
}
|
||||
}
|
||||
|
||||
export function markAsFavorite(albumId: string): void {
|
||||
if (!favoriteAlbumIds.has(albumId)) {
|
||||
favoriteAlbumIds.add(albumId);
|
||||
invoke('cache_favorite_album', { albumId }).catch(() => {});
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
@@ -131,6 +189,7 @@ export function markAsFavorite(albumId: string): void {
|
||||
export function unmarkAsFavorite(albumId: string): void {
|
||||
if (favoriteAlbumIds.has(albumId)) {
|
||||
favoriteAlbumIds.delete(albumId);
|
||||
invoke('uncache_favorite_album', { albumId }).catch(() => {});
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,195 @@
|
||||
/**
|
||||
* Artist Favorites Store
|
||||
*
|
||||
* Centralized store for tracking favorite artists.
|
||||
* Uses local SQLite persistence for instant UI updates.
|
||||
*
|
||||
* Sync strategy:
|
||||
* - On login: Fetch from Qobuz API → sync to local cache → populate store
|
||||
* - On toggle: API call first → on success → update local cache → notify UI
|
||||
* - UI reads from in-memory store (backed by local cache)
|
||||
*/
|
||||
|
||||
import { invoke } from '@tauri-apps/api/core';
|
||||
import { logRecoEvent } from '$lib/services/recoService';
|
||||
|
||||
let favoriteArtistIds = new Set<number>();
|
||||
let togglingArtistIds = new Set<number>(); // Artist IDs currently being toggled
|
||||
let isLoaded = false;
|
||||
let isLoading = false;
|
||||
const listeners = new Set<() => void>();
|
||||
|
||||
function notifyListeners(): void {
|
||||
for (const listener of listeners) {
|
||||
listener();
|
||||
}
|
||||
}
|
||||
|
||||
export function subscribe(listener: () => void): () => void {
|
||||
listeners.add(listener);
|
||||
return () => listeners.delete(listener);
|
||||
}
|
||||
|
||||
export function isArtistFavorite(artistId: number): boolean {
|
||||
return favoriteArtistIds.has(artistId);
|
||||
}
|
||||
|
||||
export function isArtistToggling(artistId: number): boolean {
|
||||
return togglingArtistIds.has(artistId);
|
||||
}
|
||||
|
||||
export function getFavoriteArtistIds(): number[] {
|
||||
return Array.from(favoriteArtistIds);
|
||||
}
|
||||
|
||||
export function isFavoritesLoaded(): boolean {
|
||||
return isLoaded;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load artist favorites from local cache first, then sync from API
|
||||
*/
|
||||
export async function loadArtistFavorites(): Promise<void> {
|
||||
if (isLoading) return;
|
||||
|
||||
isLoading = true;
|
||||
try {
|
||||
// Step 1: Load from local cache for instant UI
|
||||
try {
|
||||
const cachedIds = await invoke<number[]>('get_cached_favorite_artists');
|
||||
if (cachedIds.length > 0) {
|
||||
favoriteArtistIds = new Set(cachedIds);
|
||||
isLoaded = true;
|
||||
notifyListeners();
|
||||
console.log(`[ArtistFavorites] Loaded ${cachedIds.length} artists from local cache`);
|
||||
}
|
||||
} catch (cacheErr) {
|
||||
console.debug('[ArtistFavorites] No local cache available:', cacheErr);
|
||||
}
|
||||
|
||||
// Step 2: Fetch from API and sync to local cache
|
||||
await syncFromApi();
|
||||
|
||||
} catch (err) {
|
||||
console.error('[ArtistFavorites] Failed to load favorites:', err);
|
||||
if (!isLoaded) {
|
||||
favoriteArtistIds = new Set();
|
||||
isLoaded = true;
|
||||
notifyListeners();
|
||||
}
|
||||
} finally {
|
||||
isLoading = false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sync artist favorites from Qobuz API to local cache
|
||||
*/
|
||||
export async function syncFromApi(): Promise<void> {
|
||||
try {
|
||||
const allArtistIds: number[] = [];
|
||||
let offset = 0;
|
||||
const limit = 500;
|
||||
|
||||
while (true) {
|
||||
const result = await invoke<{ artists?: { items: Array<{ id: number }>; total?: number } }>('get_favorites', {
|
||||
favType: 'artists',
|
||||
limit,
|
||||
offset
|
||||
});
|
||||
|
||||
const items = result.artists?.items ?? [];
|
||||
if (!items.length) break;
|
||||
|
||||
allArtistIds.push(...items.map(item => item.id));
|
||||
offset += items.length;
|
||||
|
||||
if (items.length < limit) break;
|
||||
if (result.artists?.total && offset >= result.artists.total) break;
|
||||
}
|
||||
|
||||
// Sync to local cache
|
||||
await invoke('sync_cached_favorite_artists', { artistIds: allArtistIds });
|
||||
|
||||
// Update in-memory store
|
||||
favoriteArtistIds = new Set(allArtistIds);
|
||||
isLoaded = true;
|
||||
notifyListeners();
|
||||
|
||||
console.log(`[ArtistFavorites] Synced ${allArtistIds.length} artists from API`);
|
||||
} catch (err) {
|
||||
console.error('[ArtistFavorites] Failed to sync from API:', err);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Toggle artist favorite status
|
||||
* API call first → on success → update local cache → notify UI
|
||||
*/
|
||||
export async function toggleArtistFavorite(artistId: number): Promise<boolean> {
|
||||
if (togglingArtistIds.has(artistId)) {
|
||||
return favoriteArtistIds.has(artistId);
|
||||
}
|
||||
|
||||
const wasFavorite = favoriteArtistIds.has(artistId);
|
||||
const newState = !wasFavorite;
|
||||
|
||||
// Mark as toggling
|
||||
togglingArtistIds.add(artistId);
|
||||
notifyListeners();
|
||||
|
||||
try {
|
||||
if (newState) {
|
||||
await invoke('add_favorite', { favType: 'artist', itemId: String(artistId) });
|
||||
await invoke('cache_favorite_artist', { artistId });
|
||||
favoriteArtistIds.add(artistId);
|
||||
void logRecoEvent({
|
||||
eventType: 'favorite',
|
||||
itemType: 'artist',
|
||||
artistId
|
||||
});
|
||||
} else {
|
||||
await invoke('remove_favorite', { favType: 'artist', itemId: String(artistId) });
|
||||
await invoke('uncache_favorite_artist', { artistId });
|
||||
favoriteArtistIds.delete(artistId);
|
||||
}
|
||||
return newState;
|
||||
} catch (err) {
|
||||
console.error('[ArtistFavorites] Failed to toggle favorite:', err);
|
||||
return wasFavorite;
|
||||
} finally {
|
||||
togglingArtistIds.delete(artistId);
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sync local cache from an array of artist IDs
|
||||
* Used by FavoritesView after fetching from API
|
||||
*/
|
||||
export async function syncCache(artistIds: number[]): Promise<void> {
|
||||
try {
|
||||
await invoke('sync_cached_favorite_artists', { artistIds });
|
||||
favoriteArtistIds = new Set(artistIds);
|
||||
notifyListeners();
|
||||
} catch (err) {
|
||||
console.error('[ArtistFavorites] Failed to sync cache:', err);
|
||||
}
|
||||
}
|
||||
|
||||
export function markAsFavorite(artistId: number): void {
|
||||
if (!favoriteArtistIds.has(artistId)) {
|
||||
favoriteArtistIds.add(artistId);
|
||||
invoke('cache_favorite_artist', { artistId }).catch(() => {});
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
export function unmarkAsFavorite(artistId: number): void {
|
||||
if (favoriteArtistIds.has(artistId)) {
|
||||
favoriteArtistIds.delete(artistId);
|
||||
invoke('uncache_favorite_artist', { artistId }).catch(() => {});
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,12 @@
|
||||
* Favorites Store
|
||||
*
|
||||
* Centralized store for tracking favorite tracks.
|
||||
* Components can check if a track is favorite and toggle status without prop drilling.
|
||||
* Uses local SQLite persistence for instant UI updates.
|
||||
*
|
||||
* Sync strategy:
|
||||
* - On login: Fetch from Qobuz API → sync to local cache → populate store
|
||||
* - On toggle: API call first → on success → update local cache → notify UI
|
||||
* - UI reads from in-memory store (backed by local cache)
|
||||
*/
|
||||
|
||||
import { invoke } from '@tauri-apps/api/core';
|
||||
@@ -60,39 +65,90 @@ export function isFavoritesLoaded(): boolean {
|
||||
}
|
||||
|
||||
/**
|
||||
* Load favorites from backend (call once on app init)
|
||||
* Load favorites from local cache first, then sync from API
|
||||
* Call once on app init/login
|
||||
*/
|
||||
export async function loadFavorites(): Promise<void> {
|
||||
if (isLoading || isLoaded) return;
|
||||
if (isLoading) return;
|
||||
|
||||
isLoading = true;
|
||||
try {
|
||||
const response = await invoke<{ tracks?: { items?: Array<{ id: number }> } }>('get_favorites', {
|
||||
favType: 'track',
|
||||
limit: 500
|
||||
});
|
||||
|
||||
// Defensive: handle missing tracks or items
|
||||
if (response?.tracks?.items) {
|
||||
favoriteTrackIds = new Set(response.tracks.items.map(t => t.id));
|
||||
} else {
|
||||
// No favorites or unexpected response structure
|
||||
favoriteTrackIds = new Set();
|
||||
// Step 1: Load from local cache for instant UI (if available)
|
||||
try {
|
||||
const cachedIds = await invoke<number[]>('get_cached_favorite_tracks');
|
||||
if (cachedIds.length > 0) {
|
||||
favoriteTrackIds = new Set(cachedIds);
|
||||
isLoaded = true;
|
||||
notifyListeners();
|
||||
console.log(`[Favorites] Loaded ${cachedIds.length} tracks from local cache`);
|
||||
}
|
||||
} catch (cacheErr) {
|
||||
console.debug('[Favorites] No local cache available:', cacheErr);
|
||||
}
|
||||
|
||||
isLoaded = true;
|
||||
notifyListeners();
|
||||
// Step 2: Fetch from API and sync to local cache
|
||||
await syncFromApi();
|
||||
|
||||
} catch (err) {
|
||||
console.error('Failed to load favorites:', err);
|
||||
// On error, initialize as empty set
|
||||
favoriteTrackIds = new Set();
|
||||
console.error('[Favorites] Failed to load favorites:', err);
|
||||
// If no cache was loaded, initialize as empty
|
||||
if (!isLoaded) {
|
||||
favoriteTrackIds = new Set();
|
||||
isLoaded = true;
|
||||
notifyListeners();
|
||||
}
|
||||
} finally {
|
||||
isLoading = false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sync favorites from Qobuz API to local cache
|
||||
* Called on login and when FavoritesView loads
|
||||
*/
|
||||
export async function syncFromApi(): Promise<void> {
|
||||
try {
|
||||
// Fetch all favorites from API (paginated)
|
||||
const allTrackIds: number[] = [];
|
||||
let offset = 0;
|
||||
const limit = 500;
|
||||
|
||||
while (true) {
|
||||
const response = await invoke<{ tracks?: { items?: Array<{ id: number }>; total?: number } }>('get_favorites', {
|
||||
favType: 'tracks',
|
||||
limit,
|
||||
offset
|
||||
});
|
||||
|
||||
const items = response?.tracks?.items ?? [];
|
||||
if (items.length === 0) break;
|
||||
|
||||
allTrackIds.push(...items.map(t => t.id));
|
||||
offset += items.length;
|
||||
|
||||
// Check if we've fetched all
|
||||
if (items.length < limit) break;
|
||||
if (response.tracks?.total && offset >= response.tracks.total) break;
|
||||
}
|
||||
|
||||
// Sync to local cache
|
||||
await invoke('sync_cached_favorite_tracks', { trackIds: allTrackIds });
|
||||
|
||||
// Update in-memory store
|
||||
favoriteTrackIds = new Set(allTrackIds);
|
||||
isLoaded = true;
|
||||
notifyListeners();
|
||||
|
||||
console.log(`[Favorites] Synced ${allTrackIds.length} tracks from API`);
|
||||
} catch (err) {
|
||||
console.error('[Favorites] Failed to sync from API:', err);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Toggle favorite status for a track
|
||||
* API call first → on success → update local cache → notify UI
|
||||
* Returns the new favorite state
|
||||
*/
|
||||
export async function toggleTrackFavorite(trackId: number): Promise<boolean> {
|
||||
@@ -104,18 +160,19 @@ export async function toggleTrackFavorite(trackId: number): Promise<boolean> {
|
||||
const wasFavorite = favoriteTrackIds.has(trackId);
|
||||
const newState = !wasFavorite;
|
||||
|
||||
// Optimistic update + mark as toggling
|
||||
if (newState) {
|
||||
favoriteTrackIds.add(trackId);
|
||||
} else {
|
||||
favoriteTrackIds.delete(trackId);
|
||||
}
|
||||
// Mark as toggling (UI shows loading state)
|
||||
togglingTrackIds.add(trackId);
|
||||
notifyListeners();
|
||||
|
||||
try {
|
||||
// Call API first - no optimistic update
|
||||
if (newState) {
|
||||
await invoke('add_favorite', { favType: 'track', itemId: String(trackId) });
|
||||
// API succeeded - update local cache
|
||||
await invoke('cache_favorite_track', { trackId });
|
||||
// Update in-memory store
|
||||
favoriteTrackIds.add(trackId);
|
||||
// Log for recommendations
|
||||
void logRecoEvent({
|
||||
eventType: 'favorite',
|
||||
itemType: 'track',
|
||||
@@ -123,16 +180,16 @@ export async function toggleTrackFavorite(trackId: number): Promise<boolean> {
|
||||
});
|
||||
} else {
|
||||
await invoke('remove_favorite', { favType: 'track', itemId: String(trackId) });
|
||||
// API succeeded - update local cache
|
||||
await invoke('uncache_favorite_track', { trackId });
|
||||
// Update in-memory store
|
||||
favoriteTrackIds.delete(trackId);
|
||||
}
|
||||
|
||||
return newState;
|
||||
} catch (err) {
|
||||
// Rollback on error
|
||||
if (newState) {
|
||||
favoriteTrackIds.delete(trackId);
|
||||
} else {
|
||||
favoriteTrackIds.add(trackId);
|
||||
}
|
||||
console.error('Failed to toggle favorite:', err);
|
||||
// API failed - no state change, just log error
|
||||
console.error('[Favorites] Failed to toggle favorite:', err);
|
||||
return wasFavorite;
|
||||
} finally {
|
||||
// Always clear toggling state
|
||||
@@ -159,32 +216,50 @@ export async function removeTrackFavorite(trackId: number): Promise<boolean> {
|
||||
}
|
||||
|
||||
/**
|
||||
* Manually add a track ID to the local set (for when we add favorites elsewhere)
|
||||
* Sync local cache from an array of track IDs
|
||||
* Used by FavoritesView after fetching from API
|
||||
*/
|
||||
export function markAsFavorite(trackId: number): void {
|
||||
if (!favoriteTrackIds.has(trackId)) {
|
||||
favoriteTrackIds.add(trackId);
|
||||
export async function syncCache(trackIds: number[]): Promise<void> {
|
||||
try {
|
||||
await invoke('sync_cached_favorite_tracks', { trackIds });
|
||||
favoriteTrackIds = new Set(trackIds);
|
||||
notifyListeners();
|
||||
} catch (err) {
|
||||
console.error('[Favorites] Failed to sync cache:', err);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Manually remove a track ID from the local set
|
||||
* Reset store (for logout)
|
||||
*/
|
||||
export function unmarkAsFavorite(trackId: number): void {
|
||||
if (favoriteTrackIds.has(trackId)) {
|
||||
favoriteTrackIds.delete(trackId);
|
||||
notifyListeners();
|
||||
export async function reset(): Promise<void> {
|
||||
try {
|
||||
await invoke('clear_favorites_cache');
|
||||
} catch (err) {
|
||||
console.debug('[Favorites] Failed to clear cache:', err);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset store (for testing or logout)
|
||||
*/
|
||||
export function reset(): void {
|
||||
favoriteTrackIds = new Set();
|
||||
togglingTrackIds = new Set();
|
||||
isLoaded = false;
|
||||
isLoading = false;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
// Legacy functions for backwards compatibility
|
||||
export function markAsFavorite(trackId: number): void {
|
||||
if (!favoriteTrackIds.has(trackId)) {
|
||||
favoriteTrackIds.add(trackId);
|
||||
// Also update cache
|
||||
invoke('cache_favorite_track', { trackId }).catch(() => {});
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
export function unmarkAsFavorite(trackId: number): void {
|
||||
if (favoriteTrackIds.has(trackId)) {
|
||||
favoriteTrackIds.delete(trackId);
|
||||
// Also update cache
|
||||
invoke('uncache_favorite_track', { trackId }).catch(() => {});
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user