import { browser } from '$app/environment'; export { isOffline } from '$lib/utils/network'; const LAST_SYNC_KEY = 'classeo:schedule:lastSync'; let lastSyncValue = $state( browser ? localStorage.getItem(LAST_SYNC_KEY) : null ); /** * Enregistre la date de dernière synchronisation de l'EDT. */ export function recordSync(): void { if (!browser) return; const now = new Date().toISOString(); localStorage.setItem(LAST_SYNC_KEY, now); lastSyncValue = now; } /** * Récupère la date de dernière synchronisation de l'EDT (réactif via $state). */ export function getLastSyncDate(): string | null { return lastSyncValue; } /** * Pré-charge 30 jours d'EDT en cache Service Worker (7 passés + 23 futurs). * * Appelé en arrière-plan pour alimenter le cache offline (AC5). * Les requêtes sont interceptées par le SW (NetworkFirst) * et les réponses sont automatiquement mises en cache. */ export async function prefetchScheduleDays( fetchFn: (date: string) => Promise, today: Date = new Date() ): Promise { const CONCURRENCY = 5; const PAST_DAYS = 7; const FUTURE_DAYS = 23; const dates: string[] = []; for (let i = -PAST_DAYS; i <= FUTURE_DAYS; i++) { const date = new Date(today); date.setDate(date.getDate() + i); const y = date.getFullYear(); const m = String(date.getMonth() + 1).padStart(2, '0'); const d = String(date.getDate()).padStart(2, '0'); dates.push(`${y}-${m}-${d}`); } for (let i = 0; i < dates.length; i += CONCURRENCY) { const batch = dates.slice(i, i + CONCURRENCY); await Promise.allSettled(batch.map((dateStr) => fetchFn(dateStr))); } }