Skip to content

Commit

Permalink
Fix: (Renderer) splitChannel memory leak
Browse files Browse the repository at this point in the history
  • Loading branch information
rjett1 committed Dec 13, 2024
1 parent ac6b678 commit 7c79f70
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Renderer extends EventEmitter<RendererEvents> {
private lastContainerWidth = 0
private isDragging = false
private subscriptions: (() => void)[] = []
private unsubscribeOnScroll?: () => void
private unsubscribeOnScroll: (() => void)[] = []

constructor(options: WaveSurferOptions, audioElement?: HTMLElement) {
super()
Expand Down Expand Up @@ -293,7 +293,8 @@ class Renderer extends EventEmitter<RendererEvents> {
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<void> {
Expand Down Expand Up @@ -584,14 +585,16 @@ class Renderer extends EventEmitter<RendererEvents> {

// 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()
draw(canvasIndex - 1)
draw(canvasIndex)
draw(canvasIndex + 1)
})

this.unsubscribeOnScroll.push(unsubscribe)
}
}

Expand Down Expand Up @@ -677,8 +680,8 @@ class Renderer extends EventEmitter<RendererEvents> {
}

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
Expand Down

0 comments on commit 7c79f70

Please sign in to comment.