koel/resources/assets/js/stores/recently-played.ts

42 lines
888 B
TypeScript
Raw Normal View History

2022-04-15 14:24:30 +00:00
import { songStore } from '.'
import { http } from '@/services'
import { remove } from 'lodash'
2022-04-21 16:06:45 +00:00
import { reactive } from 'vue'
2022-04-15 14:24:30 +00:00
const EXCERPT_COUNT = 7
export const recentlyPlayedStore = {
2022-04-21 16:06:45 +00:00
excerptState: reactive({
2022-04-15 14:24:30 +00:00
songs: [] as Song[]
2022-04-21 16:06:45 +00:00
}),
2022-04-15 14:24:30 +00:00
2022-04-21 16:06:45 +00:00
state: reactive({
2022-04-15 14:24:30 +00:00
songs: [] as Song[]
2022-04-21 16:06:45 +00:00
}),
2022-04-15 14:24:30 +00:00
fetched: false,
2022-04-21 16:06:45 +00:00
initExcerpt (songIds: string[]) {
2022-04-15 14:24:30 +00:00
this.excerptState.songs = songStore.byIds(songIds)
},
2022-04-21 16:06:45 +00:00
async fetchAll () {
2022-04-15 14:24:30 +00:00
if (!this.fetched) {
this.state.songs = songStore.byIds(await http.get<string[]>(`interaction/recently-played`))
this.fetched = true
}
return this.state.songs
},
2022-04-21 16:06:45 +00:00
add (song: Song) {
[this.state, this.excerptState].forEach(state => {
2022-04-15 14:24:30 +00:00
// make sure there's no duplicate
remove(state.songs, s => s.id === song.id)
state.songs.unshift(song)
})
this.excerptState.songs.splice(EXCERPT_COUNT)
}
}