refactor: adjust code for lru-cache v10

Part-of: <https://dev.funkwhale.audio/funkwhale/funkwhale/-/merge_requests/2757>
This commit is contained in:
wvffle 2024-02-20 14:31:55 +00:00
parent ff6fc46c58
commit 670b522675
3 changed files with 14 additions and 17 deletions

View File

@ -101,7 +101,7 @@ export const currentTrack = computed(() => queue.value[currentIndex.value])
// Use Queue // Use Queue
export const useQueue = createGlobalState(() => { export const useQueue = createGlobalState(() => {
const { currentSound } = useTracks() const { currentSound, clearCache } = useTracks()
const createQueueTrack = async (track: Track, skipFetch = false): Promise<QueueTrack> => { const createQueueTrack = async (track: Track, skipFetch = false): Promise<QueueTrack> => {
const { default: store } = await import('~/store') const { default: store } = await import('~/store')
@ -341,6 +341,9 @@ export const useQueue = createGlobalState(() => {
await delMany(lastTracks) await delMany(lastTracks)
currentIndex.value = 0 currentIndex.value = 0
// Clear track cache
clearCache()
} }
// Radio queue populating // Radio queue populating

View File

@ -4,13 +4,13 @@ import type { Sound } from '~/api/player'
import { createGlobalState, syncRef, useTimeoutFn, whenever } from '@vueuse/core' import { createGlobalState, syncRef, useTimeoutFn, whenever } from '@vueuse/core'
import { computed, ref, watchEffect } from 'vue' import { computed, ref, watchEffect } from 'vue'
import { LRUCache } from 'lru-cache'
import { connectAudioSource } from '~/composables/audio/audio-api' import { connectAudioSource } from '~/composables/audio/audio-api'
import { usePlayer } from '~/composables/audio/player' import { usePlayer } from '~/composables/audio/player'
import { useQueue } from '~/composables/audio/queue' import { useQueue } from '~/composables/audio/queue'
import { soundImplementation } from '~/api/player' import { soundImplementation } from '~/api/player'
import useLRUCache from '~/composables/data/useLRUCache'
import useLogger from '~/composables/useLogger' import useLogger from '~/composables/useLogger'
import store from '~/store' import store from '~/store'
@ -22,11 +22,13 @@ const AUDIO_ELEMENT = document.createElement('audio')
const logger = useLogger() const logger = useLogger()
const soundPromises = new Map<number, Promise<Sound>>() const soundPromises = new Map<number, Promise<Sound>>()
const soundCache = useLRUCache<number, Sound>({ const soundCache = new LRUCache<number, Sound>({
max: 3, max: 3,
dispose: (sound) => sound.dispose() dispose: (sound) => sound.dispose()
}) })
const currentTrack = ref<QueueTrack>()
export const fetchTrackSources = async (id: number): Promise<QueueTrackSource[]> => { export const fetchTrackSources = async (id: number): Promise<QueueTrackSource[]> => {
const { uploads } = await axios.get(`tracks/${id}/`) const { uploads } = await axios.get(`tracks/${id}/`)
.then(response => response.data as Track, () => ({ uploads: [] as Upload[] })) .then(response => response.data as Track, () => ({ uploads: [] as Upload[] }))
@ -195,8 +197,6 @@ export const useTracks = createGlobalState(() => {
} }
} }
const currentTrack = ref<QueueTrack>()
// NOTE: We want to have it called only once, hence we're using createGlobalState // NOTE: We want to have it called only once, hence we're using createGlobalState
const initialize = createGlobalState(() => { const initialize = createGlobalState(() => {
const { currentIndex, currentTrack: track, queue, hasNext } = useQueue() const { currentIndex, currentTrack: track, queue, hasNext } = useQueue()
@ -226,10 +226,15 @@ export const useTracks = createGlobalState(() => {
const currentSound = computed(() => soundCache.get(currentTrack.value?.id ?? -1)) const currentSound = computed(() => soundCache.get(currentTrack.value?.id ?? -1))
const clearCache = () => {
return soundCache.clear()
}
return { return {
initialize, initialize,
createSound, createSound,
createTrack, createTrack,
clearCache,
currentSound currentSound
} }
}) })

View File

@ -1,11 +0,0 @@
import LRU from 'lru-cache'
import { reactive } from 'vue'
export default <T, K>(options: LRU.Options<T, K>) => {
const cache = new LRU(options)
// @ts-expect-error keyMap is used internally so it is not defined in the types
cache.keyMap = reactive(cache.keyMap)
return cache
}