@tanstack/virtual-core 3.18.0 → 3.19.0

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.
@@ -19,8 +19,9 @@ const getRect = (element) => {
19
19
  };
20
20
  const defaultKeyExtractor = (index) => index;
21
21
  const defaultRangeExtractor = (range) => {
22
- const start = Math.max(range.startIndex - range.overscan, 0);
23
- const end = Math.min(range.endIndex + range.overscan, range.count - 1);
22
+ const overscan = range.overscan * Math.max(range.lanes ?? 1, 1);
23
+ const start = Math.max(range.startIndex - overscan, 0);
24
+ const end = Math.min(range.endIndex + overscan, range.count - 1);
24
25
  const len = end - start + 1;
25
26
  const arr = new Array(len);
26
27
  for (let i = 0; i < len; i++) {
@@ -213,6 +214,7 @@ class Virtualizer {
213
214
  this._intendedScrollOffset = null;
214
215
  this._clampedAdjustment = null;
215
216
  this.elementsCache = /* @__PURE__ */ new Map();
217
+ this._resizeBatch = null;
216
218
  this.now = () => {
217
219
  var _a, _b, _c;
218
220
  return ((_c = (_b = (_a = this.targetWindow) == null ? void 0 : _a.performance) == null ? void 0 : _b.now) == null ? void 0 : _c.call(_b)) ?? Date.now();
@@ -227,32 +229,40 @@ class Virtualizer {
227
229
  return null;
228
230
  }
229
231
  return _ro = new this.targetWindow.ResizeObserver((entries) => {
230
- entries.forEach((entry) => {
231
- const run = () => {
232
- const node = entry.target;
233
- const index = this.indexFromElement(node);
234
- if (!node.isConnected) {
235
- this.observer.unobserve(node);
236
- for (const [cacheKey, cachedNode] of this.elementsCache) {
237
- if (cachedNode === node) {
238
- this.elementsCache.delete(cacheKey);
239
- break;
240
- }
241
- }
242
- return;
232
+ const run = () => {
233
+ const batch = { sync: false };
234
+ this._resizeBatch = batch;
235
+ try {
236
+ entries.forEach(measureEntry);
237
+ } finally {
238
+ this._resizeBatch = null;
239
+ if (batch.notify) {
240
+ this.notify(batch.sync);
241
+ this._retryClampedAdjustment();
243
242
  }
244
- if (!this.isIndexInRange(index)) return;
245
- if (this.shouldMeasureDuringScroll(index)) {
246
- this.resizeItem(
247
- index,
248
- this.options.measureElement(node, entry, this)
249
- );
250
- }
251
- };
252
- this.options.useAnimationFrameWithResizeObserver ? requestAnimationFrame(run) : run();
253
- });
243
+ }
244
+ };
245
+ this.options.useAnimationFrameWithResizeObserver ? requestAnimationFrame(run) : run();
254
246
  });
255
247
  };
248
+ const measureEntry = (entry) => {
249
+ const node = entry.target;
250
+ const index = this.indexFromElement(node);
251
+ if (!node.isConnected) {
252
+ this.observer.unobserve(node);
253
+ for (const [cacheKey, cachedNode] of this.elementsCache) {
254
+ if (cachedNode === node) {
255
+ this.elementsCache.delete(cacheKey);
256
+ break;
257
+ }
258
+ }
259
+ return;
260
+ }
261
+ if (!this.isIndexInRange(index)) return;
262
+ if (this.shouldMeasureDuringScroll(index)) {
263
+ this.resizeItem(index, this.options.measureElement(node, entry, this));
264
+ }
265
+ };
256
266
  return {
257
267
  disconnect: () => {
258
268
  var _a;
@@ -825,15 +835,17 @@ class Virtualizer {
825
835
  this.options.overscan,
826
836
  this.options.count,
827
837
  startIndex,
828
- endIndex
838
+ endIndex,
839
+ this.options.lanes
829
840
  ];
830
841
  },
831
- (rangeExtractor, overscan, count, startIndex, endIndex) => {
842
+ (rangeExtractor, overscan, count, startIndex, endIndex, lanes) => {
832
843
  return startIndex === null || endIndex === null ? [] : rangeExtractor({
833
844
  startIndex,
834
845
  endIndex,
835
846
  overscan,
836
- count
847
+ count,
848
+ lanes
837
849
  });
838
850
  },
839
851
  {
@@ -898,12 +910,13 @@ class Virtualizer {
898
910
  }
899
911
  };
900
912
  this.resizeItem = (index, size) => {
901
- var _a, _b, _c;
913
+ var _a, _b, _c, _d, _e;
902
914
  if (!this.isIndexInRange(index)) return;
915
+ if ((_a = this._resizeBatch) == null ? void 0 : _a.sync) this.getMeasurements();
903
916
  let cachedSize;
904
917
  let itemStart;
905
918
  let key;
906
- const flat = (_a = this._singleLaneMeasurements) == null ? void 0 : _a.flat;
919
+ const flat = (_b = this._singleLaneMeasurements) == null ? void 0 : _b.flat;
907
920
  if (this.options.lanes === 1 && flat != null) {
908
921
  key = this.options.getItemKey(index);
909
922
  itemStart = flat[index * 2];
@@ -918,7 +931,7 @@ class Virtualizer {
918
931
  const itemSize = this.itemSizeCache.get(key) ?? cachedSize;
919
932
  const delta = size - itemSize;
920
933
  if (delta !== 0) {
921
- const wasAtEnd = this.options.anchorTo === "end" && ((_b = this.scrollState) == null ? void 0 : _b.behavior) !== "smooth" && this.getVirtualDistanceFromEnd() <= this.options.scrollEndThreshold;
934
+ const wasAtEnd = this.options.anchorTo === "end" && ((_c = this.scrollState) == null ? void 0 : _c.behavior) !== "smooth" && this.getVirtualDistanceFromEnd() <= this.options.scrollEndThreshold;
922
935
  const prevTotalSize = wasAtEnd ? this.getTotalSize() : 0;
923
936
  const scrollOffsetWithAdj = this.getScrollOffset() + this.scrollAdjustments;
924
937
  const isFirstMeasure = !this.itemSizeCache.has(key);
@@ -937,7 +950,7 @@ class Virtualizer {
937
950
  // scrolling up" cascade.
938
951
  itemStart + itemSize <= scrollOffsetWithAdj && this.scrollDirection !== "backward"
939
952
  );
940
- const shouldAdjustScroll = ((_c = this.scrollState) == null ? void 0 : _c.behavior) !== "smooth" && (this.shouldAdjustScrollPositionOnItemSizeChange !== void 0 ? this.shouldAdjustScrollPositionOnItemSizeChange(
953
+ const shouldAdjustScroll = ((_d = this.scrollState) == null ? void 0 : _d.behavior) !== "smooth" && (this.shouldAdjustScrollPositionOnItemSizeChange !== void 0 ? this.shouldAdjustScrollPositionOnItemSizeChange(
941
954
  // The callback expects a VirtualItem; build one lazily only
942
955
  // when the consumer actually supplied a custom predicate.
943
956
  this.measurementsCache[index] ?? {
@@ -964,6 +977,11 @@ class Virtualizer {
964
977
  } else if (shouldAdjustScroll) {
965
978
  adjustedSync = this.applyScrollAdjustment(delta);
966
979
  }
980
+ if (this._resizeBatch) {
981
+ this._resizeBatch.notify = true;
982
+ (_e = this._resizeBatch).sync || (_e.sync = adjustedSync);
983
+ return;
984
+ }
967
985
  this.notify(adjustedSync);
968
986
  this._retryClampedAdjustment();
969
987
  }
@@ -1216,12 +1234,15 @@ class Virtualizer {
1216
1234
  // `resizeItem` uses that to decide whether the follow-up `notify` must be
1217
1235
  // synchronous so the grown transforms commit in the same paint (#1227).
1218
1236
  applyScrollAdjustment(delta, behavior) {
1237
+ var _a;
1219
1238
  if (delta === 0) return false;
1220
1239
  if (process.env.NODE_ENV !== "production" && this.options.debug) {
1221
1240
  console.info("correction", delta);
1222
1241
  }
1223
1242
  if (isIOSWebKit() && (this.isScrolling || this._iosTouching || this._iosJustTouchEnded)) {
1224
- this._iosDeferredAdjustment += delta;
1243
+ if (((_a = this.scrollState) == null ? void 0 : _a.index) == null) {
1244
+ this._iosDeferredAdjustment += delta;
1245
+ }
1225
1246
  return false;
1226
1247
  } else {
1227
1248
  const target = this.getScrollOffset() + this.scrollAdjustments + delta;
@@ -1264,6 +1285,7 @@ class Virtualizer {
1264
1285
  this.scrollState = null;
1265
1286
  return;
1266
1287
  }
1288
+ this.getMeasurements();
1267
1289
  const targetOffset = this.getScrollStateTarget(this.scrollState);
1268
1290
  const STABLE_FRAMES = this.scrollState.index == null ? 1 : this.options.useAnimationFrameWithResizeObserver ? 3 : 2;
1269
1291
  const targetChanged = targetOffset !== this.scrollState.lastTargetOffset;