@tanstack/virtual-core 3.19.15 → 3.19.17

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.
@@ -202,6 +202,7 @@ class Virtualizer {
202
202
  this.measurementsCache = [];
203
203
  this._singleLaneMeasurements = null;
204
204
  this.itemSizeCache = /* @__PURE__ */ new Map();
205
+ this.elementKeys = /* @__PURE__ */ new WeakMap();
205
206
  this.itemSizeCacheVersion = 0;
206
207
  this.builtMeasurementOptions = null;
207
208
  this.builtItemSizeCacheVersion = -1;
@@ -360,6 +361,16 @@ class Virtualizer {
360
361
  var _a;
361
362
  pendingObservations.delete(target);
362
363
  (_a = get()) == null ? void 0 : _a.unobserve(target);
364
+ },
365
+ isDelivering: () => delivering,
366
+ asDelivery: (fn) => {
367
+ const prev = delivering;
368
+ delivering = true;
369
+ try {
370
+ fn();
371
+ } finally {
372
+ delivering = prev;
373
+ }
363
374
  }
364
375
  };
365
376
  })();
@@ -552,12 +563,15 @@ class Virtualizer {
552
563
  this.elementsCache.forEach((cached) => {
553
564
  this.observer.observe(cached);
554
565
  });
566
+ let observing = false;
555
567
  this.unsubs.push(
556
568
  this.options.observeElementRect(this, (rect) => {
557
569
  this.scrollRect = rect;
558
570
  this.maybeNotify();
571
+ if (observing) this.measureElement(null);
559
572
  })
560
573
  );
574
+ observing = true;
561
575
  this.unsubs.push(
562
576
  this.options.observeElementOffset(this, (offset, isScrolling) => {
563
577
  var _a2, _b, _c, _d;
@@ -1094,6 +1108,19 @@ class Virtualizer {
1094
1108
  }
1095
1109
  return;
1096
1110
  }
1111
+ if (!node.isConnected && !node.getAttribute(this.options.indexAttribute)) {
1112
+ const delivering = this.observer.isDelivering();
1113
+ queueMicrotask(() => {
1114
+ if (!node.getAttribute(this.options.indexAttribute)) {
1115
+ this.indexFromElement(node);
1116
+ } else if (delivering) {
1117
+ this.observer.asDelivery(() => this.measureElement(node));
1118
+ } else {
1119
+ this.measureElement(node);
1120
+ }
1121
+ });
1122
+ return;
1123
+ }
1097
1124
  const index = this.indexFromElement(node);
1098
1125
  if (!this.isIndexInRange(index)) return;
1099
1126
  const key = this.options.getItemKey(index);
@@ -1102,18 +1129,25 @@ class Virtualizer {
1102
1129
  if (prevNode) {
1103
1130
  this.observer.unobserve(prevNode);
1104
1131
  }
1132
+ const prevKey = this.elementKeys.get(node);
1133
+ if (prevKey !== void 0 && this.elementsCache.get(prevKey) === node) {
1134
+ this.elementsCache.delete(prevKey);
1135
+ this.observer.unobserve(node);
1136
+ }
1105
1137
  this.observer.observe(node);
1106
1138
  this.elementsCache.set(key, node);
1139
+ this.elementKeys.set(node, key);
1107
1140
  }
1108
1141
  if (node.isConnected && (!this.isScrolling || this.scrollState) && this.shouldMeasureDuringScroll(index)) {
1109
1142
  this.resizeItem(index, this.options.measureElement(node, void 0, this));
1110
1143
  }
1111
1144
  };
1112
- 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;
1145
+ this.onlyPendingResizesSinceBuild = () => this.itemSizeCacheVersion === this.builtItemSizeCacheVersion + this.pendingResizes.length;
1146
+ this.canShiftFromPendingResizes = () => this.options.lanes === 1 && this._singleLaneMeasurements !== null && this.shouldAdjustScrollPositionOnItemSizeChange === void 0 && !this.lanesChangedFlag && !this.lanesSettling && this.getMeasurementOptions() === this.builtMeasurementOptions && this.onlyPendingResizesSinceBuild();
1113
1147
  this.resizeItem = (index, size) => {
1114
1148
  var _a, _b, _c;
1115
1149
  if (!this.isIndexInRange(index)) return;
1116
- if (this.getMeasurementOptions() !== this.builtMeasurementOptions) {
1150
+ if (this.getMeasurementOptions() !== this.builtMeasurementOptions || !this.onlyPendingResizesSinceBuild()) {
1117
1151
  this.getMeasurements();
1118
1152
  }
1119
1153
  let startShift = 0;