koel/resources/assets/js/stores/recentlyPlayedStore.ts
2022-04-24 11:50:45 +03:00

41 lines
902 B
TypeScript

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