@tanstack/virtual-core 3.17.9 → 3.17.10

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.
@@ -164,6 +164,27 @@ const scrollWithAdjustments = (offset, {
164
164
  };
165
165
  const windowScroll = scrollWithAdjustments;
166
166
  const elementScroll = scrollWithAdjustments;
167
+ function isAppendWithTrim(prevCount, nextCount, getPreviousKey, getNextKey) {
168
+ if (nextCount === 0) return false;
169
+ const firstKey = getNextKey(0);
170
+ const removedKeys = /* @__PURE__ */ new Set();
171
+ let removedCount = 0;
172
+ while (removedCount < prevCount) {
173
+ const key = getPreviousKey(removedCount);
174
+ if (key === firstKey) break;
175
+ removedKeys.add(key);
176
+ removedCount++;
177
+ }
178
+ const retainedCount = prevCount - removedCount;
179
+ if (retainedCount === 0 || retainedCount >= nextCount) return false;
180
+ for (let i = 0; i < retainedCount; i++) {
181
+ if (getNextKey(i) !== getPreviousKey(removedCount + i)) return false;
182
+ }
183
+ for (let i = retainedCount; i < nextCount; i++) {
184
+ if (removedKeys.has(getNextKey(i))) return false;
185
+ }
186
+ return true;
187
+ }
167
188
  class Virtualizer {
168
189
  constructor(opts) {
169
190
  this.unsubs = [];
@@ -172,7 +193,7 @@ class Virtualizer {
172
193
  this.isScrolling = false;
173
194
  this.scrollState = null;
174
195
  this.measurementsCache = [];
175
- this._flatMeasurements = null;
196
+ this._singleLaneMeasurements = null;
176
197
  this.itemSizeCache = /* @__PURE__ */ new Map();
177
198
  this.itemSizeCacheVersion = 0;
178
199
  this.laneAssignments = /* @__PURE__ */ new Map();
@@ -250,7 +271,7 @@ class Virtualizer {
250
271
  })();
251
272
  this.range = null;
252
273
  this.setOptions = (opts2) => {
253
- var _a, _b;
274
+ var _a;
254
275
  const merged = {
255
276
  debug: false,
256
277
  initialOffset: 0,
@@ -294,8 +315,10 @@ class Virtualizer {
294
315
  const prevCount = prevOptions.count;
295
316
  const nextCount = merged.count;
296
317
  const measurements = this.getMeasurements();
297
- const prevFirstKey = prevCount > 0 ? ((_a = measurements[0]) == null ? void 0 : _a.key) ?? prevOptions.getItemKey(0) : null;
298
- const prevLastKey = prevCount > 0 ? ((_b = measurements[prevCount - 1]) == null ? void 0 : _b.key) ?? prevOptions.getItemKey(prevCount - 1) : null;
318
+ const previousItems = ((_a = this._singleLaneMeasurements) == null ? void 0 : _a.items) ?? measurements;
319
+ const getPreviousKey = (index) => lazyMeasurements.getMeasurementKey(previousItems[index]);
320
+ const prevFirstKey = prevCount > 0 ? getPreviousKey(0) : null;
321
+ const prevLastKey = prevCount > 0 ? getPreviousKey(prevCount - 1) : null;
299
322
  const didCountChange = nextCount !== prevCount;
300
323
  const didEdgeKeysChange = didCountChange || prevCount > 0 && nextCount > 0 && (merged.getItemKey(0) !== prevFirstKey || merged.getItemKey(nextCount - 1) !== prevLastKey);
301
324
  if (didEdgeKeysChange) {
@@ -305,8 +328,15 @@ class Virtualizer {
305
328
  anchor = [item.key, this.getScrollOffset() - item.start];
306
329
  }
307
330
  const behavior = merged.followOnAppend === true ? "auto" : merged.followOnAppend || null;
308
- if (behavior && nextCount > prevCount && this.isAtEnd(prevOptions.scrollEndThreshold) && (prevCount === 0 || merged.getItemKey(nextCount - 1) !== prevLastKey)) {
309
- followOnAppend = behavior;
331
+ if (behavior && nextCount > 0 && this.isAtEnd(prevOptions.scrollEndThreshold) && (prevCount === 0 || merged.getItemKey(nextCount - 1) !== prevLastKey)) {
332
+ if (nextCount > prevCount || isAppendWithTrim(
333
+ prevCount,
334
+ nextCount,
335
+ getPreviousKey,
336
+ merged.getItemKey
337
+ )) {
338
+ followOnAppend = behavior;
339
+ }
310
340
  }
311
341
  }
312
342
  }
@@ -329,7 +359,7 @@ class Virtualizer {
329
359
  const anchorItem = newMeasurements[idx];
330
360
  if (anchorItem) {
331
361
  const newOffset = Math.max(0, anchorItem.start + anchorOffset);
332
- if (newOffset !== this.scrollOffset) {
362
+ if (!followOnAppend && newOffset !== this.scrollOffset) {
333
363
  anchorDelta = newOffset - this.scrollOffset;
334
364
  this.scrollOffset = newOffset;
335
365
  anchorResolved = true;
@@ -607,9 +637,11 @@ class Virtualizer {
607
637
  laneAssignmentMode,
608
638
  gap
609
639
  }, _itemSizeCacheVersion) => {
640
+ var _a;
610
641
  const itemSizeCache = this.itemSizeCache;
611
642
  if (!enabled) {
612
643
  this.measurementsCache = [];
644
+ this._singleLaneMeasurements = null;
613
645
  this.itemSizeCache.clear();
614
646
  this.laneAssignments.clear();
615
647
  return [];
@@ -625,6 +657,7 @@ class Virtualizer {
625
657
  this.lanesChangedFlag = false;
626
658
  this.lanesSettling = true;
627
659
  this.measurementsCache = [];
660
+ this._singleLaneMeasurements = null;
628
661
  this.itemSizeCache.clear();
629
662
  this.laneAssignments.clear();
630
663
  this.pendingMin = null;
@@ -642,13 +675,13 @@ class Virtualizer {
642
675
  }
643
676
  if (lanes === 1) {
644
677
  const need = count * 2;
645
- let flat = this._flatMeasurements;
678
+ let flat = (_a = this._singleLaneMeasurements) == null ? void 0 : _a.flat;
646
679
  if (!flat || flat.length < need) {
647
680
  const next = new Float64Array(need);
648
681
  if (flat && min > 0) next.set(flat.subarray(0, min * 2));
649
682
  flat = next;
650
- this._flatMeasurements = flat;
651
683
  }
684
+ const items = min === 0 ? new Array(count) : this._singleLaneMeasurements.items.slice();
652
685
  let runningStart;
653
686
  if (min === 0) {
654
687
  runningStart = paddingStart + scrollMargin;
@@ -658,13 +691,15 @@ class Virtualizer {
658
691
  }
659
692
  for (let i = min; i < count; i++) {
660
693
  const key = getItemKey(i);
694
+ items[i] = key;
661
695
  const measuredSize = itemSizeCache.get(key);
662
696
  const size = typeof measuredSize === "number" ? measuredSize : this.options.estimateSize(i);
663
697
  flat[i * 2] = runningStart;
664
698
  flat[i * 2 + 1] = size;
665
699
  runningStart += size + gap;
666
700
  }
667
- const view = lazyMeasurements.createLazyMeasurementsView(count, flat, getItemKey);
701
+ this._singleLaneMeasurements = { flat, items };
702
+ const view = lazyMeasurements.createLazyMeasurementsView(items, flat);
668
703
  this.measurementsCache = view;
669
704
  return view;
670
705
  }
@@ -759,7 +794,7 @@ class Virtualizer {
759
794
  lanes,
760
795
  // Pass the typed array so binary search + forward-walk can read
761
796
  // start/end directly from Float64Array, skipping the Proxy traps.
762
- lanes === 1 && this._flatMeasurements != null ? this._flatMeasurements : null
797
+ lanes === 1 && this._singleLaneMeasurements !== null ? this._singleLaneMeasurements.flat : null
763
798
  );
764
799
  return this.range;
765
800
  },
@@ -856,13 +891,13 @@ class Virtualizer {
856
891
  }
857
892
  };
858
893
  this.resizeItem = (index, size) => {
859
- var _a, _b;
894
+ var _a, _b, _c;
860
895
  if (!this.isIndexInRange(index)) return;
861
896
  let cachedSize;
862
897
  let itemStart;
863
898
  let key;
864
- const flat = this._flatMeasurements;
865
- if (this.options.lanes === 1 && flat !== null) {
899
+ const flat = (_a = this._singleLaneMeasurements) == null ? void 0 : _a.flat;
900
+ if (this.options.lanes === 1 && flat != null) {
866
901
  key = this.options.getItemKey(index);
867
902
  itemStart = flat[index * 2];
868
903
  cachedSize = flat[index * 2 + 1];
@@ -876,7 +911,7 @@ class Virtualizer {
876
911
  const itemSize = this.itemSizeCache.get(key) ?? cachedSize;
877
912
  const delta = size - itemSize;
878
913
  if (delta !== 0) {
879
- const wasAtEnd = this.options.anchorTo === "end" && ((_a = this.scrollState) == null ? void 0 : _a.behavior) !== "smooth" && this.getVirtualDistanceFromEnd() <= this.options.scrollEndThreshold;
914
+ const wasAtEnd = this.options.anchorTo === "end" && ((_b = this.scrollState) == null ? void 0 : _b.behavior) !== "smooth" && this.getVirtualDistanceFromEnd() <= this.options.scrollEndThreshold;
880
915
  const prevTotalSize = wasAtEnd ? this.getTotalSize() : 0;
881
916
  const scrollOffsetWithAdj = this.getScrollOffset() + this.scrollAdjustments;
882
917
  const isFirstMeasure = !this.itemSizeCache.has(key);
@@ -895,7 +930,7 @@ class Virtualizer {
895
930
  // scrolling up" cascade.
896
931
  itemStart + itemSize <= scrollOffsetWithAdj && this.scrollDirection !== "backward"
897
932
  );
898
- const shouldAdjustScroll = ((_b = this.scrollState) == null ? void 0 : _b.behavior) !== "smooth" && (this.shouldAdjustScrollPositionOnItemSizeChange !== void 0 ? this.shouldAdjustScrollPositionOnItemSizeChange(
933
+ const shouldAdjustScroll = ((_c = this.scrollState) == null ? void 0 : _c.behavior) !== "smooth" && (this.shouldAdjustScrollPositionOnItemSizeChange !== void 0 ? this.shouldAdjustScrollPositionOnItemSizeChange(
899
934
  // The callback expects a VirtualItem; build one lazily only
900
935
  // when the consumer actually supplied a custom predicate.
901
936
  this.measurementsCache[index] ?? {
@@ -943,11 +978,12 @@ class Virtualizer {
943
978
  }
944
979
  );
945
980
  this.getVirtualItemForOffset = (offset) => {
981
+ var _a;
946
982
  const measurements = this.getMeasurements();
947
983
  if (measurements.length === 0) {
948
984
  return void 0;
949
985
  }
950
- const flat = this._flatMeasurements;
986
+ const flat = (_a = this._singleLaneMeasurements) == null ? void 0 : _a.flat;
951
987
  const useFlat = this.options.lanes === 1 && flat != null;
952
988
  const idx = findNearestBinarySearch(
953
989
  0,
@@ -1082,18 +1118,18 @@ class Virtualizer {
1082
1118
  });
1083
1119
  };
1084
1120
  this.getTotalSize = () => {
1085
- var _a;
1121
+ var _a, _b;
1086
1122
  const measurements = this.getMeasurements();
1087
1123
  let end;
1088
1124
  if (measurements.length === 0) {
1089
1125
  end = this.options.paddingStart;
1090
1126
  } else if (this.options.lanes === 1) {
1091
1127
  const lastIdx = measurements.length - 1;
1092
- const flat = this._flatMeasurements;
1128
+ const flat = (_a = this._singleLaneMeasurements) == null ? void 0 : _a.flat;
1093
1129
  if (flat != null) {
1094
1130
  end = flat[lastIdx * 2] + flat[lastIdx * 2 + 1];
1095
1131
  } else {
1096
- end = ((_a = measurements[lastIdx]) == null ? void 0 : _a.end) ?? 0;
1132
+ end = ((_b = measurements[lastIdx]) == null ? void 0 : _b.end) ?? 0;
1097
1133
  }
1098
1134
  } else {
1099
1135
  const endByLane = Array(this.options.lanes).fill(null);