Line data Source code
1 : import { delay } from 'es-toolkit'
2 : import { getCurrentScope } from 'vue'
3 : import type { ComputedRef } from 'vue'
4 :
5 : import { LiteGraph } from '@/lib/litegraph/src/litegraph'
6 : import { isCloud } from '@/platform/distribution/types'
7 : import { reportError } from '@comfyorg/telemetry/reportError'
8 : import { useWorkflowService } from '@/platform/workflow/core/services/workflowService'
9 : import type { LoadedComfyWorkflow } from '@/platform/workflow/management/stores/comfyWorkflow'
10 : import { useWorkflowStore } from '@/platform/workflow/management/stores/workflowStore'
11 : import { app } from '@/scripts/app'
12 : import { useCommandStore } from '@/platform/keybindings/commandStore'
13 : import { useNodeDefStore } from '@/stores/nodeDefStore'
14 : import { reduceAllNodes } from '@/utils/graphTraversalUtil'
15 :
16 : import { useUserBusy } from './useUserBusy'
17 :
18 2355 : const IDLE_POLL_MS = 100
19 2355 : const IDLE_TIMEOUT_MS = 30_000
20 :
21 8 : async function fetchServerNodeTypes(): Promise<string[] | undefined> {
22 8 : try {
23 8 : const fetched = await app.fetchNodeDefs()
24 8 : return fetched.ok ? Object.keys(fetched.defs) : undefined
25 : } catch {
26 0 : return undefined
27 : }
28 : }
29 :
30 6188 : function isRegistered(type: string): boolean {
31 6188 : return (
32 : type in useNodeDefStore().nodeDefsByName ||
33 : type in LiteGraph.registered_node_types
34 : )
35 : }
36 :
37 6 : function canvasHasRebuildablePlaceholder(): boolean {
38 6 : return reduceAllNodes(
39 : app.rootGraph,
40 : (found, node) =>
41 : found ||
42 : (node.last_serialization !== undefined && isRegistered(node.type)),
43 : false
44 : )
45 : }
46 :
47 6 : async function rebuildWhenIdle(
48 : workflow: LoadedComfyWorkflow,
49 : userBusy: ComputedRef<boolean>,
50 : isDisposed: () => boolean
51 : ): Promise<void> {
52 6 : const workflowStore = useWorkflowStore()
53 6 : const isActive = () =>
54 : !isDisposed() && workflowStore.activeWorkflow === workflow
55 6 : const isIdle = () =>
56 : workflow.changeTracker.changeCount === 0 &&
57 : !workflow.changeTracker._restoringState &&
58 : !userBusy.value
59 6 : const deadline = Date.now() + IDLE_TIMEOUT_MS
60 6 : while (isActive() && canvasHasRebuildablePlaceholder()) {
61 1 : if (isIdle()) {
62 1 : const attempt = { deferred: false }
63 : const rebuilt = await useWorkflowService().openWorkflow(workflow, {
64 : force: true,
65 : isCurrent: () => {
66 3 : attempt.deferred = !(isActive() && isIdle())
67 3 : return !attempt.deferred
68 : },
69 : keepSavedBaseline: true
70 : })
71 1 : if (rebuilt || !attempt.deferred) return
72 : }
73 0 : if (Date.now() >= deadline) return
74 0 : await delay(IDLE_POLL_MS)
75 : }
76 : }
77 :
78 : /**
79 : * Returns the `reconnected` handler that loads node types a restarted local
80 : * ComfyUI added, and rebuilds the open workflow's missing nodes from them.
81 : */
82 2355 : export function useReconnectNodeDefinitionRefresh() {
83 : if (isCloud) return async () => {}
84 :
85 2355 : const userBusy = useUserBusy()
86 2355 : const scope = getCurrentScope()
87 2355 : const isDisposed = () => scope?.active === false
88 2355 : let inFlight: Promise<void> | null = null
89 2355 : let reconnects = 0
90 :
91 8 : async function refreshAndRebuild(): Promise<void> {
92 8 : if (isDisposed()) return
93 8 : const serverTypes = await fetchServerNodeTypes()
94 8 : if (!serverTypes || isDisposed()) return
95 6 : if (serverTypes.some((type) => !isRegistered(type))) {
96 1 : await useCommandStore().execute('Comfy.RefreshNodeDefinitions', {
97 : errorHandler: (error) => {
98 0 : throw error
99 : }
100 : })
101 : }
102 6 : if (isDisposed()) return
103 6 : const workflow = useWorkflowStore().activeWorkflow
104 6 : if (workflow) await rebuildWhenIdle(workflow, userBusy, isDisposed)
105 : }
106 :
107 8 : async function refreshUntilSettled(): Promise<void> {
108 8 : let handled: number
109 8 : do {
110 8 : handled = reconnects
111 8 : try {
112 8 : await refreshAndRebuild()
113 : } catch (error) {
114 0 : reportError(error, {
115 : surface: 'graph',
116 : errorType: 'failure_refreshing_node_definitions_after_reconnect',
117 : level: 'warning'
118 : })
119 : }
120 : } while (handled !== reconnects)
121 : }
122 :
123 2355 : return async function refreshNodeDefinitionsOnReconnect(): Promise<void> {
124 8 : reconnects += 1
125 8 : if (inFlight) return inFlight
126 8 : inFlight = refreshUntilSettled().finally(() => {
127 6 : inFlight = null
128 : })
129 8 : return inFlight
130 : }
131 : }
|