@tanstack/virtual-core 3.17.2 → 3.17.4

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.
@@ -378,6 +378,9 @@ class Virtualizer {
378
378
  this.rafId = null;
379
379
  }
380
380
  this.scrollState = null;
381
+ this._iosDeferredAdjustment = 0;
382
+ this._iosTouching = false;
383
+ this._iosJustTouchEnded = false;
381
384
  this.scrollElement = null;
382
385
  this.targetWindow = null;
383
386
  };
@@ -420,7 +423,8 @@ class Virtualizer {
420
423
  }
421
424
  this._intendedScrollOffset = null;
422
425
  this.scrollAdjustments = 0;
423
- this.scrollDirection = isScrolling ? this.getScrollOffset() < offset ? "forward" : "backward" : null;
426
+ const prevOffset = this.getScrollOffset();
427
+ this.scrollDirection = isScrolling ? prevOffset === offset ? this.scrollDirection : prevOffset < offset ? "forward" : "backward" : null;
424
428
  this.scrollOffset = offset;
425
429
  this.isScrolling = isScrolling;
426
430
  this._flushIosDeferredIfReady();
@@ -529,33 +533,6 @@ class Virtualizer {
529
533
  this.scrollOffset = this.scrollOffset ?? (typeof this.options.initialOffset === "function" ? this.options.initialOffset() : this.options.initialOffset);
530
534
  return this.scrollOffset;
531
535
  };
