2022-04-20 09:37:22 +00:00
|
|
|
import { reactive } from 'vue'
|
2024-01-11 19:30:43 +00:00
|
|
|
import { differenceBy, unionBy } from 'lodash'
|
2022-04-20 09:37:22 +00:00
|
|
|
import { arrayify } from '@/utils'
|
2022-09-15 09:07:25 +00:00
|
|
|
import { http } from '@/services'
|
2022-06-10 10:47:46 +00:00
|
|
|
import { songStore } from '@/stores'
|
2022-04-23 22:31:40 +00:00
|
|
|
|
2022-04-15 14:24:30 +00:00
|
|
|
export const queueStore = {
|
2022-12-22 15:46:51 +00:00
|
|
|
state: reactive<{ songs: Song[] }>({
|
|
|
|
songs: []
|
2022-04-20 09:37:22 +00:00
|
|
|
}),
|
2022-04-15 14:24:30 +00:00
|
|
|
|
2024-01-01 11:40:21 +00:00
|
|
|
init (savedState: QueueState) {
|
|
|
|
// don't set this.all here, as it would trigger saving state
|
|
|
|
this.state.songs = songStore.syncWithVault(savedState.songs)
|
|
|
|
|
|
|
|
if (!this.state.songs.length) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if (savedState.current_song) {
|
|
|
|
songStore.syncWithVault(savedState.current_song)[0].playback_state = 'Paused'
|
|
|
|
} else {
|
|
|
|
this.all[0].playback_state = 'Paused'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2022-04-23 22:31:40 +00:00
|
|
|
get all () {
|
2022-04-15 14:24:30 +00:00
|
|
|
return this.state.songs
|
|
|
|
},
|
|
|
|
|
|
|
|
set all (songs: Song[]) {
|
2024-01-01 11:40:21 +00:00
|
|
|
this.state.songs = songStore.syncWithVault(songs)
|
|
|
|
this.saveState()
|
2022-04-15 14:24:30 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
get first () {
|
|
|
|
return this.all[0]
|
|
|
|
},
|
|
|
|
|
|
|
|
get last () {
|
|
|
|
return this.all[this.all.length - 1]
|
|
|
|
},
|
|
|
|
|
2022-04-23 22:31:40 +00:00
|
|
|
contains (song: Song) {
|
2022-07-25 18:23:30 +00:00
|
|
|
return this.all.includes(reactive(song))
|
2022-04-15 14:24:30 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2022-04-23 22:31:40 +00:00
|
|
|
* Add song(s) to the end of the current queue.
|
2022-04-15 14:24:30 +00:00
|
|
|
*/
|
2022-04-21 18:12:11 +00:00
|
|
|
queue (songs: Song | Song[]) {
|
2022-04-15 14:24:30 +00:00
|
|
|
this.unqueue(songs)
|
2022-07-22 21:56:13 +00:00
|
|
|
this.all = unionBy(this.all, arrayify(songs), 'id')
|
2022-04-15 14:24:30 +00:00
|
|
|
},
|
|
|
|
|
2022-04-21 18:12:11 +00:00
|
|
|
queueIfNotQueued (song: Song) {
|
|
|
|
if (!this.contains(song)) {
|
|
|
|
this.queueAfterCurrent(song)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
queueToTop (songs: Song | Song[]) {
|
2022-07-22 21:56:13 +00:00
|
|
|
this.all = unionBy(arrayify(songs), this.all, 'id')
|
2022-04-15 14:24:30 +00:00
|
|
|
},
|
|
|
|
|
2022-04-21 18:12:11 +00:00
|
|
|
replaceQueueWith (songs: Song | Song[]) {
|
2024-01-01 11:40:21 +00:00
|
|
|
this.all = arrayify(songs)
|
2022-04-15 14:24:30 +00:00
|
|
|
},
|
|
|
|
|
2022-04-21 18:12:11 +00:00
|
|
|
queueAfterCurrent (songs: Song | Song[]) {
|
2022-04-20 09:37:22 +00:00
|
|
|
songs = arrayify(songs)
|
2022-04-15 14:24:30 +00:00
|
|
|
|
|
|
|
if (!this.current || !this.all.length) {
|
|
|
|
return this.queue(songs)
|
|
|
|
}
|
|
|
|
|
|
|
|
// First we unqueue the songs to make sure there are no duplicates.
|
|
|
|
this.unqueue(songs)
|
|
|
|
|
|
|
|
const head = this.all.splice(0, this.indexOf(this.current) + 1)
|
2022-07-25 18:23:30 +00:00
|
|
|
this.all = head.concat(reactive(songs), this.all)
|
2022-04-15 14:24:30 +00:00
|
|
|
},
|
|
|
|
|
2022-04-21 18:12:11 +00:00
|
|
|
unqueue (songs: Song | Song[]) {
|
2022-12-19 15:44:48 +00:00
|
|
|
songs = arrayify(songs)
|
|
|
|
songs.forEach(song => (song.playback_state = 'Stopped'))
|
|
|
|
this.all = differenceBy(this.all, songs, 'id')
|
2022-04-15 14:24:30 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Move some songs to after a target.
|
|
|
|
*/
|
2022-04-21 18:12:11 +00:00
|
|
|
move (songs: Song | Song[], target: Song) {
|
2022-04-15 14:24:30 +00:00
|
|
|
const targetIndex = this.indexOf(target)
|
2022-04-20 09:37:22 +00:00
|
|
|
const movedSongs = arrayify(songs)
|
2022-04-15 14:24:30 +00:00
|
|
|
|
|
|
|
movedSongs.forEach(song => {
|
|
|
|
this.all.splice(this.indexOf(song), 1)
|
2022-07-25 18:23:30 +00:00
|
|
|
this.all.splice(targetIndex, 0, reactive(song))
|
2022-04-15 14:24:30 +00:00
|
|
|
})
|
2024-01-01 11:40:21 +00:00
|
|
|
|
|
|
|
this.saveState()
|
2022-04-15 14:24:30 +00:00
|
|
|
},
|
|
|
|
|
2022-04-21 18:12:11 +00:00
|
|
|
clear () {
|
2022-04-15 14:24:30 +00:00
|
|
|
this.all = []
|
|
|
|
},
|
|
|
|
|
2024-01-11 19:26:38 +00:00
|
|
|
/**
|
|
|
|
* Clear the queue without saving the state.
|
|
|
|
*/
|
|
|
|
clearSilently () {
|
|
|
|
this.state.songs = []
|
|
|
|
},
|
|
|
|
|
2022-04-23 22:31:40 +00:00
|
|
|
indexOf (song: Song) {
|
2022-07-25 18:23:30 +00:00
|
|
|
return this.all.indexOf(reactive(song))
|
2022-04-15 14:24:30 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
get next () {
|
|
|
|
if (!this.current) {
|
|
|
|
return this.first
|
|
|
|
}
|
|
|
|
|
2022-12-22 15:46:51 +00:00
|
|
|
const index = this.indexOf(this.current) + 1
|
2022-04-15 14:24:30 +00:00
|
|
|
|
|
|
|
return index >= this.all.length ? undefined : this.all[index]
|
|
|
|
},
|
|
|
|
|
|
|
|
get previous () {
|
|
|
|
if (!this.current) {
|
|
|
|
return this.last
|
|
|
|
}
|
|
|
|
|
2022-12-22 15:46:51 +00:00
|
|
|
const index = this.indexOf(this.current) - 1
|
2022-04-15 14:24:30 +00:00
|
|
|
|
|
|
|
return index < 0 ? undefined : this.all[index]
|
|
|
|
},
|
|
|
|
|
|
|
|
get current () {
|
2022-06-10 10:47:46 +00:00
|
|
|
return this.all.find(song => song.playback_state !== 'Stopped')
|
2022-04-15 14:24:30 +00:00
|
|
|
},
|
|
|
|
|
2022-06-10 10:47:46 +00:00
|
|
|
async fetchRandom (limit = 500) {
|
2024-01-01 11:40:21 +00:00
|
|
|
this.all = await http.get<Song[]>(`queue/fetch?order=rand&limit=${limit}`)
|
|
|
|
return this.all
|
2022-06-10 10:47:46 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
async fetchInOrder (sortField: SongListSortField, order: SortOrder, limit = 500) {
|
2024-01-01 11:40:21 +00:00
|
|
|
this.all = await http.get<Song[]>(`queue/fetch?order=${order}&sort=${sortField}&limit=${limit}`)
|
|
|
|
return this.all
|
|
|
|
},
|
|
|
|
|
|
|
|
saveState () {
|
|
|
|
try {
|
|
|
|
http.silently.put('queue/state', { songs: this.state.songs.map(song => song.id) })
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e)
|
|
|
|
}
|
2022-04-15 14:24:30 +00:00
|
|
|
}
|
|
|
|
}
|