@tanstack/virtual-core 3.13.23 → 3.15.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.
package/dist/esm/index.js CHANGED
@@ -1,4 +1,16 @@
1
- import { debounce, memo, notUndefined, approxEqual } from "./utils.js";
1
+ import { createLazyMeasurementsView } from "./lazy-measurements.js";
2
+ import { memo, notUndefined, approxEqual, debounce } from "./utils.js";
3
+ let _isIOSResult;
4
+ const isIOSWebKit = () => {
5
+ if (_isIOSResult !== void 0) return _isIOSResult;
6
+ if (typeof navigator === "undefined") return _isIOSResult = false;
7
+ if (/iP(hone|od|ad)/.test(navigator.userAgent)) return _isIOSResult = true;
8
+ const mtp = navigator.maxTouchPoints;
9
+ return _isIOSResult = navigator.platform === "MacIntel" && mtp !== void 0 && mtp > 0;
10
+ };
11
+ const _resetIOSDetectionForTests = () => {
12
+ _isIOSResult = void 0;
13
+ };
2
14
  const getRect = (element) => {
3
15
  const { offsetWidth, offsetHeight } = element;
4
16
  return { width: offsetWidth, height: offsetHeight };
@@ -7,9 +19,10 @@ const defaultKeyExtractor = (index) => index;
7
19
  const defaultRangeExtractor = (range) => {
8
20
  const start = Math.max(range.startIndex - range.overscan, 0);
9
21
  const end = Math.min(range.endIndex + range.overscan, range.count - 1);
10
- const arr = [];
11
- for (let i = start; i <= end; i++) {
12
- arr.push(i);
22
+ const len = end - start + 1;
23
+ const arr = new Array(len);
24
+ for (let i = 0; i < len; i++) {
25
+ arr[i] = start + i;
13
26
  }
14
27
  return arr;
15
28
  };
@@ -68,7 +81,7 @@ const observeWindowRect = (instance, cb) => {
68
81
  };
69
82
  };
70
83
  const supportsScrollend = typeof window == "undefined" ? true : "onscrollend" in window;
71
- const observeElementOffset = (instance, cb) => {
84
+ const observeOffset = (instance, cb, readOffset) => {
72
85
  const element = instance.scrollElement;
73
86
  if (!element) {
74
87
  return;
@@ -77,60 +90,21 @@ const observeElementOffset = (instance, cb) => {
77
90
  if (!targetWindow) {
78
91
  return;
79
92
  }
80
- let offset = 0;
81
- const fallback = instance.options.useScrollendEvent && supportsScrollend ? () => void 0 : debounce(
82
- targetWindow,
83
- () => {
84
- cb(offset, false);
85
- },
86
- instance.options.isScrollingResetDelay
87
- );
88
- const createHandler = (isScrolling) => () => {
89
- const { horizontal, isRtl } = instance.options;
90
- offset = horizontal ? element["scrollLeft"] * (isRtl && -1 || 1) : element["scrollTop"];
91
- fallback();
92
- cb(offset, isScrolling);
93
- };
94
- const handler = createHandler(true);
95
- const endHandler = createHandler(false);
96
- element.addEventListener("scroll", handler, addEventListenerOptions);
97
93
  const registerScrollendEvent = instance.options.useScrollendEvent && supportsScrollend;
98
- if (registerScrollendEvent) {
99
- element.addEventListener("scrollend", endHandler, addEventListenerOptions);
100
- }
101
- return () => {
102
- element.removeEventListener("scroll", handler);
103
- if (registerScrollendEvent) {
104
- element.removeEventListener("scrollend", endHandler);
105
- }
106
- };
107
- };
108
- const observeWindowOffset = (instance, cb) => {
109
- const element = instance.scrollElement;
110
- if (!element) {
111
- return;
112
- }
113
- const targetWindow = instance.targetWindow;
114
- if (!targetWindow) {
115
- return;
116
- }
117
94
  let offset = 0;
118
- const fallback = instance.options.useScrollendEvent && supportsScrollend ? () => void 0 : debounce(
95
+ const fallback = registerScrollendEvent ? null : debounce(
119
96
  targetWindow,
120
- () => {
121
- cb(offset, false);
122
- },
97
+ () => cb(offset, false),
123
98
  instance.options.isScrollingResetDelay
124
99
  );
125
100
  const createHandler = (isScrolling) => () => {
126
- offset = element[instance.options.horizontal ? "scrollX" : "scrollY"];
127
- fallback();
101
+ offset = readOffset(element);
102
+ fallback == null ? void 0 : fallback();
128
103
  cb(offset, isScrolling);
129
104
  };
130
105
  const handler = createHandler(true);
131
106
  const endHandler = createHandler(false);
132
107
  element.addEventListener("scroll", handler, addEventListenerOptions);
133
- const registerScrollendEvent = instance.options.useScrollendEvent && supportsScrollend;
134
108
  if (registerScrollendEvent) {
135
109
  element.addEventListener("scrollend", endHandler, addEventListenerOptions);
136
110
  }
@@ -141,6 +115,15 @@ const observeWindowOffset = (instance, cb) => {
141
115
  }
142
116
  };
143
117
  };
118
+ const observeElementOffset = (instance, cb) => observeOffset(instance, cb, (el) => {
119
+ const { horizontal, isRtl } = instance.options;
120
+ return horizontal ? el.scrollLeft * (isRtl && -1 || 1) : el.scrollTop;
121
+ });
122
+ const observeWindowOffset = (instance, cb) => observeOffset(
123
+ instance,
124
+ cb,
125
+ (win) => instance.options.horizontal ? win.scrollX : win.scrollY
126
+ );
144
127
  const measureElement = (element, entry, instance) => {
145
128
  if (entry == null ? void 0 : entry.borderBoxSize) {
146
129
  const box = entry.borderBoxSize[0];
@@ -153,28 +136,18 @@ const measureElement = (element, entry, instance) => {
153
136
  }
154
137
  return element[instance.options.horizontal ? "offsetWidth" : "offsetHeight"];
155
138
  };
156
- const windowScroll = (offset, {
157
- adjustments = 0,
158
- behavior
159
- }, instance) => {
160
- var _a, _b;
161
- const toOffset = offset + adjustments;
162
- (_b = (_a = instance.scrollElement) == null ? void 0 : _a.scrollTo) == null ? void 0 : _b.call(_a, {
163
- [instance.options.horizontal ? "left" : "top"]: toOffset,
164
- behavior
165
- });
166
- };
167
- const elementScroll = (offset, {
139
+ const scrollWithAdjustments = (offset, {
168
140
  adjustments = 0,
169
141
  behavior
170
142
  }, instance) => {
171
143
  var _a, _b;
172
- const toOffset = offset + adjustments;
173
144
  (_b = (_a = instance.scrollElement) == null ? void 0 : _a.scrollTo) == null ? void 0 : _b.call(_a, {
174
- [instance.options.horizontal ? "left" : "top"]: toOffset,
145
+ [instance.options.horizontal ? "left" : "top"]: offset + adjustments,
175
146
  behavior
176
147
  });
177
148
  };
149
+ const windowScroll = scrollWithAdjustments;
150
+ const elementScroll = scrollWithAdjustments;
178
151
  class Virtualizer {
179
152
  constructor(opts) {
180
153
  this.unsubs = [];
@@ -183,9 +156,11 @@ class Virtualizer {
183
156
  this.isScrolling = false;
184
157
  this.scrollState = null;
185
158
  this.measurementsCache = [];
159
+ this._flatMeasurements = null;
186
160
  this.itemSizeCache = /* @__PURE__ */ new Map();
161
+ this.itemSizeCacheVersion = 0;
187
162
  this.laneAssignments = /* @__PURE__ */ new Map();
188
- this.pendingMeasuredCacheIndexes = [];
163
+ this.pendingMin = null;
189
164
  this.prevLanes = void 0;
190
165
  this.lanesChangedFlag = false;
191
166
  this.lanesSettling = false;
@@ -193,6 +168,11 @@ class Virtualizer {
193
168
  this.scrollOffset = null;
194
169
  this.scrollDirection = null;
195
170
  this.scrollAdjustments = 0;
171
+ this._iosDeferredAdjustment = 0;
172
+ this._iosTouching = false;
173
+ this._iosJustTouchEnded = false;
174
+ this._iosTouchEndTimerId = null;
175
+ this._intendedScrollOffset = null;
196
176
  this.elementsCache = /* @__PURE__ */ new Map();
197
177
  this.now = () => {
198
178
  var _a, _b, _c;
@@ -214,6 +194,12 @@ class Virtualizer {
214
194
  const index = this.indexFromElement(node);
215
195
  if (!node.isConnected) {
216
196
  this.observer.unobserve(node);
197
+ for (const [cacheKey, cachedNode] of this.elementsCache) {
198
+ if (cachedNode === node) {
199
+ this.elementsCache.delete(cacheKey);
200
+ break;
201
+ }
202
+ }
217
203
  return;
218
204
  }
219
205
  if (this.shouldMeasureDuringScroll(index)) {
@@ -245,10 +231,7 @@ class Virtualizer {
245
231
  })();
246
232
  this.range = null;
247
233
  this.setOptions = (opts2) => {
248
- Object.entries(opts2).forEach(([key, value]) => {
249
- if (typeof value === "undefined") delete opts2[key];
250
- });
251
- this.options = {
234
+ const merged = {
252
235
  debug: false,
253
236
  initialOffset: 0,
254
237
  overscan: 1,
@@ -273,8 +256,13 @@ class Virtualizer {
273
256
  isRtl: false,
274
257
  useScrollendEvent: false,
275
258
  useAnimationFrameWithResizeObserver: false,
276
- ...opts2
259
+ laneAssignmentMode: "estimate"
277
260
  };
261
+ for (const key in opts2) {
262
+ const v = opts2[key];
263
+ if (v !== void 0) merged[key] = v;
264
+ }
265
+ this.options = merged;
278
266
  };
279
267
  this.notify = (sync) => {
280
268
  var _a, _b;
@@ -345,22 +333,83 @@ class Virtualizer {
345
333
  );
346
334
  this.unsubs.push(
347
335
  this.options.observeElementOffset(this, (offset, isScrolling) => {
336
+ if (this._intendedScrollOffset !== null && Math.abs(offset - this._intendedScrollOffset) < 1.5) {
337
+ offset = this._intendedScrollOffset;
338
+ }
339
+ this._intendedScrollOffset = null;
348
340
  this.scrollAdjustments = 0;
349
341
  this.scrollDirection = isScrolling ? this.getScrollOffset() < offset ? "forward" : "backward" : null;
350
342
  this.scrollOffset = offset;
351
343
  this.isScrolling = isScrolling;
344
+ this._flushIosDeferredIfReady();
352
345
  if (this.scrollState) {
353
346
  this.scheduleScrollReconcile();
354
347
  }
355
348
  this.maybeNotify();
356
349
  })
357
350
  );
351
+ if ("addEventListener" in this.scrollElement) {
352
+ const scrollEl = this.scrollElement;
353
+ const onTouchStart = () => {
354
+ this._iosTouching = true;
355
+ this._iosJustTouchEnded = false;
356
+ if (this._iosTouchEndTimerId !== null && this.targetWindow != null) {
357
+ this.targetWindow.clearTimeout(this._iosTouchEndTimerId);
358
+ this._iosTouchEndTimerId = null;
359
+ }
360
+ };
361
+ const onTouchEnd = () => {
362
+ this._iosTouching = false;
363
+ if (!isIOSWebKit() || this.targetWindow == null) {
364
+ return;
365
+ }
366
+ this._iosJustTouchEnded = true;
367
+ this._iosTouchEndTimerId = this.targetWindow.setTimeout(() => {
368
+ this._iosJustTouchEnded = false;
369
+ this._iosTouchEndTimerId = null;
370
+ this._flushIosDeferredIfReady();
371
+ }, 150);
372
+ };
373
+ scrollEl.addEventListener(
374
+ "touchstart",
375
+ onTouchStart,
376
+ addEventListenerOptions
377
+ );
378
+ scrollEl.addEventListener(
379
+ "touchend",
380
+ onTouchEnd,
381
+ addEventListenerOptions
382
+ );
383
+ this.unsubs.push(() => {
384
+ scrollEl.removeEventListener("touchstart", onTouchStart);
385
+ scrollEl.removeEventListener("touchend", onTouchEnd);
386
+ if (this._iosTouchEndTimerId !== null && this.targetWindow != null) {
387
+ this.targetWindow.clearTimeout(this._iosTouchEndTimerId);
388
+ this._iosTouchEndTimerId = null;
389
+ }
390
+ });
391
+ }
358
392
  this._scrollToOffset(this.getScrollOffset(), {
359
393
  adjustments: void 0,
360
394
  behavior: void 0
361
395
  });
362
396
  }
363
397
  };
398
+ this._flushIosDeferredIfReady = () => {
399
+ if (this._iosDeferredAdjustment === 0) return;
400
+ if (this.isScrolling) return;
401
+ if (this._iosTouching) return;
402
+ if (this._iosJustTouchEnded) return;
403
+ const cur = this.getScrollOffset();
404
+ const max = this.getMaxScrollOffset();
405
+ if (cur < 0 || cur > max) return;
406
+ const delta = this._iosDeferredAdjustment;
407
+ this._iosDeferredAdjustment = 0;
408
+ this._scrollToOffset(cur, {
409
+ adjustments: this.scrollAdjustments += delta,
410
+ behavior: void 0
411
+ });
412
+ };
364
413
  this.rafId = null;
365
414
  this.getSize = () => {
366
415
  if (!this.options.enabled) {
@@ -412,22 +461,24 @@ class Virtualizer {
412
461
  this.options.scrollMargin,
413
462
  this.options.getItemKey,
414
463
  this.options.enabled,
415
- this.options.lanes
464
+ this.options.lanes,
465
+ this.options.laneAssignmentMode
416
466
  ],
417
- (count, paddingStart, scrollMargin, getItemKey, enabled, lanes) => {
467
+ (count, paddingStart, scrollMargin, getItemKey, enabled, lanes, laneAssignmentMode) => {
418
468
  const lanesChanged = this.prevLanes !== void 0 && this.prevLanes !== lanes;
419
469
  if (lanesChanged) {
420
470
  this.lanesChangedFlag = true;
421
471
  }
422
472
  this.prevLanes = lanes;
423
- this.pendingMeasuredCacheIndexes = [];
473
+ this.pendingMin = null;
424
474
  return {
425
475
  count,
426
476
  paddingStart,
427
477
  scrollMargin,
428
478
  getItemKey,
429
479
  enabled,
430
- lanes
480
+ lanes,
481
+ laneAssignmentMode
431
482
  };
432
483
  },
433
484
  {
@@ -435,8 +486,17 @@ class Virtualizer {
435
486
  }
436
487
  );
437
488
  this.getMeasurements = memo(
438
- () => [this.getMeasurementOptions(), this.itemSizeCache],
439
- ({ count, paddingStart, scrollMargin, getItemKey, enabled, lanes }, itemSizeCache) => {
489
+ () => [this.getMeasurementOptions(), this.itemSizeCacheVersion],
490
+ ({
491
+ count,
492
+ paddingStart,
493
+ scrollMargin,
494
+ getItemKey,
495
+ enabled,
496
+ lanes,
497
+ laneAssignmentMode
498
+ }, _itemSizeCacheVersion) => {
499
+ const itemSizeCache = this.itemSizeCache;
440
500
  if (!enabled) {
441
501
  this.measurementsCache = [];
442
502
  this.itemSizeCache.clear();
@@ -456,7 +516,7 @@ class Virtualizer {
456
516
  this.measurementsCache = [];
457
517
  this.itemSizeCache.clear();
458
518
  this.laneAssignments.clear();
459
- this.pendingMeasuredCacheIndexes = [];
519
+ this.pendingMin = null;
460
520
  }
461
521
  if (this.measurementsCache.length === 0 && !this.lanesSettling) {
462
522
  this.measurementsCache = this.options.initialMeasurementsCache;
@@ -464,11 +524,40 @@ class Virtualizer {
464
524
  this.itemSizeCache.set(item.key, item.size);
465
525
  });
466
526
  }
467
- const min = this.lanesSettling ? 0 : this.pendingMeasuredCacheIndexes.length > 0 ? Math.min(...this.pendingMeasuredCacheIndexes) : 0;
468
- this.pendingMeasuredCacheIndexes = [];
527
+ const min = this.lanesSettling ? 0 : this.pendingMin ?? 0;
528
+ this.pendingMin = null;
469
529
  if (this.lanesSettling && this.measurementsCache.length === count) {
470
530
  this.lanesSettling = false;
471
531
  }
532
+ if (lanes === 1) {
533
+ const gap = this.options.gap;
534
+ const need = count * 2;
535
+ let flat = this._flatMeasurements;
536
+ if (!flat || flat.length < need) {
537
+ const next = new Float64Array(need);
538
+ if (flat && min > 0) next.set(flat.subarray(0, min * 2));
539
+ flat = next;
540
+ this._flatMeasurements = flat;
541
+ }
542
+ let runningStart;
543
+ if (min === 0) {
544
+ runningStart = paddingStart + scrollMargin;
545
+ } else {
546
+ const prevIdx = min - 1;
547
+ runningStart = flat[prevIdx * 2] + flat[prevIdx * 2 + 1] + gap;
548
+ }
549
+ for (let i = min; i < count; i++) {
550
+ const key = getItemKey(i);
551
+ const measuredSize = itemSizeCache.get(key);
552
+ const size = typeof measuredSize === "number" ? measuredSize : this.options.estimateSize(i);
553
+ flat[i * 2] = runningStart;
554
+ flat[i * 2 + 1] = size;
555
+ runningStart += size + gap;
556
+ }
557
+ const view = createLazyMeasurementsView(count, flat, getItemKey);
558
+ this.measurementsCache = view;
559
+ return view;
560
+ }
472
561
  const measurements = this.measurementsCache.slice(0, min);
473
562
  const laneLastIndex = new Array(lanes).fill(
474
563
  void 0
@@ -484,6 +573,7 @@ class Virtualizer {
484
573
  const cachedLane = this.laneAssignments.get(i);
485
574
  let lane;
486
575
  let start;
576
+ const shouldCacheLane = laneAssignmentMode === "estimate" || itemSizeCache.has(key);
487
577
  if (cachedLane !== void 0 && this.options.lanes > 1) {
488
578
  lane = cachedLane;
489
579
  const prevIndex = laneLastIndex[lane];
@@ -493,7 +583,7 @@ class Virtualizer {
493
583
  const furthestMeasurement = this.options.lanes === 1 ? measurements[i - 1] : this.getFurthestMeasurement(measurements, i);
494
584
  start = furthestMeasurement ? furthestMeasurement.end + this.options.gap : paddingStart + scrollMargin;
495
585
  lane = furthestMeasurement ? furthestMeasurement.lane : i % this.options.lanes;
496
- if (this.options.lanes > 1) {
586
+ if (this.options.lanes > 1 && shouldCacheLane) {
497
587
  this.laneAssignments.set(i, lane);
498
588
  }
499
589
  }
@@ -530,7 +620,11 @@ class Virtualizer {
530
620
  measurements,
531
621
  outerSize,
532
622
  scrollOffset,
533
- lanes
623
+ lanes,
624
+ // Pass the typed array so binary search + forward-walk can
625
+ // read start/end directly from Float64Array, skipping the
626
+ // Proxy traps that materialize a full VirtualItem per probe.
627
+ flat: lanes === 1 && this._flatMeasurements != null ? this._flatMeasurements : null
534
628
  }) : null;
535
629
  },
536
630
  {
@@ -626,22 +720,64 @@ class Virtualizer {
626
720
  };
627
721
  this.resizeItem = (index, size) => {
628
722
  var _a;
629
- const item = this.measurementsCache[index];
630
- if (!item) return;
631
- const itemSize = this.itemSizeCache.get(item.key) ?? item.size;
723
+ if (index < 0 || index >= this.options.count) return;
724
+ let cachedSize;
725
+ let itemStart;
726
+ let key;
727
+ const flat = this._flatMeasurements;
728
+ if (this.options.lanes === 1 && flat !== null) {
729
+ key = this.options.getItemKey(index);
730
+ itemStart = flat[index * 2];
731
+ cachedSize = flat[index * 2 + 1];
732
+ } else {
733
+ const item = this.measurementsCache[index];
734
+ if (!item) return;
735
+ key = item.key;
736
+ itemStart = item.start;
737
+ cachedSize = item.size;
738
+ }
739
+ const itemSize = this.itemSizeCache.get(key) ?? cachedSize;
632
740
  const delta = size - itemSize;
633
741
  if (delta !== 0) {
634
- if (((_a = this.scrollState) == null ? void 0 : _a.behavior) !== "smooth" && (this.shouldAdjustScrollPositionOnItemSizeChange !== void 0 ? this.shouldAdjustScrollPositionOnItemSizeChange(item, delta, this) : item.start < this.getScrollOffset() + this.scrollAdjustments)) {
742
+ if (((_a = this.scrollState) == null ? void 0 : _a.behavior) !== "smooth" && (this.shouldAdjustScrollPositionOnItemSizeChange !== void 0 ? this.shouldAdjustScrollPositionOnItemSizeChange(
743
+ // The callback expects a VirtualItem; build one lazily only
744
+ // when the consumer actually supplied a custom predicate.
745
+ this.measurementsCache[index] ?? {
746
+ index,
747
+ key,
748
+ start: itemStart,
749
+ size: cachedSize,
750
+ end: itemStart + cachedSize,
751
+ lane: 0
752
+ },
753
+ delta,
754
+ this
755
+ ) : (
756
+ // Default: adjust scrollTop only when the resize is an above-
757
+ // viewport item AND we're not actively scrolling backward.
758
+ // Adjusting during backward scroll fights the user's scroll
759
+ // direction and produces the "items jump while scrolling up"
760
+ // jank reported across many issues. Users who want the old
761
+ // behavior can pass shouldAdjustScrollPositionOnItemSizeChange.
762
+ itemStart < this.getScrollOffset() + this.scrollAdjustments && this.scrollDirection !== "backward"
763
+ ))) {
635
764
  if (process.env.NODE_ENV !== "production" && this.options.debug) {
636
765
  console.info("correction", delta);
637
766
  }
638
- this._scrollToOffset(this.getScrollOffset(), {
639
- adjustments: this.scrollAdjustments += delta,
640
- behavior: void 0
641
- });
767
+ if (isIOSWebKit() && (this.isScrolling || this._iosTouching || this._iosJustTouchEnded)) {
768
+ this._iosDeferredAdjustment += delta;
769
+ } else {
770
+ this._scrollToOffset(this.getScrollOffset(), {
771
+ adjustments: this.scrollAdjustments += delta,
772
+ behavior: void 0
773
+ });
774
+ }
642
775
  }
643
- this.pendingMeasuredCacheIndexes.push(item.index);
644
- this.itemSizeCache = new Map(this.itemSizeCache.set(item.key, size));
776
+ if (this.pendingMin === null || index < this.pendingMin) {
777
+ this.pendingMin = index;
778
+ }
779
+ this.itemSizeCache.set(key, size);
780
+ this.itemSizeCacheVersion++;
645
781
  this.notify(false);
646
782
  }
647
783
  };
@@ -666,14 +802,15 @@ class Virtualizer {
666
802
  if (measurements.length === 0) {
667
803
  return void 0;
668
804
  }
669
- return notUndefined(
670
- measurements[findNearestBinarySearch(
671
- 0,
672
- measurements.length - 1,
673
- (index) => notUndefined(measurements[index]).start,
674
- offset
675
- )]
805
+ const flat = this._flatMeasurements;
806
+ const useFlat = this.options.lanes === 1 && flat != null;
807
+ const idx = findNearestBinarySearch(
808
+ 0,
809
+ measurements.length - 1,
810
+ useFlat ? (i) => flat[i * 2] : (i) => notUndefined(measurements[i]).start,
811
+ offset
676
812
  );
813
+ return notUndefined(measurements[idx]);
677
814
  };
678
815
  this.getMaxScrollOffset = () => {
679
816
  if (!this.scrollElement) return 0;
@@ -780,7 +917,13 @@ class Virtualizer {
780
917
  if (measurements.length === 0) {
781
918
  end = this.options.paddingStart;
782
919
  } else if (this.options.lanes === 1) {
783
- end = ((_a = measurements[measurements.length - 1]) == null ? void 0 : _a.end) ?? 0;
920
+ const lastIdx = measurements.length - 1;
921
+ const flat = this._flatMeasurements;
922
+ if (flat != null) {
923
+ end = flat[lastIdx * 2] + flat[lastIdx * 2 + 1];
924
+ } else {
925
+ end = ((_a = measurements[lastIdx]) == null ? void 0 : _a.end) ?? 0;
926
+ }
784
927
  } else {
785
928
  const endByLane = Array(this.options.lanes).fill(null);
786
929
  let endIndex = measurements.length - 1;
@@ -798,15 +941,36 @@ class Virtualizer {
798
941
  0
799
942
  );
800
943
  };
944
+ this.takeSnapshot = () => {
945
+ const snapshot = [];
946
+ if (this.itemSizeCache.size === 0) return snapshot;
947
+ const m = this.getMeasurements();
948
+ for (const item of m) {
949
+ if (item && this.itemSizeCache.has(item.key)) {
950
+ snapshot.push({
951
+ index: item.index,
952
+ key: item.key,
953
+ start: item.start,
954
+ size: item.size,
955
+ end: item.end,
956
+ lane: item.lane
957
+ });
958
+ }
959
+ }
960
+ return snapshot;
961
+ };
801
962
  this._scrollToOffset = (offset, {
802
963
  adjustments,
803
964
  behavior
804
965
  }) => {
966
+ this._intendedScrollOffset = offset + (adjustments ?? 0);
805
967
  this.options.scrollToFn(offset, { behavior, adjustments }, this);
806
968
  };
807
969
  this.measure = () => {
808
- this.itemSizeCache = /* @__PURE__ */ new Map();
809
- this.laneAssignments = /* @__PURE__ */ new Map();
970
+ this.pendingMin = null;
971
+ this.itemSizeCache.clear();
972
+ this.laneAssignments.clear();
973
+ this.itemSizeCacheVersion++;
810
974
  this.notify(false);
811
975
  };
812
976
  this.setOptions(opts);
@@ -838,17 +1002,28 @@ class Virtualizer {
838
1002
  if (!targetChanged && approxEqual(targetOffset, this.getScrollOffset())) {
839
1003
  this.scrollState.stableFrames++;
840
1004
  if (this.scrollState.stableFrames >= STABLE_FRAMES) {
1005
+ if (this.getScrollOffset() !== targetOffset) {
1006
+ this._scrollToOffset(targetOffset, {
1007
+ adjustments: void 0,
1008
+ behavior: "auto"
1009
+ });
1010
+ }
841
1011
  this.scrollState = null;
842
1012
  return;
843
1013
  }
844
1014
  } else {
845
1015
  this.scrollState.stableFrames = 0;
846
1016
  if (targetChanged) {
1017
+ const viewport = this.getSize() || 600;
1018
+ const distance = Math.abs(targetOffset - this.getScrollOffset());
1019
+ const keepSmooth = this.scrollState.behavior === "smooth" && distance > viewport;
847
1020
  this.scrollState.lastTargetOffset = targetOffset;
848
- this.scrollState.behavior = "auto";
1021
+ if (!keepSmooth) {
1022
+ this.scrollState.behavior = "auto";
1023
+ }
849
1024
  this._scrollToOffset(targetOffset, {
850
1025
  adjustments: void 0,
851
- behavior: "auto"
1026
+ behavior: keepSmooth ? "smooth" : "auto"
852
1027
  });
853
1028
  }
854
1029
  }
@@ -877,25 +1052,22 @@ function calculateRange({
877
1052
  measurements,
878
1053
  outerSize,
879
1054
  scrollOffset,
880
- lanes
1055
+ lanes,
1056
+ flat
881
1057
  }) {
882
1058
  const lastIndex = measurements.length - 1;
883
- const getOffset = (index) => measurements[index].start;
1059
+ const getStart = flat ? (index) => flat[index * 2] : (index) => measurements[index].start;
1060
+ const getEnd = flat ? (index) => flat[index * 2] + flat[index * 2 + 1] : (index) => measurements[index].end;
884
1061
  if (measurements.length <= lanes) {
885
1062
  return {
886
1063
  startIndex: 0,
887
1064
  endIndex: lastIndex
888
1065
  };
889
1066
  }
890
- let startIndex = findNearestBinarySearch(
891
- 0,
892
- lastIndex,
893
- getOffset,
894
- scrollOffset
895
- );
1067
+ let startIndex = findNearestBinarySearch(0, lastIndex, getStart, scrollOffset);
896
1068
  let endIndex = startIndex;
897
1069
  if (lanes === 1) {
898
- while (endIndex < lastIndex && measurements[endIndex].end < scrollOffset + outerSize) {
1070
+ while (endIndex < lastIndex && getEnd(endIndex) < scrollOffset + outerSize) {
899
1071
  endIndex++;
900
1072
  }
901
1073
  } else if (lanes > 1) {
@@ -918,6 +1090,7 @@ function calculateRange({
918
1090
  }
919
1091
  export {
920
1092
  Virtualizer,
1093
+ _resetIOSDetectionForTests,
921
1094
  approxEqual,
922
1095
  debounce,
923
1096
  defaultKeyExtractor,