@tanstack/virtual-core 3.18.0 → 3.19.0
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 +55 -33
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.cts +7 -1
- package/dist/esm/index.d.ts +7 -1
- package/dist/esm/index.js +55 -33
- package/dist/esm/index.js.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +91 -39
package/src/index.ts
CHANGED
|
@@ -56,6 +56,11 @@ export interface Range {
|
|
|
56
56
|
endIndex: number
|
|
57
57
|
overscan: number
|
|
58
58
|
count: number
|
|
59
|
+
/**
|
|
60
|
+
* Lane count of the virtualizer. When greater than 1, `overscan` counts
|
|
61
|
+
* rows of `lanes` items rather than single items.
|
|
62
|
+
*/
|
|
63
|
+
lanes?: number
|
|
59
64
|
}
|
|
60
65
|
|
|
61
66
|
type Key = number | string | bigint
|
|
@@ -84,8 +89,13 @@ const getRect = (element: HTMLElement): Rect => {
|
|
|
84
89
|
export const defaultKeyExtractor = (index: number) => index
|
|
85
90
|
|
|
86
91
|
export const defaultRangeExtractor = (range: Range) => {
|
|
87
|
-
|
|
88
|
-
|
|
92
|
+
// With lanes, the visible range is already aligned to whole rows; overscan
|
|
93
|
+
// by whole rows too, so every lane gets the same number of items rendered
|
|
94
|
+
// (and measured). Overscanning single items leaves partial rows, letting one
|
|
95
|
+
// lane collect more measured sizes than the others and drift (#1036).
|
|
96
|
+
const overscan = range.overscan * Math.max(range.lanes ?? 1, 1)
|
|
97
|
+
const start = Math.max(range.startIndex - overscan, 0)
|
|
98
|
+
const end = Math.min(range.endIndex + overscan, range.count - 1)
|
|
89
99
|
const len = end - start + 1
|
|
90
100
|
|
|
91
101
|
const arr = new Array<number>(len)
|
|
@@ -505,6 +515,9 @@ export class Virtualizer<
|
|
|
505
515
|
instance: Virtualizer<TScrollElement, TItemElement>,
|
|
506
516
|
) => boolean)
|
|
507
517
|
elementsCache = new Map<Key, TItemElement>()
|
|
518
|
+
// Set while a ResizeObserver callback measures its entries; `resizeItem`
|
|
519
|
+
// records the pending notify here instead of firing it per entry.
|
|
520
|
+
private _resizeBatch: { sync: boolean; notify?: boolean } | null = null
|
|
508
521
|
private now = () => this.targetWindow?.performance?.now?.() ?? Date.now()
|
|
509
522
|
private observer = (() => {
|
|
510
523
|
let _ro: ResizeObserver | null = null
|
|
@@ -519,46 +532,60 @@ export class Virtualizer<
|
|
|
519
532
|
}
|
|
520
533
|
|
|
521
534
|
return (_ro = new this.targetWindow.ResizeObserver((entries) => {
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
for (const [cacheKey, cachedNode] of this.elementsCache) {
|
|
538
|
-
if (cachedNode === node) {
|
|
539
|
-
this.elementsCache.delete(cacheKey)
|
|
540
|
-
break
|
|
541
|
-
}
|
|
542
|
-
}
|
|
543
|
-
return
|
|
544
|
-
}
|
|
545
|
-
|
|
546
|
-
if (!this.isIndexInRange(index)) return
|
|
547
|
-
|
|
548
|
-
if (this.shouldMeasureDuringScroll(index)) {
|
|
549
|
-
this.resizeItem(
|
|
550
|
-
index,
|
|
551
|
-
this.options.measureElement(node, entry, this),
|
|
552
|
-
)
|
|
535
|
+
const run = () => {
|
|
536
|
+
// Measure every entry before notifying. A notify that adjusted
|
|
537
|
+
// scroll re-renders synchronously, and that render can unmount
|
|
538
|
+
// nodes whose entries are still queued here, leaving them
|
|
539
|
+
// unmeasured. With lanes, a half-measured row lets one lane drift
|
|
540
|
+
// away from the others (#1036).
|
|
541
|
+
const batch: { sync: boolean; notify?: boolean } = { sync: false }
|
|
542
|
+
this._resizeBatch = batch
|
|
543
|
+
try {
|
|
544
|
+
entries.forEach(measureEntry)
|
|
545
|
+
} finally {
|
|
546
|
+
this._resizeBatch = null
|
|
547
|
+
if (batch.notify) {
|
|
548
|
+
this.notify(batch.sync)
|
|
549
|
+
this._retryClampedAdjustment()
|
|
553
550
|
}
|
|
554
551
|
}
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
552
|
+
}
|
|
553
|
+
this.options.useAnimationFrameWithResizeObserver
|
|
554
|
+
? requestAnimationFrame(run)
|
|
555
|
+
: run()
|
|
559
556
|
}))
|
|
560
557
|
}
|
|
561
558
|
|
|
559
|
+
const measureEntry = (entry: ResizeObserverEntry) => {
|
|
560
|
+
const node = entry.target as TItemElement
|
|
561
|
+
const index = this.indexFromElement(node)
|
|
562
|
+
|
|
563
|
+
if (!node.isConnected) {
|
|
564
|
+
this.observer.unobserve(node)
|
|
565
|
+
// Find the cache entry pointing to this exact node and remove
|
|
566
|
+
// it. We can't call getItemKey(index) here because items may
|
|
567
|
+
// have been removed since this node was rendered — the index
|
|
568
|
+
// could be stale and out-of-bounds in the user's data array
|
|
569
|
+
// (regression test in e2e/.../stale-index.spec.ts, fix #1148).
|
|
570
|
+
// The === comparison naturally handles the React-replaced-
|
|
571
|
+
// a-node-for-the-same-key case: that entry now points to a
|
|
572
|
+
// different node, so this loop won't match.
|
|
573
|
+
for (const [cacheKey, cachedNode] of this.elementsCache) {
|
|
574
|
+
if (cachedNode === node) {
|
|
575
|
+
this.elementsCache.delete(cacheKey)
|
|
576
|
+
break
|
|
577
|
+
}
|
|
578
|
+
}
|
|
579
|
+
return
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
if (!this.isIndexInRange(index)) return
|
|
583
|
+
|
|
584
|
+
if (this.shouldMeasureDuringScroll(index)) {
|
|
585
|
+
this.resizeItem(index, this.options.measureElement(node, entry, this))
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
|
|
562
589
|
return {
|
|
563
590
|
disconnect: () => {
|
|
564
591
|
get()?.disconnect()
|
|
@@ -760,7 +787,11 @@ export class Virtualizer<
|
|
|
760
787
|
isIOSWebKit() &&
|
|
761
788
|
(this.isScrolling || this._iosTouching || this._iosJustTouchEnded)
|
|
762
789
|
) {
|
|
763
|
-
|
|
790
|
+
// An index scroll retargets from fresh measurements in reconcileScroll;
|
|
791
|
+
// replaying this delta after it lands would count it twice.
|
|
792
|
+
if (this.scrollState?.index == null) {
|
|
793
|
+
this._iosDeferredAdjustment += delta
|
|
794
|
+
}
|
|
764
795
|
return false
|
|
765
796
|
} else {
|
|
766
797
|
const target = this.getScrollOffset() + this.scrollAdjustments + delta
|
|
@@ -1192,6 +1223,10 @@ export class Virtualizer<
|
|
|
1192
1223
|
return
|
|
1193
1224
|
}
|
|
1194
1225
|
|
|
1226
|
+
// A resize since the last render leaves measurementsCache stale until
|
|
1227
|
+
// something reads getMeasurements(); rebuild it (memoized) so an index
|
|
1228
|
+
// target reflects it even when no render ran in between.
|
|
1229
|
+
this.getMeasurements()
|
|
1195
1230
|
const targetOffset = this.getScrollStateTarget(this.scrollState)
|
|
1196
1231
|
|
|
1197
1232
|
// Index targets can still depend on cached sizes during the first rAF,
|
|
@@ -1610,9 +1645,10 @@ export class Virtualizer<
|
|
|
1610
1645
|
this.options.count,
|
|
1611
1646
|
startIndex,
|
|
1612
1647
|
endIndex,
|
|
1648
|
+
this.options.lanes,
|
|
1613
1649
|
]
|
|
1614
1650
|
},
|
|
1615
|
-
(rangeExtractor, overscan, count, startIndex, endIndex) => {
|
|
1651
|
+
(rangeExtractor, overscan, count, startIndex, endIndex, lanes) => {
|
|
1616
1652
|
return startIndex === null || endIndex === null
|
|
1617
1653
|
? []
|
|
1618
1654
|
: rangeExtractor({
|
|
@@ -1620,6 +1656,7 @@ export class Virtualizer<
|
|
|
1620
1656
|
endIndex,
|
|
1621
1657
|
overscan,
|
|
1622
1658
|
count,
|
|
1659
|
+
lanes,
|
|
1623
1660
|
})
|
|
1624
1661
|
},
|
|
1625
1662
|
{
|
|
@@ -1718,6 +1755,14 @@ export class Virtualizer<
|
|
|
1718
1755
|
resizeItem = (index: number, size: number) => {
|
|
1719
1756
|
if (!this.isIndexInRange(index)) return
|
|
1720
1757
|
|
|
1758
|
+
// An earlier entry in this ResizeObserver batch moved `scrollOffset`.
|
|
1759
|
+
// Outside a batch its sync notify would have re-rendered and rebuilt the
|
|
1760
|
+
// caches read below; rebuild them here so the anchoring check compares
|
|
1761
|
+
// this item's current start, not a stale one, against the adjusted
|
|
1762
|
+
// offset (#1218). Without a scroll move, the caches are exactly as stale
|
|
1763
|
+
// as they were before batching, so skip the rebuild.
|
|
1764
|
+
if (this._resizeBatch?.sync) this.getMeasurements()
|
|
1765
|
+
|
|
1721
1766
|
// Fast field reads. For lanes===1 we read raw start/size from the flat
|
|
1722
1767
|
// typed array, avoiding a Proxy.get + VirtualItem allocation per call.
|
|
1723
1768
|
// For lanes>1 we fall back to the cached VirtualItem array.
|
|
@@ -1809,6 +1854,13 @@ export class Virtualizer<
|
|
|
1809
1854
|
// synchronous notify flushes the render in this same callback, so both
|
|
1810
1855
|
// land in one paint. When nothing moved (or the write was deferred on
|
|
1811
1856
|
// iOS), keep the cheaper async notify.
|
|
1857
|
+
if (this._resizeBatch) {
|
|
1858
|
+
// Inside a ResizeObserver callback: notify once after every entry
|
|
1859
|
+
// is measured, synchronously if any of them moved `scrollTop`.
|
|
1860
|
+
this._resizeBatch.notify = true
|
|
1861
|
+
this._resizeBatch.sync ||= adjustedSync
|
|
1862
|
+
return
|
|
1863
|
+
}
|
|
1812
1864
|
this.notify(adjustedSync)
|
|
1813
1865
|
// A consumer that grows the sizer synchronously inside `onChange`
|
|
1814
1866
|
// (direct DOM updates) may never re-render when the range is
|