formstand 0.22.0 → 0.22.1

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/dist/index.js CHANGED
@@ -1759,10 +1759,13 @@ var mappedIdState = (prev, mapping, liveItems) => {
1759
1759
  };
1760
1760
  var MAX_ID_PATHS = 4096;
1761
1761
  var sharedIdEntries = /* @__PURE__ */ new WeakMap();
1762
- var sharedIdEntry = (store, path) => {
1763
- const existingStore = sharedIdEntries.get(store);
1764
- const forStore = existingStore ?? { byPath: /* @__PURE__ */ new Map(), seq: 1 };
1765
- if (existingStore === void 0) sharedIdEntries.set(store, forStore);
1762
+ var storeIdEntries = (store) => {
1763
+ const existing = sharedIdEntries.get(store);
1764
+ const forStore = existing ?? { byPath: /* @__PURE__ */ new Map(), seq: 1 };
1765
+ if (existing === void 0) sharedIdEntries.set(store, forStore);
1766
+ return forStore;
1767
+ };
1768
+ var sharedIdEntry = (forStore, path) => {
1766
1769
  const existing = forStore.byPath.get(path);
1767
1770
  if (existing !== void 0) {
1768
1771
  forStore.byPath.delete(path);
@@ -1792,12 +1795,40 @@ var walkOps = (start, items, ops) => {
1792
1795
  }, start);
1793
1796
  return walked.items === items ? walked : reconcileIds(walked, items);
1794
1797
  };
1798
+ var reseatDescendants = (forStore, path, prev, next) => {
1799
+ if (prev.ids.length === 0) return;
1800
+ const newIndexById = new Map(next.ids.map((id, i) => [id, i]));
1801
+ const moves = prev.ids.map((id) => newIndexById.get(id));
1802
+ if (moves.every((to, from) => to === from)) return;
1803
+ const prefix = `${path}.`;
1804
+ const descendants = [...forStore.byPath.keys()].filter(
1805
+ (key) => key.startsWith(prefix)
1806
+ );
1807
+ const relocations = descendants.flatMap((key) => {
1808
+ const rest = key.slice(prefix.length);
1809
+ const dot = rest.indexOf(".");
1810
+ const segment = dot === -1 ? rest : rest.slice(0, dot);
1811
+ const from = Number(segment);
1812
+ if (!/^\d+$/.test(segment) || from >= prev.ids.length) return [];
1813
+ const entry = forStore.byPath.get(key);
1814
+ if (entry === void 0) return [];
1815
+ const to = moves[from];
1816
+ const target = to === void 0 ? void 0 : `${prefix}${to}${dot === -1 ? "" : rest.slice(dot)}`;
1817
+ return [{ key, target, entry }];
1818
+ });
1819
+ relocations.forEach(({ key }) => forStore.byPath.delete(key));
1820
+ relocations.forEach(({ target, entry }) => {
1821
+ if (target !== void 0) forStore.byPath.set(target, entry);
1822
+ });
1823
+ };
1795
1824
  var deriveIds = (store, path, items) => {
1796
- const entry = sharedIdEntry(store, path);
1825
+ const forStore = storeIdEntries(store);
1826
+ const entry = sharedIdEntry(forStore, path);
1797
1827
  if (entry.state.items === items) return entry.state;
1798
1828
  const ops = arrayOpsFor(store, path);
1799
1829
  if (ops.length > 0) clearArrayOps(store, path);
1800
1830
  const next = walkOps(entry.state, items, ops);
1831
+ reseatDescendants(forStore, path, entry.state, next);
1801
1832
  entry.state = next;
1802
1833
  return next;
1803
1834
  };