@statelyai/flow 0.13.1 → 0.13.2

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.
@@ -809,13 +809,6 @@ type PaintZIndices = {
809
809
  nodes: Map<string, number>;
810
810
  edges: Map<string, number>;
811
811
  };
812
- /**
813
- * Create the framework-agnostic derived-geometry and query layer for a store.
814
- *
815
- * The engine owns memoized graph indices, paint order, bounds, routing, and
816
- * hit-testing. Returned collections are read-only views and may retain identity
817
- * while their semantic inputs are unchanged.
818
- */
819
812
  declare function createFlowEngine(store: FlowStore, initialOptions?: FlowEngineOptions): {
820
813
  store: import("@xstate/store").Store<FlowStoreContext, {
821
814
  createNode: {
package/dist/esm/index.js CHANGED
@@ -3676,21 +3676,22 @@ function createFlowStore(initialGraph = {
3676
3676
  const previousSelection = new Set(draft.selection);
3677
3677
  const previousNodes = new Map(ctx.graph.nodes.map((node) => [node.id, node]));
3678
3678
  const changedAppearances = /* @__PURE__ */ new Set();
3679
+ const nodes = event.graph.nodes.map((node) => {
3680
+ const previous = previousNodes.get(node.id);
3681
+ if (!previous || JSON.stringify(previous.appearance) === JSON.stringify(node.appearance)) return node;
3682
+ const next = {
3683
+ ...node,
3684
+ x: node.x - (node.measured?.anchorOffset?.x ?? 0),
3685
+ y: node.y - (node.measured?.anchorOffset?.y ?? 0)
3686
+ };
3687
+ delete next.measured;
3688
+ draft.measurements.delete(node.id);
3689
+ changedAppearances.add(node.id);
3690
+ return next;
3691
+ });
3679
3692
  draft.graph = {
3680
3693
  ...event.graph,
3681
- nodes: event.graph.nodes.map((node) => {
3682
- const previous = previousNodes.get(node.id);
3683
- if (!previous || JSON.stringify(previous.appearance) === JSON.stringify(node.appearance)) return node;
3684
- const next = {
3685
- ...node,
3686
- x: node.x - (node.measured?.anchorOffset?.x ?? 0),
3687
- y: node.y - (node.measured?.anchorOffset?.y ?? 0)
3688
- };
3689
- delete next.measured;
3690
- draft.measurements.delete(node.id);
3691
- changedAppearances.add(node.id);
3692
- return next;
3693
- })
3694
+ nodes: changedAppearances.size ? nodes : event.graph.nodes
3694
3695
  };
3695
3696
  if (changedAppearances.size) invalidatePathsForNodeGeometry(draft, changedAppearances);
3696
3697
  draft.layoutAnimation = event.layoutAnimationOffsets?.size ? {
@@ -3724,9 +3725,9 @@ function createFlowStore(initialGraph = {
3724
3725
  draft.measurements.clear();
3725
3726
  draft.derivedPortMeasurements.clear();
3726
3727
  } else {
3727
- draft.selection = new Set([...draft.selection].filter((id) => ids.has(id)));
3728
- draft.hoveredEntityIds = new Set([...draft.hoveredEntityIds].filter((id) => ids.has(id)));
3729
- draft.collapsedNodeIds = new Set([...draft.collapsedNodeIds].filter((id) => nodeIds.has(id)));
3728
+ if ([...draft.selection].some((id) => !ids.has(id))) draft.selection = new Set([...draft.selection].filter((id) => ids.has(id)));
3729
+ if ([...draft.hoveredEntityIds].some((id) => !ids.has(id))) draft.hoveredEntityIds = new Set([...draft.hoveredEntityIds].filter((id) => ids.has(id)));
3730
+ if ([...draft.collapsedNodeIds].some((id) => !nodeIds.has(id))) draft.collapsedNodeIds = new Set([...draft.collapsedNodeIds].filter((id) => nodeIds.has(id)));
3730
3731
  for (const [kind, bucket] of draft.highlights) {
3731
3732
  for (const id of bucket.keys()) if (!ids.has(id)) bucket.delete(id);
3732
3733
  if (bucket.size === 0) draft.highlights.delete(kind);
@@ -5488,6 +5489,16 @@ const MAX_ENTITY_SPATIAL_CELLS = 4096;
5488
5489
  * hit-testing. Returned collections are read-only views and may retain identity
5489
5490
  * while their semantic inputs are unchanged.
5490
5491
  */
5492
+ /** Structural equality for plain path data (objects, arrays, primitives). */
5493
+ function plainDataEqual(a, b) {
5494
+ if (a === b) return true;
5495
+ if (!a || !b || typeof a !== "object" || typeof b !== "object") return false;
5496
+ if (Array.isArray(a) !== Array.isArray(b)) return false;
5497
+ const aRecord = a;
5498
+ const bRecord = b;
5499
+ const aKeys = Object.keys(aRecord);
5500
+ return aKeys.length === Object.keys(bRecord).length && aKeys.every((key) => plainDataEqual(aRecord[key], bRecord[key]));
5501
+ }
5491
5502
  function createFlowEngine(store, initialOptions = {}) {
5492
5503
  let engineOptions = initialOptions;
5493
5504
  let optionsVersion = 0;
@@ -5497,6 +5508,7 @@ function createFlowEngine(store, initialOptions = {}) {
5497
5508
  }
5498
5509
  let snapshotEdgePathsCache = /* @__PURE__ */ new WeakMap();
5499
5510
  let stableEdgePathsCache = /* @__PURE__ */ new WeakMap();
5511
+ let edgeSourceBoundsContextReads = null;
5500
5512
  const snapOffsetCache = /* @__PURE__ */ new WeakMap();
5501
5513
  let renderableCache = /* @__PURE__ */ new WeakMap();
5502
5514
  let entityOrderCache = null;
@@ -5650,6 +5662,7 @@ function createFlowEngine(store, initialOptions = {}) {
5650
5662
  nodeIndexCache = /* @__PURE__ */ new WeakMap();
5651
5663
  obstacleCache = /* @__PURE__ */ new WeakMap();
5652
5664
  edgePathMemo.clear();
5665
+ lastCanonicalEdgePaths = null;
5653
5666
  const appearances = replace ? partial.appearances ?? {} : partial.appearances;
5654
5667
  if (appearances !== void 0 && appearances !== previousAppearances) store.trigger.configure({ appearances });
5655
5668
  }
@@ -5768,6 +5781,7 @@ function createFlowEngine(store, initialOptions = {}) {
5768
5781
  }
5769
5782
  return withLayoutOffset(resolvedBounds$1(node), node.id, c);
5770
5783
  }
5784
+ let lastCanonicalEdgePaths = null;
5771
5785
  const edgePathMemo = /* @__PURE__ */ new Map();
5772
5786
  let edgePathMemoInputs = null;
5773
5787
  function layoutOffset(id, c = ctx()) {
@@ -6569,7 +6583,7 @@ function createFlowEngine(store, initialOptions = {}) {
6569
6583
  const resolvedSourceBounds = engineOptions.resolveEdgeSourceBounds?.({
6570
6584
  edge,
6571
6585
  graph,
6572
- context,
6586
+ context: edgeSourceBoundsContextReads ? trackContextReads(context, edgeSourceBoundsContextReads) : context,
6573
6587
  source,
6574
6588
  target,
6575
6589
  sourceBounds: naturalSourceBounds,
@@ -6605,6 +6619,16 @@ function createFlowEngine(store, initialOptions = {}) {
6605
6619
  layoutOffsets: c.layoutAnimation.offsets
6606
6620
  };
6607
6621
  }
6622
+ function trackContextReads(context, reads) {
6623
+ return new Proxy(context, { get(target, key, receiver) {
6624
+ reads.add(key);
6625
+ return Reflect.get(target, key, receiver);
6626
+ } });
6627
+ }
6628
+ function sameContextReads(previous, next, reads) {
6629
+ for (const key of reads) if (previous[key] !== next[key]) return false;
6630
+ return true;
6631
+ }
6608
6632
  function sameEdgePathsCacheInputs(a, b) {
6609
6633
  return a.appearances === b.appearances && a.canonicalGraph === b.canonicalGraph && a.selection === b.selection && a.collapsedNodeIds === b.collapsedNodeIds && a.connection === b.connection && a.offsetX === b.offsetX && a.offsetY === b.offsetY && a.gridX === b.gridX && a.gridY === b.gridY && a.resizingEntityId === b.resizingEntityId && a.resizeCorners === b.resizeCorners && a.measurements === b.measurements && a.layoutOffsets === b.layoutOffsets;
6610
6634
  }
@@ -6616,14 +6640,28 @@ function createFlowEngine(store, initialOptions = {}) {
6616
6640
  const cached = byGraph?.get(graph);
6617
6641
  if (cached) return cached;
6618
6642
  const snap = getVisualSnapOffset();
6619
- const canReuseAcrossSnapshots = !engineOptions.isVisible && !engineOptions.config?.visible && !engineOptions.resolveEdgeSourceBounds && !engineOptions.edgeOwner;
6643
+ const canReuseAcrossSnapshots = !engineOptions.isVisible && !engineOptions.config?.visible && !engineOptions.edgeOwner;
6620
6644
  const inputs = edgePathsCacheInputs(c, snap);
6621
6645
  const stableCached = canReuseAcrossSnapshots ? stableEdgePathsCache.get(graph) : void 0;
6622
- const paths = stableCached && sameEdgePathsCacheInputs(stableCached.inputs, inputs) ? stableCached.paths : computeAllEdgePaths(c, graph, snap);
6623
- if (canReuseAcrossSnapshots && paths !== stableCached?.paths) stableEdgePathsCache.set(graph, {
6624
- inputs,
6625
- paths
6626
- });
6646
+ let paths;
6647
+ if (stableCached && sameEdgePathsCacheInputs(stableCached.inputs, inputs) && sameContextReads(stableCached.context, c, stableCached.contextReads)) paths = stableCached.paths;
6648
+ else {
6649
+ const outerReads = edgeSourceBoundsContextReads;
6650
+ const contextReads = /* @__PURE__ */ new Set();
6651
+ edgeSourceBoundsContextReads = contextReads;
6652
+ try {
6653
+ paths = computeAllEdgePaths(c, graph, snap);
6654
+ } finally {
6655
+ edgeSourceBoundsContextReads = outerReads;
6656
+ for (const key of contextReads) outerReads?.add(key);
6657
+ }
6658
+ if (canReuseAcrossSnapshots) stableEdgePathsCache.set(graph, {
6659
+ inputs,
6660
+ paths,
6661
+ context: c,
6662
+ contextReads
6663
+ });
6664
+ }
6627
6665
  if (!byGraph) {
6628
6666
  byGraph = /* @__PURE__ */ new WeakMap();
6629
6667
  snapshotEdgePathsCache.set(c, byGraph);
@@ -6755,6 +6793,8 @@ function createFlowEngine(store, initialOptions = {}) {
6755
6793
  labelBounds: null
6756
6794
  };
6757
6795
  }
6796
+ const previousPath = trackSourceDependencies ? lastCanonicalEdgePaths?.get(edge.id) : void 0;
6797
+ if (previousPath && plainDataEqual(previousPath, pathData)) pathData = previousPath;
6758
6798
  paths.set(edge.id, pathData);
6759
6799
  if (memoizableEdge) edgePathMemo.set(edge.id, {
6760
6800
  edge,
@@ -6766,7 +6806,10 @@ function createFlowEngine(store, initialOptions = {}) {
6766
6806
  return pathData;
6767
6807
  };
6768
6808
  for (const edge of [...graph.edges, ...foldedEdgesFor(c).edges]) computeEdgePath(edge);
6769
- if (trackSourceDependencies) edgeSourceDependents = nextEdgeSourceDependents;
6809
+ if (trackSourceDependencies) {
6810
+ edgeSourceDependents = nextEdgeSourceDependents;
6811
+ lastCanonicalEdgePaths = paths;
6812
+ }
6770
6813
  if (memoUsable && edgePathMemo.size > graph.edges.length * 2 + 16) edgePathMemo.clear();
6771
6814
  return paths;
6772
6815
  }
@@ -6917,13 +6960,13 @@ function createFlowEngine(store, initialOptions = {}) {
6917
6960
  if (cullMode === "viewport" && !rectsIntersect(hierarchyBounds(n, c), cullRect)) return false;
6918
6961
  return true;
6919
6962
  });
6920
- const edgePaths = getAllEdgePaths();
6963
+ const edgePaths = cullMode === "viewport" ? getAllEdgePaths() : void 0;
6921
6964
  return {
6922
6965
  nodes,
6923
6966
  edges: order.edges.filter((e) => {
6924
6967
  if (!isEdgeVisible(e, c.graph, c, vp)) return false;
6925
6968
  if (cullMode !== "viewport") return true;
6926
- const path = edgePaths.get(e.id);
6969
+ const path = edgePaths?.get(e.id);
6927
6970
  if (e.label) {
6928
6971
  const labelRect = path?.labelBounds ?? getEdgeLabelBounds(e.id);
6929
6972
  if (!labelRect) return false;
@@ -7631,8 +7674,8 @@ function equal(a, b) {
7631
7674
  if (Array.isArray(a) || Array.isArray(b)) return Array.isArray(a) && Array.isArray(b) && a.length === b.length && a.every((value, index) => equal(value, b[index]));
7632
7675
  const aRecord = a;
7633
7676
  const bRecord = b;
7634
- const aKeys = Object.keys(aRecord);
7635
- const bKeys = Object.keys(bRecord);
7677
+ const aKeys = Object.keys(aRecord).filter((key) => aRecord[key] !== void 0);
7678
+ const bKeys = Object.keys(bRecord).filter((key) => bRecord[key] !== void 0);
7636
7679
  return aKeys.length === bKeys.length && aKeys.every((key) => Object.prototype.hasOwnProperty.call(bRecord, key) && equal(aRecord[key], bRecord[key]));
7637
7680
  }
7638
7681
  function graphAttrs(graph) {
@@ -7780,6 +7823,33 @@ function createFlowChangeSet(before, after, metadata) {
7780
7823
  inverse
7781
7824
  };
7782
7825
  }
7826
+ /**
7827
+ * Swap each entity the change set did not add or update for its object in
7828
+ * `before`, so memoized views keyed on entity identity skip unchanged
7829
+ * entities. An updated edge keeps its previous label object when the label is
7830
+ * unchanged. Arrays whose entries all match `before` are kept too. Mutates and
7831
+ * returns `after`, which must be a fresh clone.
7832
+ */
7833
+ function reuseUnchangedEntities(before, after, changeSet) {
7834
+ const changedNodeIds = /* @__PURE__ */ new Set([...(changeSet?.nodes.added ?? []).map((node) => node.id), ...(changeSet?.nodes.updated ?? []).map((update) => update.id)]);
7835
+ const changedEdgeIds = /* @__PURE__ */ new Set([...(changeSet?.edges.added ?? []).map((edge) => edge.id), ...(changeSet?.edges.updated ?? []).map((update) => update.id)]);
7836
+ const beforeNodes = new Map(before.nodes.map((node) => [node.id, node]));
7837
+ const beforeEdges = new Map(before.edges.map((edge) => [edge.id, edge]));
7838
+ const nodes = after.nodes.map((node) => changedNodeIds.has(node.id) ? node : beforeNodes.get(node.id) ?? node);
7839
+ const edges = after.edges.map((edge) => {
7840
+ const previous = beforeEdges.get(edge.id);
7841
+ if (!previous) return edge;
7842
+ if (!changedEdgeIds.has(edge.id)) return previous;
7843
+ if (previous.label && equal(previous.label, edge.label)) edge.label = previous.label;
7844
+ return edge;
7845
+ });
7846
+ after.nodes = sameEntries(nodes, before.nodes) ? before.nodes : nodes;
7847
+ after.edges = sameEntries(edges, before.edges) ? before.edges : edges;
7848
+ return after;
7849
+ }
7850
+ function sameEntries(left, right) {
7851
+ return left.length === right.length && left.every((value, index) => value === right[index]);
7852
+ }
7783
7853
  function strictConflict(path, message) {
7784
7854
  return new FlowGraphValidationError([{
7785
7855
  code: "change_set_conflict",
@@ -8862,13 +8932,14 @@ function createFlow(options = {}) {
8862
8932
  return null;
8863
8933
  }
8864
8934
  if (!replacementOptions) validateDraftConnections(validated);
8865
- const changeSet = createFlowChangeSet(currentGraph(), validated, {
8935
+ const before = currentGraph();
8936
+ const changeSet = createFlowChangeSet(before, validated, {
8866
8937
  revision: graphRevision + 1,
8867
8938
  ...transactionOptions
8868
8939
  });
8869
8940
  if (!changeSet && !replacementOptions && !layoutAnimationOffsets && !resetMeasurementIds.length) return null;
8870
8941
  store.trigger.replaceGraph({
8871
- graph: validated,
8942
+ graph: reuseUnchangedEntities(before, validated, changeSet),
8872
8943
  graphChanged: changeSet !== null,
8873
8944
  resetMeasurementIds,
8874
8945
  ...transactionOptions,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@statelyai/flow",
3
- "version": "0.13.1",
3
+ "version": "0.13.2",
4
4
  "description": "Framework-agnostic flow visualization engine. Builds on @statelyai/graph.",
5
5
  "type": "module",
6
6
  "sideEffects": true,