@tanstack/virtual-core 3.19.12 → 3.19.13

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.
@@ -203,11 +203,15 @@ class Virtualizer {
203
203
  this._singleLaneMeasurements = null;
204
204
  this.itemSizeCache = /* @__PURE__ */ new Map();
205
205
  this.itemSizeCacheVersion = 0;
206
+ this.builtMeasurementOptions = null;
207
+ this.builtItemSizeCacheVersion = -1;
208
+ this.pendingResizes = [];
206
209
  this.laneAssignments = /* @__PURE__ */ new Map();
207
210
  this.pendingMin = null;
208
211
  this.prevLanes = void 0;
209
212
  this.lanesChangedFlag = false;
210
213
  this.lanesSettling = false;
214
+ this.initialMeasurementsCacheApplied = false;
211
215
  this.pendingScrollAnchor = null;
212
216
  this.scrollRect = null;
213
217
  this.scrollOffset = null;
@@ -770,23 +774,28 @@ class Virtualizer {
770
774
  this.isIndexInRange = (index) => index >= 0 && index < this.options.count;
771
775
  this.getMeasurements = utils.memo(
772
776
  () => [this.getMeasurementOptions(), this.itemSizeCacheVersion],
773
- ({
774
- count,
775
- paddingStart,
776
- scrollMargin,
777
- getItemKey,
778
- enabled,
779
- lanes,
780
- laneAssignmentMode,
781
- gap
782
- }, _itemSizeCacheVersion) => {
777
+ (measurementOptions, itemSizeCacheVersion) => {
783
778
  var _a;
779
+ const {
780
+ count,
781
+ paddingStart,
782
+ scrollMargin,
783
+ getItemKey,
784
+ enabled,
785
+ lanes,
786
+ laneAssignmentMode,
787
+ gap
788
+ } = measurementOptions;
789
+ this.builtMeasurementOptions = measurementOptions;
790
+ this.builtItemSizeCacheVersion = itemSizeCacheVersion;
791
+ this.pendingResizes = [];
784
792
  const itemSizeCache = this.itemSizeCache;
785
793
  if (!enabled) {
786
794
  this.measurementsCache = [];
787
795
  this._singleLaneMeasurements = null;
788
796
  this.itemSizeCache.clear();
789
797
  this.laneAssignments.clear();
798
+ this.initialMeasurementsCacheApplied = false;
790
799
  return [];
791
800
  }
792
801
  if (this.laneAssignments.size > count) {
@@ -805,7 +814,8 @@ class Virtualizer {
805
814
  this.laneAssignments.clear();
806
815
  this.pendingMin = null;
807
816
  }
808
- if (this.measurementsCache.length === 0 && !this.lanesSettling) {
817
+ if (!this.initialMeasurementsCacheApplied && this.measurementsCache.length === 0 && !this.lanesSettling) {
818
+ this.initialMeasurementsCacheApplied = true;
809
819
  this.measurementsCache = this.options.initialMeasurementsCache;
810
820
  this.measurementsCache.forEach((item) => {
811
821
  this.itemSizeCache.set(item.key, item.size);
@@ -1040,17 +1050,27 @@ class Virtualizer {
1040
1050
  this.resizeItem(index, this.options.measureElement(node, void 0, this));
1041
1051
  }
1042
1052
  };
1053
+ this.canShiftFromPendingResizes = () => this.options.lanes === 1 && this._singleLaneMeasurements !== null && this.shouldAdjustScrollPositionOnItemSizeChange === void 0 && !this.lanesChangedFlag && !this.lanesSettling && this.getMeasurementOptions() === this.builtMeasurementOptions && this.itemSizeCacheVersion === this.builtItemSizeCacheVersion + this.pendingResizes.length;
1043
1054
  this.resizeItem = (index, size) => {
1044
1055
  var _a, _b, _c;
1045
1056
  if (!this.isIndexInRange(index)) return;
1046
- if ((_a = this._resizeBatch) == null ? void 0 : _a.sync) this.getMeasurements();
1057
+ let startShift = 0;
1058
+ if ((_a = this._resizeBatch) == null ? void 0 : _a.sync) {
1059
+ if (this.canShiftFromPendingResizes()) {
1060
+ for (const resize of this.pendingResizes) {
1061
+ if (resize.index < index) startShift += resize.delta;
1062
+ }
1063
+ } else {
1064
+ this.getMeasurements();
1065
+ }
1066
+ }
1047
1067
  let cachedSize;
1048
1068
  let itemStart;
1049
1069
  let key;
1050
1070
  const flat = (_b = this._singleLaneMeasurements) == null ? void 0 : _b.flat;
1051
1071
  if (this.options.lanes === 1 && flat != null) {
1052
1072
  key = this.options.getItemKey(index);
1053
- itemStart = flat[index * 2];
1073
+ itemStart = flat[index * 2] + startShift;
1054
1074
  cachedSize = flat[index * 2 + 1];
1055
1075
  } else {
1056
1076
  const item = this.measurementsCache[index];
@@ -1100,6 +1120,7 @@ class Virtualizer {
1100
1120
  }
1101
1121
  this.itemSizeCache.set(key, size);
1102
1122
  this.itemSizeCacheVersion++;
1123
+ this.pendingResizes.push({ index, delta });
1103
1124
  let adjustedSync = false;
1104
1125
  if (!this._awaitingCommandReadBack()) {
1105
1126
  if (wasAtEnd) {