From 82f6ca576672ffd257028074f8a5fc8f13263f8e Mon Sep 17 00:00:00 2001 From: Beka Westberg Date: Thu, 17 Aug 2023 11:39:09 -0700 Subject: [PATCH] fix: saveConnections listener not being disposed (#7407) --- core/icons/mutator_icon.ts | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/core/icons/mutator_icon.ts b/core/icons/mutator_icon.ts index f0798a39e..3684ed8de 100644 --- a/core/icons/mutator_icon.ts +++ b/core/icons/mutator_icon.ts @@ -60,6 +60,15 @@ export class MutatorIcon extends Icon implements IHasBubble { /** The PID tracking updating the workkspace in response to user events. */ private updateWorkspacePid: ReturnType | null = null; + /** + * The change listener in the main workspace that triggers the saveConnections + * method when anything in the main workspace changes. + * + * Only actually registered to listen for events while the mutator bubble is + * open. + */ + private saveConnectionsListener: (() => void) | null = null; + constructor( private readonly flyoutBlockTypes: string[], protected readonly sourceBlock: BlockSvg, @@ -169,6 +178,12 @@ export class MutatorIcon extends Icon implements IHasBubble { } else { this.miniWorkspaceBubble?.dispose(); this.miniWorkspaceBubble = null; + if (this.saveConnectionsListener) { + this.sourceBlock.workspace.removeChangeListener( + this.saveConnectionsListener, + ); + } + this.saveConnectionsListener = null; } eventUtils.fire( @@ -254,12 +269,12 @@ export class MutatorIcon extends Icon implements IHasBubble { /** Adds a listen to the source block that triggers saving connections. */ private addSaveConnectionsListener() { if (!this.sourceBlock.saveConnections || !this.rootBlock) return; - const saveConnectionsListener = () => { + this.saveConnectionsListener = () => { if (!this.sourceBlock.saveConnections || !this.rootBlock) return; this.sourceBlock.saveConnections(this.rootBlock); }; - saveConnectionsListener(); - this.sourceBlock.workspace.addChangeListener(saveConnectionsListener); + this.saveConnectionsListener(); + this.sourceBlock.workspace.addChangeListener(this.saveConnectionsListener); } /**