@statelyai/flow 0.14.0 → 0.15.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -1
- package/dist/esm/index.d.ts +5 -2
- package/dist/esm/index.d.ts.map +1 -0
- package/dist/esm/index.js +19 -14
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/layout/dagre.d.ts +2 -1
- package/dist/esm/layout/dagre.d.ts.map +1 -0
- package/dist/esm/layout/dagre.js +2 -0
- package/dist/esm/layout/dagre.js.map +1 -0
- package/dist/esm/layout/elk.d.ts +2 -1
- package/dist/esm/layout/elk.d.ts.map +1 -0
- package/dist/esm/layout/elk.js +2 -0
- package/dist/esm/layout/elk.js.map +1 -0
- package/dist/esm/layout/stately.d.ts +2 -1
- package/dist/esm/layout/stately.d.ts.map +1 -0
- package/dist/esm/layout/stately.js +2 -0
- package/dist/esm/layout/stately.js.map +1 -0
- package/dist/esm/shared.d.ts +2 -1
- package/dist/esm/shared.d.ts.map +1 -0
- package/dist/esm/shared.js +2 -0
- package/dist/esm/shared.js.map +1 -0
- package/package.json +4 -3
- package/src/__fixtures__/nested.ts +106 -0
- package/src/appearances.test.ts +428 -0
- package/src/appearances.ts +144 -0
- package/src/authoritative-route.ts +14 -0
- package/src/changeSet.test.ts +132 -0
- package/src/changeSet.ts +337 -0
- package/src/config.ts +41 -0
- package/src/configure.test.ts +212 -0
- package/src/connection-lifetime.ts +1 -0
- package/src/connectionPreview.test.ts +102 -0
- package/src/connectionPreview.ts +82 -0
- package/src/content.ts +203 -0
- package/src/createFlow.fit-frame.test.ts +256 -0
- package/src/createFlow.modern.test.ts +743 -0
- package/src/createFlow.test.ts +632 -0
- package/src/createFlow.ts +3463 -0
- package/src/createFlow.view.test.ts +212 -0
- package/src/createFlow.viewport-controls.test.ts +221 -0
- package/src/edge-geometry.ts +318 -0
- package/src/engine.cache.test.ts +728 -0
- package/src/engine.hierarchy.test.ts +1023 -0
- package/src/engine.test.ts +1965 -0
- package/src/engine.ts +3385 -0
- package/src/entity-changes.test.ts +309 -0
- package/src/entity-changes.ts +395 -0
- package/src/examples/flow-based-math.test.ts +25 -0
- package/src/examples/flow-based-math.ts +210 -0
- package/src/external-edit.test.ts +337 -0
- package/src/external-edit.ts +358 -0
- package/src/factories.test.ts +104 -0
- package/src/factories.ts +70 -0
- package/src/geometry.ts +13 -0
- package/src/graph-index.test.ts +56 -0
- package/src/graph-index.ts +194 -0
- package/src/hierarchy.test.ts +175 -0
- package/src/hierarchy.ts +330 -0
- package/src/highlights.test.ts +130 -0
- package/src/index.ts +469 -0
- package/src/input/index.ts +615 -0
- package/src/input/input.test.ts +2577 -0
- package/src/input/keymap.ts +111 -0
- package/src/input/modes.ts +1715 -0
- package/src/input/reparent.test.ts +214 -0
- package/src/layout/dagre.ts +138 -0
- package/src/layout/elk.ts +1382 -0
- package/src/layout/index.ts +12 -0
- package/src/layout/layout.test.ts +1248 -0
- package/src/layout/shared.ts +260 -0
- package/src/layout/stately-globals.d.ts +9 -0
- package/src/layout/stately.test.ts +246 -0
- package/src/layout/stately.ts +528 -0
- package/src/layout-animation.test.ts +223 -0
- package/src/layout-contract.test.ts +414 -0
- package/src/marker.test.ts +23 -0
- package/src/marker.ts +23 -0
- package/src/measurement-anchor.test.ts +232 -0
- package/src/minimap.test.ts +96 -0
- package/src/minimap.ts +137 -0
- package/src/nested-fixture.test.ts +178 -0
- package/src/port-measurement.test.ts +27 -0
- package/src/port-measurements.test.ts +187 -0
- package/src/profile.ts +124 -0
- package/src/projections.test.ts +103 -0
- package/src/projections.ts +295 -0
- package/src/render-invalidation.test.ts +62 -0
- package/src/render-invalidation.ts +55 -0
- package/src/renderer-geometry.test.ts +356 -0
- package/src/renderer-transactions.test.ts +234 -0
- package/src/routing/avoid.ts +417 -0
- package/src/routing/index.test.ts +361 -0
- package/src/routing/index.ts +366 -0
- package/src/routing/readTracking.test.ts +103 -0
- package/src/routing/readTracking.ts +233 -0
- package/src/selection-commands.test.ts +355 -0
- package/src/serialize.test.ts +167 -0
- package/src/serialize.ts +254 -0
- package/src/shapes/index.test.ts +45 -0
- package/src/shapes/index.ts +142 -0
- package/src/snap.test.ts +123 -0
- package/src/snap.ts +395 -0
- package/src/spatial.test.ts +676 -0
- package/src/spatial.ts +457 -0
- package/src/store/index.ts +4121 -0
- package/src/store.test.ts +2903 -0
- package/src/toolbar.test.ts +31 -0
- package/src/toolbar.ts +62 -0
- package/src/types.test.ts +15 -0
- package/src/types.ts +487 -0
- package/src/validation.ts +558 -0
- package/src/view.ts +172 -0
- package/src/viewport/animate.ts +285 -0
- package/src/viewport/index.ts +296 -0
- package/src/viewport.test.ts +569 -0
|
@@ -0,0 +1,309 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest';
|
|
2
|
+
import { createFlowStore } from './store';
|
|
3
|
+
import { createFlowEngine } from './engine';
|
|
4
|
+
import { createEntityChangeNotifier } from './entity-changes';
|
|
5
|
+
import { createNode, createEdge, createGraph } from './factories';
|
|
6
|
+
import { straightRouter } from './routing';
|
|
7
|
+
|
|
8
|
+
// Mechanism tests for the entity-change notifier: one store subscription,
|
|
9
|
+
// O(changed) dispatch. This is what lets thousands of mounted entity views
|
|
10
|
+
// coexist without thousands of per-event selector evaluations.
|
|
11
|
+
|
|
12
|
+
function makeRig() {
|
|
13
|
+
const store = createFlowStore(
|
|
14
|
+
createGraph({
|
|
15
|
+
nodes: [
|
|
16
|
+
createNode({ id: 'a', x: 0, y: 0 }),
|
|
17
|
+
createNode({ id: 'b', x: 300, y: 0 }),
|
|
18
|
+
createNode({ id: 'c', x: 600, y: 0 }),
|
|
19
|
+
],
|
|
20
|
+
edges: [
|
|
21
|
+
createEdge({ id: 'ab', sourceId: 'a', targetId: 'b' }),
|
|
22
|
+
createEdge({
|
|
23
|
+
id: 'bc',
|
|
24
|
+
sourceId: 'b',
|
|
25
|
+
targetId: 'c',
|
|
26
|
+
label: { x: 450, y: 25, width: 60, height: 20 },
|
|
27
|
+
}),
|
|
28
|
+
],
|
|
29
|
+
}),
|
|
30
|
+
);
|
|
31
|
+
const notifier = createEntityChangeNotifier(store);
|
|
32
|
+
const counts: Record<string, number> = {};
|
|
33
|
+
for (const id of ['a', 'b', 'c', 'ab', 'bc']) {
|
|
34
|
+
counts[id] = 0;
|
|
35
|
+
notifier.subscribe(id, () => {
|
|
36
|
+
counts[id]++;
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
const resetCounts = () => {
|
|
40
|
+
for (const id of Object.keys(counts)) counts[id] = 0;
|
|
41
|
+
};
|
|
42
|
+
return { store, notifier, counts, resetCounts };
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
describe('createEntityChangeNotifier', () => {
|
|
46
|
+
it('dragMove notifies only the moved node and its incident edges', () => {
|
|
47
|
+
const { store, counts, resetCounts } = makeRig();
|
|
48
|
+
store.trigger.select({ ids: ['a'] });
|
|
49
|
+
store.trigger.dragStart({ id: 'a' });
|
|
50
|
+
resetCounts();
|
|
51
|
+
|
|
52
|
+
store.trigger.dragMove({ dx: 10, dy: 0 });
|
|
53
|
+
|
|
54
|
+
expect(counts.a).toBe(1);
|
|
55
|
+
expect(counts.ab).toBe(1);
|
|
56
|
+
expect(counts.b).toBe(0);
|
|
57
|
+
expect(counts.c).toBe(0);
|
|
58
|
+
expect(counts.bc).toBe(0);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it('selection changes notify exactly the affected ids', () => {
|
|
62
|
+
const { store, counts, resetCounts } = makeRig();
|
|
63
|
+
store.trigger.select({ ids: ['a'] });
|
|
64
|
+
resetCounts();
|
|
65
|
+
|
|
66
|
+
store.trigger.select({ ids: ['b'] }); // a deselected, b selected
|
|
67
|
+
expect(counts.a).toBe(1);
|
|
68
|
+
expect(counts.b).toBe(1);
|
|
69
|
+
expect(counts.c).toBe(0);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it('a config revision bump (runtime reconfigure) notifies every entity', () => {
|
|
73
|
+
const { store, counts, resetCounts } = makeRig();
|
|
74
|
+
resetCounts();
|
|
75
|
+
|
|
76
|
+
// Swapping engine options (e.g. the edge router) bumps configRevision
|
|
77
|
+
// without touching the graph; every entity's derived render may change.
|
|
78
|
+
store.trigger.bumpConfigRevision();
|
|
79
|
+
|
|
80
|
+
expect(counts.a).toBe(1);
|
|
81
|
+
expect(counts.b).toBe(1);
|
|
82
|
+
expect(counts.c).toBe(1);
|
|
83
|
+
expect(counts.ab).toBe(1);
|
|
84
|
+
expect(counts.bc).toBe(1);
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it('focus changes notify the previous and next focused entity', () => {
|
|
88
|
+
const { store, counts, resetCounts } = makeRig();
|
|
89
|
+
store.trigger.focus({ entityId: 'a' });
|
|
90
|
+
expect(counts.a).toBe(1);
|
|
91
|
+
resetCounts();
|
|
92
|
+
|
|
93
|
+
store.trigger.focus({ entityId: 'b' });
|
|
94
|
+
expect(counts.a).toBe(1);
|
|
95
|
+
expect(counts.b).toBe(1);
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it('updateNodeData notifies that node', () => {
|
|
99
|
+
const { store, counts, resetCounts } = makeRig();
|
|
100
|
+
resetCounts();
|
|
101
|
+
store.trigger.updateNodeData({ id: 'b', data: { label: 'hi' } });
|
|
102
|
+
expect(counts.b).toBe(1);
|
|
103
|
+
expect(counts.c).toBe(0);
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it('deleteNode notifies the node and its incident edges', () => {
|
|
107
|
+
const { store, counts, resetCounts } = makeRig();
|
|
108
|
+
resetCounts();
|
|
109
|
+
store.trigger.deleteNode({ id: 'b' });
|
|
110
|
+
expect(counts.b).toBeGreaterThan(0);
|
|
111
|
+
expect(counts.ab).toBeGreaterThan(0);
|
|
112
|
+
expect(counts.bc).toBeGreaterThan(0);
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it('edge label drags notify only that edge', () => {
|
|
116
|
+
const { store, counts, resetCounts } = makeRig();
|
|
117
|
+
store.trigger.select({ ids: ['bc'] });
|
|
118
|
+
store.trigger.dragStart({ id: 'bc' });
|
|
119
|
+
resetCounts();
|
|
120
|
+
|
|
121
|
+
store.trigger.dragMove({ dx: 5, dy: 5 });
|
|
122
|
+
expect(counts.bc).toBe(1);
|
|
123
|
+
expect(counts.ab).toBe(0);
|
|
124
|
+
expect(counts.a).toBe(0);
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
it('edge label drags notify edges that route from that label', () => {
|
|
128
|
+
const store = createFlowStore(
|
|
129
|
+
createGraph({
|
|
130
|
+
nodes: [
|
|
131
|
+
createNode({ id: 'a', x: 0, y: 40, width: 60, height: 40 }),
|
|
132
|
+
createNode({ id: 'b', x: 300, y: 40, width: 60, height: 40 }),
|
|
133
|
+
],
|
|
134
|
+
edges: [
|
|
135
|
+
createEdge({
|
|
136
|
+
id: 'branch',
|
|
137
|
+
sourceId: 'a',
|
|
138
|
+
targetId: 'a',
|
|
139
|
+
labelAnchor: 'fixed',
|
|
140
|
+
label: { x: 100, y: 40, width: 40, height: 20 },
|
|
141
|
+
}),
|
|
142
|
+
createEdge<{ branchFrom?: string }>({
|
|
143
|
+
id: 'child',
|
|
144
|
+
sourceId: 'a',
|
|
145
|
+
targetId: 'b',
|
|
146
|
+
data: { branchFrom: 'branch' },
|
|
147
|
+
}),
|
|
148
|
+
],
|
|
149
|
+
}),
|
|
150
|
+
);
|
|
151
|
+
const engine = createFlowEngine(store, {
|
|
152
|
+
edgeRouter: straightRouter,
|
|
153
|
+
resolveEdgeSourceBounds: ({ edge, getEdgeLabelBounds }) => {
|
|
154
|
+
const branchFrom = (edge.data as { branchFrom?: string }).branchFrom;
|
|
155
|
+
return branchFrom ? getEdgeLabelBounds(branchFrom) : undefined;
|
|
156
|
+
},
|
|
157
|
+
});
|
|
158
|
+
engine.getAllEdgePaths();
|
|
159
|
+
const notifier = createEntityChangeNotifier(store, {
|
|
160
|
+
getEdgeDependents: engine.getEdgeSourceDependents,
|
|
161
|
+
});
|
|
162
|
+
const counts = { branch: 0, child: 0 };
|
|
163
|
+
notifier.subscribe('branch', () => counts.branch++);
|
|
164
|
+
notifier.subscribe('child', () => counts.child++);
|
|
165
|
+
|
|
166
|
+
store.trigger.select({ ids: ['branch'] });
|
|
167
|
+
store.trigger.dragStart({ id: 'branch' });
|
|
168
|
+
counts.branch = 0;
|
|
169
|
+
counts.child = 0;
|
|
170
|
+
|
|
171
|
+
store.trigger.dragMove({ dx: 5, dy: 5 });
|
|
172
|
+
|
|
173
|
+
expect(counts.branch).toBe(1);
|
|
174
|
+
expect(counts.child).toBe(1);
|
|
175
|
+
notifier.destroy();
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
it('drag gesture boundaries notify only the selected cohort', () => {
|
|
179
|
+
const { store, counts, resetCounts } = makeRig();
|
|
180
|
+
store.trigger.select({ ids: ['a'] });
|
|
181
|
+
resetCounts();
|
|
182
|
+
|
|
183
|
+
store.trigger.dragStart({ id: 'a' }); // idle → dragging
|
|
184
|
+
expect(counts.a).toBe(1);
|
|
185
|
+
expect(counts.b).toBe(0);
|
|
186
|
+
expect(counts.c).toBe(0);
|
|
187
|
+
expect(counts.ab).toBe(0);
|
|
188
|
+
expect(counts.bc).toBe(0);
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
it('drag end notifies the moved cohort and incident edges', () => {
|
|
192
|
+
const { store, counts, resetCounts } = makeRig();
|
|
193
|
+
store.trigger.select({ ids: ['a'] });
|
|
194
|
+
store.trigger.dragStart({ id: 'a' });
|
|
195
|
+
store.trigger.dragMove({ dx: 10, dy: 0 });
|
|
196
|
+
resetCounts();
|
|
197
|
+
|
|
198
|
+
store.trigger.dragEnd();
|
|
199
|
+
|
|
200
|
+
expect(counts.a).toBe(1);
|
|
201
|
+
expect(counts.ab).toBe(1);
|
|
202
|
+
expect(counts.b).toBe(0);
|
|
203
|
+
expect(counts.c).toBe(0);
|
|
204
|
+
expect(counts.bc).toBe(0);
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
it('snap-line drags notify the moved cohort, not fixed targets', () => {
|
|
208
|
+
const store = createFlowStore(
|
|
209
|
+
createGraph({
|
|
210
|
+
nodes: [createNode({ id: 'a' }), createNode({ id: 'b', x: 300 })],
|
|
211
|
+
edges: [],
|
|
212
|
+
}),
|
|
213
|
+
{ enableSnapLines: true },
|
|
214
|
+
);
|
|
215
|
+
const notifier = createEntityChangeNotifier(store);
|
|
216
|
+
let aNotified = 0;
|
|
217
|
+
let bNotified = 0;
|
|
218
|
+
notifier.subscribe('a', () => {
|
|
219
|
+
aNotified++;
|
|
220
|
+
});
|
|
221
|
+
notifier.subscribe('b', () => {
|
|
222
|
+
bNotified++;
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
store.trigger.select({ ids: ['a'] });
|
|
226
|
+
store.trigger.dragStart({ id: 'a' });
|
|
227
|
+
aNotified = 0;
|
|
228
|
+
bNotified = 0;
|
|
229
|
+
store.trigger.dragMove({ dx: 10, dy: 0 });
|
|
230
|
+
expect(aNotified).toBe(1);
|
|
231
|
+
expect(bNotified).toBe(0);
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
it('viewport pan transitions notify no entities', () => {
|
|
235
|
+
const { store, counts, resetCounts } = makeRig();
|
|
236
|
+
resetCounts();
|
|
237
|
+
|
|
238
|
+
store.trigger.panStart({ x: 0, y: 0 });
|
|
239
|
+
store.trigger.pan({ x: 40, y: 20 });
|
|
240
|
+
store.trigger.panEnd();
|
|
241
|
+
|
|
242
|
+
expect(counts).toEqual({ a: 0, b: 0, c: 0, ab: 0, bc: 0 });
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
it('unsubscribe stops notifications; destroy detaches from the store', () => {
|
|
246
|
+
const { store, notifier } = makeRig();
|
|
247
|
+
let calls = 0;
|
|
248
|
+
const unsubscribe = notifier.subscribe('a', () => {
|
|
249
|
+
calls++;
|
|
250
|
+
});
|
|
251
|
+
store.trigger.updateNodeData({ id: 'a', data: { x: 1 } });
|
|
252
|
+
expect(calls).toBe(1);
|
|
253
|
+
|
|
254
|
+
unsubscribe();
|
|
255
|
+
store.trigger.updateNodeData({ id: 'a', data: { x: 2 } });
|
|
256
|
+
expect(calls).toBe(1);
|
|
257
|
+
|
|
258
|
+
notifier.destroy();
|
|
259
|
+
store.trigger.updateNodeData({ id: 'a', data: { x: 3 } });
|
|
260
|
+
expect(calls).toBe(1);
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
it('node additions and removals notify the affected ids', () => {
|
|
264
|
+
const { store, notifier, counts, resetCounts } = makeRig();
|
|
265
|
+
let dNotified = 0;
|
|
266
|
+
notifier.subscribe('d', () => {
|
|
267
|
+
dNotified++;
|
|
268
|
+
});
|
|
269
|
+
resetCounts();
|
|
270
|
+
|
|
271
|
+
store.trigger.createNode({ node: createNode({ id: 'd', x: 900 }) });
|
|
272
|
+
expect(dNotified).toBe(1);
|
|
273
|
+
expect(counts.a).toBe(0);
|
|
274
|
+
});
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
describe('hierarchy-aware notifications', () => {
|
|
278
|
+
it('a child change notifies its ancestor groups (derived bounds re-render)', () => {
|
|
279
|
+
const store = createFlowStore(
|
|
280
|
+
createGraph({
|
|
281
|
+
nodes: [
|
|
282
|
+
createNode({ id: 'outer', width: 120, height: 40, group: { type: 'derived' } }),
|
|
283
|
+
{ ...createNode({ id: 'inner', width: 120, height: 40, group: { type: 'derived' } }), parentId: 'outer' },
|
|
284
|
+
{ ...createNode({ id: 'leaf', x: 100, y: 100 }), parentId: 'inner' },
|
|
285
|
+
createNode({ id: 'unrelated', x: 900 }),
|
|
286
|
+
],
|
|
287
|
+
edges: [createEdge({ id: 'ge', sourceId: 'outer', targetId: 'unrelated' })],
|
|
288
|
+
}),
|
|
289
|
+
);
|
|
290
|
+
const notifier = createEntityChangeNotifier(store);
|
|
291
|
+
const counts: Record<string, number> = { outer: 0, inner: 0, unrelated: 0, ge: 0 };
|
|
292
|
+
for (const id of Object.keys(counts)) {
|
|
293
|
+
notifier.subscribe(id, () => {
|
|
294
|
+
counts[id]++;
|
|
295
|
+
});
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
store.trigger.select({ ids: ['leaf'] });
|
|
299
|
+
store.trigger.dragStart({ id: 'leaf' });
|
|
300
|
+
for (const id of Object.keys(counts)) counts[id] = 0;
|
|
301
|
+
|
|
302
|
+
store.trigger.dragMove({ dx: 10, dy: 0 });
|
|
303
|
+
|
|
304
|
+
expect(counts.inner).toBe(1); // ancestor group bounds changed
|
|
305
|
+
expect(counts.outer).toBe(1); // ... recursively
|
|
306
|
+
expect(counts.ge).toBe(1); // edges attached to the ancestor too
|
|
307
|
+
expect(counts.unrelated).toBe(0);
|
|
308
|
+
});
|
|
309
|
+
});
|
|
@@ -0,0 +1,395 @@
|
|
|
1
|
+
import type { FlowStore, FlowStoreContext } from './store';
|
|
2
|
+
import type { FlowEdge } from './types';
|
|
3
|
+
import {
|
|
4
|
+
indexChildren,
|
|
5
|
+
indexEdgeAdjacency,
|
|
6
|
+
indexNodes,
|
|
7
|
+
indexEdges,
|
|
8
|
+
} from './graph-index';
|
|
9
|
+
|
|
10
|
+
// Entity-change notifier
|
|
11
|
+
//
|
|
12
|
+
// One store subscription that diffs consecutive snapshots and notifies
|
|
13
|
+
// per-entity listeners in O(changed) instead of O(listeners). Immer's
|
|
14
|
+
// structural sharing makes the diff cheap and precise: an entity's object
|
|
15
|
+
// identity changes exactly when the entity changed, so dragging one node
|
|
16
|
+
// notifies that node and its incident edges — not the other N thousand
|
|
17
|
+
// mounted entity views.
|
|
18
|
+
//
|
|
19
|
+
// Global fallbacks remain for mode/configuration/collapse changes. Gesture
|
|
20
|
+
// boundaries and snap-line drags instead notify the selected cohort, its
|
|
21
|
+
// hierarchy, and affected routes.
|
|
22
|
+
//
|
|
23
|
+
// Limitation: custom `visible` config predicates or engine `isVisible` hooks
|
|
24
|
+
// that read context fields other than `mode` are not tracked — views whose
|
|
25
|
+
// visibility depends on such predicates should subscribe via a plain store
|
|
26
|
+
// subscription instead.
|
|
27
|
+
|
|
28
|
+
export type FlowEntityChangeNotifier = {
|
|
29
|
+
/** Listen for changes that may affect the entity with this id. */
|
|
30
|
+
subscribe(id: string, listener: () => void): () => void;
|
|
31
|
+
/** Detach the underlying store subscription and drop all listeners. */
|
|
32
|
+
destroy(): void;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
export type FlowEntityChangeNotifierOptions = {
|
|
36
|
+
getEdgeDependents?: (edgeId: string) => Iterable<string>;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
function adjacency(edges: FlowEdge[]): Map<string, FlowEdge[]> {
|
|
40
|
+
return indexEdgeAdjacency(edges).byNode;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function computeDirty(
|
|
44
|
+
prev: FlowStoreContext,
|
|
45
|
+
next: FlowStoreContext,
|
|
46
|
+
options: FlowEntityChangeNotifierOptions,
|
|
47
|
+
dragDirtyIds?: ReadonlySet<string>,
|
|
48
|
+
): Set<string> | 'all' {
|
|
49
|
+
if (next.mode !== prev.mode) return 'all';
|
|
50
|
+
// Runtime reconfigure (e.g. swapping the edge router / engine options) bumps
|
|
51
|
+
// configRevision without touching the graph; it can change every entity's
|
|
52
|
+
// derived rendering, so re-notify all per-entity subscribers.
|
|
53
|
+
if (next.configRevision !== prev.configRevision) return 'all';
|
|
54
|
+
// Collapsing/expanding a group flips descendant visibility and reshapes the
|
|
55
|
+
// group's derived bounds (and reroutes boundary edges to proxies) without a
|
|
56
|
+
// graph mutation — re-notify every entity.
|
|
57
|
+
if (next.collapsedNodeIds !== prev.collapsedNodeIds) return 'all';
|
|
58
|
+
const dirty = new Set<string>();
|
|
59
|
+
const markEdge = (edgeId: string) => {
|
|
60
|
+
dirty.add(edgeId);
|
|
61
|
+
for (const dependentId of options.getEdgeDependents?.(edgeId) ?? []) {
|
|
62
|
+
dirty.add(dependentId);
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
if (next.routingRevision !== prev.routingRevision) {
|
|
66
|
+
for (const edge of next.graph.edges) dirty.add(edge.id);
|
|
67
|
+
}
|
|
68
|
+
const markIncidentEdges = (nodeId: string, edges: FlowEdge[]) => {
|
|
69
|
+
const incident = adjacency(edges).get(nodeId);
|
|
70
|
+
if (incident) for (const edge of incident) markEdge(edge.id);
|
|
71
|
+
};
|
|
72
|
+
// Group hierarchy: a changed node also changes the derived bounds of every
|
|
73
|
+
// ancestor group, so mark the parent chain (and its incident edges) dirty.
|
|
74
|
+
const markAncestors = (node: { parentId?: string | null }) => {
|
|
75
|
+
const nextIndex = indexNodes(next.graph.nodes);
|
|
76
|
+
const seen = new Set<string>();
|
|
77
|
+
let parentId = node.parentId ?? null;
|
|
78
|
+
while (parentId && !seen.has(parentId)) {
|
|
79
|
+
seen.add(parentId);
|
|
80
|
+
dirty.add(parentId);
|
|
81
|
+
markIncidentEdges(parentId, next.graph.edges);
|
|
82
|
+
parentId = nextIndex.get(parentId)?.parentId ?? null;
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
// `dragRevision` identifies the one hot transition whose exact affected
|
|
87
|
+
// cohort was indexed at the gesture boundary. Avoid diffing both graph
|
|
88
|
+
// arrays on every pointer frame; non-drag graph mutations retain the full
|
|
89
|
+
// structural diff below.
|
|
90
|
+
if (next.dragRevision !== prev.dragRevision && dragDirtyIds) {
|
|
91
|
+
return new Set(dragDirtyIds);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// Gesture boundaries change transient styling only for their cohort. Pan,
|
|
95
|
+
// box-selection, and connection state do not alter entity render state.
|
|
96
|
+
if (
|
|
97
|
+
next.canvasState !== prev.canvasState &&
|
|
98
|
+
(next.canvasState === 'dragging' || prev.canvasState === 'dragging')
|
|
99
|
+
) {
|
|
100
|
+
if (prev.canvasState === 'dragging' && dragDirtyIds) {
|
|
101
|
+
// Imperative observers own transient geometry during the gesture. Wake
|
|
102
|
+
// its exact affected cohort on exit so declarative consumers reconcile
|
|
103
|
+
// to the committed node, label, and path data.
|
|
104
|
+
for (const id of dragDirtyIds) dirty.add(id);
|
|
105
|
+
} else {
|
|
106
|
+
for (const id of new Set([...prev.selection, ...next.selection])) {
|
|
107
|
+
dirty.add(id);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// Snap offsets move the selected cohort, its hierarchy, and affected paths;
|
|
113
|
+
// target nodes remain fixed and do not need entity notifications.
|
|
114
|
+
if (next.enableSnapLines && next.canvasState === 'dragging') {
|
|
115
|
+
const nodes = indexNodes(next.graph.nodes);
|
|
116
|
+
const edges = indexEdges(next.graph.edges);
|
|
117
|
+
for (const id of next.selection) {
|
|
118
|
+
dirty.add(id);
|
|
119
|
+
const node = nodes.get(id);
|
|
120
|
+
if (node) {
|
|
121
|
+
markIncidentEdges(id, next.graph.edges);
|
|
122
|
+
markAncestors(node);
|
|
123
|
+
} else if (edges.has(id)) {
|
|
124
|
+
markEdge(id);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
if (next.layoutAnimation.offsets !== prev.layoutAnimation.offsets) {
|
|
130
|
+
const prevNodes = indexNodes(prev.graph.nodes);
|
|
131
|
+
const nextNodes = indexNodes(next.graph.nodes);
|
|
132
|
+
const ids = new Set([
|
|
133
|
+
...prev.layoutAnimation.offsets.keys(),
|
|
134
|
+
...next.layoutAnimation.offsets.keys(),
|
|
135
|
+
]);
|
|
136
|
+
for (const id of ids) {
|
|
137
|
+
const before = prev.layoutAnimation.offsets.get(id);
|
|
138
|
+
const after = next.layoutAnimation.offsets.get(id);
|
|
139
|
+
if (before?.x === after?.x && before?.y === after?.y) continue;
|
|
140
|
+
const node = nextNodes.get(id) ?? prevNodes.get(id);
|
|
141
|
+
if (node) {
|
|
142
|
+
dirty.add(id);
|
|
143
|
+
markIncidentEdges(id, next.graph.edges);
|
|
144
|
+
markAncestors(node);
|
|
145
|
+
} else {
|
|
146
|
+
markEdge(id);
|
|
147
|
+
const edge =
|
|
148
|
+
indexEdges(next.graph.edges).get(id) ??
|
|
149
|
+
indexEdges(prev.graph.edges).get(id);
|
|
150
|
+
if (edge) {
|
|
151
|
+
const source = nextNodes.get(edge.sourceId) ?? prevNodes.get(edge.sourceId);
|
|
152
|
+
const target = nextNodes.get(edge.targetId) ?? prevNodes.get(edge.targetId);
|
|
153
|
+
if (source?.group) dirty.add(source.id);
|
|
154
|
+
if (target?.group) dirty.add(target.id);
|
|
155
|
+
markAncestors(source ?? {});
|
|
156
|
+
markAncestors(target ?? {});
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
if (next.graph.nodes !== prev.graph.nodes) {
|
|
163
|
+
const prevIndex = indexNodes(prev.graph.nodes);
|
|
164
|
+
const nextIndex = indexNodes(next.graph.nodes);
|
|
165
|
+
for (const node of next.graph.nodes) {
|
|
166
|
+
const before = prevIndex.get(node.id);
|
|
167
|
+
if (before !== node) {
|
|
168
|
+
dirty.add(node.id);
|
|
169
|
+
markIncidentEdges(node.id, next.graph.edges);
|
|
170
|
+
markAncestors(node);
|
|
171
|
+
// Reparenting: the previous parent chain shrinks too.
|
|
172
|
+
if (before && (before.parentId ?? null) !== (node.parentId ?? null)) {
|
|
173
|
+
markAncestors(before);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
for (const node of prev.graph.nodes) {
|
|
178
|
+
if (!nextIndex.has(node.id)) {
|
|
179
|
+
dirty.add(node.id);
|
|
180
|
+
markIncidentEdges(node.id, prev.graph.edges);
|
|
181
|
+
markAncestors(node);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
if (next.graph.edges !== prev.graph.edges) {
|
|
187
|
+
const prevIndex = indexEdges(prev.graph.edges);
|
|
188
|
+
const nextIndex = indexEdges(next.graph.edges);
|
|
189
|
+
const nodeIndex = indexNodes(next.graph.nodes);
|
|
190
|
+
// A changed edge (e.g. a dragged label) can change the derived bounds of
|
|
191
|
+
// the group containing it — dirty both endpoints' ancestor chains.
|
|
192
|
+
const markEndpointAncestors = (edge: FlowEdge) => {
|
|
193
|
+
const source = nodeIndex.get(edge.sourceId);
|
|
194
|
+
const target = nodeIndex.get(edge.targetId);
|
|
195
|
+
markAncestors(source ?? {});
|
|
196
|
+
markAncestors(target ?? {});
|
|
197
|
+
// The owner can be an endpoint itself (edge into its own descendant),
|
|
198
|
+
// but only group endpoints derive bounds from contained labels.
|
|
199
|
+
if (source?.group) dirty.add(source.id);
|
|
200
|
+
if (target?.group) dirty.add(target.id);
|
|
201
|
+
};
|
|
202
|
+
for (const edge of next.graph.edges) {
|
|
203
|
+
if (prevIndex.get(edge.id) !== edge) {
|
|
204
|
+
markEdge(edge.id);
|
|
205
|
+
markEndpointAncestors(edge as FlowEdge);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
for (const edge of prev.graph.edges) {
|
|
209
|
+
if (!nextIndex.has(edge.id)) {
|
|
210
|
+
markEdge(edge.id);
|
|
211
|
+
markEndpointAncestors(edge as FlowEdge);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
if (next.selection !== prev.selection) {
|
|
217
|
+
for (const id of next.selection) {
|
|
218
|
+
if (!prev.selection.has(id)) dirty.add(id);
|
|
219
|
+
}
|
|
220
|
+
for (const id of prev.selection) {
|
|
221
|
+
if (!next.selection.has(id)) dirty.add(id);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
if (next.hoveredEntityIds !== prev.hoveredEntityIds) {
|
|
226
|
+
for (const id of next.hoveredEntityIds) {
|
|
227
|
+
if (!prev.hoveredEntityIds.has(id)) dirty.add(id);
|
|
228
|
+
}
|
|
229
|
+
for (const id of prev.hoveredEntityIds) {
|
|
230
|
+
if (!next.hoveredEntityIds.has(id)) dirty.add(id);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
if (next.highlights !== prev.highlights) {
|
|
235
|
+
const kinds = new Set([...prev.highlights.keys(), ...next.highlights.keys()]);
|
|
236
|
+
for (const kind of kinds) {
|
|
237
|
+
const before = prev.highlights.get(kind);
|
|
238
|
+
const after = next.highlights.get(kind);
|
|
239
|
+
if (before === after) continue;
|
|
240
|
+
// Bucket changed: anything in either version may have different meta.
|
|
241
|
+
if (before) for (const id of before.keys()) dirty.add(id);
|
|
242
|
+
if (after) for (const id of after.keys()) dirty.add(id);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
const idPairs: Array<[string | null, string | null]> = [
|
|
247
|
+
[prev.draggedEntityId, next.draggedEntityId],
|
|
248
|
+
[prev.focusedEntityId, next.focusedEntityId],
|
|
249
|
+
[prev.resizingEntityId, next.resizingEntityId],
|
|
250
|
+
];
|
|
251
|
+
for (const [before, after] of idPairs) {
|
|
252
|
+
if (before !== after) {
|
|
253
|
+
if (before) dirty.add(before);
|
|
254
|
+
if (after) dirty.add(after);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
// An in-flight resize changes the resized node's bounds (and its edges'
|
|
259
|
+
// paths) on every move without touching the node object.
|
|
260
|
+
if (next.resizeCorners !== prev.resizeCorners && next.resizingEntityId) {
|
|
261
|
+
dirty.add(next.resizingEntityId);
|
|
262
|
+
markIncidentEdges(next.resizingEntityId, next.graph.edges);
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
return dirty;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
function dirtyIdsForDrag(
|
|
269
|
+
context: FlowStoreContext,
|
|
270
|
+
options: FlowEntityChangeNotifierOptions,
|
|
271
|
+
): Set<string> {
|
|
272
|
+
const dirty = new Set<string>();
|
|
273
|
+
const nodes = indexNodes(context.graph.nodes);
|
|
274
|
+
const edges = indexEdges(context.graph.edges);
|
|
275
|
+
const children = indexChildren(context.graph.nodes);
|
|
276
|
+
const markEdge = (id: string) => {
|
|
277
|
+
dirty.add(id);
|
|
278
|
+
for (const dependentId of options.getEdgeDependents?.(id) ?? []) {
|
|
279
|
+
dirty.add(dependentId);
|
|
280
|
+
}
|
|
281
|
+
};
|
|
282
|
+
const markIncident = (id: string) => {
|
|
283
|
+
for (const edge of adjacency(context.graph.edges).get(id) ?? []) {
|
|
284
|
+
markEdge(edge.id);
|
|
285
|
+
}
|
|
286
|
+
};
|
|
287
|
+
const markAncestors = (id: string) => {
|
|
288
|
+
const seen = new Set<string>();
|
|
289
|
+
let parentId = nodes.get(id)?.parentId ?? null;
|
|
290
|
+
while (parentId && !seen.has(parentId)) {
|
|
291
|
+
seen.add(parentId);
|
|
292
|
+
dirty.add(parentId);
|
|
293
|
+
markIncident(parentId);
|
|
294
|
+
parentId = nodes.get(parentId)?.parentId ?? null;
|
|
295
|
+
}
|
|
296
|
+
};
|
|
297
|
+
const markNodeSubtree = (id: string, seen = new Set<string>()) => {
|
|
298
|
+
if (seen.has(id)) return;
|
|
299
|
+
seen.add(id);
|
|
300
|
+
dirty.add(id);
|
|
301
|
+
markIncident(id);
|
|
302
|
+
markAncestors(id);
|
|
303
|
+
for (const child of children.get(id) ?? []) {
|
|
304
|
+
markNodeSubtree(child.id, seen);
|
|
305
|
+
}
|
|
306
|
+
};
|
|
307
|
+
|
|
308
|
+
for (const id of context.selection) {
|
|
309
|
+
if (nodes.has(id)) markNodeSubtree(id);
|
|
310
|
+
else {
|
|
311
|
+
const edge = edges.get(id);
|
|
312
|
+
if (!edge) continue;
|
|
313
|
+
markEdge(id);
|
|
314
|
+
const source = nodes.get(edge.sourceId);
|
|
315
|
+
const target = nodes.get(edge.targetId);
|
|
316
|
+
if (source?.group) dirty.add(source.id);
|
|
317
|
+
if (target?.group) dirty.add(target.id);
|
|
318
|
+
markAncestors(edge.sourceId);
|
|
319
|
+
markAncestors(edge.targetId);
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
return dirty;
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
export function createEntityChangeNotifier(
|
|
326
|
+
store: FlowStore,
|
|
327
|
+
options: FlowEntityChangeNotifierOptions = {},
|
|
328
|
+
): FlowEntityChangeNotifier {
|
|
329
|
+
const listeners = new Map<string, Set<() => void>>();
|
|
330
|
+
let prev = store.getSnapshot().context;
|
|
331
|
+
let activeDragDirtyIds: Set<string> | undefined;
|
|
332
|
+
|
|
333
|
+
const subscription = store.subscribe((snapshot) => {
|
|
334
|
+
const next = snapshot.context;
|
|
335
|
+
if (next === prev) return;
|
|
336
|
+
if (
|
|
337
|
+
next.canvasState === 'dragging' &&
|
|
338
|
+
(prev.canvasState !== 'dragging' || next.selection !== prev.selection)
|
|
339
|
+
) {
|
|
340
|
+
activeDragDirtyIds = dirtyIdsForDrag(next, options);
|
|
341
|
+
}
|
|
342
|
+
const dirty = computeDirty(prev, next, options, activeDragDirtyIds);
|
|
343
|
+
if (dirty !== 'all' && next.routingRevision !== prev.routingRevision) {
|
|
344
|
+
// Folded edges have synthetic ids outside the authored edge array.
|
|
345
|
+
const nodes = indexNodes(next.graph.nodes);
|
|
346
|
+
for (const id of listeners.keys()) if (!nodes.has(id)) dirty.add(id);
|
|
347
|
+
}
|
|
348
|
+
if (
|
|
349
|
+
next.canvasState === 'dragging' &&
|
|
350
|
+
next.dragRevision === prev.dragRevision &&
|
|
351
|
+
(next.graph.nodes !== prev.graph.nodes || next.graph.edges !== prev.graph.edges)
|
|
352
|
+
) {
|
|
353
|
+
// A non-drag graph mutation occurred during the gesture; refresh the
|
|
354
|
+
// next frame's exact cohort after the full structural diff above.
|
|
355
|
+
activeDragDirtyIds = dirtyIdsForDrag(next, options);
|
|
356
|
+
} else if (next.canvasState !== 'dragging') {
|
|
357
|
+
activeDragDirtyIds = undefined;
|
|
358
|
+
}
|
|
359
|
+
prev = next;
|
|
360
|
+
|
|
361
|
+
if (dirty === 'all') {
|
|
362
|
+
for (const set of listeners.values()) {
|
|
363
|
+
for (const listener of [...set]) listener();
|
|
364
|
+
}
|
|
365
|
+
return;
|
|
366
|
+
}
|
|
367
|
+
for (const id of dirty) {
|
|
368
|
+
const set = listeners.get(id);
|
|
369
|
+
if (set) {
|
|
370
|
+
for (const listener of [...set]) listener();
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
});
|
|
374
|
+
|
|
375
|
+
return {
|
|
376
|
+
subscribe(id, listener) {
|
|
377
|
+
let set = listeners.get(id);
|
|
378
|
+
if (!set) {
|
|
379
|
+
set = new Set();
|
|
380
|
+
listeners.set(id, set);
|
|
381
|
+
}
|
|
382
|
+
set.add(listener);
|
|
383
|
+
return () => {
|
|
384
|
+
set.delete(listener);
|
|
385
|
+
if (set.size === 0) {
|
|
386
|
+
listeners.delete(id);
|
|
387
|
+
}
|
|
388
|
+
};
|
|
389
|
+
},
|
|
390
|
+
destroy() {
|
|
391
|
+
subscription.unsubscribe();
|
|
392
|
+
listeners.clear();
|
|
393
|
+
},
|
|
394
|
+
};
|
|
395
|
+
}
|