Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: (Renderer) splitChannels memory leak #3959

Merged
merged 1 commit into from
Dec 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading