2024-04-17 14:23:54 -04:00
|
|
|
import { writable, readable, readonly, type Writable, type Readable } from 'svelte/store'
|
2024-01-25 03:05:13 -05:00
|
|
|
import type { AlertType } from '$lib/components/util/alert.svelte'
|
2024-01-21 23:51:15 -05:00
|
|
|
|
2024-01-27 01:38:04 -05:00
|
|
|
export const pageWidth: Writable<number> = writable()
|
2024-01-21 23:51:15 -05:00
|
|
|
|
2024-01-27 01:38:04 -05:00
|
|
|
export const newestAlert: Writable<[AlertType, string]> = writable()
|
2024-01-21 23:51:15 -05:00
|
|
|
|
|
|
|
|
const youtubeMusicBackground: string = 'https://www.gstatic.com/youtube/media/ytm/images/sbg/wsbg@4000x2250.png' // Default Youtube music background
|
|
|
|
|
export const backgroundImage: Writable<string> = writable(youtubeMusicBackground)
|
2024-04-16 10:05:11 -04:00
|
|
|
|
|
|
|
|
class Queue {
|
2024-04-22 14:18:42 -04:00
|
|
|
private currentPosition: number // -1 means there is no current position
|
2024-04-17 14:23:54 -04:00
|
|
|
private songs: Song[]
|
2024-04-16 10:05:11 -04:00
|
|
|
|
|
|
|
|
constructor() {
|
2024-04-22 14:18:42 -04:00
|
|
|
this.currentPosition = -1
|
2024-04-17 14:23:54 -04:00
|
|
|
this.songs = []
|
2024-04-16 10:05:11 -04:00
|
|
|
}
|
|
|
|
|
|
2024-04-22 14:18:42 -04:00
|
|
|
get current() {
|
2024-05-16 21:45:39 -04:00
|
|
|
if (this.songs.length === 0) return null
|
|
|
|
|
|
|
|
|
|
if (this.currentPosition === -1) this.currentPosition = 0
|
|
|
|
|
return this.songs[this.currentPosition]
|
2024-04-16 10:05:11 -04:00
|
|
|
}
|
|
|
|
|
|
2024-04-22 14:18:42 -04:00
|
|
|
set current(newSong: Song | null) {
|
|
|
|
|
if (newSong === null) {
|
|
|
|
|
this.currentPosition = -1
|
2024-04-17 14:23:54 -04:00
|
|
|
} else {
|
2024-04-22 14:18:42 -04:00
|
|
|
const queuePosition = this.songs.findIndex((song) => song === newSong)
|
|
|
|
|
if (queuePosition < 0) {
|
|
|
|
|
this.songs = [newSong]
|
|
|
|
|
this.currentPosition = 0
|
|
|
|
|
} else {
|
|
|
|
|
this.currentPosition = queuePosition
|
|
|
|
|
}
|
2024-04-16 10:05:11 -04:00
|
|
|
}
|
2024-04-22 14:18:42 -04:00
|
|
|
writableQueue.set(this)
|
|
|
|
|
}
|
2024-04-16 10:05:11 -04:00
|
|
|
|
2024-04-22 14:18:42 -04:00
|
|
|
get list() {
|
|
|
|
|
return this.songs
|
2024-04-16 10:05:11 -04:00
|
|
|
}
|
|
|
|
|
|
2024-04-22 14:18:42 -04:00
|
|
|
public next() {
|
2024-05-16 21:45:39 -04:00
|
|
|
if (this.songs.length === 0 || this.songs.length <= this.currentPosition + 1) return
|
2024-04-22 14:18:42 -04:00
|
|
|
|
|
|
|
|
this.currentPosition += 1
|
|
|
|
|
writableQueue.set(this)
|
2024-04-17 14:23:54 -04:00
|
|
|
}
|
2024-04-16 10:05:11 -04:00
|
|
|
|
2024-04-22 14:18:42 -04:00
|
|
|
public previous() {
|
|
|
|
|
if (this.songs.length === 0 || this.currentPosition <= 0) return
|
|
|
|
|
|
|
|
|
|
this.currentPosition -= 1
|
|
|
|
|
writableQueue.set(this)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public enqueue(...songs: Song[]) {
|
|
|
|
|
this.songs.push(...songs)
|
|
|
|
|
writableQueue.set(this)
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-28 00:46:34 -04:00
|
|
|
public setQueue(...songs: Song[]) {
|
|
|
|
|
this.songs = songs
|
|
|
|
|
this.currentPosition = songs.length === 0 ? -1 : 0
|
|
|
|
|
writableQueue.set(this)
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-22 14:18:42 -04:00
|
|
|
public clear() {
|
|
|
|
|
this.currentPosition = -1
|
|
|
|
|
this.songs = []
|
|
|
|
|
writableQueue.set(this)
|
2024-04-16 10:05:11 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-22 14:18:42 -04:00
|
|
|
const writableQueue: Writable<Queue> = writable(new Queue())
|
|
|
|
|
export const queue: Readable<Queue> = readonly(writableQueue)
|