@@ -187,8 +187,13 @@ export class SubgraphNode extends LGraphNode implements BaseLGraph {
187187 }
188188
189189 #setWidget( subgraphInput : Readonly < SubgraphInput > , input : INodeInputSlot , widget : Readonly < IBaseWidget > ) {
190- // Use the first matching widget
191- const promotedWidget = toConcreteWidget ( widget , this ) . createCopyForNode ( this )
190+ const concreteWidget = toConcreteWidget ( widget , this )
191+
192+ const promotedWidget = concreteWidget . createCopyForNode ( this )
193+
194+ // Set parentSubgraphNode for all promoted widgets to track their origin
195+ promotedWidget . parentSubgraphNode = this
196+
192197 Object . assign ( promotedWidget , {
193198 get name ( ) {
194199 return subgraphInput . name
@@ -212,6 +217,9 @@ export class SubgraphNode extends LGraphNode implements BaseLGraph {
212217
213218 this . widgets . push ( promotedWidget )
214219
220+ // Dispatch widget-promoted event
221+ this . subgraph . events . dispatch ( "widget-promoted" , { widget : promotedWidget , subgraphNode : this } )
222+
215223 input . widget = { name : subgraphInput . name }
216224 input . _widget = promotedWidget
217225 }
@@ -307,7 +315,28 @@ export class SubgraphNode extends LGraphNode implements BaseLGraph {
307315 return nodes
308316 }
309317
318+ override removeWidgetByName ( name : string ) : void {
319+ const widget = this . widgets . find ( w => w . name === name )
320+ if ( widget ) {
321+ this . subgraph . events . dispatch ( "widget-unpromoted" , { widget, subgraphNode : this } )
322+ }
323+ super . removeWidgetByName ( name )
324+ }
325+
326+ override ensureWidgetRemoved ( widget : IBaseWidget ) : void {
327+ if ( this . widgets . includes ( widget ) ) {
328+ this . subgraph . events . dispatch ( "widget-unpromoted" , { widget, subgraphNode : this } )
329+ }
330+ super . ensureWidgetRemoved ( widget )
331+ }
332+
310333 override onRemoved ( ) : void {
334+ // Clean up all promoted widgets
335+ for ( const widget of this . widgets ) {
336+ widget . parentSubgraphNode = undefined
337+ this . subgraph . events . dispatch ( "widget-unpromoted" , { widget, subgraphNode : this } )
338+ }
339+
311340 for ( const input of this . inputs ) {
312341 input . _listenerController ?. abort ( )
313342 }
0 commit comments