Line data Source code
1 : import { defineStore } from 'pinia'
2 : import { reactive, toRaw } from 'vue'
3 :
4 : import {
5 : toOwningGraphId,
6 : toRootGraphId
7 : } from '@comfyorg/graph-model/graphScopeId'
8 : import type {
9 : GraphScope,
10 : OwningGraphId,
11 : RootGraphId
12 : } from '@comfyorg/graph-model/graphScopeId'
13 : import type { NodeState } from '@comfyorg/graph-model/nodeState'
14 : import type { NodeId } from '@comfyorg/graph-model/nodeId'
15 : import type { UUID } from '@/utils/uuid'
16 :
17 : /**
18 : * One {@link NodeState} per node in a root-flat, owner-indexed bucket.
19 : * See docs/architecture/node-data-store.md.
20 : */
21 2356 : export const useNodeDataStore = defineStore('nodeData', () => {
22 : interface RootNodeBucket {
23 : byId: Map<NodeId, NodeState>
24 : idsByOwner: Map<OwningGraphId, Set<NodeId>>
25 : }
26 :
27 2355 : const roots = reactive(new Map<RootGraphId, RootNodeBucket>())
28 :
29 4056 : function rootBucket(rootGraphId: RootGraphId): RootNodeBucket {
30 4056 : const existing = roots.get(rootGraphId)
31 4056 : if (existing) return existing
32 4056 : const created = reactive<RootNodeBucket>({
33 : byId: new Map(),
34 : idsByOwner: new Map()
35 : })
36 4056 : roots.set(rootGraphId, created)
37 4056 : return created
38 : }
39 :
40 24904 : function scope(rootGraphId: UUID, owningGraphId: UUID) {
41 24904 : return {
42 : rootGraphId: toRootGraphId(rootGraphId),
43 : owningGraphId: toOwningGraphId(owningGraphId)
44 : }
45 : }
46 :
47 : /**
48 : * @returns The registered state. Re-registering the same raw state under the
49 : * same owner returns the incumbent; `undefined` means a distinct state
50 : * occupies the node ID.
51 : */
52 23237 : function registerNode(
53 : graphScope: GraphScope,
54 : state: NodeState
55 : ): NodeState | undefined {
56 23237 : const existingBucket = roots.get(graphScope.rootGraphId)
57 23237 : const incumbent = existingBucket?.byId.get(state.id)
58 23237 : if (
59 : incumbent &&
60 : toRaw(incumbent) === toRaw(state) &&
61 : incumbent.graphId === graphScope.owningGraphId
62 : )
63 0 : return incumbent
64 23237 : if (incumbent) return undefined
65 23237 : const bucket = existingBucket ?? rootBucket(graphScope.rootGraphId)
66 23237 : const registered: NodeState = reactive(
67 : Object.assign(state, { graphId: graphScope.owningGraphId })
68 : )
69 23237 : bucket.byId.set(registered.id, registered)
70 23237 : const ownerIds = bucket.idsByOwner.get(graphScope.owningGraphId)
71 23237 : if (ownerIds) ownerIds.add(registered.id)
72 : else
73 4520 : bucket.idsByOwner.set(
74 : graphScope.owningGraphId,
75 : reactive(new Set([registered.id]))
76 : )
77 23236 : return registered
78 : }
79 :
80 24904 : function getGraphNodesFor(
81 : rootGraphId: UUID,
82 : owningGraphId: UUID
83 : ): NodeState[] {
84 24904 : const graphScope = scope(rootGraphId, owningGraphId)
85 24904 : const bucket = roots.get(graphScope.rootGraphId)
86 24904 : const ids = bucket?.idsByOwner.get(graphScope.owningGraphId)
87 24904 : if (!bucket || !ids) return []
88 21415 : return [...ids].flatMap((id) => {
89 107291 : const state = bucket.byId.get(id)
90 107291 : return state ? [state] : []
91 : })
92 : }
93 :
94 57 : function ownsNode(graphScope: GraphScope, state: NodeState): boolean {
95 57 : const registered = roots.get(graphScope.rootGraphId)?.byId.get(state.id)
96 57 : return (
97 : registered?.graphId === graphScope.owningGraphId &&
98 : toRaw(registered) === toRaw(state)
99 : )
100 : }
101 :
102 10979 : function deleteNode(graphScope: GraphScope, state: NodeState): boolean {
103 10979 : const bucket = roots.get(graphScope.rootGraphId)
104 10979 : const registered = bucket?.byId.get(state.id)
105 10979 : if (
106 : !bucket ||
107 : registered?.graphId !== graphScope.owningGraphId ||
108 : toRaw(registered) !== toRaw(state)
109 : )
110 0 : return false
111 10979 : bucket.byId.delete(state.id)
112 10979 : const ownerIds = bucket.idsByOwner.get(graphScope.owningGraphId)
113 10979 : ownerIds?.delete(state.id)
114 10979 : if (ownerIds?.size === 0) bucket.idsByOwner.delete(graphScope.owningGraphId)
115 10979 : if (bucket.byId.size === 0) roots.delete(graphScope.rootGraphId)
116 10979 : return true
117 : }
118 :
119 11709 : function clearGraph(rootGraphId: UUID): void {
120 11709 : roots.delete(toRootGraphId(rootGraphId))
121 : }
122 :
123 767 : function clearOwner(graphScope: GraphScope): void {
124 767 : const bucket = roots.get(graphScope.rootGraphId)
125 767 : const ids = bucket?.idsByOwner.get(graphScope.owningGraphId)
126 767 : if (!bucket || !ids) return
127 0 : for (const id of ids) bucket.byId.delete(id)
128 0 : bucket.idsByOwner.delete(graphScope.owningGraphId)
129 0 : if (bucket.byId.size === 0) roots.delete(graphScope.rootGraphId)
130 : }
131 :
132 2355 : return {
133 : clearOwner,
134 : clearGraph,
135 : deleteNode,
136 : getGraphNodesFor,
137 : ownsNode,
138 : registerNode
139 : }
140 : })
|