add types
This commit is contained in:
86
src/lib/stores.svelte.ts
Normal file
86
src/lib/stores.svelte.ts
Normal file
@@ -0,0 +1,86 @@
|
||||
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')) || '[]')
|
||||
);
|
||||
$effect.root(() => {
|
||||
$effect(() => {
|
||||
localStorage.setItem('favoritedStreams', JSON.stringify(Array.from(favoritedStreams)));
|
||||
});
|
||||
});
|
||||
|
||||
export const tagList = [
|
||||
'acoustic',
|
||||
'electronic',
|
||||
'orchestral',
|
||||
'rock',
|
||||
'pop',
|
||||
'metal',
|
||||
'aggressive',
|
||||
'folk',
|
||||
'jazzy',
|
||||
'dance.music',
|
||||
'untz',
|
||||
'breakbeats',
|
||||
'electronica',
|
||||
'chiptune',
|
||||
'left.field',
|
||||
'denpa',
|
||||
'vocaloid',
|
||||
'funky',
|
||||
'lush',
|
||||
'noisy',
|
||||
'psychedelic',
|
||||
'dark',
|
||||
'calm',
|
||||
'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