Line data Source code
1 : import type { ComfyTemplateInputDownloadProgress } from '@comfyorg/comfyui-desktop-bridge-types'
2 :
3 : interface TemplateInputDownloadGraphSyncDependencies {
4 : getReferencedInputNames: () => ReadonlySet<string>
5 : refreshGraphBindings: (completedInputNames: string[]) => Promise<void>
6 : reportError: (error: unknown) => void
7 : }
8 :
9 2355 : function createTemplateInputDownloadGraphSync({
10 : getReferencedInputNames,
11 : refreshGraphBindings,
12 : reportError
13 : }: TemplateInputDownloadGraphSyncDependencies) {
14 2355 : const completedInputNames = new Set<string>()
15 2355 : let disposed = false
16 : /**
17 : * Read through a call rather than the binding. The early return in
18 : * `syncCurrentGraph` narrows `disposed` to `false` for the rest of that
19 : * function, but the loop below resumes after an await, by which point
20 : * `dispose` may have flipped it.
21 : */
22 2355 : const isDisposed = () => disposed
23 2355 : let scheduled = false
24 2355 : let rerunRequested = false
25 2355 : let inFlight: Promise<void> | null = null
26 :
27 9 : async function flushCurrentGraph() {
28 9 : const referenced = getReferencedInputNames()
29 9 : const matched = [...completedInputNames].filter((filename) =>
30 : referenced.has(filename)
31 : )
32 9 : if (!matched.length) return
33 :
34 0 : try {
35 0 : await refreshGraphBindings(matched)
36 : } catch (error) {
37 : // Keep the names: the files are on disk, and the next sync - another
38 : // completion or another template opening - is the only thing that can
39 : // rebind them.
40 0 : reportError(error)
41 0 : return
42 : }
43 :
44 0 : const currentReferences = getReferencedInputNames()
45 0 : for (const filename of matched) {
46 0 : if (currentReferences.has(filename)) completedInputNames.delete(filename)
47 : }
48 : }
49 :
50 0 : function scheduleSync() {
51 0 : if (disposed || scheduled) return
52 0 : scheduled = true
53 0 : queueMicrotask(() => {
54 0 : scheduled = false
55 0 : if (!disposed) void syncCurrentGraph()
56 : })
57 : }
58 :
59 9 : function syncCurrentGraph(): Promise<void> {
60 9 : if (disposed) return Promise.resolve()
61 9 : if (inFlight) {
62 0 : rerunRequested = true
63 0 : return inFlight
64 : }
65 :
66 : // Reruns stay inside this awaited block so callers do not continue before
67 : // every requested refresh attempt has settled.
68 9 : const trackedRun = (async () => {
69 9 : await flushCurrentGraph()
70 9 : while (rerunRequested && !isDisposed()) {
71 0 : rerunRequested = false
72 0 : await flushCurrentGraph()
73 : }
74 : })().finally(() => {
75 9 : if (inFlight === trackedRun) inFlight = null
76 : })
77 9 : inFlight = trackedRun
78 9 : return trackedRun
79 : }
80 :
81 0 : function handleProgress(progress: ComfyTemplateInputDownloadProgress) {
82 0 : if (progress.status !== 'completed') return
83 0 : completedInputNames.add(progress.filename)
84 0 : scheduleSync()
85 : }
86 :
87 0 : function dispose() {
88 0 : disposed = true
89 : }
90 :
91 2355 : return { handleProgress, syncCurrentGraph, dispose }
92 : }
93 :
94 : type TemplateInputDownloadGraphSync = ReturnType<
95 : typeof createTemplateInputDownloadGraphSync
96 : >
97 :
98 2355 : let activeGraphSync: TemplateInputDownloadGraphSync | null = null
99 :
100 2355 : export function startTemplateInputDownloadGraphSync(
101 : dependencies: TemplateInputDownloadGraphSyncDependencies
102 : ) {
103 2355 : activeGraphSync?.dispose()
104 2355 : const graphSync = createTemplateInputDownloadGraphSync(dependencies)
105 2355 : activeGraphSync = graphSync
106 :
107 2355 : return {
108 : handleProgress: graphSync.handleProgress,
109 : syncCurrentGraph: graphSync.syncCurrentGraph,
110 : dispose() {
111 0 : graphSync.dispose()
112 0 : if (activeGraphSync === graphSync) activeGraphSync = null
113 : }
114 : }
115 : }
116 :
117 9 : export function syncCompletedTemplateInputsWithCurrentGraph(): Promise<void> {
118 9 : return activeGraphSync?.syncCurrentGraph() ?? Promise.resolve()
119 : }
|