mnfst 0.5.322 → 0.5.323

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.
@@ -2,7 +2,7 @@
2
2
 
3
3
  (function () {
4
4
 
5
- const MANIFEST_BUILD_VERSION = '0.5.322';
5
+ const MANIFEST_BUILD_VERSION = '0.5.323';
6
6
 
7
7
  /* Manifest Data Sources - Appwrite Integration */
8
8
 
@@ -2,7 +2,7 @@
2
2
 
3
3
  (function () {
4
4
 
5
- const MANIFEST_BUILD_VERSION = '0.5.322';
5
+ const MANIFEST_BUILD_VERSION = '0.5.323';
6
6
 
7
7
  /* Manifest Data Sources - Configuration */
8
8
 
@@ -307,6 +307,12 @@ let landingResolvers = [];
307
307
  let landingFrame = null;
308
308
  let landingTimer = null;
309
309
  const LANDING_FALLBACK_MS = 50;
310
+ // Backpressure: realtime rows arriving within twice the cost of a long flush land together; loads never wait
311
+ const LANDING_BUSY_MS = 8;
312
+ const LANDING_MAX_WAIT_MS = 250;
313
+ let landingCost = 0;
314
+ let landingEndedAt = 0;
315
+ let landingDeferred = false;
310
316
 
311
317
  // Local writes made while a flush is pending win over it (local-last)
312
318
  const pendingLocal = new Map(); // source -> Map<$id, { patch, removed }>
@@ -357,6 +363,83 @@ const rowKey = (row) => (row && typeof row === 'object' && (typeof row.$id === '
357
363
  ? row.$id
358
364
  : null;
359
365
 
366
+ // $id -> position per source array; rebuilt when the array's length moved or a hit no longer matches
367
+ const rowIndexes = new WeakMap(); // raw array -> { map, len }
368
+
369
+ function buildRowIndex(arr) {
370
+ const map = new Map();
371
+ for (let i = 0; i < arr.length; i++) { const id = rowKey(arr[i]); if (id !== null) map.set(id, i); }
372
+ const entry = { map, len: arr.length };
373
+ rowIndexes.set(arr, entry);
374
+ return entry;
375
+ }
376
+
377
+ function indexOfRow(arr, id) {
378
+ if (!Array.isArray(arr) || id === null || id === undefined) return -1;
379
+ let entry = rowIndexes.get(arr);
380
+ if (!entry || entry.len !== arr.length) entry = buildRowIndex(arr);
381
+ let i = entry.map.get(id);
382
+ if (i !== undefined && rowKey(arr[i]) !== id) i = buildRowIndex(arr).map.get(id);
383
+ return i === undefined ? -1 : i;
384
+ }
385
+
386
+ function dropRowIndex(arr) { if (arr) rowIndexes.delete(arr); }
387
+
388
+ // Rows were pushed onto the end: extend the index instead of rebuilding it
389
+ function extendRowIndex(arr) {
390
+ const entry = rowIndexes.get(arr);
391
+ if (!entry || entry.len > arr.length) return;
392
+ for (let i = entry.len; i < arr.length; i++) { const id = rowKey(arr[i]); if (id !== null) entry.map.set(id, i); }
393
+ entry.len = arr.length;
394
+ }
395
+
396
+ function findRow(dataSourceName, id) {
397
+ const store = typeof Alpine !== 'undefined' ? Alpine.store('data') : null;
398
+ const arr = store ? rawOf(store)[dataSourceName] : null;
399
+ const i = indexOfRow(arr, id);
400
+ return i === -1 ? null : arr[i];
401
+ }
402
+
403
+ // Queued by realtime, not yet in the store
404
+ function isRowPending(dataSourceName, id) {
405
+ for (const op of pendingLandings) {
406
+ if (op.source === dataSourceName && Array.isArray(op.rows) && op.rows.some(row => rowKey(row) === id)) return true;
407
+ }
408
+ return false;
409
+ }
410
+
411
+ // Structural edit in place: one reactive write (length to 0) wakes every reader, rows return on the raw array
412
+ function setRows(store, dataSourceName, next) {
413
+ const raw = rawOf(store)[dataSourceName];
414
+ const cur = store[dataSourceName];
415
+ if (!Array.isArray(raw)) {
416
+ store[dataSourceName] = next;
417
+ const arr = rawOf(store)[dataSourceName];
418
+ rawDataStore.set(dataSourceName, arr);
419
+ attachMethods(arr, dataSourceName);
420
+ return;
421
+ }
422
+ dropRowIndex(raw);
423
+ if (!raw.length) {
424
+ for (const row of next) cur.push(row);
425
+ return;
426
+ }
427
+ cur.length = 0;
428
+ for (let i = 0; i < next.length; i++) raw.push(next[i]);
429
+ }
430
+
431
+ function insertRowAt(store, dataSourceName, index, row) {
432
+ const next = rawOf(store)[dataSourceName].slice();
433
+ next.splice(index, 0, rawOf(row));
434
+ setRows(store, dataSourceName, next);
435
+ }
436
+
437
+ function removeRowAt(store, dataSourceName, index, replacement) {
438
+ const next = rawOf(store)[dataSourceName].slice();
439
+ if (replacement === undefined) next.splice(index, 1); else next.splice(index, 1, rawOf(replacement));
440
+ setRows(store, dataSourceName, next);
441
+ }
442
+
360
443
  function ensureStoreShape(store) {
361
444
  if (!store._v) store._v = {};
362
445
  }
@@ -372,16 +455,21 @@ function touchSources(store, sources) {
372
455
  store._dataVersion = (store._dataVersion || 0) + 1;
373
456
  }
374
457
 
458
+ // Sync writes (they can run inside Alpine's flush) are re-bumped by the next post-settle hammer
459
+ const writtenSources = new Set();
460
+
375
461
  function touchSource(dataSourceName) {
376
462
  const store = typeof Alpine !== 'undefined' ? Alpine.store('data') : null;
463
+ writtenSources.add(dataSourceName);
377
464
  if (store) touchSources(store, [dataSourceName]);
378
465
  }
379
466
 
380
467
  // Post-settle hammer: re-run every `$x` reader (Alpine scheduler swallow)
381
- function bumpAllVersions() {
468
+ function bumpAllVersions(only) {
382
469
  const store = typeof Alpine !== 'undefined' ? Alpine.store('data') : null;
383
470
  if (!store) return;
384
471
  ensureStoreShape(store);
472
+ if (only) { if (only.size) touchSources(store, only); return; }
385
473
  const raw = rawOf(store);
386
474
  const sources = new Set(Object.keys(raw._v || {}));
387
475
  for (const key of Object.keys(raw)) {
@@ -409,6 +497,7 @@ function mergeRowFields(target, fields, dataSourceName) {
409
497
  if (key === '$files') continue;
410
498
  const value = fields[key];
411
499
  if (sameScalarOrList(raw[key], value)) continue;
500
+ if (key === '$id') dropRowIndex(rawDataStore.get(dataSourceName));
412
501
  target[key] = createReactiveReferences(value, dataSourceName);
413
502
  }
414
503
  }
@@ -470,6 +559,7 @@ function resetSource(dataSourceName) {
470
559
  proxies?.clearArrayProxyCacheForDataSource?.(dataSourceName);
471
560
  proxies?.clearRouteProxyCacheForDataSource?.(dataSourceName);
472
561
  proxies?.clearNestedProxyCacheForDataSource?.(dataSourceName);
562
+ writtenSources.add(dataSourceName);
473
563
  touchSources(store, [dataSourceName]);
474
564
  }
475
565
 
@@ -482,6 +572,7 @@ function setSourceState(dataSourceName, patch) {
482
572
  const next = { ...current, ...patch };
483
573
  if (patch.error !== undefined) next.errorTime = patch.error ? (patch.errorTime || Date.now()) : null;
484
574
  store[key] = next;
575
+ writtenSources.add(dataSourceName);
485
576
  return next;
486
577
  }
487
578
 
@@ -559,15 +650,15 @@ function touchPending(dataSourceName) {
559
650
  const key = `_${dataSourceName}_state`;
560
651
  const current = store[key] || { loading: false, error: null, ready: false, stale: true };
561
652
  const pending = pendingCount(dataSourceName);
562
- if (current.pending !== pending) store[key] = { ...current, pending };
653
+ if (current.pending !== pending) { store[key] = { ...current, pending }; writtenSources.add(dataSourceName); }
563
654
  }
564
655
 
565
656
  // Local row insert: at its sort position on a sorted source, else at the end
566
657
  function insertRow(store, dataSourceName, row) {
567
658
  const target = store[dataSourceName];
568
659
  const order = window.ManifestDataMain?.orderFor?.(dataSourceName);
569
- if (!order) { target.push(row); return; }
570
- target.splice(sortedIndex(rawOf(store)[dataSourceName], rawOf(row), compareBy(order)), 0, row);
660
+ if (!order) { target.push(row); extendRowIndex(rawOf(store)[dataSourceName]); return; }
661
+ insertRowAt(store, dataSourceName, sortedIndex(rawOf(store)[dataSourceName], rawOf(row), compareBy(order)), row);
571
662
  }
572
663
 
573
664
  // After a landing: held writes win over the landed payload. reads: this flush's read
@@ -575,12 +666,12 @@ function insertRow(store, dataSourceName, row) {
575
666
  function reapplyPending(store, dataSourceName, readLandings = []) {
576
667
  const byId = pendingWrites.get(dataSourceName);
577
668
  if (!byId) return false;
578
- const curRaw = rawOf(store)[dataSourceName];
579
- if (!Array.isArray(curRaw)) return false;
669
+ const rawRows = () => rawOf(store)[dataSourceName];
670
+ if (!Array.isArray(rawRows())) return false;
580
671
  const now = Date.now();
581
672
  let changed = false;
582
673
  for (const [id, list] of [...byId]) {
583
- let i = curRaw.findIndex(row => rowKey(row) === id);
674
+ let i = indexOfRow(rawRows(), id);
584
675
  // Only reads that carried this row (a replace carries every row) speak for it
585
676
  const reads = readLandings.filter(r => r.ids === null || r.ids.has(id)).map(r => r.token);
586
677
  for (const write of [...list]) {
@@ -593,16 +684,16 @@ function reapplyPending(store, dataSourceName, readLandings = []) {
593
684
  if (!reads.some(t => t <= write.settledAt)) continue; // realtime is newer than the write
594
685
  }
595
686
  if (write.kind === 'delete') {
596
- if (i !== -1) { store[dataSourceName].splice(i, 1); i = -1; changed = true; }
687
+ if (i !== -1) { removeRowAt(store, dataSourceName, i); i = -1; changed = true; }
597
688
  } else if (write.kind === 'create') {
598
689
  // A placeholder stays only where it belongs (a $query that excludes it drops it)
599
690
  if (i === -1 && (window.ManifestDataMain?.rowFitsSource?.(dataSourceName, write.fields) ?? true)) {
600
691
  insertRow(store, dataSourceName, createReactiveReferences(write.fields, dataSourceName));
601
- i = curRaw.findIndex(row => rowKey(row) === id);
692
+ i = indexOfRow(rawRows(), id);
602
693
  changed = true;
603
694
  }
604
695
  } else if (i !== -1) {
605
- const row = curRaw[i];
696
+ const row = rawRows()[i];
606
697
  if (Object.keys(write.fields).some(k => !sameScalarOrList(row[k], write.fields[k]))) {
607
698
  mergeRowFields(store[dataSourceName][i], write.fields, dataSourceName);
608
699
  changed = true;
@@ -697,7 +788,7 @@ function upsertOrdered(store, dataSourceName, rows, order) {
697
788
  for (const row of rows) {
698
789
  const list = next || curRaw;
699
790
  const id = rowKey(row);
700
- const i = id !== null ? list.findIndex(r => rowKey(r) === id) : -1;
791
+ const i = id === null ? -1 : (next ? next.findIndex(r => rowKey(r) === id) : indexOfRow(curRaw, id));
701
792
  if (i !== -1) {
702
793
  // Only a changed sort value moves a held row; the server's placement otherwise stands
703
794
  const keyChanged = order.some(([key]) => Object.prototype.hasOwnProperty.call(row, key) && !sameScalarOrList(list[i][key], row[key]));
@@ -730,15 +821,13 @@ function upsertRows(store, dataSourceName, rows, mode, order) {
730
821
  return;
731
822
  }
732
823
  const cur = store[dataSourceName];
733
- const index = new Map();
734
- curRaw.forEach((row, i) => { const id = rowKey(row); if (id !== null) index.set(id, i); });
735
824
  const fresh = new Map();
736
825
  const next = [];
737
826
  const appended = [];
738
827
  for (const row of rows) {
739
828
  const id = rowKey(row);
740
- const i = id !== null ? index.get(id) : undefined;
741
- if (i !== undefined) {
829
+ const i = indexOfRow(curRaw, id);
830
+ if (i !== -1) {
742
831
  mergeRowFields(cur[i], row, dataSourceName);
743
832
  if (mode === 'replace') next.push(curRaw[i]);
744
833
  continue;
@@ -755,20 +844,19 @@ function upsertRows(store, dataSourceName, rows, mode, order) {
755
844
  if (next.length !== curRaw.length || next.some((row, i) => row !== curRaw[i])) store[dataSourceName] = next;
756
845
  } else if (appended.length) {
757
846
  cur.push(...appended);
847
+ extendRowIndex(curRaw);
758
848
  }
759
849
  }
760
850
 
761
851
  function removeRows(store, dataSourceName, ids) {
762
852
  const curRaw = rawOf(store)[dataSourceName];
763
853
  if (!Array.isArray(curRaw) || !ids?.length) return false;
854
+ if (ids.length === 1 && indexOfRow(curRaw, ids[0]) === -1) return false;
764
855
  const set = new Set(ids);
765
- const cur = store[dataSourceName];
766
- let removed = false;
767
- for (let i = curRaw.length - 1; i >= 0; i--) {
768
- const id = rowKey(curRaw[i]);
769
- if (id !== null && set.has(id)) { cur.splice(i, 1); removed = true; }
770
- }
771
- return removed;
856
+ const next = curRaw.filter(row => { const id = rowKey(row); return id === null || !set.has(id); });
857
+ if (next.length === curRaw.length) return false;
858
+ setRows(store, dataSourceName, next);
859
+ return true;
772
860
  }
773
861
 
774
862
  // Synchronous write of data + state for one source (no version bump); returns the new state
@@ -834,7 +922,7 @@ function updateStore(dataSourceName, data, options = {}) {
834
922
  const state = writeSource(dataSourceName, data, { ...options, mode: 'replace' });
835
923
  if (!state) return;
836
924
  touchSource(dataSourceName);
837
- if (!state.loading) checkAndDispatchRenderReady();
925
+ if (!state.loading) checkAndDispatchRenderReady([dataSourceName]);
838
926
  }
839
927
 
840
928
  // Network landing (page load, paged append, realtime batch): buffered, applied
@@ -858,8 +946,27 @@ function queueLanding(op) {
858
946
  });
859
947
  }
860
948
 
949
+ function isRealtimeOp(op) {
950
+ for (const key of Object.keys(op.options || {})) if (!MERGE_KEYS.has(key)) return false;
951
+ return !!op.remove || (Array.isArray(op.rows) && op.options?.mode === 'append');
952
+ }
953
+
861
954
  function scheduleLandingFlush() {
955
+ const urgent = !isRealtimeOp(pendingLandings[pendingLandings.length - 1]);
956
+ if (landingDeferred && urgent) {
957
+ clearTimeout(landingTimer);
958
+ landingTimer = null;
959
+ landingDeferred = false;
960
+ }
862
961
  if (landingFrame !== null || landingTimer !== null) return;
962
+ if (!urgent && landingCost > LANDING_BUSY_MS && pendingLandings.every(isRealtimeOp)) {
963
+ const wait = Math.min(LANDING_MAX_WAIT_MS, landingCost * 2) - (performance.now() - landingEndedAt);
964
+ if (wait > 16) {
965
+ landingDeferred = true;
966
+ landingTimer = setTimeout(flushLandings, wait);
967
+ return;
968
+ }
969
+ }
863
970
  const hidden = typeof document !== 'undefined' && document.hidden;
864
971
  if (!hidden && typeof requestAnimationFrame === 'function') {
865
972
  landingFrame = requestAnimationFrame(flushLandings);
@@ -880,9 +987,40 @@ function noteLocalWrite(dataSourceName, id, note) {
880
987
  byId.set(id, { removed: false, patch: { ...base, ...note.patch } });
881
988
  }
882
989
 
990
+ // Same-source appends (realtime rows) and removes queued back to back apply as one write
991
+ const MERGE_KEYS = new Set(['mode', 'generation', 'order']);
992
+ function mergesWith(prev, op) {
993
+ if (prev.remove || op.remove) return !!(prev.remove && op.remove);
994
+ const a = prev.options, b = op.options;
995
+ if (!Array.isArray(prev.rows) || !Array.isArray(op.rows) || a.mode !== 'append' || b.mode !== 'append') return false;
996
+ for (const key of Object.keys(a)) if (!MERGE_KEYS.has(key)) return false;
997
+ for (const key of Object.keys(b)) if (!MERGE_KEYS.has(key)) return false;
998
+ return a.generation === b.generation && (a.order === b.order || JSON.stringify(a.order) === JSON.stringify(b.order));
999
+ }
1000
+
1001
+ function groupLandings(ops) {
1002
+ const groups = [];
1003
+ const last = new Map();
1004
+ for (const op of ops) {
1005
+ const prev = last.get(op.source);
1006
+ if (prev && mergesWith(prev, op)) {
1007
+ if (op.remove) prev.remove = prev.remove.concat(op.remove); else prev.rows = prev.rows.concat(op.rows);
1008
+ continue;
1009
+ }
1010
+ const group = { source: op.source, remove: op.remove, rows: op.rows, options: op.options, op };
1011
+ groups.push(group);
1012
+ last.set(op.source, group);
1013
+ }
1014
+ return groups;
1015
+ }
1016
+
883
1017
  function flushLandings() {
884
1018
  if (landingFrame !== null) { cancelAnimationFrame(landingFrame); landingFrame = null; }
885
1019
  if (landingTimer !== null) { clearTimeout(landingTimer); landingTimer = null; }
1020
+ landingDeferred = false;
1021
+ const startedAt = typeof performance !== 'undefined' ? performance.now() : 0;
1022
+ // Measured on the next task: after the microtask flush of the effects this write re-runs
1023
+ if (startedAt) setTimeout(() => { landingEndedAt = performance.now(); landingCost = landingEndedAt - startedAt; }, 0);
886
1024
  const ops = pendingLandings.splice(0);
887
1025
  const resolvers = landingResolvers.splice(0);
888
1026
  const local = new Map(pendingLocal);
@@ -893,22 +1031,23 @@ function flushLandings() {
893
1031
  const touched = new Set();
894
1032
  const landed = new Set(); // network-origin landings (persistence snapshots these)
895
1033
  const skipped = new Set();
896
- let settled = false;
1034
+ const settled = new Set();
897
1035
  if (store) {
898
- for (const op of ops) {
1036
+ for (const group of groupLandings(ops)) {
1037
+ const op = group.op;
899
1038
  try {
900
1039
  if (op.remove) {
901
- if (removeRows(store, op.source, op.remove)) { touched.add(op.source); landed.add(op.source); }
1040
+ if (removeRows(store, op.source, group.remove)) { touched.add(op.source); landed.add(op.source); }
902
1041
  continue;
903
1042
  }
904
1043
  // A newer read began since this landing was queued (e.g. an identity change): it never applies
905
1044
  if (typeof op.options?.readToken === 'number' && !isLatestRead(op.source, op.options.readToken)) { skipped.add(op); continue; }
906
- const state = writeSource(op.source, op.rows, op.options);
1045
+ const state = writeSource(op.source, group.rows, op.options);
907
1046
  if (!state) continue;
908
1047
  touched.add(op.source);
909
1048
  if (!op.options.persistHydration && op.rows !== null && op.rows !== undefined) landed.add(op.source);
910
1049
  // Only load completions (explicit loading state) feed render-ready; realtime upserts never do
911
- if (op.options.loading !== undefined && !state.loading) settled = true;
1050
+ if (op.options.loading !== undefined && !state.loading) settled.add(op.source);
912
1051
  } catch (error) {
913
1052
  console.error(`[Manifest Data] Landing failed for "${op.source}":`, error);
914
1053
  }
@@ -925,8 +1064,7 @@ function flushLandings() {
925
1064
  if (removeRows(store, source, [id])) touched.add(source);
926
1065
  continue;
927
1066
  }
928
- const curRaw = rawOf(store)[source];
929
- const i = Array.isArray(curRaw) ? curRaw.findIndex(row => rowKey(row) === id) : -1;
1067
+ const i = indexOfRow(rawOf(store)[source], id);
930
1068
  if (i !== -1) { mergeRowFields(store[source][i], note.patch, source); touched.add(source); }
931
1069
  }
932
1070
  }
@@ -934,7 +1072,7 @@ function flushLandings() {
934
1072
  }
935
1073
  resolvers.forEach((resolve, i) => resolve(!skipped.has(ops[i])));
936
1074
  if (landed.size) window.ManifestDataPersist?.onLanded?.(landed);
937
- if (settled) checkAndDispatchRenderReady();
1075
+ if (settled.size) checkAndDispatchRenderReady(settled);
938
1076
  }
939
1077
 
940
1078
  // Cross-source `all`: built on first read after a change, versioned by _v.all
@@ -1397,7 +1535,11 @@ function setupTeamChangeListener() {
1397
1535
  }
1398
1536
 
1399
1537
  // Dispatch manifest:render-ready once all sources settle (debounced to coalesce).
1400
- function checkAndDispatchRenderReady() {
1538
+ let hammeredAll = false;
1539
+
1540
+ function checkAndDispatchRenderReady(sources) {
1541
+ if (sources) for (const source of sources) writtenSources.add(source);
1542
+ else hammeredAll = false;
1401
1543
  if (_renderReadyTimer) {
1402
1544
  clearTimeout(_renderReadyTimer);
1403
1545
  }
@@ -1436,8 +1578,12 @@ function checkAndDispatchRenderReady() {
1436
1578
  // swallow) — an x-if that read this store before data arrived may
1437
1579
  // never re-run. One post-settle bump on a fresh task re-runs
1438
1580
  // anything dropped.
1581
+ // First bump of a page-load or locale change: every source. Later: sources written or settled since
1582
+ const only = hammeredAll ? new Set(writtenSources) : null;
1583
+ writtenSources.clear();
1584
+ hammeredAll = true;
1439
1585
  setTimeout(() => {
1440
- try { bumpAllVersions(); } catch (_) { /* no-op */ }
1586
+ try { bumpAllVersions(only); } catch (_) { /* no-op */ }
1441
1587
  }, 50);
1442
1588
  } catch {
1443
1589
  // Silently fail — the render script has its own timeout fallback
@@ -1766,6 +1912,11 @@ window.ManifestDataStore = {
1766
1912
  setSourceState,
1767
1913
  runDeduped,
1768
1914
  insertRow,
1915
+ insertRowAt,
1916
+ removeRowAt,
1917
+ indexOfRow,
1918
+ findRow,
1919
+ isRowPending,
1769
1920
  holdWrite,
1770
1921
  releaseWrite,
1771
1922
  settleWrite,
@@ -3215,7 +3366,8 @@ function findRawIndex(dataSourceName, entryId) {
3215
3366
  const raw = window.ManifestDataStore?.rawOf?.(store) || store;
3216
3367
  const arr = raw?.[dataSourceName];
3217
3368
  if (!Array.isArray(arr)) return { store, arr: null, index: -1 };
3218
- return { store, arr, index: arr.findIndex(entry => entry && entry.$id === entryId) };
3369
+ const indexOfRow = window.ManifestDataStore?.indexOfRow;
3370
+ return { store, arr, index: indexOfRow ? indexOfRow(arr, entryId) : arr.findIndex(entry => entry && entry.$id === entryId) };
3219
3371
  }
3220
3372
 
3221
3373
  // Update a single entry in the store (scoped update)
@@ -3297,9 +3449,9 @@ function removeEntryFromStore(dataSourceName, entryId, options = {}) {
3297
3449
  // Store original entry for rollback
3298
3450
  const originalEntry = arr[index];
3299
3451
 
3300
- // Local write: splice the tracked array, sync
3452
+ // Local write: one structural write on the tracked source, sync
3301
3453
  const ds = window.ManifestDataStore;
3302
- store[dataSourceName].splice(index, 1);
3454
+ ds.removeRowAt(store, dataSourceName, index);
3303
3455
  ds.noteLocalWrite(dataSourceName, entryId, { removed: true });
3304
3456
  ds.touchSource(dataSourceName);
3305
3457
  clearSourceCaches(dataSourceName);
@@ -3374,7 +3526,7 @@ function insertEntryAt(dataSourceName, entry, index, options = {}) {
3374
3526
  if (window.ManifestDataMain?.orderFor?.(dataSourceName) && ds.insertRow) {
3375
3527
  ds.insertRow(store, dataSourceName, row);
3376
3528
  } else {
3377
- store[dataSourceName].splice(restoreIndex(arr, options.order, entry.$id, index), 0, row);
3529
+ ds.insertRowAt(store, dataSourceName, restoreIndex(arr, options.order, entry.$id, index), row);
3378
3530
  }
3379
3531
  ds.touchSource(dataSourceName);
3380
3532
  clearSourceCaches(dataSourceName);
@@ -3471,7 +3623,7 @@ async function executeMutation(mutationConfig) {
3471
3623
  const store = Alpine.store('data');
3472
3624
  // Snapshot only the patched fields: rollback restores those, nothing else
3473
3625
  if (store && store[dataSourceName] && Array.isArray(store[dataSourceName])) {
3474
- const found = store[dataSourceName].find(e => e.$id === entryId);
3626
+ const found = ds0?.findRow ? ds0.findRow(dataSourceName, entryId) : store[dataSourceName].find(e => e.$id === entryId);
3475
3627
  originalData = found && data ? Object.fromEntries(Object.keys(data).map(k => [k, cloneValue(found[k])])) : null;
3476
3628
  }
3477
3629
 
@@ -3535,11 +3687,11 @@ async function executeMutation(mutationConfig) {
3535
3687
  // Ack: swap the temp row for the server row in place (new $id → new $files binding)
3536
3688
  const { store, arr, index } = findRawIndex(dataSourceName, optimisticData.$id);
3537
3689
  // Realtime echo of this create may have landed first under the real $id
3538
- const held = (arr && result.$id !== optimisticData.$id) ? arr.findIndex(r => r && r.$id === result.$id) : -1;
3690
+ const held = (arr && result.$id !== optimisticData.$id) ? window.ManifestDataStore.indexOfRow(arr, result.$id) : -1;
3539
3691
  if (store && arr && held !== -1) {
3540
3692
  const ds = window.ManifestDataStore;
3541
3693
  ds.mergeRowFields(store[dataSourceName][held], result, dataSourceName);
3542
- if (index !== -1) store[dataSourceName].splice(index, 1);
3694
+ if (index !== -1) ds.removeRowAt(store, dataSourceName, index);
3543
3695
  ds.touchSource(dataSourceName);
3544
3696
  clearSourceCaches(dataSourceName);
3545
3697
  } else if (store && arr) {
@@ -3548,10 +3700,10 @@ async function executeMutation(mutationConfig) {
3548
3700
  const ds = window.ManifestDataStore;
3549
3701
  const created = ds.createReactiveReferences(result, dataSourceName);
3550
3702
  if (window.ManifestDataMain?.orderFor?.(dataSourceName)) {
3551
- store[dataSourceName].splice(index, 1);
3703
+ ds.removeRowAt(store, dataSourceName, index);
3552
3704
  ds.insertRow(store, dataSourceName, created);
3553
3705
  } else {
3554
- store[dataSourceName].splice(index, 1, created); // unsorted: keeps its place
3706
+ ds.removeRowAt(store, dataSourceName, index, created); // unsorted: keeps its place
3555
3707
  }
3556
3708
  ds.touchSource(dataSourceName);
3557
3709
  clearSourceCaches(dataSourceName);
@@ -11715,7 +11867,7 @@ function realtimeAppend(dataSourceName, row, currentRows, generation, order) {
11715
11867
  let evicted = null;
11716
11868
  for (let i = 0; i < meta.appended.length; i++) {
11717
11869
  // Oldest appended row still landed; pending (unflushed) ids stay tracked
11718
- if (currentRows.some(r => r && r.$id === meta.appended[i])) {
11870
+ if (window.ManifestDataStore.findRow(dataSourceName, meta.appended[i])) {
11719
11871
  evicted = meta.appended.splice(i, 1)[0];
11720
11872
  break;
11721
11873
  }
@@ -11785,8 +11937,8 @@ async function handleTableRealtimeEvent(dataSourceName, databaseId, tableId, sco
11785
11937
  // Scope AND queries must match before the row lands
11786
11938
  const rowMatchesScope = await checkRowMatchesScope(row, scope, scopeColumns);
11787
11939
  if (rowMatchesScope && eventMatchesQueries(row, queriesConfig)) {
11788
- if (currentRows.find(r => r.$id === row.$id)) {
11789
- // Echo of a row already held (optimistic create): merge, never counts as growth
11940
+ if (window.ManifestDataStore.findRow(dataSourceName, row.$id) || window.ManifestDataStore.isRowPending(dataSourceName, row.$id)) {
11941
+ // Echo of a row already held or queued (optimistic create): merge, never counts as growth
11790
11942
  landRows(dataSourceName, [row], { mode: 'append', generation, order });
11791
11943
  } else if (!beyondLoaded(dataSourceName, row)) {
11792
11944
  realtimeAppend(dataSourceName, row, currentRows, generation, order);
@@ -11798,16 +11950,27 @@ async function handleTableRealtimeEvent(dataSourceName, databaseId, tableId, sco
11798
11950
  } else if (eventType === 'update') {
11799
11951
  const row = payload?.$id ? payload : (payload?.row || payload);
11800
11952
  if (row && row.$id) {
11801
- const existingRow = currentRows.find(r => r.$id === row.$id);
11953
+ const existingRow = window.ManifestDataStore.findRow(dataSourceName, row.$id);
11802
11954
  const rowMatchesScope = await checkRowMatchesScope(row, scope, scopeColumns);
11955
+ // Queued by an earlier event of this flush window, not yet in the store
11956
+ const queued = !existingRow && window.ManifestDataStore.isRowPending(dataSourceName, row.$id);
11803
11957
  if (!rowMatchesScope || !eventMatchesQueries(row, queriesConfig)) {
11804
11958
  // Row stopped matching scope or queries, remove it
11805
- if (existingRow) {
11959
+ if (existingRow || queued) {
11806
11960
  landRemove(dataSourceName, [row.$id], { generation });
11807
11961
  forgetRealtimeRow(dataSourceName, row.$id);
11808
11962
  }
11809
11963
  return;
11810
11964
  }
11965
+ if (queued) {
11966
+ if (beyondLoaded(dataSourceName, row)) {
11967
+ landRemove(dataSourceName, [row.$id], { generation });
11968
+ forgetRealtimeRow(dataSourceName, row.$id);
11969
+ } else {
11970
+ landRows(dataSourceName, [row], { mode: 'append', generation, order });
11971
+ }
11972
+ return;
11973
+ }
11811
11974
  if (!existingRow) {
11812
11975
  // Row not in list, but matches now - add it (capped like a create)
11813
11976
  if (!beyondLoaded(dataSourceName, row)) realtimeAppend(dataSourceName, row, currentRows, generation, order);