@tanstack/virtual-core 3.17.9 → 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 +56 -20
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.cts +2 -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 +2 -2
- package/dist/esm/index.js +57 -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 +76 -28
- package/src/lazy-measurements.ts +13 -10
package/src/index.ts
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
createLazyMeasurementsView,
|
|
3
|
+
getMeasurementKey,
|
|
4
|
+
} from './lazy-measurements'
|
|
2
5
|
import { approxEqual, debounce, memo, notUndefined } from './utils'
|
|
3
6
|
|
|
4
7
|
// Browser-aware iOS detection. Programmatic `scrollTo`/`scrollTop` writes
|
|
@@ -398,6 +401,36 @@ type PendingScrollAnchor = [
|
|
|
398
401
|
anchorDelta: number,
|
|
399
402
|
]
|
|
400
403
|
|
|
404
|
+
function isAppendWithTrim(
|
|
405
|
+
prevCount: number,
|
|
406
|
+
nextCount: number,
|
|
407
|
+
getPreviousKey: (index: number) => Key,
|
|
408
|
+
getNextKey: (index: number) => Key,
|
|
409
|
+
): boolean {
|
|
410
|
+
if (nextCount === 0) return false
|
|
411
|
+
|
|
412
|
+
const firstKey = getNextKey(0)
|
|
413
|
+
const removedKeys = new Set<Key>()
|
|
414
|
+
let removedCount = 0
|
|
415
|
+
while (removedCount < prevCount) {
|
|
416
|
+
const key = getPreviousKey(removedCount)
|
|
417
|
+
if (key === firstKey) break
|
|
418
|
+
removedKeys.add(key)
|
|
419
|
+
removedCount++
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
const retainedCount = prevCount - removedCount
|
|
423
|
+
if (retainedCount === 0 || retainedCount >= nextCount) return false
|
|
424
|
+
|
|
425
|
+
for (let i = 0; i < retainedCount; i++) {
|
|
426
|
+
if (getNextKey(i) !== getPreviousKey(removedCount + i)) return false
|
|
427
|
+
}
|
|
428
|
+
for (let i = retainedCount; i < nextCount; i++) {
|
|
429
|
+
if (removedKeys.has(getNextKey(i))) return false
|
|
430
|
+
}
|
|
431
|
+
return true
|
|
432
|
+
}
|
|
433
|
+
|
|
401
434
|
export class Virtualizer<
|
|
402
435
|
TScrollElement extends Element | Window,
|
|
403
436
|
TItemElement extends Element,
|
|
@@ -409,9 +442,12 @@ export class Virtualizer<
|
|
|
409
442
|
isScrolling = false
|
|
410
443
|
private scrollState: ScrollState | null = null
|
|
411
444
|
measurementsCache: Array<VirtualItem> = []
|
|
412
|
-
//
|
|
413
|
-
//
|
|
414
|
-
private
|
|
445
|
+
// Keys belong to the layout build, even when VirtualItems are read later.
|
|
446
|
+
// The flat [start, size, ...] buffer is reused across builds.
|
|
447
|
+
private _singleLaneMeasurements: {
|
|
448
|
+
flat: Float64Array
|
|
449
|
+
items: Array<Key | VirtualItem>
|
|
450
|
+
} | null = null
|
|
415
451
|
itemSizeCache = new Map<Key, number>()
|
|
416
452
|
private itemSizeCacheVersion = 0
|
|
417
453
|
private laneAssignments = new Map<number, number>() // index → lane cache
|
|
@@ -591,15 +627,11 @@ export class Virtualizer<
|
|
|
591
627
|
const prevCount = prevOptions.count
|
|
592
628
|
const nextCount = merged.count
|
|
593
629
|
const measurements = this.getMeasurements()
|
|
594
|
-
const
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
const prevLastKey =
|
|
599
|
-
prevCount > 0
|
|
600
|
-
? (measurements[prevCount - 1]?.key ??
|
|
601
|
-
prevOptions.getItemKey(prevCount - 1))
|
|
602
|
-
: null
|
|
630
|
+
const previousItems = this._singleLaneMeasurements?.items ?? measurements
|
|
631
|
+
const getPreviousKey = (index: number) =>
|
|
632
|
+
getMeasurementKey(previousItems[index]!)
|
|
633
|
+
const prevFirstKey = prevCount > 0 ? getPreviousKey(0) : null
|
|
634
|
+
const prevLastKey = prevCount > 0 ? getPreviousKey(prevCount - 1) : null
|
|
603
635
|
const didCountChange = nextCount !== prevCount
|
|
604
636
|
const didEdgeKeysChange =
|
|
605
637
|
didCountChange ||
|
|
@@ -627,11 +659,21 @@ export class Virtualizer<
|
|
|
627
659
|
|
|
628
660
|
if (
|
|
629
661
|
behavior &&
|
|
630
|
-
nextCount >
|
|
662
|
+
nextCount > 0 &&
|
|
631
663
|
this.isAtEnd(prevOptions.scrollEndThreshold) &&
|
|
632
664
|
(prevCount === 0 || merged.getItemKey(nextCount - 1) !== prevLastKey)
|
|
633
665
|
) {
|
|
634
|
-
|
|
666
|
+
if (
|
|
667
|
+
nextCount > prevCount ||
|
|
668
|
+
isAppendWithTrim(
|
|
669
|
+
prevCount,
|
|
670
|
+
nextCount,
|
|
671
|
+
getPreviousKey,
|
|
672
|
+
merged.getItemKey,
|
|
673
|
+
)
|
|
674
|
+
) {
|
|
675
|
+
followOnAppend = behavior
|
|
676
|
+
}
|
|
635
677
|
}
|
|
636
678
|
}
|
|
637
679
|
}
|
|
@@ -672,7 +714,8 @@ export class Virtualizer<
|
|
|
672
714
|
// (rubber-band), and a negative tracked offset never self-heals
|
|
673
715
|
// when the element cannot scroll (#1229).
|
|
674
716
|
const newOffset = Math.max(0, anchorItem.start + anchorOffset)
|
|
675
|
-
|
|
717
|
+
// A no-op end scroll emits no event to correct a reading-anchor offset.
|
|
718
|
+
if (!followOnAppend && newOffset !== this.scrollOffset) {
|
|
676
719
|
anchorDelta = newOffset - this.scrollOffset
|
|
677
720
|
this.scrollOffset = newOffset
|
|
678
721
|
anchorResolved = true
|
|
@@ -1272,6 +1315,7 @@ export class Virtualizer<
|
|
|
1272
1315
|
const itemSizeCache = this.itemSizeCache
|
|
1273
1316
|
if (!enabled) {
|
|
1274
1317
|
this.measurementsCache = []
|
|
1318
|
+
this._singleLaneMeasurements = null
|
|
1275
1319
|
this.itemSizeCache.clear()
|
|
1276
1320
|
this.laneAssignments.clear()
|
|
1277
1321
|
return []
|
|
@@ -1291,6 +1335,7 @@ export class Virtualizer<
|
|
|
1291
1335
|
this.lanesChangedFlag = false // Reset immediately
|
|
1292
1336
|
this.lanesSettling = true // Start settling period
|
|
1293
1337
|
this.measurementsCache = []
|
|
1338
|
+
this._singleLaneMeasurements = null
|
|
1294
1339
|
this.itemSizeCache.clear()
|
|
1295
1340
|
this.laneAssignments.clear() // Clear lane cache for new lane count
|
|
1296
1341
|
// Force min = 0 on the rebuild
|
|
@@ -1320,21 +1365,22 @@ export class Virtualizer<
|
|
|
1320
1365
|
// per-item VirtualItem object allocation. We write start/size pairs
|
|
1321
1366
|
// into a Float64Array and return a Proxy that builds VirtualItem
|
|
1322
1367
|
// objects on demand (only the indices a consumer actually reads).
|
|
1323
|
-
//
|
|
1324
|
-
// At n=100k this drops cold-mount cost from ~2.5ms (eager object
|
|
1325
|
-
// allocation) to roughly the cost of a single typed-array fill.
|
|
1326
1368
|
if (lanes === 1) {
|
|
1327
1369
|
// Reuse flat backing if large enough; else grow (preserving data
|
|
1328
1370
|
// before `min` to mirror the slice-and-rebuild contract).
|
|
1329
1371
|
const need = count * 2
|
|
1330
|
-
let flat = this.
|
|
1372
|
+
let flat = this._singleLaneMeasurements?.flat
|
|
1331
1373
|
if (!flat || flat.length < need) {
|
|
1332
1374
|
const next = new Float64Array(need)
|
|
1333
1375
|
if (flat && min > 0) next.set(flat.subarray(0, min * 2))
|
|
1334
1376
|
flat = next
|
|
1335
|
-
this._flatMeasurements = flat
|
|
1336
1377
|
}
|
|
1337
1378
|
|
|
1379
|
+
const items: Array<Key | VirtualItem> =
|
|
1380
|
+
min === 0
|
|
1381
|
+
? new Array(count)
|
|
1382
|
+
: this._singleLaneMeasurements!.items.slice()
|
|
1383
|
+
|
|
1338
1384
|
let runningStart: number
|
|
1339
1385
|
if (min === 0) {
|
|
1340
1386
|
runningStart = paddingStart + scrollMargin
|
|
@@ -1346,6 +1392,7 @@ export class Virtualizer<
|
|
|
1346
1392
|
|
|
1347
1393
|
for (let i = min; i < count; i++) {
|
|
1348
1394
|
const key = getItemKey(i)
|
|
1395
|
+
items[i] = key
|
|
1349
1396
|
const measuredSize = itemSizeCache.get(key)
|
|
1350
1397
|
const size =
|
|
1351
1398
|
typeof measuredSize === 'number'
|
|
@@ -1356,7 +1403,8 @@ export class Virtualizer<
|
|
|
1356
1403
|
runningStart += size + gap
|
|
1357
1404
|
}
|
|
1358
1405
|
|
|
1359
|
-
|
|
1406
|
+
this._singleLaneMeasurements = { flat, items }
|
|
1407
|
+
const view = createLazyMeasurementsView(items, flat)
|
|
1360
1408
|
this.measurementsCache = view
|
|
1361
1409
|
return view
|
|
1362
1410
|
}
|
|
@@ -1490,8 +1538,8 @@ export class Virtualizer<
|
|
|
1490
1538
|
lanes,
|
|
1491
1539
|
// Pass the typed array so binary search + forward-walk can read
|
|
1492
1540
|
// start/end directly from Float64Array, skipping the Proxy traps.
|
|
1493
|
-
lanes === 1 && this.
|
|
1494
|
-
? this.
|
|
1541
|
+
lanes === 1 && this._singleLaneMeasurements !== null
|
|
1542
|
+
? this._singleLaneMeasurements.flat
|
|
1495
1543
|
: null,
|
|
1496
1544
|
)
|
|
1497
1545
|
return this.range
|
|
@@ -1627,8 +1675,8 @@ export class Virtualizer<
|
|
|
1627
1675
|
let cachedSize: number
|
|
1628
1676
|
let itemStart: number
|
|
1629
1677
|
let key: Key
|
|
1630
|
-
const flat = this.
|
|
1631
|
-
if (this.options.lanes === 1 && flat
|
|
1678
|
+
const flat = this._singleLaneMeasurements?.flat
|
|
1679
|
+
if (this.options.lanes === 1 && flat != null) {
|
|
1632
1680
|
key = this.options.getItemKey(index)
|
|
1633
1681
|
itemStart = flat[index * 2]!
|
|
1634
1682
|
cachedSize = flat[index * 2 + 1]!
|
|
@@ -1749,7 +1797,7 @@ export class Virtualizer<
|
|
|
1749
1797
|
// Same fast-path as calculateRange: read start values directly from the
|
|
1750
1798
|
// typed array during binary search to skip the Proxy.get materialization
|
|
1751
1799
|
// per probe.
|
|
1752
|
-
const flat = this.
|
|
1800
|
+
const flat = this._singleLaneMeasurements?.flat
|
|
1753
1801
|
const useFlat = this.options.lanes === 1 && flat != null
|
|
1754
1802
|
const idx = findNearestBinarySearch(
|
|
1755
1803
|
0,
|
|
@@ -1969,7 +2017,7 @@ export class Virtualizer<
|
|
|
1969
2017
|
// when available; avoids a Proxy.get + VirtualItem materialization
|
|
1970
2018
|
// just to call getTotalSize (which React renders trigger every commit).
|
|
1971
2019
|
const lastIdx = measurements.length - 1
|
|
1972
|
-
const flat = this.
|
|
2020
|
+
const flat = this._singleLaneMeasurements?.flat
|
|
1973
2021
|
if (flat != null) {
|
|
1974
2022
|
end = flat[lastIdx * 2]! + flat[lastIdx * 2 + 1]!
|
|
1975
2023
|
} else {
|
package/src/lazy-measurements.ts
CHANGED
|
@@ -1,19 +1,22 @@
|
|
|
1
1
|
// Lazy materialization for the lanes===1 fast path. Backed by a
|
|
2
2
|
// Float64Array (stride 2: start, size, …); VirtualItems are constructed on
|
|
3
|
-
// first indexed read
|
|
4
|
-
// large list counts where most items are never visible.
|
|
3
|
+
// first indexed read, replacing the stored key. Saves the per-item object
|
|
4
|
+
// allocation at large list counts where most items are never visible.
|
|
5
5
|
|
|
6
6
|
import type { VirtualItem } from './index'
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
export function getMeasurementKey(
|
|
9
|
+
item: VirtualItem | VirtualItem['key'],
|
|
10
|
+
): VirtualItem['key'] {
|
|
11
|
+
return typeof item === 'object' ? item.key : item
|
|
12
|
+
}
|
|
9
13
|
|
|
10
14
|
export function createLazyMeasurementsView(
|
|
11
|
-
|
|
15
|
+
cache: Array<VirtualItem | VirtualItem['key']>,
|
|
12
16
|
flat: Float64Array,
|
|
13
|
-
getItemKey: (i: number) => Key,
|
|
14
17
|
): Array<VirtualItem> {
|
|
15
|
-
const
|
|
16
|
-
return new Proxy(cache
|
|
18
|
+
const count = cache.length
|
|
19
|
+
return new Proxy(cache, {
|
|
17
20
|
get(target, prop, receiver) {
|
|
18
21
|
if (typeof prop === 'string') {
|
|
19
22
|
// Cheap digit-prefix sniff before number coerce.
|
|
@@ -21,12 +24,12 @@ export function createLazyMeasurementsView(
|
|
|
21
24
|
if (c >= 48 && c <= 57) {
|
|
22
25
|
const i = +prop
|
|
23
26
|
if (Number.isInteger(i) && i >= 0 && i < count) {
|
|
24
|
-
let v = target[i]
|
|
25
|
-
if (
|
|
27
|
+
let v = target[i]!
|
|
28
|
+
if (typeof v !== 'object') {
|
|
26
29
|
const s = flat[i * 2]!
|
|
27
30
|
v = target[i] = {
|
|
28
31
|
index: i,
|
|
29
|
-
key:
|
|
32
|
+
key: v,
|
|
30
33
|
start: s,
|
|
31
34
|
size: flat[i * 2 + 1]!,
|
|
32
35
|
end: s + flat[i * 2 + 1]!,
|