@tanstack/virtual-core 3.17.9 → 3.17.11

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.
@@ -96,7 +96,7 @@ const observeOffset = (instance, cb, readOffset) => {
96
96
  let offset = 0;
97
97
  const fallback = registerScrollendEvent ? null : utils.debounce(
98
98
  targetWindow,
99
- () => cb(offset, false),
99
+ () => cb(readOffset(element), false),
100
100
  instance.options.isScrollingResetDelay
101
101
  );
102
102
  const createHandler = (isScrolling) => () => {
@@ -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;
@@ -396,7 +426,7 @@ class Virtualizer {
396
426
  };
397
427
  };
398
428
  this._willUpdate = () => {
399
- var _a;
429
+ var _a, _b;
400
430
  const scrollElement = this.options.enabled ? this.options.getScrollElement() : null;
401
431
  if (this.scrollElement !== scrollElement) {
402
432
  this.cleanup();
@@ -498,7 +528,11 @@ class Virtualizer {
498
528
  if (anchorDelta !== 0) {
499
529
  this._iosDeferredAdjustment += anchorDelta;
500
530
  }
501
- } else {
531
+ } else if (((_b = this.scrollState) == null ? void 0 : _b.behavior) === "smooth" && !utils.approxEqual(
532
+ this.getScrollOffset() - anchorDelta,
533
+ this.scrollState.lastTargetOffset
534
+ )) ;
535
+ else {
502
536
  this._scrollToOffset(this.getScrollOffset(), {
503
537
  adjustments: void 0,
504
538
  behavior: void 0
@@ -607,9 +641,11 @@ class Virtualizer {
607
641
  laneAssignmentMode,
608
642
  gap
609
643
  }, _itemSizeCacheVersion) => {
644
+ var _a;
610
645
  const itemSizeCache = this.itemSizeCache;
611
646
  if (!enabled) {
612
647
  this.measurementsCache = [];
648
+ this._singleLaneMeasurements = null;
613
649
  this.itemSizeCache.clear();
614
650
  this.laneAssignments.clear();
615
651
  return [];
@@ -625,6 +661,7 @@ class Virtualizer {
625
661
  this.lanesChangedFlag = false;
626
662
  this.lanesSettling = true;
627
663
  this.measurementsCache = [];
664
+ this._singleLaneMeasurements = null;
628
665
  this.itemSizeCache.clear();
629
666
  this.laneAssignments.clear();
630
667
  this.pendingMin = null;
@@ -642,13 +679,13 @@ class Virtualizer {
642
679
  }
643
680
  if (lanes === 1) {
644
681
  const need = count * 2;
645
- let flat = this._flatMeasurements;
682
+ let flat = (_a = this._singleLaneMeasurements) == null ? void 0 : _a.flat;
646
683
  if (!flat || flat.length < need) {
647
684
  const next = new Float64Array(need);
648
685
  if (flat && min > 0) next.set(flat.subarray(0, min * 2));
649
686
  flat = next;
650
- this._flatMeasurements = flat;
651
687
  }
688
+ const items = min === 0 ? new Array(count) : this._singleLaneMeasurements.items.slice();
652
689
  let runningStart;
653
690
  if (min === 0) {
654
691
  runningStart = paddingStart + scrollMargin;
@@ -658,13 +695,15 @@ class Virtualizer {
658
695
  }
659
696
  for (let i = min; i < count; i++) {
660
697
  const key = getItemKey(i);
698
+ items[i] = key;
661
699
  const measuredSize = itemSizeCache.get(key);
662
700
  const size = typeof measuredSize === "number" ? measuredSize : this.options.estimateSize(i);
663
701
  flat[i * 2] = runningStart;
664
702
  flat[i * 2 + 1] = size;
665
703
  runningStart += size + gap;
666
704
  }
667
- const view = lazyMeasurements.createLazyMeasurementsView(count, flat, getItemKey);
705
+ this._singleLaneMeasurements = { flat, items };
706
+ const view = lazyMeasurements.createLazyMeasurementsView(items, flat);
668
707
  this.measurementsCache = view;
669
708
  return view;
670
709
  }
@@ -759,7 +798,7 @@ class Virtualizer {
759
798
  lanes,
760
799
  // Pass the typed array so binary search + forward-walk can read
761
800
  // start/end directly from Float64Array, skipping the Proxy traps.
762
- lanes === 1 && this._flatMeasurements != null ? this._flatMeasurements : null
801
+ lanes === 1 && this._singleLaneMeasurements !== null ? this._singleLaneMeasurements.flat : null
763
802
  );
764
803
  return this.range;
765
804
  },
@@ -856,13 +895,13 @@ class Virtualizer {
856
895
  }
857
896
  };
858
897
  this.resizeItem = (index, size) => {
859
- var _a, _b;
898
+ var _a, _b, _c;
860
899
  if (!this.isIndexInRange(index)) return;
861
900
  let cachedSize;
862
901
  let itemStart;
863
902
  let key;
864
- const flat = this._flatMeasurements;
865
- if (this.options.lanes === 1 && flat !== null) {
903
+ const flat = (_a = this._singleLaneMeasurements) == null ? void 0 : _a.flat;
904
+ if (this.options.lanes === 1 && flat != null) {
866
905
  key = this.options.getItemKey(index);
867
906
  itemStart = flat[index * 2];
868
907
  cachedSize = flat[index * 2 + 1];
@@ -876,7 +915,7 @@ class Virtualizer {
876
915
  const itemSize = this.itemSizeCache.get(key) ?? cachedSize;
877
916
  const delta = size - itemSize;
878
917
  if (delta !== 0) {
879
- const wasAtEnd = this.options.anchorTo === "end" && ((_a = this.scrollState) == null ? void 0 : _a.behavior) !== "smooth" && this.getVirtualDistanceFromEnd() <= this.options.scrollEndThreshold;
918
+ const wasAtEnd = this.options.anchorTo === "end" && ((_b = this.scrollState) == null ? void 0 : _b.behavior) !== "smooth" && this.getVirtualDistanceFromEnd() <= this.options.scrollEndThreshold;
880
919
  const prevTotalSize = wasAtEnd ? this.getTotalSize() : 0;
881
920
  const scrollOffsetWithAdj = this.getScrollOffset() + this.scrollAdjustments;
882
921
  const isFirstMeasure = !this.itemSizeCache.has(key);
@@ -895,7 +934,7 @@ class Virtualizer {
895
934
  // scrolling up" cascade.
896
935
  itemStart + itemSize <= scrollOffsetWithAdj && this.scrollDirection !== "backward"
897
936
  );
898
- const shouldAdjustScroll = ((_b = this.scrollState) == null ? void 0 : _b.behavior) !== "smooth" && (this.shouldAdjustScrollPositionOnItemSizeChange !== void 0 ? this.shouldAdjustScrollPositionOnItemSizeChange(
937
+ const shouldAdjustScroll = ((_c = this.scrollState) == null ? void 0 : _c.behavior) !== "smooth" && (this.shouldAdjustScrollPositionOnItemSizeChange !== void 0 ? this.shouldAdjustScrollPositionOnItemSizeChange(
899
938
  // The callback expects a VirtualItem; build one lazily only
900
939
  // when the consumer actually supplied a custom predicate.
901
940
  this.measurementsCache[index] ?? {
@@ -943,11 +982,12 @@ class Virtualizer {
943
982
  }
944
983
  );
945
984
  this.getVirtualItemForOffset = (offset) => {
985
+ var _a;
946
986
  const measurements = this.getMeasurements();
947
987
  if (measurements.length === 0) {
948
988
  return void 0;
949
989
  }
950
- const flat = this._flatMeasurements;
990
+ const flat = (_a = this._singleLaneMeasurements) == null ? void 0 : _a.flat;
951
991
  const useFlat = this.options.lanes === 1 && flat != null;
952
992
  const idx = findNearestBinarySearch(
953
993
  0,
@@ -1082,18 +1122,18 @@ class Virtualizer {
1082
1122
  });
1083
1123
  };
1084
1124
  this.getTotalSize = () => {
1085
- var _a;
1125
+ var _a, _b;
1086
1126
  const measurements = this.getMeasurements();
1087
1127
  let end;
1088
1128
  if (measurements.length === 0) {
1089
1129
  end = this.options.paddingStart;
1090
1130
  } else if (this.options.lanes === 1) {
1091
1131
  const lastIdx = measurements.length - 1;
1092
- const flat = this._flatMeasurements;
1132
+ const flat = (_a = this._singleLaneMeasurements) == null ? void 0 : _a.flat;
1093
1133
  if (flat != null) {
1094
1134
  end = flat[lastIdx * 2] + flat[lastIdx * 2 + 1];
1095
1135
  } else {
1096
- end = ((_a = measurements[lastIdx]) == null ? void 0 : _a.end) ?? 0;
1136
+ end = ((_b = measurements[lastIdx]) == null ? void 0 : _b.end) ?? 0;
1097
1137
  }
1098
1138
  } else {
1099
1139
  const endByLane = Array(this.options.lanes).fill(null);