Prune widgets on graph configure (#3716)

This commit is contained in:
Chenlei Hu
2025-05-01 16:07:42 -04:00
committed by GitHub
parent f868fac6e9
commit 4461210f43
2 changed files with 18 additions and 0 deletions

View File

@@ -62,6 +62,7 @@ import { deserialiseAndCreate } from '@/utils/vintageClipboard'
import { type ComfyApi, PromptExecutionError, api } from './api'
import { defaultGraph } from './defaultGraph'
import { pruneWidgets } from './domWidget'
import {
getFlacMetadata,
getLatentMetadata,
@@ -707,6 +708,8 @@ export class ComfyApp {
node.onAfterGraphConfigured?.()
}
pruneWidgets(this.nodes)
return r
}
}

View File

@@ -314,3 +314,18 @@ LGraphNode.prototype.addDOMWidget = function <
return widget
}
/**
* Prunes widgets that are no longer in the graph.
* @param nodes The nodes to prune widgets for.
*/
export const pruneWidgets = (nodes: LGraphNode[]) => {
const nodeSet = new Set(nodes)
const domWidgetStore = useDomWidgetStore()
for (const widgetState of domWidgetStore.widgetStates.values()) {
const widget = widgetState.widget
if (!nodeSet.has(widget.node as LGraphNode)) {
domWidgetStore.unregisterWidget(widget.id)
}
}
}