Line data Source code
1 : import { readonly, shallowRef } from 'vue'
2 :
3 : import { reportError } from '@comfyorg/telemetry/reportError'
4 : import type { GraphScope } from '@comfyorg/graph-model/graphScopeId'
5 : import type { NodeId } from '@comfyorg/graph-model/nodeId'
6 :
7 : interface MinimapDecorationTarget extends GraphScope {
8 : readonly nodeId: NodeId
9 : }
10 :
11 : interface MinimapNodeDecoration {
12 : readonly target: MinimapDecorationTarget
13 : readonly enter?: 'pop'
14 : }
15 :
16 : export interface MinimapDecorationLayer {
17 : replace(rows: readonly MinimapNodeDecoration[]): void
18 : dispose(): void
19 : }
20 :
21 : export interface ResolvedMinimapNodeDecoration extends MinimapNodeDecoration {
22 : readonly enteredAt?: number
23 : }
24 :
25 : interface RegisteredLayer {
26 : readonly rows: Map<string, ResolvedMinimapNodeDecoration>
27 : disposed: boolean
28 : }
29 :
30 2355 : const layers = new Map<string, RegisteredLayer>()
31 2355 : const revision = shallowRef(0)
32 :
33 0 : function targetKey(target: MinimapDecorationTarget): string {
34 0 : return `${target.rootGraphId}:${target.owningGraphId}:${target.nodeId}`
35 : }
36 :
37 0 : function copyDecoration(
38 : row: MinimapNodeDecoration,
39 : enteredAt?: number
40 : ): ResolvedMinimapNodeDecoration {
41 0 : return {
42 : target: { ...row.target },
43 : enter: row.enter,
44 : enteredAt
45 : }
46 : }
47 :
48 1 : function invalidate(): void {
49 1 : revision.value += 1
50 : }
51 :
52 : /**
53 : * Registers transient, graph-scoped minimap decorations. Producers own which
54 : * nodes are decorated and when the layer is disposed; the minimap exclusively
55 : * owns projection, palette, paint order, and animation scheduling.
56 : */
57 1 : export function registerMinimapDecorationLayer(
58 : id: string
59 : ): MinimapDecorationLayer {
60 1 : const stale = layers.get(id)
61 1 : if (stale) {
62 : // Still reported: one id held twice at once is a producer bug worth seeing.
63 : // But the newest registrant takes the id over, because handing it back a
64 : // no-op layer left the *surviving* producer unable to paint anything for
65 : // the rest of the session - the outgoing one is the replaced party, so
66 : // keeping its layer kept the id claimed and the decoration dead.
67 0 : reportError(new Error(`Minimap decoration layer exists: ${id}`), {
68 : surface: 'graph',
69 : errorType: 'minimap_decoration_layer_duplicate'
70 : })
71 : // Marking it disposed is what makes the takeover safe: the outgoing
72 : // producer's own `dispose()` then returns early instead of deleting the
73 : // replacement's layer, and its `replace()` calls stop mutating anything.
74 0 : stale.disposed = true
75 : }
76 :
77 1 : const layer: RegisteredLayer = { rows: new Map(), disposed: false }
78 1 : layers.set(id, layer)
79 1 : if (stale) invalidate()
80 :
81 1 : return {
82 : replace(rows) {
83 1 : if (layer.disposed) return
84 1 : const next = new Map<string, ResolvedMinimapNodeDecoration>()
85 1 : const now = performance.now()
86 1 : for (const [index, row] of rows.entries()) {
87 0 : const key = targetKey(row.target)
88 0 : const previous = layer.rows.get(key)
89 0 : const enteredAt =
90 : row.enter === 'pop'
91 : ? (previous?.enteredAt ?? now + Math.min(index, 8) * 50)
92 : : undefined
93 0 : next.set(key, copyDecoration(row, enteredAt))
94 : }
95 1 : layer.rows.clear()
96 1 : for (const [key, row] of next) layer.rows.set(key, row)
97 1 : invalidate()
98 : },
99 : dispose() {
100 0 : if (layer.disposed) return
101 0 : layer.disposed = true
102 0 : layers.delete(id)
103 0 : invalidate()
104 : }
105 : }
106 : }
107 :
108 2355 : export const minimapDecorationRevision = readonly(revision)
109 :
110 8562 : export function getMinimapDecorations(
111 : scope: GraphScope
112 : ): readonly ResolvedMinimapNodeDecoration[] {
113 8562 : return [...layers.entries()]
114 : .sort(([left], [right]) => left.localeCompare(right))
115 : .flatMap(([, layer]) => [...layer.rows.values()])
116 : .filter(
117 : ({ target }) =>
118 : target.rootGraphId === scope.rootGraphId &&
119 : target.owningGraphId === scope.owningGraphId
120 : )
121 : .map((row) => copyDecoration(row, row.enteredAt))
122 : }
|