feat(player): unregister all events on sound instance dispose

Part-of: <https://dev.funkwhale.audio/funkwhale/funkwhale/-/merge_requests/2346>
This commit is contained in:
Kasper Seweryn 2023-03-06 21:02:14 +01:00 committed by Marge
parent ae4ca7b0cb
commit 87c9eb3982
1 changed files with 37 additions and 35 deletions

View File

@ -47,6 +47,7 @@ export class HTMLSound implements Sound {
#soundLoopEventHook = createEventHook<HTMLSound>()
#soundEndEventHook = createEventHook<HTMLSound>()
#ignoreError = false
#scope = effectScope()
readonly isErrored = ref(false)
readonly isLoaded = ref(false)
@ -72,6 +73,7 @@ export class HTMLSound implements Sound {
console.log('CREATED SOUND INSTANCE', this)
this.#scope.run(() => {
useEventListener(this.#audio, 'ended', () => this.#soundEndEventHook.trigger(this))
useEventListener(this.#audio, 'timeupdate', () => {
if (this.#audio.currentTime === 0) {
@ -106,6 +108,7 @@ export class HTMLSound implements Sound {
this.isErrored.value = true
this.isLoaded.value = true
})
})
}
async preload () {
@ -115,17 +118,16 @@ export class HTMLSound implements Sound {
}
async dispose () {
// Remove all event listeners
this.#scope.stop()
// Stop audio playback
this.audioNode.disconnect()
this.#audio.pause()
// https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/readyState
if (this.#audio.readyState !== 4) {
this.#ignoreError = true
// Cancel any request downloading the source
this.#audio.src = ''
this.#audio.load()
this.#ignoreError = false
}
}
async play () {