@tanstack/virtual-core 3.19.16 → 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
  })();
@@ -1097,6 +1108,19 @@ class Virtualizer {
1097
1108
  }
1098
1109
  return;
1099
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
+ }
1100
1124
  const index = this.indexFromElement(node);
1101
1125
  if (!this.isIndexInRange(index)) return;
1102
1126
  const key = this.options.getItemKey(index);
@@ -1105,18 +1129,25 @@ class Virtualizer {
1105
1129
  if (prevNode) {
1106
1130
  this.observer.unobserve(prevNode);
1107
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
+ }
1108
1137
  this.observer.observe(node);
1109
1138
  this.elementsCache.set(key, node);
1139
+ this.elementKeys.set(node, key);
1110
1140
  }
1111
1141
  if (node.isConnected && (!this.isScrolling || this.scrollState) && this.shouldMeasureDuringScroll(index)) {
1112
1142
  this.resizeItem(index, this.options.measureElement(node, void 0, this));
1113
1143
  }
1114
1144
  };
1115
- 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();
1116
1147
  this.resizeItem = (index, size) => {
1117
1148
  var _a, _b, _c;
1118
1149
  if (!this.isIndexInRange(index)) return;
1119
- if (this.getMeasurementOptions() !== this.builtMeasurementOptions) {
1150
+ if (this.getMeasurementOptions() !== this.builtMeasurementOptions || !this.onlyPendingResizesSinceBuild()) {
1120
1151
  this.getMeasurements();
1121
1152
  }
1122
1153
  let startShift = 0;