mirror of
https://github.com/koel/koel
synced 2024-12-21 01:53:11 +00:00
41 lines
902 B
TypeScript
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)
|
|
}
|
|
}
|