Line data Source code
1 : import { defineStore } from 'pinia'
2 : import { reactive } from 'vue'
3 :
4 : import { reportError } from '@comfyorg/telemetry/reportError'
5 : import type {
6 : GraphScope,
7 : OwningGraphId,
8 : RootGraphId
9 : } from '@comfyorg/graph-model/graphScopeId'
10 : import type { LinkId } from '@comfyorg/graph-model/linkId'
11 : import type { LinkPresentation } from '@comfyorg/graph-model/linkPresentation'
12 :
13 2356 : const EMPTY_LINK_IDS: readonly LinkId[] = []
14 :
15 3054 : function isDefaultLinkPresentation(
16 : hidden: boolean | undefined,
17 : label: string | undefined
18 : ): boolean {
19 3054 : return !hidden && label === undefined
20 : }
21 :
22 3054 : function compactLinkPresentation(
23 : hidden: boolean | undefined,
24 : label: string | undefined
25 : ): LinkPresentation | undefined {
26 3054 : if (isDefaultLinkPresentation(hidden, label)) return undefined
27 3054 : return {
28 : ...(hidden && { hidden: true }),
29 : ...(label !== undefined && { label })
30 : }
31 : }
32 :
33 : interface OwnedLinkPresentation {
34 : graphId: OwningGraphId
35 : presentation: LinkPresentation
36 : }
37 :
38 : interface RootPresentationBucket {
39 : byId: Map<LinkId, OwnedLinkPresentation>
40 : idsByOwner: Map<OwningGraphId, Set<LinkId>>
41 : }
42 :
43 : /**
44 : * Link presentation store, partitioned by root graph and keyed by link id —
45 : * the durable sidecar of the link topology store for non-topology link state
46 : * (`extra.linkPresentation` in the wire format). Entries exist only for links
47 : * with non-default presentation, so the store's contents are exactly the
48 : * serialization set.
49 : */
50 2356 : export const useLinkPresentationStore = defineStore('linkPresentation', () => {
51 2355 : const roots = reactive(new Map<RootGraphId, RootPresentationBucket>())
52 :
53 12 : function createRootBucket(rootGraphId: RootGraphId): RootPresentationBucket {
54 12 : const created = reactive<RootPresentationBucket>({
55 : byId: new Map(),
56 : idsByOwner: new Map()
57 : })
58 12 : roots.set(rootGraphId, created)
59 12 : return created
60 : }
61 :
62 22 : function index(
63 : bucket: RootPresentationBucket,
64 : entry: OwnedLinkPresentation,
65 : linkId: LinkId
66 : ): void {
67 22 : const ownerIds = bucket.idsByOwner.get(entry.graphId)
68 22 : if (ownerIds) ownerIds.add(linkId)
69 12 : else bucket.idsByOwner.set(entry.graphId, reactive(new Set([linkId])))
70 : }
71 :
72 9 : function displace(
73 : rootGraphId: RootGraphId,
74 : bucket: RootPresentationBucket,
75 : linkId: LinkId,
76 : graphId: OwningGraphId
77 : ): void {
78 9 : bucket.byId.delete(linkId)
79 9 : const ownerIds = bucket.idsByOwner.get(graphId)
80 9 : ownerIds?.delete(linkId)
81 9 : if (ownerIds?.size === 0) bucket.idsByOwner.delete(graphId)
82 9 : if (bucket.byId.size === 0) roots.delete(rootGraphId)
83 : }
84 :
85 : /** First writer owns the entry; a different owner cannot overwrite it. */
86 3054 : function patch(
87 : scope: GraphScope,
88 : linkId: LinkId,
89 : partial: LinkPresentation
90 : ): void {
91 3054 : const bucket = roots.get(scope.rootGraphId)
92 3054 : const incumbent = bucket?.byId.get(linkId)
93 3054 : if (incumbent && incumbent.graphId !== scope.owningGraphId) {
94 0 : reportError(new Error('Link presentation ownership conflict'), {
95 : surface: 'platform',
96 : errorType: 'link_presentation_ownership_conflict',
97 : context: {
98 : linkId,
99 : incumbentGraphId: incumbent.graphId,
100 : requestingGraphId: scope.owningGraphId
101 : }
102 : })
103 0 : return
104 : }
105 : const hidden =
106 : 'hidden' in partial ? partial.hidden : incumbent?.presentation.hidden
107 : const label =
108 : 'label' in partial ? partial.label : incumbent?.presentation.label
109 3054 : const compacted = compactLinkPresentation(hidden, label)
110 3054 : if (!compacted) {
111 3032 : if (bucket && incumbent) {
112 5 : displace(scope.rootGraphId, bucket, linkId, incumbent.graphId)
113 : }
114 3032 : return
115 : }
116 3054 : const target = bucket ?? createRootBucket(scope.rootGraphId)
117 3054 : const entry = { graphId: scope.owningGraphId, presentation: compacted }
118 3054 : target.byId.set(linkId, entry)
119 3054 : index(target, entry, linkId)
120 : }
121 :
122 : /** For stashing presentation across a transfer. */
123 14526 : function take(
124 : scope: GraphScope,
125 : linkId: LinkId
126 : ): LinkPresentation | undefined {
127 14526 : const bucket = roots.get(scope.rootGraphId)
128 14526 : const entry = bucket?.byId.get(linkId)
129 14526 : if (!bucket || !entry || entry.graphId !== scope.owningGraphId) return
130 4 : displace(scope.rootGraphId, bucket, linkId, entry.graphId)
131 4 : return entry.presentation
132 : }
133 :
134 232249 : function getPresentation(
135 : scope: GraphScope,
136 : linkId: LinkId
137 : ): Readonly<LinkPresentation> | undefined {
138 232249 : const entry = roots.get(scope.rootGraphId)?.byId.get(linkId)
139 232249 : return entry?.graphId === scope.owningGraphId
140 : ? entry.presentation
141 : : undefined
142 : }
143 :
144 31150 : function graphHiddenLinkIds(scope: GraphScope): readonly LinkId[] {
145 31150 : const bucket = roots.get(scope.rootGraphId)
146 31150 : const ownerIds = bucket?.idsByOwner.get(scope.owningGraphId)
147 31150 : if (!bucket || !ownerIds) return EMPTY_LINK_IDS
148 227 : return [...ownerIds].filter(
149 : (linkId) => bucket.byId.get(linkId)?.presentation.hidden
150 : )
151 : }
152 :
153 11709 : function clearGraph(rootGraphId: RootGraphId): void {
154 11709 : roots.delete(rootGraphId)
155 : }
156 :
157 767 : function clearOwner(scope: GraphScope): void {
158 767 : const bucket = roots.get(scope.rootGraphId)
159 767 : const ownerIds = bucket?.idsByOwner.get(scope.owningGraphId)
160 767 : if (!bucket || !ownerIds) return
161 0 : for (const linkId of Array.from(ownerIds)) {
162 0 : displace(scope.rootGraphId, bucket, linkId, scope.owningGraphId)
163 : }
164 : }
165 :
166 2355 : return {
167 : patch,
168 : take,
169 : getPresentation,
170 : graphHiddenLinkIds,
171 : clearGraph,
172 : clearOwner
173 : }
174 : })
|