@tanstack/virtual-core 3.17.8 → 3.17.10
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/cjs/index.cjs +81 -20
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.cts +4 -2
- package/dist/cjs/lazy-measurements.cjs +8 -4
- package/dist/cjs/lazy-measurements.cjs.map +1 -1
- package/dist/cjs/lazy-measurements.d.cts +2 -3
- package/dist/esm/index.d.ts +4 -2
- package/dist/esm/index.js +82 -21
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/lazy-measurements.d.ts +2 -3
- package/dist/esm/lazy-measurements.js +9 -5
- package/dist/esm/lazy-measurements.js.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +148 -28
- package/src/lazy-measurements.ts +13 -10
package/dist/cjs/index.cjs
CHANGED
|
@@ -164,6 +164,27 @@ const scrollWithAdjustments = (offset, {
|
|
|
164
164
|
};
|
|
165
165
|
const windowScroll = scrollWithAdjustments;
|
|
166
166
|
const elementScroll = scrollWithAdjustments;
|
|
167
|
+
function isAppendWithTrim(prevCount, nextCount, getPreviousKey, getNextKey) {
|
|
168
|
+
if (nextCount === 0) return false;
|
|
169
|
+
const firstKey = getNextKey(0);
|
|
170
|
+
const removedKeys = /* @__PURE__ */ new Set();
|
|
171
|
+
let removedCount = 0;
|
|
172
|
+
while (removedCount < prevCount) {
|
|
173
|
+
const key = getPreviousKey(removedCount);
|
|
174
|
+
if (key === firstKey) break;
|
|
175
|
+
removedKeys.add(key);
|
|
176
|
+
removedCount++;
|
|
177
|
+
}
|
|
178
|
+
const retainedCount = prevCount - removedCount;
|
|
179
|
+
if (retainedCount === 0 || retainedCount >= nextCount) return false;
|
|
180
|
+
for (let i = 0; i < retainedCount; i++) {
|
|
181
|
+
if (getNextKey(i) !== getPreviousKey(removedCount + i)) return false;
|
|
182
|
+
}
|
|
183
|
+
for (let i = retainedCount; i < nextCount; i++) {
|
|
184
|
+
if (removedKeys.has(getNextKey(i))) return false;
|
|
185
|
+
}
|
|
186
|
+
return true;
|
|
187
|
+
}
|
|
167
188
|
class Virtualizer {
|
|
168
189
|
constructor(opts) {
|
|
169
190
|
this.unsubs = [];
|
|
@@ -172,7 +193,7 @@ class Virtualizer {
|
|
|
172
193
|
this.isScrolling = false;
|
|
173
194
|
this.scrollState = null;
|
|
174
195
|
this.measurementsCache = [];
|
|
175
|
-
this.
|
|
196
|
+
this._singleLaneMeasurements = null;
|
|
176
197
|
this.itemSizeCache = /* @__PURE__ */ new Map();
|
|
177
198
|
this.itemSizeCacheVersion = 0;
|
|
178
199
|
this.laneAssignments = /* @__PURE__ */ new Map();
|
|
@@ -190,6 +211,7 @@ class Virtualizer {
|
|
|
190
211
|
this._iosJustTouchEnded = false;
|
|
191
212
|
this._iosTouchEndTimerId = null;
|
|
192
213
|
this._intendedScrollOffset = null;
|
|
214
|
+
this._clampedAdjustment = null;
|
|
193
215
|
this.elementsCache = /* @__PURE__ */ new Map();
|
|
194
216
|
this.now = () => {
|
|
195
217
|
var _a, _b, _c;
|
|
@@ -249,7 +271,7 @@ class Virtualizer {
|
|
|
249
271
|
})();
|
|
250
272
|
this.range = null;
|
|
251
273
|
this.setOptions = (opts2) => {
|
|
252
|
-
var _a
|
|
274
|
+
var _a;
|
|
253
275
|
const merged = {
|
|
254
276
|
debug: false,
|
|
255
277
|
initialOffset: 0,
|
|
@@ -293,8 +315,10 @@ class Virtualizer {
|
|
|
293
315
|
const prevCount = prevOptions.count;
|
|
294
316
|
const nextCount = merged.count;
|
|
295
317
|
const measurements = this.getMeasurements();
|
|
296
|
-
const
|
|
297
|
-
const
|
|
318
|
+
const previousItems = ((_a = this._singleLaneMeasurements) == null ? void 0 : _a.items) ?? measurements;
|
|
319
|
+
const getPreviousKey = (index) => lazyMeasurements.getMeasurementKey(previousItems[index]);
|
|
320
|
+
const prevFirstKey = prevCount > 0 ? getPreviousKey(0) : null;
|
|
321
|
+
const prevLastKey = prevCount > 0 ? getPreviousKey(prevCount - 1) : null;
|
|
298
322
|
const didCountChange = nextCount !== prevCount;
|
|
299
323
|
const didEdgeKeysChange = didCountChange || prevCount > 0 && nextCount > 0 && (merged.getItemKey(0) !== prevFirstKey || merged.getItemKey(nextCount - 1) !== prevLastKey);
|
|
300
324
|
if (didEdgeKeysChange) {
|
|
@@ -304,8 +328,15 @@ class Virtualizer {
|
|
|
304
328
|
anchor = [item.key, this.getScrollOffset() - item.start];
|
|
305
329
|
}
|
|
306
330
|
const behavior = merged.followOnAppend === true ? "auto" : merged.followOnAppend || null;
|
|
307
|
-
if (behavior && nextCount >
|
|
308
|
-
|
|
331
|
+
if (behavior && nextCount > 0 && this.isAtEnd(prevOptions.scrollEndThreshold) && (prevCount === 0 || merged.getItemKey(nextCount - 1) !== prevLastKey)) {
|
|
332
|
+
if (nextCount > prevCount || isAppendWithTrim(
|
|
333
|
+
prevCount,
|
|
334
|
+
nextCount,
|
|
335
|
+
getPreviousKey,
|
|
336
|
+
merged.getItemKey
|
|
337
|
+
)) {
|
|
338
|
+
followOnAppend = behavior;
|
|
339
|
+
}
|
|
309
340
|
}
|
|
310
341
|
}
|
|
311
342
|
}
|
|
@@ -328,7 +359,7 @@ class Virtualizer {
|
|
|
328
359
|
const anchorItem = newMeasurements[idx];
|
|
329
360
|
if (anchorItem) {
|
|
330
361
|
const newOffset = Math.max(0, anchorItem.start + anchorOffset);
|
|
331
|
-
if (newOffset !== this.scrollOffset) {
|
|
362
|
+
if (!followOnAppend && newOffset !== this.scrollOffset) {
|
|
332
363
|
anchorDelta = newOffset - this.scrollOffset;
|
|
333
364
|
this.scrollOffset = newOffset;
|
|
334
365
|
anchorResolved = true;
|
|
@@ -385,6 +416,7 @@ class Virtualizer {
|
|
|
385
416
|
this._iosDeferredAdjustment = 0;
|
|
386
417
|
this._iosTouching = false;
|
|
387
418
|
this._iosJustTouchEnded = false;
|
|
419
|
+
this._clampedAdjustment = null;
|
|
388
420
|
this.scrollElement = null;
|
|
389
421
|
this.targetWindow = null;
|
|
390
422
|
};
|
|
@@ -426,6 +458,9 @@ class Virtualizer {
|
|
|
426
458
|
offset = this._intendedScrollOffset;
|
|
427
459
|
}
|
|
428
460
|
this._intendedScrollOffset = null;
|
|
461
|
+
if (this._clampedAdjustment !== null && Math.abs(offset - this._clampedAdjustment.maxAtWrite) >= 1.5) {
|
|
462
|
+
this._clampedAdjustment = null;
|
|
463
|
+
}
|
|
429
464
|
this.scrollAdjustments = 0;
|
|
430
465
|
const prevOffset = this.getScrollOffset();
|
|
431
466
|
this.scrollDirection = isScrolling ? prevOffset === offset ? this.scrollDirection : prevOffset < offset ? "forward" : "backward" : null;
|
|
@@ -504,6 +539,21 @@ class Virtualizer {
|
|
|
504
539
|
this.scrollToEnd({ behavior: followOnAppend });
|
|
505
540
|
}
|
|
506
541
|
}
|
|
542
|
+
this._retryClampedAdjustment();
|
|
543
|
+
};
|
|
544
|
+
this._retryClampedAdjustment = () => {
|
|
545
|
+
if (this._clampedAdjustment === null || !this.scrollElement || !this.options.enabled) {
|
|
546
|
+
return;
|
|
547
|
+
}
|
|
548
|
+
const { target, maxAtWrite } = this._clampedAdjustment;
|
|
549
|
+
const max = this.getMaxScrollOffset();
|
|
550
|
+
if (max > maxAtWrite + 0.5) {
|
|
551
|
+
this._clampedAdjustment = target > max + 0.5 ? { target, maxAtWrite: max } : null;
|
|
552
|
+
this._scrollToOffset(target, {
|
|
553
|
+
adjustments: void 0,
|
|
554
|
+
behavior: void 0
|
|
555
|
+
});
|
|
556
|
+
}
|
|
507
557
|
};
|
|
508
558
|
this._flushIosDeferredIfReady = () => {
|
|
509
559
|
if (this._iosDeferredAdjustment === 0) return;
|
|
@@ -587,9 +637,11 @@ class Virtualizer {
|
|
|
587
637
|
laneAssignmentMode,
|
|
588
638
|
gap
|
|
589
639
|
}, _itemSizeCacheVersion) => {
|
|
640
|
+
var _a;
|
|
590
641
|
const itemSizeCache = this.itemSizeCache;
|
|
591
642
|
if (!enabled) {
|
|
592
643
|
this.measurementsCache = [];
|
|
644
|
+
this._singleLaneMeasurements = null;
|
|
593
645
|
this.itemSizeCache.clear();
|
|
594
646
|
this.laneAssignments.clear();
|
|
595
647
|
return [];
|
|
@@ -605,6 +657,7 @@ class Virtualizer {
|
|
|
605
657
|
this.lanesChangedFlag = false;
|
|
606
658
|
this.lanesSettling = true;
|
|
607
659
|
this.measurementsCache = [];
|
|
660
|
+
this._singleLaneMeasurements = null;
|
|
608
661
|
this.itemSizeCache.clear();
|
|
609
662
|
this.laneAssignments.clear();
|
|
610
663
|
this.pendingMin = null;
|
|
@@ -622,13 +675,13 @@ class Virtualizer {
|
|
|
622
675
|
}
|
|
623
676
|
if (lanes === 1) {
|
|
624
677
|
const need = count * 2;
|
|
625
|
-
let flat = this.
|
|
678
|
+
let flat = (_a = this._singleLaneMeasurements) == null ? void 0 : _a.flat;
|
|
626
679
|
if (!flat || flat.length < need) {
|
|
627
680
|
const next = new Float64Array(need);
|
|
628
681
|
if (flat && min > 0) next.set(flat.subarray(0, min * 2));
|
|
629
682
|
flat = next;
|
|
630
|
-
this._flatMeasurements = flat;
|
|
631
683
|
}
|
|
684
|
+
const items = min === 0 ? new Array(count) : this._singleLaneMeasurements.items.slice();
|
|
632
685
|
let runningStart;
|
|
633
686
|
if (min === 0) {
|
|
634
687
|
runningStart = paddingStart + scrollMargin;
|
|
@@ -638,13 +691,15 @@ class Virtualizer {
|
|
|
638
691
|
}
|
|
639
692
|
for (let i = min; i < count; i++) {
|
|
640
693
|
const key = getItemKey(i);
|
|
694
|
+
items[i] = key;
|
|
641
695
|
const measuredSize = itemSizeCache.get(key);
|
|
642
696
|
const size = typeof measuredSize === "number" ? measuredSize : this.options.estimateSize(i);
|
|
643
697
|
flat[i * 2] = runningStart;
|
|
644
698
|
flat[i * 2 + 1] = size;
|
|
645
699
|
runningStart += size + gap;
|
|
646
700
|
}
|
|
647
|
-
|
|
701
|
+
this._singleLaneMeasurements = { flat, items };
|
|
702
|
+
const view = lazyMeasurements.createLazyMeasurementsView(items, flat);
|
|
648
703
|
this.measurementsCache = view;
|
|
649
704
|
return view;
|
|
650
705
|
}
|
|
@@ -739,7 +794,7 @@ class Virtualizer {
|
|
|
739
794
|
lanes,
|
|
740
795
|
// Pass the typed array so binary search + forward-walk can read
|
|
741
796
|
// start/end directly from Float64Array, skipping the Proxy traps.
|
|
742
|
-
lanes === 1 && this.
|
|
797
|
+
lanes === 1 && this._singleLaneMeasurements !== null ? this._singleLaneMeasurements.flat : null
|
|
743
798
|
);
|
|
744
799
|
return this.range;
|
|
745
800
|
},
|
|
@@ -836,13 +891,13 @@ class Virtualizer {
|
|
|
836
891
|
}
|
|
837
892
|
};
|
|
838
893
|
this.resizeItem = (index, size) => {
|
|
839
|
-
var _a, _b;
|
|
894
|
+
var _a, _b, _c;
|
|
840
895
|
if (!this.isIndexInRange(index)) return;
|
|
841
896
|
let cachedSize;
|
|
842
897
|
let itemStart;
|
|
843
898
|
let key;
|
|
844
|
-
const flat = this.
|
|
845
|
-
if (this.options.lanes === 1 && flat
|
|
899
|
+
const flat = (_a = this._singleLaneMeasurements) == null ? void 0 : _a.flat;
|
|
900
|
+
if (this.options.lanes === 1 && flat != null) {
|
|
846
901
|
key = this.options.getItemKey(index);
|
|
847
902
|
itemStart = flat[index * 2];
|
|
848
903
|
cachedSize = flat[index * 2 + 1];
|
|
@@ -856,7 +911,7 @@ class Virtualizer {
|
|
|
856
911
|
const itemSize = this.itemSizeCache.get(key) ?? cachedSize;
|
|
857
912
|
const delta = size - itemSize;
|
|
858
913
|
if (delta !== 0) {
|
|
859
|
-
const wasAtEnd = this.options.anchorTo === "end" && ((
|
|
914
|
+
const wasAtEnd = this.options.anchorTo === "end" && ((_b = this.scrollState) == null ? void 0 : _b.behavior) !== "smooth" && this.getVirtualDistanceFromEnd() <= this.options.scrollEndThreshold;
|
|
860
915
|
const prevTotalSize = wasAtEnd ? this.getTotalSize() : 0;
|
|
861
916
|
const scrollOffsetWithAdj = this.getScrollOffset() + this.scrollAdjustments;
|
|
862
917
|
const isFirstMeasure = !this.itemSizeCache.has(key);
|
|
@@ -875,7 +930,7 @@ class Virtualizer {
|
|
|
875
930
|
// scrolling up" cascade.
|
|
876
931
|
itemStart + itemSize <= scrollOffsetWithAdj && this.scrollDirection !== "backward"
|
|
877
932
|
);
|
|
878
|
-
const shouldAdjustScroll = ((
|
|
933
|
+
const shouldAdjustScroll = ((_c = this.scrollState) == null ? void 0 : _c.behavior) !== "smooth" && (this.shouldAdjustScrollPositionOnItemSizeChange !== void 0 ? this.shouldAdjustScrollPositionOnItemSizeChange(
|
|
879
934
|
// The callback expects a VirtualItem; build one lazily only
|
|
880
935
|
// when the consumer actually supplied a custom predicate.
|
|
881
936
|
this.measurementsCache[index] ?? {
|
|
@@ -903,6 +958,7 @@ class Virtualizer {
|
|
|
903
958
|
adjustedSync = this.applyScrollAdjustment(delta);
|
|
904
959
|
}
|
|
905
960
|
this.notify(adjustedSync);
|
|
961
|
+
this._retryClampedAdjustment();
|
|
906
962
|
}
|
|
907
963
|
};
|
|
908
964
|
this.getVirtualItems = utils.memo(
|
|
@@ -922,11 +978,12 @@ class Virtualizer {
|
|
|
922
978
|
}
|
|
923
979
|
);
|
|
924
980
|
this.getVirtualItemForOffset = (offset) => {
|
|
981
|
+
var _a;
|
|
925
982
|
const measurements = this.getMeasurements();
|
|
926
983
|
if (measurements.length === 0) {
|
|
927
984
|
return void 0;
|
|
928
985
|
}
|
|
929
|
-
const flat = this.
|
|
986
|
+
const flat = (_a = this._singleLaneMeasurements) == null ? void 0 : _a.flat;
|
|
930
987
|
const useFlat = this.options.lanes === 1 && flat != null;
|
|
931
988
|
const idx = findNearestBinarySearch(
|
|
932
989
|
0,
|
|
@@ -1061,18 +1118,18 @@ class Virtualizer {
|
|
|
1061
1118
|
});
|
|
1062
1119
|
};
|
|
1063
1120
|
this.getTotalSize = () => {
|
|
1064
|
-
var _a;
|
|
1121
|
+
var _a, _b;
|
|
1065
1122
|
const measurements = this.getMeasurements();
|
|
1066
1123
|
let end;
|
|
1067
1124
|
if (measurements.length === 0) {
|
|
1068
1125
|
end = this.options.paddingStart;
|
|
1069
1126
|
} else if (this.options.lanes === 1) {
|
|
1070
1127
|
const lastIdx = measurements.length - 1;
|
|
1071
|
-
const flat = this.
|
|
1128
|
+
const flat = (_a = this._singleLaneMeasurements) == null ? void 0 : _a.flat;
|
|
1072
1129
|
if (flat != null) {
|
|
1073
1130
|
end = flat[lastIdx * 2] + flat[lastIdx * 2 + 1];
|
|
1074
1131
|
} else {
|
|
1075
|
-
end = ((
|
|
1132
|
+
end = ((_b = measurements[lastIdx]) == null ? void 0 : _b.end) ?? 0;
|
|
1076
1133
|
}
|
|
1077
1134
|
} else {
|
|
1078
1135
|
const endByLane = Array(this.options.lanes).fill(null);
|
|
@@ -1138,6 +1195,10 @@ class Virtualizer {
|
|
|
1138
1195
|
this._iosDeferredAdjustment += delta;
|
|
1139
1196
|
return false;
|
|
1140
1197
|
} else {
|
|
1198
|
+
const target = this.getScrollOffset() + this.scrollAdjustments + delta;
|
|
1199
|
+
const el = this.scrollElement;
|
|
1200
|
+
const maxAtWrite = el !== null && ("scrollHeight" in el || "document" in el) ? this.getMaxScrollOffset() : null;
|
|
1201
|
+
this._clampedAdjustment = maxAtWrite !== null && target > maxAtWrite + 0.5 ? { target, maxAtWrite } : null;
|
|
1141
1202
|
this._scrollToOffset(this.getScrollOffset(), {
|
|
1142
1203
|
adjustments: this.scrollAdjustments += delta,
|
|
1143
1204
|
behavior
|