feat: dispose sound instances when removed from LRU cache

Part-of: <https://dev.funkwhale.audio/funkwhale/funkwhale/-/merge_requests/2346>
This commit is contained in:
Kasper Seweryn 2023-01-29 19:52:58 +01:00 committed by Georg krause
parent 50b1487877
commit 6b79b8e63a
2 changed files with 11 additions and 2 deletions

View File

@ -90,11 +90,16 @@ export class HTMLSound implements Sound {
} }
preload () { preload () {
console.log('CALLING PRELOAD ON', this)
this.#audio.load() this.#audio.load()
} }
dispose () { dispose () {
this.audioNode.disconnect() this.audioNode.disconnect()
// Cancel any request downloading the source
this.#audio.src = ''
this.#audio.load()
} }
async play () { async play () {

View File

@ -19,7 +19,10 @@ const ALLOWED_PLAY_TYPES: (CanPlayTypeResult | undefined)[] = ['maybe', 'probabl
const AUDIO_ELEMENT = document.createElement('audio') const AUDIO_ELEMENT = document.createElement('audio')
const soundPromises = new Map<number, Promise<Sound>>() const soundPromises = new Map<number, Promise<Sound>>()
const soundCache = useLRUCache<number, Sound>({ max: 10 }) const soundCache = useLRUCache<number, Sound>({
max: 10,
dispose: (sound) => sound.dispose()
})
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}/`)
@ -124,6 +127,7 @@ export const useTracks = createGlobalState(() => {
// Preload next track // Preload next track
const { start: preload, stop: abortPreload } = useTimeoutFn(async (track: QueueTrack) => { const { start: preload, stop: abortPreload } = useTimeoutFn(async (track: QueueTrack) => {
const sound = await createSound(track) const sound = await createSound(track)
sound.__track = track
await sound.preload() await sound.preload()
}, 100, { immediate: false }) }, 100, { immediate: false })
@ -153,7 +157,7 @@ export const useTracks = createGlobalState(() => {
await sound.play() await sound.play()
} }
} }
const currentTrack = ref<QueueTrack>() 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