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

158 lines
3.6 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 = {
2022-06-10 10:47:46 +00:00
state: reactive({
2022-07-25 18:23:30 +00:00
songs: [] as Song[]
2022-04-20 09:37:22 +00:00
}),
2022-04-15 14:24:30 +00:00
init () {
// We don't have anything to do here yet.
// How about another song then?
//
// LITTLE WING
// -- Jimi Hendrix
//
// Well she's walking
// Through the clouds
// With a circus mind
// That's running wild
// Butterflies and zebras and moonbeams and fairy tales
2022-04-15 14:24:30 +00:00
// That's all she ever thinks about
// Riding with the wind
//
// When I'm sad
// She comes to me
// With a thousand smiles
// She gives to me free
// It's alright she said
// It's alright
// Take anything you want from me
// Anything...
},
get all () {
2022-04-15 14:24:30 +00:00
return this.state.songs
},
set all (songs: Song[]) {
2022-07-25 18:23:30 +00:00
this.state.songs = reactive(songs)
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[]) {
2022-07-25 18:23:30 +00:00
this.state.songs = reactive(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[]) {
2022-07-22 21:56:13 +00:00
this.all = differenceBy(this.all, arrayify(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
})
},
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.all.map(song => song.id).indexOf(this.current.id) + 1
return index >= this.all.length ? undefined : this.all[index]
},
get previous () {
if (!this.current) {
return this.last
}
const index = this.all.map(song => song.id).indexOf(this.current.id) - 1
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
},
shuffle () {
2022-04-15 14:24:30 +00:00
this.all = shuffle(this.all)
2022-06-10 10:47:46 +00:00
},
async fetchRandom (limit = 500) {
const songs = await http.get<Song[]>(`queue/fetch?order=rand&limit=${limit}`)
2022-06-10 10:47:46 +00:00
this.state.songs = songStore.syncWithVault(songs)
2022-10-13 15:18:47 +00:00
return this.state.songs
2022-06-10 10:47:46 +00:00
},
async fetchInOrder (sortField: SongListSortField, order: SortOrder, limit = 500) {
const songs = await http.get<Song[]>(`queue/fetch?order=${order}&sort=${sortField}&limit=${limit}`)
2022-06-10 10:47:46 +00:00
this.state.songs = songStore.syncWithVault(songs)
2022-10-13 15:18:47 +00:00
return this.state.songs
2022-04-15 14:24:30 +00:00
}
}