feat: persist station sync catalogs and status
This commit is contained in:
@@ -1,6 +1,11 @@
|
||||
const MANAGED_CATALOG_CACHE_PREFIX = 'radioplayer-managed-catalog-';
|
||||
const MANAGED_CATALOG_SOURCE_HEADER = 'x-radioplayer-managed-source';
|
||||
|
||||
import {
|
||||
loadPersistedManagedStationCatalog,
|
||||
persistManagedStationCatalog,
|
||||
} from '../storage/stationCatalogPersistence.js';
|
||||
|
||||
export type ManagedCatalogSource = 'remote' | 'cached-remote' | 'bundled' | 'unknown';
|
||||
|
||||
let lastManagedCatalogSource: ManagedCatalogSource = 'unknown';
|
||||
@@ -44,10 +49,26 @@ function setLastManagedCatalogSource(source: ManagedCatalogSource) {
|
||||
lastManagedCatalogSource = source;
|
||||
}
|
||||
|
||||
function normalizeManagedCatalogSource(source: unknown): ManagedCatalogSource {
|
||||
return source === 'remote' || source === 'cached-remote' || source === 'bundled'
|
||||
? source
|
||||
: 'unknown';
|
||||
}
|
||||
|
||||
export function getLastManagedCatalogSource(): ManagedCatalogSource {
|
||||
return lastManagedCatalogSource;
|
||||
}
|
||||
|
||||
export async function loadPersistedManagedStations(): Promise<unknown[] | null> {
|
||||
const persisted = await loadPersistedManagedStationCatalog();
|
||||
if (!persisted) {
|
||||
return null;
|
||||
}
|
||||
|
||||
setLastManagedCatalogSource(normalizeManagedCatalogSource(persisted.source));
|
||||
return normalizeManagedStationsPayload(persisted.stations);
|
||||
}
|
||||
|
||||
export async function loadManagedStations(): Promise<unknown[]> {
|
||||
const remoteCatalogUrl = `${import.meta.env.BASE_URL}api/managed-stations.json`;
|
||||
const bundledCatalogUrl = `${import.meta.env.BASE_URL}stations.json`;
|
||||
@@ -92,6 +113,7 @@ export async function loadManagedStations(): Promise<unknown[]> {
|
||||
throw new Error(`Failed to load managed stations: ${response.status}`);
|
||||
}
|
||||
|
||||
const stations = await response.json();
|
||||
return normalizeManagedStationsPayload(stations);
|
||||
const stations = normalizeManagedStationsPayload(await response.json());
|
||||
await persistManagedStationCatalog(stations, getLastManagedCatalogSource());
|
||||
return stations;
|
||||
}
|
||||
Reference in New Issue
Block a user