/** * Smart preloading service for upcoming tracks * Automatically queues downloads for the next few tracks in the queue * * TRACES: UR-004, UR-011 | DR-006, DR-015 */ import { invoke } from '@tauri-apps/api/core'; import { auth } from '$lib/stores/auth'; interface PreloadResult { queuedCount: number; alreadyDownloaded: number; skipped: number; } interface PreloadOptions { /** Enable debug logging */ debug?: boolean; /** Override user ID (defaults to current session user) */ userId?: string; } /** * Trigger preloading for upcoming tracks in the queue * This should be called after playback starts or advances to the next track */ export async function preloadUpcomingTracks(options: PreloadOptions = {}): Promise { const { debug = false, userId: overrideUserId } = options; try { // Get current user ID const userId = overrideUserId || auth.getUserId(); if (!userId) { if (debug) console.log('[Preload] No active user session, skipping preload'); return; } if (debug) console.log('[Preload] Triggering preload for user:', userId); const result = await invoke('player_preload_upcoming', { userId, downloadBasePath: '/downloads' // This parameter is currently unused in the backend }); if (debug) { console.log('[Preload] Result:', { queued: result.queuedCount, alreadyDownloaded: result.alreadyDownloaded, skipped: result.skipped }); } // Log meaningful results if (result.queuedCount > 0) { console.log(`[Preload] Queued ${result.queuedCount} track(s) for background download`); } } catch (error) { // Fail silently - preloading is a background optimization // Don't interrupt the user's playback experience console.warn('[Preload] Failed to preload upcoming tracks:', error); } } /** * Update smart cache configuration */ export async function updateCacheConfig(config: { queuePrecacheEnabled?: boolean; queuePrecacheCount?: number; albumAffinityEnabled?: boolean; albumAffinityThreshold?: number; storageLimit?: number; wifiOnly?: boolean; }): Promise { await invoke('player_set_cache_config', { config }); } /** * Get current cache configuration */ export async function getCacheConfig(): Promise<{ queuePrecacheEnabled: boolean; queuePrecacheCount: number; albumAffinityEnabled: boolean; albumAffinityThreshold: number; storageLimit: number; wifiOnly: boolean; }> { return await invoke('player_get_cache_config'); }