Skip to content

Black Friday

Annual Pro is $90 for the first year, normally $120. Or start monthly at $6 for two months, then $12.

Ends Dec 4, 11:59 PM PT.

Create an account
← Public packages

@noah/mega-radio

MEGA Radio (the WAKE album) for members of the MEGA.dev Discord: sign in with Kody, verify with Discord, listen with repeat.

FollowForkOpen app

Files

catalog.ts

40 lines · 1.3 KB · TypeScript
/** 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<Catalog> {
	return cleanCatalog(radio)
}

/**
 * Keys of fully stored radio files.
 *
 * @returns e.g. ['radio/01.mp3', 'radio/01.png']
 */
export async function storedKeys(): Promise<string[]> {
	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<View> {
	const [c, keys] = await Promise.all([loadCatalog(), storedKeys()])
	return maskLocked(view(c, keys, now))
}