@tanstack/virtual-core 3.17.2 → 3.17.4
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 -58
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.cts +0 -1
- package/dist/esm/index.d.ts +0 -1
- package/dist/esm/index.js +81 -58
- package/dist/esm/index.js.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +135 -103
package/src/index.ts
CHANGED
|
@@ -741,6 +741,19 @@ export class Virtualizer<
|
|
|
741
741
|
this.rafId = null
|
|
742
742
|
}
|
|
743
743
|
this.scrollState = null
|
|
744
|
+
// The iOS gesture/deferral state is scoped to the current scroll
|
|
745
|
+
// element: the touch listeners that maintain it were just removed, and
|
|
746
|
+
// an in-flight touch keeps targeting the old element (implicit touch
|
|
747
|
+
// capture), so the new element never reports it. Carrying the state
|
|
748
|
+
// over would replay a stale deferred delta on the new element's first
|
|
749
|
+
// flush, and a cleanup that lands mid-touch or inside the post-touchend
|
|
750
|
+
// grace window would strand _iosTouching / _iosJustTouchEnded as true
|
|
751
|
+
// (the listener unsub clears the grace timer, and with it the only
|
|
752
|
+
// pending reset of the flag), deferring every adjustment on the new
|
|
753
|
+
// element until its next touch cycle.
|
|
754
|
+
this._iosDeferredAdjustment = 0
|
|
755
|
+
this._iosTouching = false
|
|
756
|
+
this._iosJustTouchEnded = false
|
|
744
757
|
this.scrollElement = null
|
|
745
758
|
this.targetWindow = null
|
|
746
759
|
}
|
|
@@ -816,10 +829,17 @@ export class Virtualizer<
|
|
|
816
829
|
this._intendedScrollOffset = null
|
|
817
830
|
|
|
818
831
|
this.scrollAdjustments = 0
|
|
832
|
+
// If the offset hasn't moved, this is the echo of our own
|
|
833
|
+
// adjustment write — `applyScrollAdjustment` already folded it
|
|
834
|
+
// into `scrollOffset`. There's no direction to infer, so leave
|
|
835
|
+
// it alone; a real gesture always moves the offset.
|
|
836
|
+
const prevOffset = this.getScrollOffset()
|
|
819
837
|
this.scrollDirection = isScrolling
|
|
820
|
-
?
|
|
821
|
-
?
|
|
822
|
-
:
|
|
838
|
+
? prevOffset === offset
|
|
839
|
+
? this.scrollDirection
|
|
840
|
+
: prevOffset < offset
|
|
841
|
+
? 'forward'
|
|
842
|
+
: 'backward'
|
|
823
843
|
: null
|
|
824
844
|
this.scrollOffset = offset
|
|
825
845
|
this.isScrolling = isScrolling
|
|
@@ -1075,47 +1095,6 @@ export class Virtualizer<
|
|
|
1075
1095
|
return this.scrollOffset
|
|
1076
1096
|
}
|
|
1077
1097
|
|
|
1078
|
-
private getFurthestMeasurement = (
|
|
1079
|
-
measurements: Array<VirtualItem>,
|
|
1080
|
-
index: number,
|
|
1081
|
-
) => {
|
|
1082
|
-
const furthestMeasurementsFound = new Map<number, true>()
|
|
1083
|
-
const furthestMeasurements = new Map<number, VirtualItem>()
|
|
1084
|
-
for (let m = index - 1; m >= 0; m--) {
|
|
1085
|
-
const measurement = measurements[m]!
|
|
1086
|
-
|
|
1087
|
-
if (furthestMeasurementsFound.has(measurement.lane)) {
|
|
1088
|
-
continue
|
|
1089
|
-
}
|
|
1090
|
-
|
|
1091
|
-
const previousFurthestMeasurement = furthestMeasurements.get(
|
|
1092
|
-
measurement.lane,
|
|
1093
|
-
)
|
|
1094
|
-
if (
|
|
1095
|
-
previousFurthestMeasurement == null ||
|
|
1096
|
-
measurement.end > previousFurthestMeasurement.end
|
|
1097
|
-
) {
|
|
1098
|
-
furthestMeasurements.set(measurement.lane, measurement)
|
|
1099
|
-
} else if (measurement.end < previousFurthestMeasurement.end) {
|
|
1100
|
-
furthestMeasurementsFound.set(measurement.lane, true)
|
|
1101
|
-
}
|
|
1102
|
-
|
|
1103
|
-
if (furthestMeasurementsFound.size === this.options.lanes) {
|
|
1104
|
-
break
|
|
1105
|
-
}
|
|
1106
|
-
}
|
|
1107
|
-
|
|
1108
|
-
return furthestMeasurements.size === this.options.lanes
|
|
1109
|
-
? Array.from(furthestMeasurements.values()).sort((a, b) => {
|
|
1110
|
-
if (a.end === b.end) {
|
|
1111
|
-
return a.index - b.index
|
|
1112
|
-
}
|
|
1113
|
-
|
|
1114
|
-
return a.end - b.end
|
|
1115
|
-
})[0]
|
|
1116
|
-
: undefined
|
|
1117
|
-
}
|
|
1118
|
-
|
|
1119
1098
|
private getMeasurementOptions = memo(
|
|
1120
1099
|
() => [
|
|
1121
1100
|
this.options.count,
|
|
@@ -1125,6 +1104,7 @@ export class Virtualizer<
|
|
|
1125
1104
|
this.options.enabled,
|
|
1126
1105
|
this.options.lanes,
|
|
1127
1106
|
this.options.laneAssignmentMode,
|
|
1107
|
+
this.options.gap,
|
|
1128
1108
|
],
|
|
1129
1109
|
(
|
|
1130
1110
|
count,
|
|
@@ -1134,6 +1114,7 @@ export class Virtualizer<
|
|
|
1134
1114
|
enabled,
|
|
1135
1115
|
lanes,
|
|
1136
1116
|
laneAssignmentMode,
|
|
1117
|
+
gap,
|
|
1137
1118
|
) => {
|
|
1138
1119
|
const lanesChanged =
|
|
1139
1120
|
this.prevLanes !== undefined && this.prevLanes !== lanes
|
|
@@ -1154,6 +1135,7 @@ export class Virtualizer<
|
|
|
1154
1135
|
enabled,
|
|
1155
1136
|
lanes,
|
|
1156
1137
|
laneAssignmentMode,
|
|
1138
|
+
gap,
|
|
1157
1139
|
}
|
|
1158
1140
|
},
|
|
1159
1141
|
{
|
|
@@ -1172,6 +1154,7 @@ export class Virtualizer<
|
|
|
1172
1154
|
enabled,
|
|
1173
1155
|
lanes,
|
|
1174
1156
|
laneAssignmentMode,
|
|
1157
|
+
gap,
|
|
1175
1158
|
},
|
|
1176
1159
|
_itemSizeCacheVersion,
|
|
1177
1160
|
) => {
|
|
@@ -1230,7 +1213,6 @@ export class Virtualizer<
|
|
|
1230
1213
|
// At n=100k this drops cold-mount cost from ~2.5ms (eager object
|
|
1231
1214
|
// allocation) to roughly the cost of a single typed-array fill.
|
|
1232
1215
|
if (lanes === 1) {
|
|
1233
|
-
const gap = this.options.gap
|
|
1234
1216
|
// Reuse flat backing if large enough; else grow (preserving data
|
|
1235
1217
|
// before `min` to mirror the slice-and-rebuild contract).
|
|
1236
1218
|
const need = count * 2
|
|
@@ -1274,12 +1256,21 @@ export class Virtualizer<
|
|
|
1274
1256
|
const laneLastIndex: Array<number | undefined> = new Array(lanes).fill(
|
|
1275
1257
|
undefined,
|
|
1276
1258
|
)
|
|
1259
|
+
// Running end position of each lane's last item, so the shortest lane
|
|
1260
|
+
// can be found with an O(lanes) argmin instead of the old backward walk
|
|
1261
|
+
// through `measurements` (getFurthestMeasurement). `filledLanes` tracks
|
|
1262
|
+
// how many lanes have at least one item, mirroring the previous
|
|
1263
|
+
// "all lanes seen → shortest lane, else i % lanes" branch.
|
|
1264
|
+
const laneEnds = new Float64Array(lanes)
|
|
1265
|
+
let filledLanes = 0
|
|
1277
1266
|
|
|
1278
1267
|
// Initialize from existing measurements (before min)
|
|
1279
1268
|
for (let m = 0; m < min; m++) {
|
|
1280
1269
|
const item = measurements[m]
|
|
1281
1270
|
if (item) {
|
|
1271
|
+
if (laneLastIndex[item.lane] === undefined) filledLanes++
|
|
1282
1272
|
laneLastIndex[item.lane] = m
|
|
1273
|
+
laneEnds[item.lane] = item.end
|
|
1283
1274
|
}
|
|
1284
1275
|
}
|
|
1285
1276
|
|
|
@@ -1301,24 +1292,37 @@ export class Virtualizer<
|
|
|
1301
1292
|
const prevInLane =
|
|
1302
1293
|
prevIndex !== undefined ? measurements[prevIndex] : undefined
|
|
1303
1294
|
start = prevInLane
|
|
1304
|
-
? prevInLane.end +
|
|
1305
|
-
: paddingStart + scrollMargin
|
|
1306
|
-
} else {
|
|
1307
|
-
// No cache - use original logic (find shortest lane)
|
|
1308
|
-
const furthestMeasurement =
|
|
1309
|
-
this.options.lanes === 1
|
|
1310
|
-
? measurements[i - 1]
|
|
1311
|
-
: this.getFurthestMeasurement(measurements, i)
|
|
1312
|
-
|
|
1313
|
-
start = furthestMeasurement
|
|
1314
|
-
? furthestMeasurement.end + this.options.gap
|
|
1295
|
+
? prevInLane.end + gap
|
|
1315
1296
|
: paddingStart + scrollMargin
|
|
1297
|
+
} else if (filledLanes === lanes) {
|
|
1298
|
+
// No cache, every lane seeded: place in the shortest lane.
|
|
1299
|
+
// Read the running per-lane ends (O(lanes) argmin) instead of the
|
|
1300
|
+
// old backward scan. Tie-break on the lane's last-item index to
|
|
1301
|
+
// preserve the previous sort-by-(end, index) placement exactly.
|
|
1302
|
+
let bestLane = 0
|
|
1303
|
+
let bestEnd = laneEnds[0]!
|
|
1304
|
+
let bestIdx = laneLastIndex[0]!
|
|
1305
|
+
for (let l = 1; l < lanes; l++) {
|
|
1306
|
+
const e = laneEnds[l]!
|
|
1307
|
+
if (e < bestEnd || (e === bestEnd && laneLastIndex[l]! < bestIdx)) {
|
|
1308
|
+
bestLane = l
|
|
1309
|
+
bestEnd = e
|
|
1310
|
+
bestIdx = laneLastIndex[l]!
|
|
1311
|
+
}
|
|
1312
|
+
}
|
|
1313
|
+
lane = bestLane
|
|
1314
|
+
start = bestEnd + gap
|
|
1316
1315
|
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1316
|
+
if (shouldCacheLane) {
|
|
1317
|
+
this.laneAssignments.set(i, lane)
|
|
1318
|
+
}
|
|
1319
|
+
} else {
|
|
1320
|
+
// No cache and not every lane seeded yet — seed lanes in order,
|
|
1321
|
+
// matching the previous `i % lanes` fallback for the first row.
|
|
1322
|
+
lane = i % this.options.lanes
|
|
1323
|
+
start = paddingStart + scrollMargin
|
|
1320
1324
|
|
|
1321
|
-
if (
|
|
1325
|
+
if (shouldCacheLane) {
|
|
1322
1326
|
this.laneAssignments.set(i, lane)
|
|
1323
1327
|
}
|
|
1324
1328
|
}
|
|
@@ -1340,8 +1344,10 @@ export class Virtualizer<
|
|
|
1340
1344
|
lane,
|
|
1341
1345
|
}
|
|
1342
1346
|
|
|
1343
|
-
// ✅ Performance: Update lane's last item index
|
|
1347
|
+
// ✅ Performance: Update lane's last item index + running end
|
|
1348
|
+
if (laneLastIndex[lane] === undefined) filledLanes++
|
|
1344
1349
|
laneLastIndex[lane] = i
|
|
1350
|
+
laneEnds[lane] = end
|
|
1345
1351
|
}
|
|
1346
1352
|
|
|
1347
1353
|
this.measurementsCache = measurements
|
|
@@ -1362,22 +1368,22 @@ export class Virtualizer<
|
|
|
1362
1368
|
this.options.lanes,
|
|
1363
1369
|
],
|
|
1364
1370
|
(measurements, outerSize, scrollOffset, lanes) => {
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
|
|
1371
|
+
if (measurements.length === 0 || outerSize === 0) {
|
|
1372
|
+
this.range = null
|
|
1373
|
+
return null
|
|
1374
|
+
}
|
|
1375
|
+
this.range = calculateRangeImpl(
|
|
1376
|
+
measurements,
|
|
1377
|
+
outerSize,
|
|
1378
|
+
scrollOffset,
|
|
1379
|
+
lanes,
|
|
1380
|
+
// Pass the typed array so binary search + forward-walk can read
|
|
1381
|
+
// start/end directly from Float64Array, skipping the Proxy traps.
|
|
1382
|
+
lanes === 1 && this._flatMeasurements != null
|
|
1383
|
+
? this._flatMeasurements
|
|
1384
|
+
: null,
|
|
1385
|
+
)
|
|
1386
|
+
return this.range
|
|
1381
1387
|
},
|
|
1382
1388
|
{
|
|
1383
1389
|
key: process.env.NODE_ENV !== 'production' && 'calculateRange',
|
|
@@ -1923,45 +1929,71 @@ const findNearestBinarySearch = (
|
|
|
1923
1929
|
}
|
|
1924
1930
|
}
|
|
1925
1931
|
|
|
1926
|
-
|
|
1927
|
-
|
|
1928
|
-
|
|
1929
|
-
|
|
1930
|
-
|
|
1931
|
-
|
|
1932
|
-
|
|
1933
|
-
|
|
1934
|
-
|
|
1935
|
-
|
|
1936
|
-
|
|
1937
|
-
|
|
1938
|
-
|
|
1932
|
+
// Monomorphic Float64Array variant — reads start values directly at stride
|
|
1933
|
+
// 2 instead of through a getter closure. JITs the inner load to a typed-
|
|
1934
|
+
// array bounds-check + load with no indirect call.
|
|
1935
|
+
function findNearestBinarySearchFlat(
|
|
1936
|
+
flat: Float64Array,
|
|
1937
|
+
high: number,
|
|
1938
|
+
value: number,
|
|
1939
|
+
) {
|
|
1940
|
+
let low = 0
|
|
1941
|
+
while (low <= high) {
|
|
1942
|
+
const middle = ((low + high) / 2) | 0
|
|
1943
|
+
const currentValue = flat[middle * 2]!
|
|
1944
|
+
|
|
1945
|
+
if (currentValue < value) {
|
|
1946
|
+
low = middle + 1
|
|
1947
|
+
} else if (currentValue > value) {
|
|
1948
|
+
high = middle - 1
|
|
1949
|
+
} else {
|
|
1950
|
+
return middle
|
|
1951
|
+
}
|
|
1952
|
+
}
|
|
1953
|
+
return low > 0 ? low - 1 : 0
|
|
1954
|
+
}
|
|
1955
|
+
|
|
1956
|
+
function calculateRangeImpl(
|
|
1957
|
+
measurements: Array<VirtualItem>,
|
|
1958
|
+
outerSize: number,
|
|
1959
|
+
scrollOffset: number,
|
|
1960
|
+
lanes: number,
|
|
1961
|
+
flat: Float64Array | null,
|
|
1962
|
+
) {
|
|
1939
1963
|
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
1964
|
|
|
1950
1965
|
// handle case when item count is less than or equal to lanes
|
|
1951
1966
|
if (measurements.length <= lanes) {
|
|
1952
|
-
return {
|
|
1953
|
-
|
|
1954
|
-
|
|
1967
|
+
return { startIndex: 0, endIndex: lastIndex }
|
|
1968
|
+
}
|
|
1969
|
+
|
|
1970
|
+
if (lanes === 1 && flat !== null) {
|
|
1971
|
+
// Hot single-lane path: typed-array reads, no closures, no Proxy traps.
|
|
1972
|
+
const startIndex = findNearestBinarySearchFlat(
|
|
1973
|
+
flat,
|
|
1974
|
+
lastIndex,
|
|
1975
|
+
scrollOffset,
|
|
1976
|
+
)
|
|
1977
|
+
let endIndex = startIndex
|
|
1978
|
+
const limit = scrollOffset + outerSize
|
|
1979
|
+
while (
|
|
1980
|
+
endIndex < lastIndex &&
|
|
1981
|
+
flat[endIndex * 2]! + flat[endIndex * 2 + 1]! < limit
|
|
1982
|
+
) {
|
|
1983
|
+
endIndex++
|
|
1955
1984
|
}
|
|
1985
|
+
return { startIndex, endIndex }
|
|
1956
1986
|
}
|
|
1957
1987
|
|
|
1988
|
+
// Fallback (lanes > 1 or no flat array): closure-based reads.
|
|
1989
|
+
const getStart = (index: number) => measurements[index]!.start
|
|
1958
1990
|
let startIndex = findNearestBinarySearch(0, lastIndex, getStart, scrollOffset)
|
|
1959
1991
|
let endIndex = startIndex
|
|
1960
1992
|
|
|
1961
1993
|
if (lanes === 1) {
|
|
1962
1994
|
while (
|
|
1963
1995
|
endIndex < lastIndex &&
|
|
1964
|
-
|
|
1996
|
+
measurements[endIndex]!.end < scrollOffset + outerSize
|
|
1965
1997
|
) {
|
|
1966
1998
|
endIndex++
|
|
1967
1999
|
}
|