diff --git a/src/lib/database.ts b/src/lib/database.ts index f787d27..19abcb8 100644 --- a/src/lib/database.ts +++ b/src/lib/database.ts @@ -1,5 +1,5 @@ import { Database } from 'bun:sqlite'; -import type { Stream, StreamSummary } from './types.js'; +import type { Stream, StreamSummary } from './types.ts'; const dbName = './db/strimserve.db'; diff --git a/src/lib/stores.svelte.ts b/src/lib/stores.svelte.ts index f7cb693..7e1254b 100644 --- a/src/lib/stores.svelte.ts +++ b/src/lib/stores.svelte.ts @@ -1,10 +1,5 @@ -import { writable } from 'svelte/store'; import { browser } from '$app/environment'; import { SvelteSet } from 'svelte/reactivity'; -import type { Stream } from '$lib/types'; - -export const currentStream = writable(null); -export const currentSongIndex = writable(null); export const favoritedStreams = new SvelteSet( JSON.parse((browser && localStorage.getItem('favoritedStreams')) || '[]') @@ -42,45 +37,3 @@ export const tagList = [ 'moody', 'uplifting' ]; - -let timestampList: number[]; - -// utility - -function locationOf(element: number, array: number[], start?: number, end?: number): number { - start = start || 0; - end = end || array.length; - const pivot = parseInt(String(start + (end - start) / 2), 10); - if (end - start <= 1 || array[pivot] === element) return pivot; - if (array[pivot] < element) { - return locationOf(element, array, pivot, end); - } else { - return locationOf(element, array, start, pivot); - } -} - -// exported methods - -export function getSongAtTime(currentTime: number): number { - return locationOf(currentTime, timestampList) - 1; -} - -// less operations needed when doing regular lookups -export function updateCurrentSong(currentTime: number, songIndex: number): void { - function updateCurrentSongRecurse(songIndex: number): number { - if (currentTime >= timestampList[songIndex + 2]) { - return updateCurrentSongRecurse(songIndex + 1); - } else if (currentTime < timestampList[songIndex + 1]) { - return updateCurrentSongRecurse(songIndex - 1); - } - return songIndex; - } - - currentSongIndex.set(updateCurrentSongRecurse(songIndex)); -} - -export function updateCurrentStream(stream: Stream): void { - currentStream.set(stream); - timestampList = [-Infinity, ...stream.tracks.map((track) => track[0]), Infinity]; - currentSongIndex.set(0); -} diff --git a/src/lib/stream-context.svelte.ts b/src/lib/stream-context.svelte.ts new file mode 100644 index 0000000..a0917f5 --- /dev/null +++ b/src/lib/stream-context.svelte.ts @@ -0,0 +1,47 @@ +import { createContext } from 'svelte'; +import type { Stream, StreamSummary } from './types.ts'; + +export class StreamContext { + streams: StreamSummary[]; + current = $state(null); + songIndex = $state(null); + #timestamps: number[] = []; + + constructor(streams: StreamSummary[]) { + this.streams = streams; + } + + setCurrent(stream: Stream) { + this.current = stream; + this.#timestamps = [-Infinity, ...stream.tracks.map((t) => t[0]), Infinity]; + this.songIndex = 0; + } + + clearCurrent() { + this.current = null; + this.songIndex = null; + } + + getSongAtTime(time: number): number { + return this.#locationOf(time, this.#timestamps) - 1; + } + + updateCurrentSong(currentTime: number, songIndex: number) { + const ts = this.#timestamps; + const recurse = (idx: number): number => { + if (currentTime >= ts[idx + 2]) return recurse(idx + 1); + if (currentTime < ts[idx + 1]) return recurse(idx - 1); + return idx; + }; + this.songIndex = recurse(songIndex); + } + + #locationOf(element: number, array: number[], start = 0, end = array.length): number { + const pivot = Math.floor(start + (end - start) / 2); + if (end - start <= 1 || array[pivot] === element) return pivot; + if (array[pivot] < element) return this.#locationOf(element, array, pivot, end); + return this.#locationOf(element, array, start, pivot); + } +} + +export const [getStreamContext, setStreamContext] = createContext(); diff --git a/src/routes/streams/+layout.server.ts b/src/routes/streams/+layout.server.ts index 0d60e5b..35fc81c 100644 --- a/src/routes/streams/+layout.server.ts +++ b/src/routes/streams/+layout.server.ts @@ -1,4 +1,4 @@ -import { getStreams } from '$lib/database.js'; +import { getStreams } from '$lib/database.ts'; export function load() { return { diff --git a/src/routes/streams/+layout.svelte b/src/routes/streams/+layout.svelte index 46df136..931ef21 100644 --- a/src/routes/streams/+layout.svelte +++ b/src/routes/streams/+layout.svelte @@ -1,11 +1,16 @@
- +
{@render children?.()}
diff --git a/src/routes/streams/Sidebar.svelte b/src/routes/streams/Sidebar.svelte index cc0f304..6e92e11 100644 --- a/src/routes/streams/Sidebar.svelte +++ b/src/routes/streams/Sidebar.svelte @@ -1,12 +1,13 @@
diff --git a/src/routes/streams/[stream_id]/+page.server.ts b/src/routes/streams/[stream_id]/+page.server.ts index b322592..a6683a5 100644 --- a/src/routes/streams/[stream_id]/+page.server.ts +++ b/src/routes/streams/[stream_id]/+page.server.ts @@ -1,7 +1,7 @@ import fs from 'fs'; import path from 'path'; import { error } from '@sveltejs/kit'; -import { getStreamInfo } from '$lib/database.js'; +import { getStreamInfo } from '$lib/database.ts'; import { dev } from '$app/environment'; import { STREAM_JSON_LOCATION } from '$env/static/private'; diff --git a/src/routes/streams/[stream_id]/+page.svelte b/src/routes/streams/[stream_id]/+page.svelte index 5a6dd9f..31ce1f3 100644 --- a/src/routes/streams/[stream_id]/+page.svelte +++ b/src/routes/streams/[stream_id]/+page.svelte @@ -5,13 +5,13 @@ import MetadataEditor from './MetadataEditor.svelte'; import Player from './Player.svelte'; import { dev } from '$app/environment'; - import { currentStream, updateCurrentStream } from '$lib/stores.svelte.js'; - import type { Stream } from '$lib/types'; + import { getStreamContext } from '$lib/stream-context.svelte.ts'; let { data } = $props(); + const ctx = getStreamContext(); run(() => { - updateCurrentStream(data.stream); + ctx.setCurrent(data.stream); }); @@ -23,8 +23,8 @@
- {#key $currentStream} - + {#key ctx.current} + {/key}
diff --git a/src/routes/streams/[stream_id]/MetadataEditor.svelte b/src/routes/streams/[stream_id]/MetadataEditor.svelte index 4b63d55..5d3d9e3 100644 --- a/src/routes/streams/[stream_id]/MetadataEditor.svelte +++ b/src/routes/streams/[stream_id]/MetadataEditor.svelte @@ -1,6 +1,6 @@ @@ -18,25 +20,25 @@ {formattedStreamDate} | apt-get's auditorium -{#if $currentStream} +{#if ctx.current}
-

ID: {shorthandCode($currentStream.id)}

+

ID: {shorthandCode(ctx.current.id)}

{formattedStreamDate}

-
Tags: {$currentStream.tags.join(', ')}
+
Tags: {ctx.current.tags.join(', ')}
- {@html carta.renderSSR($currentStream.description || 'No description available.')} + {@html carta.renderSSR(ctx.current.description || 'No description available.')}
- {#each $currentStream.tracks as track, i} - + {#each ctx.current.tracks as track, i} +
TimestampArtistTitle
{formatTrackTime(track[0])}