@tanstack/virtual-core 3.17.1 → 3.17.3
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 +51 -21
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/esm/index.js +51 -21
- package/dist/esm/index.js.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +106 -45
package/src/index.ts
CHANGED
|
@@ -692,6 +692,19 @@ export class Virtualizer<
|
|
|
692
692
|
adjustments: (this.scrollAdjustments += delta),
|
|
693
693
|
behavior,
|
|
694
694
|
})
|
|
695
|
+
// Eagerly carry the intended target in `scrollOffset` so callers that
|
|
696
|
+
// read it before the next scroll event — notably the next `resizeItem`
|
|
697
|
+
// tick's `getVirtualDistanceFromEnd()` / `wasAtEnd` check — see the
|
|
698
|
+
// post-adjustment position even when the DOM `scrollTop` write was
|
|
699
|
+
// clamped because the consumer hasn't grown the sizer yet (`notify()`
|
|
700
|
+
// runs after this in `resizeItem`). Same idea as the eager
|
|
701
|
+
// `scrollOffset` adjustment for prepend in `setOptions` (#1176). The
|
|
702
|
+
// adjustment is now baked into `scrollOffset`, so zero
|
|
703
|
+
// `scrollAdjustments` to keep their sum invariant.
|
|
704
|
+
if (this.scrollOffset !== null) {
|
|
705
|
+
this.scrollOffset += this.scrollAdjustments
|
|
706
|
+
this.scrollAdjustments = 0
|
|
707
|
+
}
|
|
695
708
|
}
|
|
696
709
|
}
|
|
697
710
|
|
|
@@ -772,6 +785,21 @@ export class Virtualizer<
|
|
|
772
785
|
|
|
773
786
|
this.unsubs.push(
|
|
774
787
|
this.options.observeElementOffset(this, (offset, isScrolling) => {
|
|
788
|
+
// A scroll event that reports movement but lands on the offset we
|
|
789
|
+
// already hold — and isn't a self-write read-back — is a spurious
|
|
790
|
+
// no-op re-emit that Safari/Firefox fire after a re-render's layout
|
|
791
|
+
// (Chrome doesn't). Treating it as scrolling re-arms `isScrolling`,
|
|
792
|
+
// which forces a render that triggers another such event: an
|
|
793
|
+
// infinite re-render loop. Ignore it. (Self-writes are handled by
|
|
794
|
+
// the `_intendedScrollOffset` reconciliation just below.)
|
|
795
|
+
if (
|
|
796
|
+
isScrolling &&
|
|
797
|
+
this._intendedScrollOffset === null &&
|
|
798
|
+
offset === this.scrollOffset
|
|
799
|
+
) {
|
|
800
|
+
return
|
|
801
|
+
}
|
|
802
|
+
|
|
775
803
|
// If this scroll event looks like the browser's read-back of a
|
|
776
804
|
// value we just wrote, prefer our intended (sub-pixel-accurate)
|
|
777
805
|
// value over the browser's rounded one. The 1.5 px tolerance is
|
|
@@ -788,10 +816,17 @@ export class Virtualizer<
|
|
|
788
816
|
this._intendedScrollOffset = null
|
|
789
817
|
|
|
790
818
|
this.scrollAdjustments = 0
|
|
819
|
+
// If the offset hasn't moved, this is the echo of our own
|
|
820
|
+
// adjustment write — `applyScrollAdjustment` already folded it
|
|
821
|
+
// into `scrollOffset`. There's no direction to infer, so leave
|
|
822
|
+
// it alone; a real gesture always moves the offset.
|
|
823
|
+
const prevOffset = this.getScrollOffset()
|
|
791
824
|
this.scrollDirection = isScrolling
|
|
792
|
-
?
|
|
793
|
-
?
|
|
794
|
-
:
|
|
825
|
+
? prevOffset === offset
|
|
826
|
+
? this.scrollDirection
|
|
827
|
+
: prevOffset < offset
|
|
828
|
+
? 'forward'
|
|
829
|
+
: 'backward'
|
|
795
830
|
: null
|
|
796
831
|
this.scrollOffset = offset
|
|
797
832
|
this.isScrolling = isScrolling
|
|
@@ -1334,22 +1369,22 @@ export class Virtualizer<
|
|
|
1334
1369
|
this.options.lanes,
|
|
1335
1370
|
],
|
|
1336
1371
|
(measurements, outerSize, scrollOffset, lanes) => {
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
|
|
1372
|
+
if (measurements.length === 0 || outerSize === 0) {
|
|
1373
|
+
this.range = null
|
|
1374
|
+
return null
|
|
1375
|
+
}
|
|
1376
|
+
this.range = calculateRangeImpl(
|
|
1377
|
+
measurements,
|
|
1378
|
+
outerSize,
|
|
1379
|
+
scrollOffset,
|
|
1380
|
+
lanes,
|
|
1381
|
+
// Pass the typed array so binary search + forward-walk can read
|
|
1382
|
+
// start/end directly from Float64Array, skipping the Proxy traps.
|
|
1383
|
+
lanes === 1 && this._flatMeasurements != null
|
|
1384
|
+
? this._flatMeasurements
|
|
1385
|
+
: null,
|
|
1386
|
+
)
|
|
1387
|
+
return this.range
|
|
1353
1388
|
},
|
|
1354
1389
|
{
|
|
1355
1390
|
key: process.env.NODE_ENV !== 'production' && 'calculateRange',
|
|
@@ -1895,45 +1930,71 @@ const findNearestBinarySearch = (
|
|
|
1895
1930
|
}
|
|
1896
1931
|
}
|
|
1897
1932
|
|
|
1898
|
-
|
|
1899
|
-
|
|
1900
|
-
|
|
1901
|
-
|
|
1902
|
-
|
|
1903
|
-
|
|
1904
|
-
|
|
1905
|
-
|
|
1906
|
-
|
|
1907
|
-
|
|
1908
|
-
|
|
1909
|
-
|
|
1910
|
-
|
|
1933
|
+
// Monomorphic Float64Array variant — reads start values directly at stride
|
|
1934
|
+
// 2 instead of through a getter closure. JITs the inner load to a typed-
|
|
1935
|
+
// array bounds-check + load with no indirect call.
|
|
1936
|
+
function findNearestBinarySearchFlat(
|
|
1937
|
+
flat: Float64Array,
|
|
1938
|
+
high: number,
|
|
1939
|
+
value: number,
|
|
1940
|
+
) {
|
|
1941
|
+
let low = 0
|
|
1942
|
+
while (low <= high) {
|
|
1943
|
+
const middle = ((low + high) / 2) | 0
|
|
1944
|
+
const currentValue = flat[middle * 2]!
|
|
1945
|
+
|
|
1946
|
+
if (currentValue < value) {
|
|
1947
|
+
low = middle + 1
|
|
1948
|
+
} else if (currentValue > value) {
|
|
1949
|
+
high = middle - 1
|
|
1950
|
+
} else {
|
|
1951
|
+
return middle
|
|
1952
|
+
}
|
|
1953
|
+
}
|
|
1954
|
+
return low > 0 ? low - 1 : 0
|
|
1955
|
+
}
|
|
1956
|
+
|
|
1957
|
+
function calculateRangeImpl(
|
|
1958
|
+
measurements: Array<VirtualItem>,
|
|
1959
|
+
outerSize: number,
|
|
1960
|
+
scrollOffset: number,
|
|
1961
|
+
lanes: number,
|
|
1962
|
+
flat: Float64Array | null,
|
|
1963
|
+
) {
|
|
1911
1964
|
const lastIndex = measurements.length - 1
|
|
1912
|
-
// When the lanes===1 fast-path is active, read start/end directly from the
|
|
1913
|
-
// flat Float64Array instead of going through the lazy-view Proxy. Cuts
|
|
1914
|
-
// ~17 Proxy.get traps per scroll for the binary search alone.
|
|
1915
|
-
const getStart = flat
|
|
1916
|
-
? (index: number) => flat[index * 2]!
|
|
1917
|
-
: (index: number) => measurements[index]!.start
|
|
1918
|
-
const getEnd = flat
|
|
1919
|
-
? (index: number) => flat[index * 2]! + flat[index * 2 + 1]!
|
|
1920
|
-
: (index: number) => measurements[index]!.end
|
|
1921
1965
|
|
|
1922
1966
|
// handle case when item count is less than or equal to lanes
|
|
1923
1967
|
if (measurements.length <= lanes) {
|
|
1924
|
-
return {
|
|
1925
|
-
|
|
1926
|
-
|
|
1968
|
+
return { startIndex: 0, endIndex: lastIndex }
|
|
1969
|
+
}
|
|
1970
|
+
|
|
1971
|
+
if (lanes === 1 && flat !== null) {
|
|
1972
|
+
// Hot single-lane path: typed-array reads, no closures, no Proxy traps.
|
|
1973
|
+
const startIndex = findNearestBinarySearchFlat(
|
|
1974
|
+
flat,
|
|
1975
|
+
lastIndex,
|
|
1976
|
+
scrollOffset,
|
|
1977
|
+
)
|
|
1978
|
+
let endIndex = startIndex
|
|
1979
|
+
const limit = scrollOffset + outerSize
|
|
1980
|
+
while (
|
|
1981
|
+
endIndex < lastIndex &&
|
|
1982
|
+
flat[endIndex * 2]! + flat[endIndex * 2 + 1]! < limit
|
|
1983
|
+
) {
|
|
1984
|
+
endIndex++
|
|
1927
1985
|
}
|
|
1986
|
+
return { startIndex, endIndex }
|
|
1928
1987
|
}
|
|
1929
1988
|
|
|
1989
|
+
// Fallback (lanes > 1 or no flat array): closure-based reads.
|
|
1990
|
+
const getStart = (index: number) => measurements[index]!.start
|
|
1930
1991
|
let startIndex = findNearestBinarySearch(0, lastIndex, getStart, scrollOffset)
|
|
1931
1992
|
let endIndex = startIndex
|
|
1932
1993
|
|
|
1933
1994
|
if (lanes === 1) {
|
|
1934
1995
|
while (
|
|
1935
1996
|
endIndex < lastIndex &&
|
|
1936
|
-
|
|
1997
|
+
measurements[endIndex]!.end < scrollOffset + outerSize
|
|
1937
1998
|
) {
|
|
1938
1999
|
endIndex++
|
|
1939
2000
|
}
|