11import { createSharedComposable } from '@vueuse/core'
2- import { readonly , ref , shallowRef , watch } from 'vue'
2+ import { shallowRef , watch } from 'vue'
33
44import { useGraphNodeManager } from '@/composables/graph/useGraphNodeManager'
5- import type {
6- GraphNodeManager ,
7- VueNodeData
8- } from '@/composables/graph/useGraphNodeManager'
5+ import type { GraphNodeManager } from '@/composables/graph/useGraphNodeManager'
96import { useVueFeatureFlags } from '@/composables/useVueFeatureFlags'
107import type { LGraphCanvas , LGraphNode } from '@/lib/litegraph/src/litegraph'
118import { useCanvasStore } from '@/renderer/core/canvas/canvasStore'
@@ -22,31 +19,19 @@ function useVueNodeLifecycleIndividual() {
2219 const { shouldRenderVueNodes } = useVueFeatureFlags ( )
2320
2421 const nodeManager = shallowRef < GraphNodeManager | null > ( null )
25- const cleanupNodeManager = shallowRef < ( ( ) => void ) | null > ( null )
2622
27- // Sync management
28- const slotSync = shallowRef < ReturnType < typeof useSlotLayoutSync > | null > ( null )
29- const slotSyncStarted = ref ( false )
30- const linkSync = shallowRef < ReturnType < typeof useLinkLayoutSync > | null > ( null )
31-
32- // Vue node data state
33- const vueNodeData = ref < ReadonlyMap < string , VueNodeData > > ( new Map ( ) )
34-
35- // Trigger for forcing computed re-evaluation
36- const nodeDataTrigger = ref ( 0 )
23+ const { startSync } = useLayoutSync ( )
24+ const linkSyncManager = useLinkLayoutSync ( )
25+ const slotSyncManager = useSlotLayoutSync ( )
3726
3827 const initializeNodeManager = ( ) => {
3928 // Use canvas graph if available (handles subgraph contexts), fallback to app graph
40- const activeGraph = comfyApp . canvas ?. graph || comfyApp . graph
29+ const activeGraph = comfyApp . canvas ?. graph
4130 if ( ! activeGraph || nodeManager . value ) return
4231
4332 // Initialize the core node manager
4433 const manager = useGraphNodeManager ( activeGraph )
4534 nodeManager . value = manager
46- cleanupNodeManager . value = manager . cleanup
47-
48- // Use the manager's data maps
49- vueNodeData . value = manager . vueNodeData
5035
5136 // Initialize layout system with existing nodes from active graph
5237 const nodes = activeGraph . _nodes . map ( ( node : LGraphNode ) => ( {
@@ -76,46 +61,29 @@ function useVueNodeLifecycleIndividual() {
7661 }
7762
7863 // Initialize layout sync (one-way: Layout Store → LiteGraph)
79- const { startSync } = useLayoutSync ( )
8064 startSync ( canvasStore . canvas )
8165
82- // Initialize link layout sync for event-driven updates
83- const linkSyncManager = useLinkLayoutSync ( )
84- linkSync . value = linkSyncManager
8566 if ( comfyApp . canvas ) {
8667 linkSyncManager . start ( comfyApp . canvas )
8768 }
88-
89- // Force computed properties to re-evaluate
90- nodeDataTrigger . value ++
9169 }
9270
9371 const disposeNodeManagerAndSyncs = ( ) => {
9472 if ( ! nodeManager . value ) return
9573
9674 try {
97- cleanupNodeManager . value ?. ( )
75+ nodeManager . value . cleanup ( )
9876 } catch {
9977 /* empty */
10078 }
10179 nodeManager . value = null
102- cleanupNodeManager . value = null
103-
104- // Clean up link layout sync
105- if ( linkSync . value ) {
106- linkSync . value . stop ( )
107- linkSync . value = null
108- }
10980
110- // Reset reactive maps to clean state
111- vueNodeData . value = new Map ( )
81+ linkSyncManager . stop ( )
11282 }
11383
11484 // Watch for Vue nodes enabled state changes
11585 watch (
116- ( ) =>
117- shouldRenderVueNodes . value &&
118- Boolean ( comfyApp . canvas ?. graph || comfyApp . graph ) ,
86+ ( ) => shouldRenderVueNodes . value && Boolean ( comfyApp . canvas ?. graph ) ,
11987 ( enabled ) => {
12088 if ( enabled ) {
12189 initializeNodeManager ( )
@@ -138,47 +106,42 @@ function useVueNodeLifecycleIndividual() {
138106 }
139107
140108 // Switching to Vue
141- if ( vueMode && slotSyncStarted . value ) {
142- slotSync . value ?. stop ( )
143- slotSyncStarted . value = false
109+ if ( vueMode ) {
110+ slotSyncManager . stop ( )
144111 }
145112
146113 // Switching to LG
147114 const shouldRun = Boolean ( canvas ?. graph ) && ! vueMode
148- if ( shouldRun && ! slotSyncStarted . value && canvas ) {
149- // Initialize slot sync if not already created
150- if ( ! slotSync . value ) {
151- slotSync . value = useSlotLayoutSync ( )
152- }
153- const started = slotSync . value . attemptStart ( canvas as LGraphCanvas )
154- slotSyncStarted . value = started
115+ if ( shouldRun && canvas ) {
116+ slotSyncManager . attemptStart ( canvas as LGraphCanvas )
155117 }
156118 } ,
157119 { immediate : true }
158120 )
159121
160122 // Handle case where Vue nodes are enabled but graph starts empty
161123 const setupEmptyGraphListener = ( ) => {
124+ const activeGraph = comfyApp . canvas ?. graph
162125 if (
163- shouldRenderVueNodes . value &&
164- comfyApp . graph &&
165- ! nodeManager . value &&
166- comfyApp . graph . _nodes . length === 0
126+ ! shouldRenderVueNodes . value ||
127+ nodeManager . value ||
128+ activeGraph ?. _nodes . length !== 0
167129 ) {
168- const originalOnNodeAdded = comfyApp . graph . onNodeAdded
169- comfyApp . graph . onNodeAdded = function ( node : LGraphNode ) {
170- // Restore original handler
171- comfyApp . graph . onNodeAdded = originalOnNodeAdded
172-
173- // Initialize node manager if needed
174- if ( shouldRenderVueNodes . value && ! nodeManager . value ) {
175- initializeNodeManager ( )
176- }
177-
178- // Call original handler
179- if ( originalOnNodeAdded ) {
180- originalOnNodeAdded . call ( this , node )
181- }
130+ return
131+ }
132+ const originalOnNodeAdded = activeGraph . onNodeAdded
133+ activeGraph . onNodeAdded = function ( node : LGraphNode ) {
134+ // Restore original handler
135+ activeGraph . onNodeAdded = originalOnNodeAdded
136+
137+ // Initialize node manager if needed
138+ if ( shouldRenderVueNodes . value && ! nodeManager . value ) {
139+ initializeNodeManager ( )
140+ }
141+
142+ // Call original handler
143+ if ( originalOnNodeAdded ) {
144+ originalOnNodeAdded . call ( this , node )
182145 }
183146 }
184147 }
@@ -189,20 +152,12 @@ function useVueNodeLifecycleIndividual() {
189152 nodeManager . value . cleanup ( )
190153 nodeManager . value = null
191154 }
192- if ( slotSyncStarted . value ) {
193- slotSync . value ?. stop ( )
194- slotSyncStarted . value = false
195- }
196- slotSync . value = null
197- if ( linkSync . value ) {
198- linkSync . value . stop ( )
199- linkSync . value = null
200- }
155+ slotSyncManager . stop ( )
156+ linkSyncManager . stop ( )
201157 }
202158
203159 return {
204- vueNodeData,
205- nodeManager : readonly ( nodeManager ) ,
160+ nodeManager,
206161
207162 // Lifecycle methods
208163 initializeNodeManager,
0 commit comments