Line data Source code
1 : import type {
2 : LGraph,
3 : LGraphNode,
4 : Subgraph
5 : } from '@comfyorg/graph-model/entityRegistry'
6 : import type { IBaseWidget } from '@comfyorg/graph-model/widgets'
7 : import type { WidgetRefusalReport } from '@/lib/litegraph/src/node/widgetsView'
8 : import { refuseAmbiguousNodeWidgets } from '@/lib/litegraph/src/node/widgetsView'
9 : import { isNodeBindable } from '@/lib/litegraph/src/utils/type'
10 : import { getWidgetIds } from '@/lib/litegraph/src/utils/widget'
11 : import { reportError } from '@comfyorg/telemetry/reportError'
12 : import { usePreviewExposureStore } from '@/stores/previewExposureStore'
13 : import { useWidgetValueStore } from '@/stores/widgetValueStore'
14 : import type { RefusedWidget } from '@comfyorg/graph-model/widgetId'
15 :
16 : import { registerNodeState, unregisterNodeState } from './nodeShellState'
17 :
18 : import type { NodeId } from '@comfyorg/graph-model/nodeId'
19 :
20 : import type { UUID } from '@/utils/uuid'
21 :
22 2356 : const REFUSAL_TEARDOWN_ERROR_TYPE = 'failure_tearing_down_refused_widget'
23 :
24 2356 : const REFUSAL_ALERTS: Record<
25 : RefusedWidget<IBaseWidget>['cause'],
26 : {
27 : errorType: string
28 : message: (nodeId: LGraphNode['id'], name: string | undefined) => string
29 : }
30 : > = {
31 : 'unreadable-name': {
32 : errorType: 'failure_reading_widget_name',
33 : message: (nodeId) =>
34 : `Refused a widget on node ${nodeId}: no widget identity can be derived from its name`
35 : },
36 : 'unresolved-duplicate': {
37 : errorType: 'failure_resolving_widget_duplicate_name',
38 : message: (nodeId, name) =>
39 : `Kept a widget named "${name}" that node ${nodeId} already has under that name: the rename was declined rather than impossible, so the widget is left in place and the pair is unresolved for now`
40 : },
41 : 'duplicate-name': {
42 : errorType: 'failure_renaming_widget_duplicate_name',
43 : message: (nodeId, name) =>
44 : `Refused a widget named "${name}": node ${nodeId} already has a widget of that name and the duplicate cannot be renamed`
45 : }
46 : }
47 :
48 7 : function safeRead(read: () => unknown): string | undefined {
49 7 : try {
50 7 : const value = read()
51 7 : return value === undefined ? undefined : String(value)
52 : } catch {
53 0 : return undefined
54 : }
55 : }
56 :
57 7 : function reportWidgetRefusal(
58 : node: LGraphNode,
59 : report: WidgetRefusalReport
60 : ): void {
61 7 : if (report.kind === 'teardown-failure') {
62 0 : reportError(report.error, {
63 : errorType: REFUSAL_TEARDOWN_ERROR_TYPE,
64 : surface: 'graph',
65 : level: 'warning',
66 : tags: { node_type: node.type },
67 : context: { nodeId: String(node.id) }
68 : })
69 0 : return
70 : }
71 :
72 7 : const { widget, cause, name } = report.finding
73 7 : const alert = REFUSAL_ALERTS[cause]
74 7 : reportError(new Error(alert.message(node.id, name)), {
75 : errorType: alert.errorType,
76 : surface: 'graph',
77 : level: 'warning',
78 : tags: { node_type: node.type },
79 : context: {
80 : nodeId: String(node.id),
81 : widgetName: name,
82 : widgetType: safeRead(() => widget.type)
83 : }
84 : })
85 : }
86 :
87 : /**
88 : * Registers a node's shell state and its widget bindings with the app
89 : * stores. Call once the node has a valid id and graph reference. Retries
90 : * with a freshly minted id on a registration collision.
91 : */
92 23236 : export function attachNodeToStores(
93 : graph: LGraph | Subgraph,
94 : node: LGraphNode,
95 : mintId: () => NodeId
96 : ): void {
97 23236 : while (!registerNodeState(graph, node)) {
98 1 : const collidedId = node.id
99 1 : node.id = mintId()
100 1 : console.warn(
101 : `[nodeShell] Node id ${collidedId} is already registered in root graph ${graph.rootGraph.id}; reminted as ${node.id}.`
102 : )
103 : }
104 :
105 23236 : refuseAmbiguousNodeWidgets(node, (report) =>
106 : reportWidgetRefusal(node, report)
107 : )
108 23236 : if (!node.widgets) return
109 23196 : for (const widget of node.widgets) {
110 47550 : if (isNodeBindable(widget)) widget.setNodeId(node.id)
111 : }
112 19852 : useWidgetValueStore().setNodeWidgetOrder(
113 : graph.rootGraph.id,
114 : node.id,
115 : getWidgetIds(node.widgets)
116 : )
117 : }
118 :
119 : /**
120 : * Whether a detached node's widget values leave the store with it. A node that
121 : * may come back — undo of a deletion — keeps its values and drops only its
122 : * ordering; a node whose whole graph is going away takes its values along.
123 : */
124 : type WidgetDetachMode = 'keep-values' | 'discard-values'
125 :
126 10979 : function releaseNodePreviewExposures(
127 : rootGraphId: UUID,
128 : node: LGraphNode
129 : ): void {
130 10979 : const previewExposureStore = usePreviewExposureStore()
131 10979 : const hostNodeLocator = String(node.id)
132 10979 : if (!previewExposureStore.getExposures(rootGraphId, hostNodeLocator).length) {
133 10979 : return
134 : }
135 0 : previewExposureStore.setExposures(rootGraphId, hostNodeLocator, [])
136 : }
137 :
138 : /**
139 : * The inverse of {@link attachNodeToStores}: drops the node's shell state, the
140 : * widget order it registered, and the preview exposures it hosts.
141 : */
142 10979 : export function detachNodeFromStores(
143 : graph: Pick<LGraph, 'rootGraph'>,
144 : node: LGraphNode,
145 : mode: WidgetDetachMode = 'keep-values'
146 : ): void {
147 10979 : const rootGraphId = graph.rootGraph.id
148 10979 : unregisterNodeState(node)
149 10979 : useWidgetValueStore().releaseNodeWidgets(rootGraphId, node.id, {
150 : discardValues: mode === 'discard-values'
151 : })
152 10979 : releaseNodePreviewExposures(rootGraphId, node)
153 : }
154 :
155 : /**
156 : * Detaches every node a graph owns, including those inside the subgraph
157 : * definitions it holds. Used when a graph's nodes leave the stores without a
158 : * whole-bucket wipe: subgraph-definition removal, and clearing a graph that
159 : * shares its bucket with other graphs. The graph is going away, so its nodes'
160 : * widget values go with it — the same reach as the wipe a root graph performs.
161 : */
162 20 : export function detachAllNodesFromStores(
163 : graph: Pick<LGraph, '_nodes' | '_subgraphs' | 'rootGraph'>
164 : ): void {
165 20 : for (const node of graph._nodes) {
166 36 : detachNodeFromStores(graph, node, 'discard-values')
167 : }
168 20 : for (const subgraph of graph._subgraphs.values()) {
169 0 : detachAllNodesFromStores(subgraph)
170 : }
171 : }
|