koel/resources/assets/js/stores/queueStore.ts

155 lines
3.4 KiB
TypeScript
Raw Normal View History

2022-04-20 09:37:22 +00:00
import { reactive } from 'vue'
2022-07-25 18:23:30 +00:00
import { differenceBy, shuffle, unionBy } from 'lodash'
2022-04-20 09:37:22 +00:00
import { arrayify } from '@/utils'
import { http } from '@/services'
2022-06-10 10:47:46 +00:00
import { songStore } from '@/stores'
2022-04-15 14:24:30 +00:00
export const queueStore = {
state: reactive<{ songs: Song[] }>({
songs: []
2022-04-20 09:37:22 +00:00
}),
2022-04-15 14:24:30 +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'
}
},
get all () {
2022-04-15 14:24:30 +00:00
return this.state.songs
},
set all (songs: Song[]) {
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]
},
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
},
/**
* Add song(s) to the end of the current queue.
2022-04-15 14:24:30 +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
},
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
},
replaceQueueWith (songs: Song | Song[]) {
this.all = arrayify(songs)
2022-04-15 14:24:30 +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
},
unqueue (songs: Song | Song[]) {
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.
*/
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
})
this.saveState()
2022-04-15 14:24:30 +00:00
},
clear () {
2022-04-15 14:24:30 +00:00
this.all = []
},
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
}
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
}
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) {
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) {
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
}
}