@@ -48,11 +48,14 @@ class WorkflowCollection(QAbstractListModel):
48
48
_icon_remote = theme .icon ("web-connection" )
49
49
_icon_document = theme .icon ("file-kra" )
50
50
51
+ loaded = pyqtSignal ()
52
+
51
53
def __init__ (self , connection : Connection , folder : Path | None = None ):
52
54
super ().__init__ ()
53
55
self ._connection = connection
54
56
self ._folder = folder or user_data_dir / "workflows"
55
57
self ._workflows : list [CustomWorkflow ] = []
58
+ self ._pending_workflows : list [tuple [str , WorkflowSource , dict ]] = []
56
59
57
60
self ._connection .state_changed .connect (self ._handle_connection )
58
61
self ._connection .workflow_published .connect (self ._process_remote_workflow )
@@ -63,6 +66,10 @@ def _handle_connection(self, state: ConnectionState):
63
66
self .clear ()
64
67
65
68
if state is ConnectionState .connected :
69
+ for id , source , graph in self ._pending_workflows :
70
+ self ._process_workflow (id , source , graph )
71
+ self ._pending_workflows .clear ()
72
+
66
73
for file in self ._folder .glob ("*.json" ):
67
74
try :
68
75
self ._process_file (file )
@@ -72,31 +79,36 @@ def _handle_connection(self, state: ConnectionState):
72
79
for wf in self ._connection .workflows .keys ():
73
80
self ._process_remote_workflow (wf )
74
81
82
+ self .loaded .emit ()
83
+
75
84
def _node_inputs (self ):
76
85
return self ._connection .client .models .node_inputs
77
86
78
- def _create_workflow (
87
+ def _process_workflow (
79
88
self , id : str , source : WorkflowSource , graph : dict , path : Path | None = None
80
89
):
81
- wf = ComfyWorkflow .import_graph (graph , self ._node_inputs ())
82
- return CustomWorkflow (id , source , wf , path )
83
-
84
- def _process_remote_workflow (self , id : str ):
85
- graph = self ._connection .workflows [id ]
86
- self ._process (self ._create_workflow (id , WorkflowSource .remote , graph ))
87
-
88
- def _process_file (self , file : Path ):
89
- with file .open ("r" ) as f :
90
- graph = json .load (f )
91
- self ._process (self ._create_workflow (file .stem , WorkflowSource .local , graph , file ))
90
+ if self ._connection .state is not ConnectionState .connected :
91
+ self ._pending_workflows .append ((id , source , graph ))
92
+ return
92
93
93
- def _process (self , workflow : CustomWorkflow ):
94
+ comfy_flow = ComfyWorkflow .import_graph (graph , self ._node_inputs ())
95
+ workflow = CustomWorkflow (id , source , comfy_flow , path )
94
96
idx = self .find_index (workflow .id )
95
97
if idx .isValid ():
96
98
self ._workflows [idx .row ()] = workflow
97
99
self .dataChanged .emit (idx , idx )
98
100
else :
99
101
self .append (workflow )
102
+ return idx
103
+
104
+ def _process_remote_workflow (self , id : str ):
105
+ graph = self ._connection .workflows [id ]
106
+ self ._process_workflow (id , WorkflowSource .remote , graph )
107
+
108
+ def _process_file (self , file : Path ):
109
+ with file .open ("r" ) as f :
110
+ graph = json .load (f )
111
+ self ._process_workflow (file .stem , WorkflowSource .local , graph , file )
100
112
101
113
def rowCount (self , parent = QModelIndex ()):
102
114
return len (self ._workflows )
@@ -121,7 +133,7 @@ def append(self, item: CustomWorkflow):
121
133
self .endInsertRows ()
122
134
123
135
def add_from_document (self , id : str , graph : dict ):
124
- self .append ( self . _create_workflow ( id , WorkflowSource .document , graph ) )
136
+ self ._process_workflow ( id , WorkflowSource .document , graph )
125
137
126
138
def remove (self , id : str ):
127
139
idx = self .find_index (id )
@@ -154,7 +166,7 @@ def save_as(self, id: str, graph: dict):
154
166
self ._folder .mkdir (exist_ok = True )
155
167
path = self ._folder / f"{ id } .json"
156
168
path .write_text (json .dumps (graph , indent = 2 ))
157
- self .append ( self . _create_workflow ( id , WorkflowSource .local , graph , path ) )
169
+ self ._process_workflow ( id , WorkflowSource .local , graph , path )
158
170
return id
159
171
160
172
def import_file (self , filepath : Path ):
@@ -336,12 +348,16 @@ def __init__(self, workflows: WorkflowCollection, generator: ImageGenerator, job
336
348
337
349
jobs .job_finished .connect (self ._handle_job_finished )
338
350
workflows .dataChanged .connect (self ._update_workflow )
339
- workflows .rowsInserted .connect (self ._set_default_workflow )
351
+ workflows .loaded .connect (self ._set_default_workflow )
340
352
self ._set_default_workflow ()
341
353
342
354
def _set_default_workflow (self ):
343
355
if not self .workflow_id and len (self ._workflows ) > 0 :
344
356
self .workflow_id = self ._workflows [0 ].id
357
+ else :
358
+ current_index = self ._workflows .find_index (self .workflow_id )
359
+ if current_index .isValid ():
360
+ self ._update_workflow (current_index , QModelIndex ())
345
361
346
362
def _update_workflow (self , idx : QModelIndex , _ : QModelIndex ):
347
363
wf = self ._workflows [idx .row ()]
@@ -358,10 +374,13 @@ def _set_workflow_id(self, id: str):
358
374
self ._workflow_id = id
359
375
self .workflow_id_changed .emit (id )
360
376
self .modified .emit (self , "workflow_id" )
361
- self ._update_workflow (self ._workflows .find_index (id ), QModelIndex ())
377
+ index = self ._workflows .find_index (id )
378
+ if index .isValid (): # might be invalid when loading document before connecting
379
+ self ._update_workflow (index , QModelIndex ())
362
380
363
381
def set_graph (self , id : str , graph : dict ):
364
382
if self ._workflows .find (id ) is None :
383
+ id = "Document Workflow (embedded)"
365
384
self ._workflows .add_from_document (id , graph )
366
385
self .workflow_id = id
367
386
0 commit comments