-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor stores into reactive classes
- Loading branch information
Showing
9 changed files
with
156 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import type AudioPlayer from "$lib/components/AudioPlayer.svelte"; | ||
import { browser } from "$app/environment"; | ||
|
||
export interface PlaylistEntry { | ||
id: number; | ||
title: string; | ||
url: string; | ||
} | ||
|
||
export class Playlist { | ||
#plSeq = 0; | ||
#entries: PlaylistEntry[] = $state([]); | ||
|
||
addSong(url: string, title: string) { | ||
this.#entries = [...this.#entries, { url, title, id: this.#plSeq++ }]; | ||
} | ||
|
||
length() { | ||
return this.#entries.length; | ||
} | ||
|
||
getNext() { | ||
return this.#entries[0]; | ||
} | ||
|
||
getList() { | ||
return this.#entries; | ||
} | ||
|
||
advance() { | ||
this.#entries = this.#entries.slice(1); | ||
} | ||
|
||
clear() { | ||
this.#entries = []; | ||
} | ||
|
||
remove(index: number) { | ||
this.#entries = [...this.#entries.slice(0, index), ...this.#entries.slice(index + 1)]; | ||
} | ||
|
||
move(id: number, destIdx: number) { | ||
const elIdx = this.#entries.findIndex((x) => x.id === id); | ||
if (elIdx >= 0) { | ||
const moveItem = this.#entries[elIdx]; | ||
const tempList = [...this.#entries.slice(0, elIdx), ...this.#entries.slice(elIdx + 1)]; | ||
this.#entries = [...tempList.slice(0, destIdx), moveItem, ...tempList.slice(destIdx)]; | ||
} | ||
} | ||
|
||
moveUp(index: number) { | ||
if (index === 0) return; | ||
[this.#entries[index - 1], this.#entries[index]] = [ | ||
this.#entries[index], | ||
this.#entries[index - 1], | ||
]; | ||
} | ||
|
||
moveDown(index: number) { | ||
if (index === this.#entries.length - 1) return; | ||
[this.#entries[index + 1], this.#entries[index]] = [ | ||
this.#entries[index], | ||
this.#entries[index + 1], | ||
]; | ||
} | ||
} | ||
|
||
export const GlobalAudio = new (class { | ||
#Volume = $state((browser && localStorage?.audioVolume) ?? 0.5); | ||
|
||
Player = $state(null as AudioPlayer | null); | ||
CurrentSong = $state(null as string | null); | ||
Playlist = new Playlist(); | ||
|
||
set Volume(v: number) { | ||
this.#Volume = v; | ||
if (browser) localStorage.audioVolume = v; | ||
} | ||
|
||
get Volume() { | ||
return this.#Volume; | ||
} | ||
})(); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.