diff --git a/src/renderer.ts b/src/renderer.ts index 914b4838b..f86e3ea5e 100644 --- a/src/renderer.ts +++ b/src/renderer.ts @@ -31,7 +31,7 @@ class Renderer extends EventEmitter { private lastContainerWidth = 0 private isDragging = false private subscriptions: (() => void)[] = [] - private unsubscribeOnScroll?: () => void + private unsubscribeOnScroll: (() => void)[] = [] constructor(options: WaveSurferOptions, audioElement?: HTMLElement) { super() @@ -293,7 +293,8 @@ class Renderer extends EventEmitter { this.subscriptions.forEach((unsubscribe) => unsubscribe()) this.container.remove() this.resizeObserver?.disconnect() - this.unsubscribeOnScroll?.() + this.unsubscribeOnScroll?.forEach((unsubscribe) => unsubscribe()) + this.unsubscribeOnScroll = [] } private createDelay(delayMs = 10): () => Promise { @@ -584,7 +585,7 @@ class Renderer extends EventEmitter { // Subscribe to the scroll event to draw additional canvases if (numCanvases > 1) { - this.unsubscribeOnScroll = this.on('scroll', () => { + const unsubscribe = this.on('scroll', () => { const { scrollLeft } = this.scrollContainer const canvasIndex = Math.floor((scrollLeft / totalWidth) * numCanvases) clearCanvases() @@ -592,6 +593,8 @@ class Renderer extends EventEmitter { draw(canvasIndex) draw(canvasIndex + 1) }) + + this.unsubscribeOnScroll.push(unsubscribe) } } @@ -677,8 +680,8 @@ class Renderer extends EventEmitter { } reRender() { - this.unsubscribeOnScroll?.() - delete this.unsubscribeOnScroll + this.unsubscribeOnScroll.forEach((unsubscribe) => unsubscribe()) + this.unsubscribeOnScroll = [] // Return if the waveform has not been rendered yet if (!this.audioData) return