Line data Source code
1 : import {
2 : linksMap,
3 : nodesMap,
4 : OPAQUE_WIDGETS_KEY
5 : } from '@comfyorg/comfy-multi-player'
6 : import * as Y from 'yjs'
7 :
8 : import { definitionsObserverRoot } from './agentSubgraphDefinitions'
9 : import type { FrameChanges, NodeChange } from './liveGraphApplier'
10 : import { readDocGroups, SYNCED_NODE_FIELDS } from './liveGraphApplier'
11 :
12 : /**
13 : * Collects what the Y observers on one follower document report between two
14 : * `take()` calls, in the shape the applier consumes. Observers fire
15 : * synchronously inside `applyRemoteUpdate`, so the frame that delivered an
16 : * update sees exactly that update's changes. Changes stay collected until a
17 : * graph is there to take them, so a frame delivered before the graph loads is
18 : * applied once it does.
19 : */
20 1 : export class DocChangeCollector {
21 : private readonly nodes = new Map<string, NodeChange>()
22 : private readonly widgets = new Map<string, Set<string> | 'all'>()
23 : private readonly resyncNodes = new Map<string, Set<string>>()
24 : private readonly links = new Set<string>()
25 : private readonly interiors = new Map<
26 : string,
27 : Map<string, Set<string> | 'all'>
28 : >()
29 : private readonly interiorDefinitions = new Set<string>()
30 : private readonly groups = new Set<string>()
31 : private readonly nodesMap: Y.Map<Y.Map<unknown>>
32 : private readonly linksMap: Y.Map<unknown>
33 : private readonly definitionsMap: Y.Map<unknown>
34 : private readonly metaMap: Y.Map<unknown>
35 :
36 : constructor(doc: Y.Doc) {
37 0 : this.nodesMap = nodesMap(doc)
38 0 : this.linksMap = linksMap(doc)
39 0 : this.definitionsMap = definitionsObserverRoot(doc)
40 0 : this.metaMap = doc.getMap<unknown>('meta')
41 0 : this.nodesMap.observeDeep(this.onNodesChanged)
42 0 : this.linksMap.observe(this.onLinksChanged)
43 0 : this.definitionsMap.observeDeep(this.onDefinitionsChanged)
44 0 : this.metaMap.observe(this.onMetaChanged)
45 : }
46 :
47 : /** Returns everything collected so far without clearing it. */
48 : peek(): FrameChanges {
49 0 : return {
50 : nodes: new Map(this.nodes),
51 : widgets: new Map(this.widgets),
52 : resyncNodes: new Map(
53 : [...this.resyncNodes].map(([id, keys]) => [id, new Set(keys)])
54 : ),
55 : links: new Set(this.links),
56 : interiors: new Map(
57 : [...this.interiors].map(([definitionId, nodes]) => [
58 : definitionId,
59 : new Map(nodes)
60 : ])
61 : ),
62 : interiorDefinitions: new Set(this.interiorDefinitions),
63 : groups: new Set(this.groups)
64 : }
65 : }
66 :
67 : /** Returns and clears everything collected so far. */
68 : take(): FrameChanges {
69 0 : const changes = this.peek()
70 0 : this.discard()
71 0 : return changes
72 : }
73 :
74 : discard(): void {
75 0 : this.nodes.clear()
76 0 : this.widgets.clear()
77 0 : this.resyncNodes.clear()
78 0 : this.links.clear()
79 0 : this.interiors.clear()
80 0 : this.interiorDefinitions.clear()
81 0 : this.groups.clear()
82 : }
83 :
84 : destroy(): void {
85 0 : this.nodesMap.unobserveDeep(this.onNodesChanged)
86 0 : this.linksMap.unobserve(this.onLinksChanged)
87 0 : this.definitionsMap.unobserveDeep(this.onDefinitionsChanged)
88 0 : this.metaMap.unobserve(this.onMetaChanged)
89 0 : this.discard()
90 : }
91 :
92 : private markInterior(
93 : definitionId: string,
94 : nodeId: string,
95 : names: ReadonlySet<string> | 'all'
96 : ): void {
97 0 : let nodes = this.interiors.get(definitionId)
98 0 : if (!nodes) {
99 0 : nodes = new Map()
100 0 : this.interiors.set(definitionId, nodes)
101 : }
102 0 : if (names === 'all') {
103 0 : if (nodes.get(nodeId) === undefined) nodes.set(nodeId, 'all')
104 0 : return
105 : }
106 0 : const current = nodes.get(nodeId)
107 0 : const merged = current === 'all' || !current ? new Set<string>() : current
108 0 : for (const name of names) merged.add(name)
109 0 : nodes.set(nodeId, merged)
110 : }
111 :
112 : private readonly onDefinitionsChanged = (
113 : events: Y.YEvent<Y.AbstractType<unknown>>[]
114 : ): void => {
115 0 : for (const event of events) this.collectDefinitionEvent(event)
116 : }
117 :
118 : private collectDefinitionEvent(
119 : event: Y.YEvent<Y.AbstractType<unknown>>
120 : ): void {
121 0 : const path = event.path.map(String)
122 0 : const nodesIndex = path.lastIndexOf('nodes')
123 0 : if (nodesIndex >= 1 && path.length > nodesIndex + 1) {
124 0 : this.collectInteriorEvent(event, path, nodesIndex)
125 0 : return
126 : }
127 0 : this.collectDefinitionResync(event, path)
128 : }
129 :
130 : private collectInteriorEvent(
131 : event: Y.YEvent<Y.AbstractType<unknown>>,
132 : path: string[],
133 : nodesIndex: number
134 : ): void {
135 0 : const definitionId = path[nodesIndex - 1]
136 0 : const nodeId = path[nodesIndex + 1]
137 : const tail = path[nodesIndex + 2]
138 0 : if (tail === 'widgets' && event instanceof Y.YMapEvent) {
139 0 : this.markInterior(definitionId, nodeId, new Set(event.keysChanged))
140 0 : return
141 : }
142 0 : this.markInterior(definitionId, nodeId, 'all')
143 : }
144 :
145 : private collectDefinitionResync(
146 : event: Y.YEvent<Y.AbstractType<unknown>>,
147 : path: string[]
148 : ): void {
149 0 : const subgraphsIndex = path.lastIndexOf('subgraphs')
150 0 : const definitionId =
151 : subgraphsIndex >= 0 && path.length > subgraphsIndex + 1
152 : ? path[subgraphsIndex + 1]
153 : : path[0]
154 0 : if (definitionId) this.interiorDefinitions.add(definitionId)
155 0 : if (event.target === this.definitionsMap && event instanceof Y.YMapEvent) {
156 0 : for (const id of event.keysChanged) this.interiorDefinitions.add(id)
157 : }
158 : }
159 :
160 : private readonly onNodesChanged = (
161 : events: Y.YEvent<Y.AbstractType<unknown>>[]
162 : ): void => {
163 0 : for (const event of events) {
164 0 : if (event instanceof Y.YArrayEvent) this.onOpaqueWidgetsChanged(event)
165 0 : else if (event instanceof Y.YMapEvent) this.onNodeMapChanged(event)
166 : }
167 : }
168 :
169 : private onNodeMapChanged(event: Y.YMapEvent<unknown>): void {
170 0 : if (event.target === this.nodesMap) this.onNodeEntriesChanged(event)
171 0 : else if (event.path.length === 1) this.onNodeFieldsChanged(event)
172 0 : else if (event.path[1] === 'widgets') this.onNamedWidgetsChanged(event)
173 0 : else if (event.path[1] === 'flags')
174 0 : this.markFields(
175 : String(event.path[0]),
176 : [...event.keysChanged].map((key) => `flags.${key}`)
177 : )
178 : }
179 :
180 : private onOpaqueWidgetsChanged(event: Y.YArrayEvent<unknown>): void {
181 0 : if (event.path.length === 2 && event.path[1] === OPAQUE_WIDGETS_KEY)
182 0 : this.widgets.set(String(event.path[0]), 'all')
183 : }
184 :
185 : private onNodeEntriesChanged(event: Y.YMapEvent<unknown>): void {
186 0 : for (const [id, change] of event.changes.keys)
187 0 : this.nodes.set(id, change.action)
188 : }
189 :
190 : private onNamedWidgetsChanged(event: Y.YMapEvent<unknown>): void {
191 0 : const id = String(event.path[0])
192 0 : const current = this.widgets.get(id)
193 0 : if (current === 'all') return
194 0 : const names = current ?? new Set<string>()
195 0 : for (const name of event.keysChanged) names.add(name)
196 0 : this.widgets.set(id, names)
197 : }
198 :
199 : private onNodeFieldsChanged(event: Y.YMapEvent<unknown>): void {
200 0 : const id = String(event.path[0])
201 0 : if (
202 : event.keysChanged.has('widgets') ||
203 : event.keysChanged.has(OPAQUE_WIDGETS_KEY)
204 : )
205 0 : this.widgets.set(id, 'all')
206 0 : this.markFields(id, event.keysChanged)
207 : }
208 :
209 : private markFields(id: string, keys: Iterable<string>): void {
210 0 : const synced = [...keys].filter(
211 : (key) => SYNCED_NODE_FIELDS.has(key) || key.startsWith('flags.')
212 : )
213 0 : if (synced.length === 0) return
214 0 : const current = this.resyncNodes.get(id) ?? new Set<string>()
215 0 : for (const key of synced) current.add(key)
216 0 : this.resyncNodes.set(id, current)
217 : }
218 :
219 : private readonly onLinksChanged = (event: Y.YMapEvent<unknown>): void => {
220 0 : for (const id of event.keysChanged) this.links.add(id)
221 : }
222 :
223 : private readonly onMetaChanged = (event: Y.YMapEvent<unknown>): void => {
224 0 : const change = event.changes.keys.get('groups')
225 0 : if (!change) return
226 0 : const before = readDocGroups(change.oldValue)
227 0 : const after = readDocGroups(this.metaMap.get('groups'))
228 0 : for (const id of before.keys()) if (!after.has(id)) this.groups.add(id)
229 0 : for (const id of after.keys()) if (!before.has(id)) this.groups.add(id)
230 : }
231 : }
|