@tanstack/virtual-core 3.19.12 → 3.19.13

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tanstack/virtual-core",
3
- "version": "3.19.12",
3
+ "version": "3.19.13",
4
4
  "description": "Headless UI for virtualizing scrollable elements in TS/JS + Frameworks",
5
5
  "author": "Tanner Linsley",
6
6
  "license": "MIT",
package/src/index.ts CHANGED
@@ -496,12 +496,20 @@ export class Virtualizer<
496
496
  } | null = null
497
497
  itemSizeCache = new Map<Key, number>()
498
498
  private itemSizeCacheVersion = 0
499
+ private builtMeasurementOptions: object | null = null
500
+ private builtItemSizeCacheVersion = -1
501
+ private pendingResizes: Array<{ index: number; delta: number }> = []
499
502
  private laneAssignments = new Map<number, number>() // index → lane cache
500
503
  // Earliest index dirtied since last getMeasurements() rebuild, or null.
501
504
  private pendingMin: number | null = null
502
505
  private prevLanes: number | undefined = undefined
503
506
  private lanesChangedFlag = false
504
507
  private lanesSettling = false
508
+ // Whether `initialMeasurementsCache` has seeded `itemSizeCache`. It is
509
+ // read once: a list that empties and refills keeps the sizes measured
510
+ // since, rather than going back to the snapshot's. `enabled: false`
511
+ // resets it along with the sizes.
512
+ private initialMeasurementsCacheApplied = false
505
513
  private pendingScrollAnchor: PendingScrollAnchor | null = null
506
514
  scrollRect: Rect | null = null
507
515
  /**
@@ -1674,8 +1682,8 @@ export class Virtualizer<
1674
1682
 
1675
1683
  private getMeasurements = memo(
1676
1684
  () => [this.getMeasurementOptions(), this.itemSizeCacheVersion],
1677
- (
1678
- {
1685
+ (measurementOptions, itemSizeCacheVersion) => {
1686
+ const {
1679
1687
  count,
1680
1688
  paddingStart,
1681
1689
  scrollMargin,
@@ -1684,15 +1692,17 @@ export class Virtualizer<
1684
1692
  lanes,
1685
1693
  laneAssignmentMode,
1686
1694
  gap,
1687
- },
1688
- _itemSizeCacheVersion,
1689
- ) => {
1695
+ } = measurementOptions
1696
+ this.builtMeasurementOptions = measurementOptions
1697
+ this.builtItemSizeCacheVersion = itemSizeCacheVersion
1698
+ this.pendingResizes = []
1690
1699
  const itemSizeCache = this.itemSizeCache
1691
1700
  if (!enabled) {
1692
1701
  this.measurementsCache = []
1693
1702
  this._singleLaneMeasurements = null
1694
1703
  this.itemSizeCache.clear()
1695
1704
  this.laneAssignments.clear()
1705
+ this.initialMeasurementsCacheApplied = false
1696
1706
  return []
1697
1707
  }
1698
1708
 
@@ -1719,7 +1729,12 @@ export class Virtualizer<
1719
1729
 
1720
1730
  // Don't restore from initialMeasurementsCache during lane changes
1721
1731
  // as it contains stale lane assignments from the previous lane count
1722
- if (this.measurementsCache.length === 0 && !this.lanesSettling) {
1732
+ if (
1733
+ !this.initialMeasurementsCacheApplied &&
1734
+ this.measurementsCache.length === 0 &&
1735
+ !this.lanesSettling
1736
+ ) {
1737
+ this.initialMeasurementsCacheApplied = true
1723
1738
  this.measurementsCache = this.options.initialMeasurementsCache
1724
1739
  this.measurementsCache.forEach((item) => {
1725
1740
  this.itemSizeCache.set(item.key, item.size)
@@ -2058,6 +2073,16 @@ export class Virtualizer<
2058
2073
  }
2059
2074
  }
2060
2075
 
2076
+ private canShiftFromPendingResizes = (): boolean =>
2077
+ this.options.lanes === 1 &&
2078
+ this._singleLaneMeasurements !== null &&
2079
+ this.shouldAdjustScrollPositionOnItemSizeChange === undefined &&
2080
+ !this.lanesChangedFlag &&
2081
+ !this.lanesSettling &&
2082
+ this.getMeasurementOptions() === this.builtMeasurementOptions &&
2083
+ this.itemSizeCacheVersion ===
2084
+ this.builtItemSizeCacheVersion + this.pendingResizes.length
2085
+
2061
2086
  resizeItem = (index: number, size: number) => {
2062
2087
  if (!this.isIndexInRange(index)) return
2063
2088
 
@@ -2067,7 +2092,16 @@ export class Virtualizer<
2067
2092
  // this item's current start, not a stale one, against the adjusted
2068
2093
  // offset (#1218). Without a scroll move, the caches are exactly as stale
2069
2094
  // as they were before batching, so skip the rebuild.
2070
- if (this._resizeBatch?.sync) this.getMeasurements()
2095
+ let startShift = 0
2096
+ if (this._resizeBatch?.sync) {
2097
+ if (this.canShiftFromPendingResizes()) {
2098
+ for (const resize of this.pendingResizes) {
2099
+ if (resize.index < index) startShift += resize.delta
2100
+ }
2101
+ } else {
2102
+ this.getMeasurements()
2103
+ }
2104
+ }
2071
2105
 
2072
2106
  // Fast field reads. For lanes===1 we read raw start/size from the flat
2073
2107
  // typed array, avoiding a Proxy.get + VirtualItem allocation per call.
@@ -2078,7 +2112,7 @@ export class Virtualizer<
2078
2112
  const flat = this._singleLaneMeasurements?.flat
2079
2113
  if (this.options.lanes === 1 && flat != null) {
2080
2114
  key = this.options.getItemKey(index)
2081
- itemStart = flat[index * 2]!
2115
+ itemStart = flat[index * 2]! + startShift
2082
2116
  cachedSize = flat[index * 2 + 1]!
2083
2117
  } else {
2084
2118
  const item = this.measurementsCache[index]
@@ -2139,6 +2173,7 @@ export class Virtualizer<
2139
2173
  }
2140
2174
  this.itemSizeCache.set(key, size)
2141
2175
  this.itemSizeCacheVersion++
2176
+ this.pendingResizes.push({ index, delta })
2142
2177
 
2143
2178
  // Before a command's read-back, an index or end target is retargeted
2144
2179
  // from fresh measurements by reconcileScroll next frame; an offset