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