2022-06-10 10:47:46 +00:00
|
|
|
import { reactive } from 'vue'
|
2022-09-15 09:07:25 +00:00
|
|
|
import { http } from '@/services'
|
2022-04-15 14:24:30 +00:00
|
|
|
import { remove } from 'lodash'
|
2022-06-10 10:47:46 +00:00
|
|
|
import { songStore } from '@/stores'
|
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({
|
2024-05-19 05:49:42 +00:00
|
|
|
playables: [] as Playable[]
|
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({
|
2024-05-19 05:49:42 +00:00
|
|
|
playables: [] as Playable[]
|
2022-04-21 16:06:45 +00:00
|
|
|
}),
|
2022-04-15 14:24:30 +00:00
|
|
|
|
2022-06-10 10:47:46 +00:00
|
|
|
async fetch () {
|
2024-05-19 05:49:42 +00:00
|
|
|
this.state.playables = songStore.syncWithVault(await http.get<Playable[]>('songs/recently-played'))
|
|
|
|
return this.state.playables
|
2022-04-15 14:24:30 +00:00
|
|
|
},
|
|
|
|
|
2024-05-19 05:49:42 +00:00
|
|
|
async add (playable: Playable) {
|
|
|
|
if (!this.state.playables.length) {
|
2022-06-10 10:47:46 +00:00
|
|
|
await this.fetch()
|
2022-04-15 14:24:30 +00:00
|
|
|
}
|
|
|
|
|
2022-04-21 16:06:45 +00:00
|
|
|
[this.state, this.excerptState].forEach(state => {
|
2022-04-15 14:24:30 +00:00
|
|
|
// make sure there's no duplicate
|
2024-05-19 05:49:42 +00:00
|
|
|
remove(state.playables, s => s.id === playable.id)
|
|
|
|
state.playables.unshift(playable)
|
2022-04-15 14:24:30 +00:00
|
|
|
})
|
|
|
|
|
2024-05-19 05:49:42 +00:00
|
|
|
this.excerptState.playables.splice(EXCERPT_COUNT)
|
2022-04-15 14:24:30 +00:00
|
|
|
}
|
|
|
|
}
|