more ts switches, use setcontext instead of stores
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user