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

157 lines
3.5 KiB
TypeScript
Raw Normal View History

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'
import { arrayify, logger, moveItemsInList } 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
2024-01-18 11:13:05 +00:00
i: 0,
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, type: MoveType) {
this.state.songs = moveItemsInList(this.state.songs, songs, target, type)
this.saveState()
2022-04-15 14:24:30 +00:00
},
clear () {
2022-04-15 14:24:30 +00:00
this.all = []
},
/**
* Clear the queue without saving the state.
*/
clearSilently () {
this.state.songs = []
},
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 () {
2024-01-24 22:39:47 +00:00
return this.all.find(({ playback_state }) => 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 {
2024-01-24 22:39:47 +00:00
http.silently.put('queue/state', { songs: this.state.songs.map(({ id }) => id) })
} catch (e) {
2024-01-24 22:39:47 +00:00
logger.error(e)
}
2022-04-15 14:24:30 +00:00
}
}