Add queue shuffling

This commit is contained in:
wvffle 2022-10-20 14:46:04 +00:00 committed by Georg Krause
commit aba816e8bf
No known key found for this signature in database
GPG key ID: 2970D504B2183D22
2 changed files with 39 additions and 13 deletions

View file

@ -3,11 +3,37 @@ import type { Track } from '~/types'
import { isPlaying, looping, LoopingMode } from '~/composables/audio/player'
import { currentSound } from '~/composables/audio/tracks'
import { toReactive, useStorage } from '@vueuse/core'
import { shuffle as shuffleArray } from 'lodash-es'
import { useClamp } from '@vueuse/math'
import { computed, ref } from 'vue'
import { computed } from 'vue'
// import useWebWorker from '~/composables/useWebWorker'
// const { post, onMessageReceived } = useWebWorker('queue')
// Queue
export const tracks = toReactive(useStorage('queue:tracks', [] as Track[]))
export const queue = computed(() => {
if (isShuffled.value) {
const tracksById = tracks.reduce((acc, track) => {
acc[track.id] = track
return acc
}, {} as Record<number, Track>)
return shuffledIds.value.map(id => tracksById[id])
}
return tracks
})
export const enqueue = (...newTracks: Track[]) => {
tracks.push(...newTracks)
// Shuffle new tracks
if (isShuffled.value) {
shuffledIds.value.push(...shuffleIds(newTracks))
}
}
// Current Index
export const currentIndex = useClamp(useStorage('queue:index', 0), 0, () => tracks.length)
@ -54,6 +80,14 @@ export const playNext = async () => {
}
// Shuffle
export const isShuffling = ref(false)
// TODO: Shuffle
export const shuffle = () => undefined
const shuffleIds = (tracks: Track[]) => shuffleArray(tracks.map(track => track.id))
const shuffledIds = useStorage('queue:shuffled-ids', [] as number[])
export const isShuffled = computed(() => shuffledIds.value.length !== 0)
export const shuffle = () => {
if (isShuffled.value) {
shuffledIds.value.length = 0
return
}
shuffledIds.value = shuffleIds(tracks)
}