@tanstack/virtual-core 3.17.2 → 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 +44 -21
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/esm/index.js +44 -21
- package/dist/esm/index.js.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +78 -45
package/src/index.ts
CHANGED
|
@@ -816,10 +816,17 @@ export class Virtualizer<
|
|
|
816
816
|
this._intendedScrollOffset = null
|
|
817
817
|
|
|
818
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()
|
|
819
824
|
this.scrollDirection = isScrolling
|
|
820
|
-
?
|
|
821
|
-
?
|
|
822
|
-
:
|
|
825
|
+
? prevOffset === offset
|
|
826
|
+
? this.scrollDirection
|
|
827
|
+
: prevOffset < offset
|
|
828
|
+
? 'forward'
|
|
829
|
+
: 'backward'
|
|
823
830
|
: null
|
|
824
831
|
this.scrollOffset = offset
|
|
825
832
|
this.isScrolling = isScrolling
|
|
@@ -1362,22 +1369,22 @@ export class Virtualizer<
|
|
|
1362
1369
|
this.options.lanes,
|
|
1363
1370
|
],
|
|
1364
1371
|
(measurements, outerSize, scrollOffset, lanes) => {
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
|
|
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
|
|
1381
1388
|
},
|
|
1382
1389
|
{
|
|
1383
1390
|
key: process.env.NODE_ENV !== 'production' && 'calculateRange',
|
|
@@ -1923,45 +1930,71 @@ const findNearestBinarySearch = (
|
|
|
1923
1930
|
}
|
|
1924
1931
|
}
|
|
1925
1932
|
|
|
1926
|
-
|
|
1927
|
-
|
|
1928
|
-
|
|
1929
|
-
|
|
1930
|
-
|
|
1931
|
-
|
|
1932
|
-
|
|
1933
|
-
|
|
1934
|
-
|
|
1935
|
-
|
|
1936
|
-
|
|
1937
|
-
|
|
1938
|
-
|
|
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
|
+
) {
|
|
1939
1964
|
const lastIndex = measurements.length - 1
|
|
1940
|
-
// When the lanes===1 fast-path is active, read start/end directly from the
|
|
1941
|
-
// flat Float64Array instead of going through the lazy-view Proxy. Cuts
|
|
1942
|
-
// ~17 Proxy.get traps per scroll for the binary search alone.
|
|
1943
|
-
const getStart = flat
|
|
1944
|
-
? (index: number) => flat[index * 2]!
|
|
1945
|
-
: (index: number) => measurements[index]!.start
|
|
1946
|
-
const getEnd = flat
|
|
1947
|
-
? (index: number) => flat[index * 2]! + flat[index * 2 + 1]!
|
|
1948
|
-
: (index: number) => measurements[index]!.end
|
|
1949
1965
|
|
|
1950
1966
|
// handle case when item count is less than or equal to lanes
|
|
1951
1967
|
if (measurements.length <= lanes) {
|
|
1952
|
-
return {
|
|
1953
|
-
|
|
1954
|
-
|
|
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++
|
|
1955
1985
|
}
|
|
1986
|
+
return { startIndex, endIndex }
|
|
1956
1987
|
}
|
|
1957
1988
|
|
|
1989
|
+
// Fallback (lanes > 1 or no flat array): closure-based reads.
|
|
1990
|
+
const getStart = (index: number) => measurements[index]!.start
|
|
1958
1991
|
let startIndex = findNearestBinarySearch(0, lastIndex, getStart, scrollOffset)
|
|
1959
1992
|
let endIndex = startIndex
|
|
1960
1993
|
|
|
1961
1994
|
if (lanes === 1) {
|
|
1962
1995
|
while (
|
|
1963
1996
|
endIndex < lastIndex &&
|
|
1964
|
-
|
|
1997
|
+
measurements[endIndex]!.end < scrollOffset + outerSize
|
|
1965
1998
|
) {
|
|
1966
1999
|
endIndex++
|
|
1967
2000
|
}
|