/** The radio track list (src/tracks.ts) and which files are complete (package storage). */ import { packageStorage } from 'kody:runtime' import { cleanCatalog, maskLocked, radio, view, type Catalog, type View } from './tracks.ts' /** * The track list from src/tracks.ts. * * @returns the catalog */ export async function loadCatalog(): Promise { return cleanCatalog(radio) } /** * Keys of fully stored radio files. * * @returns e.g. ['radio/01.mp3', 'radio/01.png'] */ export async function storedKeys(): Promise { try { const r = (await packageStorage().sql( "SELECT m.code AS code FROM audio_meta m WHERE substr(m.code, 1, 6) = 'radio/' AND (SELECT COUNT(*) FROM audio_chunk c WHERE c.code = m.code) = m.chunks", )) as { rows?: { code: string }[] } | null return (r?.rows ?? []).map((x) => String(x.code)) } catch { return [] // tables not created yet } } /** * The listener view right now. A track that is not released yet shows as `?????` with no length, so a * title is never public before the track opens. * * @param now - the current instant * @returns album, page and tracks with `released` */ export async function loadView(now = new Date()): Promise { const [c, keys] = await Promise.all([loadCatalog(), storedKeys()]) return maskLocked(view(c, keys, now)) }