Line data Source code
1 : import { nodesMap } from '@comfyorg/comfy-multi-player'
2 : import type { Op } from '@comfyorg/comfy-multi-player'
3 : import type * as Y from 'yjs'
4 :
5 : import type { FrameChanges, NodeChange } from './liveGraphApplier'
6 : import { docLinksIncident } from './liveGraphApplier'
7 :
8 : /**
9 : * The live-graph changes that put a host-rejected human batch back the way
10 : * the document has it. Each op names exactly the node, widget, link, or field
11 : * register it claimed; replaying those registers from the document undoes the
12 : * live edit without touching anything else. A node the document lacks is
13 : * removed live along with its links; one it holds is restored with the
14 : * document links incident to it. Interior (`path`) writes are skipped: a
15 : * rejected op never reached the document, so the live interior already
16 : * holds the document value and there is nothing to undo here.
17 : */
18 0 : export function changesForRejectedOps(
19 : doc: Y.Doc,
20 : ops: readonly Op[]
21 : ): FrameChanges {
22 0 : const nodes = new Map<string, NodeChange>()
23 0 : const widgets = new Map<string, Set<string>>()
24 0 : const resyncNodes = new Map<string, Set<string>>()
25 0 : const links = new Set<string>()
26 0 : const docNodes = nodesMap(doc)
27 0 : const settleNode = (id: string): void => {
28 0 : if (!docNodes.has(id)) {
29 0 : nodes.set(id, 'delete')
30 0 : return
31 : }
32 0 : nodes.set(id, 'add')
33 0 : for (const link of docLinksIncident(doc, id)) links.add(String(link.id))
34 : }
35 0 : for (const op of ops) {
36 0 : if ('path' in op && op.path != null) continue
37 0 : switch (op.op) {
38 : case 'add_node':
39 : case 'delete_node':
40 0 : settleNode(String(op.node_id))
41 0 : break
42 : case 'clear':
43 0 : for (const id of op.removed_nodes) settleNode(String(id))
44 0 : break
45 : case 'set_widget': {
46 0 : const id = String(op.node_id)
47 0 : const names = widgets.get(id) ?? new Set<string>()
48 0 : names.add(op.widget)
49 0 : widgets.set(id, names)
50 0 : break
51 : }
52 : case 'set_node_field': {
53 0 : const id = String(op.node_id)
54 0 : const keys = resyncNodes.get(id) ?? new Set<string>()
55 0 : keys.add(op.field)
56 0 : resyncNodes.set(id, keys)
57 0 : break
58 : }
59 : case 'connect':
60 : case 'disconnect':
61 0 : links.add(String(op.link_id))
62 : break
63 : default:
64 : break
65 : }
66 : }
67 0 : return { nodes, widgets, resyncNodes, links }
68 : }
|