532
- this.getFurthestMeasurement = (measurements, index) => {
533
- const furthestMeasurementsFound = /* @__PURE__ */ new Map();
534
- const furthestMeasurements = /* @__PURE__ */ new Map();
535
- for (let m = index - 1; m >= 0; m--) {
536
- const measurement = measurements[m];
537
- if (furthestMeasurementsFound.has(measurement.lane)) {
538
- continue;
539
- }
540
- const previousFurthestMeasurement = furthestMeasurements.get(
541
- measurement.lane
542
- );
543
- if (previousFurthestMeasurement == null || measurement.end > previousFurthestMeasurement.end) {
544
- furthestMeasurements.set(measurement.lane, measurement);
545
- } else if (measurement.end < previousFurthestMeasurement.end) {
546
- furthestMeasurementsFound.set(measurement.lane, true);
547
- }
548
- if (furthestMeasurementsFound.size === this.options.lanes) {
549
- break;
550
- }
551
- }
552
- return furthestMeasurements.size === this.options.lanes ? Array.from(furthestMeasurements.values()).sort((a, b) => {
553
- if (a.end === b.end) {
554
- return a.index - b.index;
555
- }
556
- return a.end - b.end;
557
- })[0] : void 0;
558
- };
559
536
  this.getMeasurementOptions = utils.memo(
560
537
  () => [
561
538
  this.options.count,
@@ -564,9 +541,10 @@ class Virtualizer {
564
541
  this.options.getItemKey,
565
542
  this.options.enabled,
566
543
  this.options.lanes,
567
- this.options.laneAssignmentMode
544
+ this.options.laneAssignmentMode,
545
+ this.options.gap
568
546
  ],
569
- (count, paddingStart, scrollMargin, getItemKey, enabled, lanes, laneAssignmentMode) => {
547
+ (count, paddingStart, scrollMargin, getItemKey, enabled, lanes, laneAssignmentMode, gap) => {
570
548
  const lanesChanged = this.prevLanes !== void 0 && this.prevLanes !== lanes;
571
549
  if (lanesChanged) {
572
550
  this.lanesChangedFlag = true;
@@ -580,7 +558,8 @@ class Virtualizer {
580
558
  getItemKey,
581
559
  enabled,
582
560
  lanes,
583
- laneAssignmentMode
561
+ laneAssignmentMode,
562
+ gap
584
563
  };
585
564
  },
586
565
  {
@@ -596,7 +575,8 @@ class Virtualizer {
596
575
  getItemKey,
597
576
  enabled,
598
577
  lanes,
599
- laneAssignmentMode
578
+ laneAssignmentMode,
579
+ gap
600
580
  }, _itemSizeCacheVersion) => {
601
581
  const itemSizeCache = this.itemSizeCache;
602
582
  if (!enabled) {
@@ -632,7 +612,6 @@ class Virtualizer {
632
612
  this.lanesSettling = false;
633
613
  }
634
614
  if (lanes === 1) {
635
- const gap = this.options.gap;
636
615
  const need = count * 2;
637
616
  let flat = this._flatMeasurements;
638
617
  if (!flat || flat.length < need) {
@@ -664,10 +643,14 @@ class Virtualizer {
664
643
  const laneLastIndex = new Array(lanes).fill(
665
644
  void 0
666
645
  );
646
+ const laneEnds = new Float64Array(lanes);
647
+ let filledLanes = 0;
667
648
  for (let m = 0; m < min; m++) {
668
649
  const item = measurements[m];
669
650
  if (item) {
651
+ if (laneLastIndex[item.lane] === void 0) filledLanes++;
670
652
  laneLastIndex[item.lane] = m;
653
+ laneEnds[item.lane] = item.end;
671
654
  }
672
655
  }
673
656
  for (let i = min; i < count; i++) {
@@ -680,12 +663,28 @@ class Virtualizer {
680
663
  lane = cachedLane;
681
664
  const prevIndex = laneLastIndex[lane];
682
665
  const prevInLane = prevIndex !== void 0 ? measurements[prevIndex] : void 0;
683
- start = prevInLane ? prevInLane.end + this.options.gap : paddingStart + scrollMargin;
666
+ start = prevInLane ? prevInLane.end + gap : paddingStart + scrollMargin;
667
+ } else if (filledLanes === lanes) {
668
+ let bestLane = 0;
669
+ let bestEnd = laneEnds[0];
670
+ let bestIdx = laneLastIndex[0];
671
+ for (let l = 1; l < lanes; l++) {
672
+ const e = laneEnds[l];
673
+ if (e < bestEnd || e === bestEnd && laneLastIndex[l] < bestIdx) {
674
+ bestLane = l;
675
+ bestEnd = e;
676
+ bestIdx = laneLastIndex[l];
677
+ }
678
+ }
679
+ lane = bestLane;
680
+ start = bestEnd + gap;
681
+ if (shouldCacheLane) {
682
+ this.laneAssignments.set(i, lane);
683
+ }
684
684
  } else {
685
- const furthestMeasurement = this.options.lanes === 1 ? measurements[i - 1] : this.getFurthestMeasurement(measurements, i);
686
- start = furthestMeasurement ? furthestMeasurement.end + this.options.gap : paddingStart + scrollMargin;
687
- lane = furthestMeasurement ? furthestMeasurement.lane : i % this.options.lanes;
688
- if (this.options.lanes > 1 && shouldCacheLane) {
685
+ lane = i % this.options.lanes;
686
+ start = paddingStart + scrollMargin;
687
+ if (shouldCacheLane) {
689
688
  this.laneAssignments.set(i, lane);
690
689
  }
691
690
  }
@@ -700,7 +699,9 @@ class Virtualizer {
700
699
  key,
701
700
  lane
702
701
  };
702
+ if (laneLastIndex[lane] === void 0) filledLanes++;
703
703
  laneLastIndex[lane] = i;
704
+ laneEnds[lane] = end;
704
705
  }
705
706
  this.measurementsCache = measurements;
706
707
  return measurements;
@@ -718,16 +719,20 @@ class Virtualizer {
718
719
  this.options.lanes
719
720
  ],
720
721
  (measurements, outerSize, scrollOffset, lanes) => {
721
- return this.range = measurements.length > 0 && outerSize > 0 ? calculateRange({
722
+ if (measurements.length === 0 || outerSize === 0) {
723
+ this.range = null;
724
+ return null;
725
+ }
726
+ this.range = calculateRangeImpl(
722
727
  measurements,
723
728
  outerSize,
724
729
  scrollOffset,
725
730
  lanes,
726
- // Pass the typed array so binary search + forward-walk can
727
- // read start/end directly from Float64Array, skipping the
728
- // Proxy traps that materialize a full VirtualItem per probe.
729
- flat: lanes === 1 && this._flatMeasurements != null ? this._flatMeasurements : null
730
- }) : null;
731
+ // Pass the typed array so binary search + forward-walk can read
732
+ // start/end directly from Float64Array, skipping the Proxy traps.
733
+ lanes === 1 && this._flatMeasurements != null ? this._flatMeasurements : null
734
+ );
735
+ return this.range;
731
736
  },
732
737
  {
733
738
  key: process.env.NODE_ENV !== "production" && "calculateRange",
@@ -1187,26 +1192,44 @@ const findNearestBinarySearch = (low, high, getCurrentValue, value) => {
1187
1192
  return 0;
1188
1193
  }
1189
1194
  };
1190
- function calculateRange({
1191
- measurements,
1192
- outerSize,
1193
- scrollOffset,
1194
- lanes,
1195
- flat
1196
- }) {
1195
+ function findNearestBinarySearchFlat(flat, high, value) {
1196
+ let low = 0;
1197
+ while (low <= high) {
1198
+ const middle = (low + high) / 2 | 0;
1199
+ const currentValue = flat[middle * 2];
1200
+ if (currentValue < value) {
1201
+ low = middle + 1;
1202
+ } else if (currentValue > value) {
1203
+ high = middle - 1;
1204
+ } else {
1205
+ return middle;
1206
+ }
1207
+ }
1208
+ return low > 0 ? low - 1 : 0;
1209
+ }
1210
+ function calculateRangeImpl(measurements, outerSize, scrollOffset, lanes, flat) {
1197
1211
  const lastIndex = measurements.length - 1;
1198
- const getStart = flat ? (index) => flat[index * 2] : (index) => measurements[index].start;
1199
- const getEnd = flat ? (index) => flat[index * 2] + flat[index * 2 + 1] : (index) => measurements[index].end;
1200
1212
  if (measurements.length <= lanes) {
1201
- return {
1202
- startIndex: 0,
1203
- endIndex: lastIndex
1204
- };
1213
+ return { startIndex: 0, endIndex: lastIndex };
1214
+ }
1215
+ if (lanes === 1 && flat !== null) {
1216
+ const startIndex2 = findNearestBinarySearchFlat(
1217
+ flat,
1218
+ lastIndex,
1219
+ scrollOffset
1220
+ );
1221
+ let endIndex2 = startIndex2;
1222
+ const limit = scrollOffset + outerSize;
1223
+ while (endIndex2 < lastIndex && flat[endIndex2 * 2] + flat[endIndex2 * 2 + 1] < limit) {
1224
+ endIndex2++;
1225
+ }
1226
+ return { startIndex: startIndex2, endIndex: endIndex2 };
1205
1227
  }
1228
+ const getStart = (index) => measurements[index].start;
1206
1229
  let startIndex = findNearestBinarySearch(0, lastIndex, getStart, scrollOffset);
1207
1230
  let endIndex = startIndex;
1208
1231
  if (lanes === 1) {
1209
- while (endIndex < lastIndex && getEnd(endIndex) < scrollOffset + outerSize) {
1232
+ while (endIndex < lastIndex && measurements[endIndex].end < scrollOffset + outerSize) {
1210
1233
  endIndex++;
1211
1234
  }
1212
1235
  } else if (lanes > 1) {