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

41 lines
874 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'
const EXCERPT_COUNT = 7
export const recentlyPlayedStore = {
excerptState: {
songs: [] as Song[]
},
state: {
songs: [] as Song[]
},
fetched: false,
initExcerpt (songIds: string[]): void {
this.excerptState.songs = songStore.byIds(songIds)
},
async fetchAll (): Promise<Song[]> {
if (!this.fetched) {
this.state.songs = songStore.byIds(await http.get<string[]>(`interaction/recently-played`))
this.fetched = true
}
return this.state.songs
},
add (song: Song): void {
[this.state, this.excerptState].forEach((state): void => {
// make sure there's no duplicate
remove(state.songs, s => s.id === song.id)
state.songs.unshift(song)
})
this.excerptState.songs.splice(EXCERPT_COUNT)
}
}