more ts switches, use setcontext instead of stores

This commit is contained in:
2026-03-15 17:20:16 +01:00
parent b47a433414
commit 8573b82515
13 changed files with 115 additions and 99 deletions

View File

@@ -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<Stream | null>(null);
export const currentSongIndex = writable<number | null>(null);
export const favoritedStreams = new SvelteSet<string>(
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);
}