Line data Source code
1 : import type {
2 : LGraph,
3 : LGraphCanvas,
4 : LGraphNode
5 : } from '@/lib/litegraph/src/litegraph'
6 : import type { useExecutionStore } from '@/stores/executionStore'
7 : import type { NodeLocatorId } from '@comfyorg/graph-model/nodeIdentification'
8 :
9 : type ProgressStates = Readonly<
10 : ReturnType<typeof useExecutionStore>['nodeLocationProgressStates']
11 : >
12 : type ProgressState = ProgressStates[NodeLocatorId]
13 :
14 : export interface NodeProgressCanvasSync {
15 : dispose: () => void
16 : sync: (
17 : states: ProgressStates,
18 : canvas: LGraphCanvas | null,
19 : graph: LGraph | null
20 : ) => void
21 : }
22 :
23 22541 : function progressValue(state: ProgressState | undefined) {
24 22541 : return state?.state === 'running' ? state.value / state.max : undefined
25 : }
26 :
27 2355 : export function createNodeProgressCanvasSync(
28 : nodeToLocator: (node: LGraphNode) => NodeLocatorId
29 : ): NodeProgressCanvasSync {
30 2355 : let activeCanvas: LGraphCanvas | null = null
31 2355 : let activeGraph: LGraph | null = null
32 2355 : let activeStates: ProgressStates = {}
33 2355 : let nodeLocators = new WeakMap<LGraphNode, NodeLocatorId>()
34 2355 : const nodesByLocator = new Map<NodeLocatorId, LGraphNode[]>()
35 :
36 91 : function markDirty() {
37 91 : activeCanvas?.setDirty(true, false)
38 : }
39 :
40 22214 : function setNodeProgress(node: LGraphNode, nextProgress: number | undefined) {
41 22214 : if (Object.is(node.progress, nextProgress)) return false
42 102 : node.progress = nextProgress
43 102 : return true
44 : }
45 :
46 22113 : function addNode(node: LGraphNode) {
47 22113 : const locator = nodeToLocator(node)
48 22113 : nodeLocators.set(node, locator)
49 22113 : const nodes = nodesByLocator.get(locator)
50 22113 : if (nodes) nodes.push(node)
51 22094 : else nodesByLocator.set(locator, [node])
52 22113 : return locator
53 : }
54 :
55 10088 : function removeNode(node: LGraphNode) {
56 10088 : const locator = nodeLocators.get(node)
57 10088 : if (!locator) return
58 10088 : const nodes = nodesByLocator.get(locator)
59 10088 : if (!nodes) return
60 10088 : const nextNodes = nodes.filter((candidate) => candidate !== node)
61 10088 : if (nextNodes.length) nodesByLocator.set(locator, nextNodes)
62 10088 : else nodesByLocator.delete(locator)
63 10088 : nodeLocators.delete(node)
64 : }
65 :
66 21667 : function onNodeAdded(event: CustomEvent<{ node: LGraphNode }>) {
67 21667 : const node = event.detail.node
68 21667 : const locator = addNode(node)
69 21667 : if (setNodeProgress(node, progressValue(activeStates[locator]))) markDirty()
70 : }
71 :
72 10088 : function onNodeRemoved(event: CustomEvent<{ node: LGraphNode }>) {
73 10088 : removeNode(event.detail.node)
74 : }
75 :
76 2569 : function detachGraph() {
77 2569 : activeGraph?.events.removeEventListener('node:added', onNodeAdded)
78 2569 : activeGraph?.events.removeEventListener(
79 : 'node:before-removed',
80 : onNodeRemoved
81 : )
82 : }
83 :
84 2569 : function replaceGraph(graph: LGraph | null) {
85 2569 : detachGraph()
86 2569 : activeGraph = graph
87 2569 : nodesByLocator.clear()
88 2569 : nodeLocators = new WeakMap()
89 2569 : if (!graph) return false
90 :
91 2569 : let progressChanged = false
92 2569 : for (const node of graph.nodes) {
93 446 : const locator = addNode(node)
94 446 : progressChanged =
95 : setNodeProgress(node, progressValue(activeStates[locator])) ||
96 : progressChanged
97 : }
98 2569 : graph.events.addEventListener('node:added', onNodeAdded)
99 2569 : graph.events.addEventListener('node:before-removed', onNodeRemoved)
100 2569 : return progressChanged
101 : }
102 :
103 6542 : function sync(
104 : states: ProgressStates,
105 : canvas: LGraphCanvas | null,
106 : graph: LGraph | null
107 : ) {
108 6542 : const previousStates = activeStates
109 6542 : activeCanvas = canvas
110 6542 : activeStates = states
111 :
112 6542 : if (graph !== activeGraph) {
113 2569 : if (replaceGraph(graph)) markDirty()
114 2569 : return
115 : }
116 :
117 3973 : let progressChanged = false
118 3973 : const changedLocators = new Set([
119 : ...Object.keys(previousStates),
120 : ...Object.keys(states)
121 : ] as NodeLocatorId[])
122 6529 : for (const locator of changedLocators) {
123 214 : const previousProgress = progressValue(previousStates[locator])
124 214 : const nextProgress = progressValue(states[locator])
125 214 : if (Object.is(previousProgress, nextProgress)) continue
126 214 : for (const node of nodesByLocator.get(locator) ?? []) {
127 101 : progressChanged = setNodeProgress(node, nextProgress) || progressChanged
128 : }
129 : }
130 6542 : if (progressChanged) markDirty()
131 : }
132 :
133 0 : function dispose() {
134 0 : detachGraph()
135 0 : activeCanvas = null
136 0 : activeGraph = null
137 0 : activeStates = {}
138 0 : nodesByLocator.clear()
139 0 : nodeLocators = new WeakMap()
140 : }
141 :
142 2355 : return { dispose, sync }
143 : }
|