@tanstack/virtual-core 3.17.6 → 3.17.8

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/src/index.ts CHANGED
@@ -217,6 +217,10 @@ const observeOffset = <T extends Element | Window>(
217
217
  if (registerScrollendEvent) {
218
218
  element.removeEventListener('scrollend', endHandler)
219
219
  }
220
+ // Removing the listener doesn't retract a reset already queued by the
221
+ // last scroll, and that call would land on a virtualizer that has been
222
+ // torn down — in React, a dispatch into an unmounted tree.
223
+ fallback?.cancel()
220
224
  }
221
225
  }
222
226
 
@@ -420,7 +424,7 @@ export class Virtualizer<
420
424
  scrollRect: Rect | null = null
421
425
  scrollOffset: number | null = null
422
426
  scrollDirection: ScrollDirection | null = null
423
- private scrollAdjustments = 0
427
+ scrollAdjustments = 0
424
428
  // Sum of size-change deltas above-viewport that were skipped during
425
429
  // iOS momentum scroll (writing scrollTop mid-momentum cancels it).
426
430
  // Flushed in a single scrollTo when iOS is fully settled.
@@ -489,6 +493,8 @@ export class Virtualizer<
489
493
  return
490
494
  }
491
495
 
496
+ if (!this.isIndexInRange(index)) return
497
+
492
498
  if (this.shouldMeasureDuringScroll(index)) {
493
499
  this.resizeItem(
494
500
  index,
@@ -679,8 +685,15 @@ export class Virtualizer<
679
685
  this.options.onChange?.(this, sync)
680
686
  }
681
687
 
682
- private applyScrollAdjustment(delta: number, behavior?: ScrollBehavior) {
683
- if (delta === 0) return
688
+ // Returns `true` when it performed a synchronous `scrollTop` write this
689
+ // tick, `false` when the delta was zero or the write was deferred (iOS).
690
+ // `resizeItem` uses that to decide whether the follow-up `notify` must be
691
+ // synchronous so the grown transforms commit in the same paint (#1227).
692
+ private applyScrollAdjustment(
693
+ delta: number,
694
+ behavior?: ScrollBehavior,
695
+ ): boolean {
696
+ if (delta === 0) return false
684
697
 
685
698
  if (process.env.NODE_ENV !== 'production' && this.options.debug) {
686
699
  console.info('correction', delta)
@@ -691,6 +704,7 @@ export class Virtualizer<
691
704
  (this.isScrolling || this._iosTouching || this._iosJustTouchEnded)
692
705
  ) {
693
706
  this._iosDeferredAdjustment += delta
707
+ return false
694
708
  } else {
695
709
  this._scrollToOffset(this.getScrollOffset(), {
696
710
  adjustments: (this.scrollAdjustments += delta),
@@ -716,6 +730,7 @@ export class Virtualizer<
716
730
  if (this.scrollOffset < 0) this.scrollOffset = 0
717
731
  this.scrollAdjustments = 0
718
732
  }
733
+ return true
719
734
  }
720
735
  }
721
736
 
@@ -752,6 +767,13 @@ export class Virtualizer<
752
767
  this.rafId = null
753
768
  }
754
769
  this.scrollState = null
770
+ // The debounce cancelled above is the only thing that writes `isScrolling`
771
+ // back to false, so a cleanup inside the reset window would strand it, and
772
+ // the direction derived from it, as true. That matters because `cleanup`
773
+ // also runs when the scroll element changes or `enabled` goes false, where
774
+ // the instance lives on.
775
+ this.isScrolling = false
776
+ this.scrollDirection = null
755
777
  // The iOS gesture/deferral state is scoped to the current scroll
756
778
  // element: the touch listeners that maintain it were just removed, and
757
779
  // an in-flight touch keeps targeting the old element (implicit touch
@@ -1162,6 +1184,9 @@ export class Virtualizer<
1162
1184
  },
1163
1185
  )
1164
1186
 
1187
+ private isIndexInRange = (index: number): boolean =>
1188
+ index >= 0 && index < this.options.count
1189
+
1165
1190
  private getMeasurements = memo(
1166
1191
  () => [this.getMeasurementOptions(), this.itemSizeCacheVersion],
1167
1192
  (
@@ -1502,6 +1527,8 @@ export class Virtualizer<
1502
1527
  }
1503
1528
 
1504
1529
  const index = this.indexFromElement(node)
1530
+ if (!this.isIndexInRange(index)) return
1531
+
1505
1532
  const key = this.options.getItemKey(index)
1506
1533
  const prevNode = this.elementsCache.get(key)
1507
1534
 
@@ -1525,7 +1552,7 @@ export class Virtualizer<
1525
1552
  }
1526
1553
 
1527
1554
  resizeItem = (index: number, size: number) => {
1528
- if (index < 0 || index >= this.options.count) return
1555
+ if (!this.isIndexInRange(index)) return
1529
1556
 
1530
1557
  // Fast field reads. For lanes===1 we read raw start/size from the flat
1531
1558
  // typed array, avoiding a Proxy.get + VirtualItem allocation per call.
@@ -1599,13 +1626,26 @@ export class Virtualizer<
1599
1626
  this.itemSizeCache.set(key, size)
1600
1627
  this.itemSizeCacheVersion++
1601
1628
 
1629
+ let adjustedSync = false
1602
1630
  if (wasAtEnd) {
1603
- this.applyScrollAdjustment(this.getTotalSize() - prevTotalSize)
1631
+ adjustedSync = this.applyScrollAdjustment(
1632
+ this.getTotalSize() - prevTotalSize,
1633
+ )
1604
1634
  } else if (shouldAdjustScroll) {
1605
- this.applyScrollAdjustment(delta)
1635
+ adjustedSync = this.applyScrollAdjustment(delta)
1606
1636
  }
1607
1637
 
1608
- this.notify(false)
1638
+ // When we just moved `scrollTop` to compensate for an above-viewport
1639
+ // resize, the grown item transforms must commit in the SAME frame as
1640
+ // that write. `applyScrollAdjustment` writes `scrollTop` synchronously
1641
+ // inside this ResizeObserver callback, but an async `notify` schedules
1642
+ // the transform render for a later commit — so the browser can paint
1643
+ // one frame with the new `scrollTop` and the old positions, and the
1644
+ // viewport visibly jumps by `delta` before snapping back (#1227). A
1645
+ // synchronous notify flushes the render in this same callback, so both
1646
+ // land in one paint. When nothing moved (or the write was deferred on
1647
+ // iOS), keep the cheaper async notify.
1648
+ this.notify(adjustedSync)
1609
1649
  }
1610
1650
  }
1611
1651
 
package/src/utils.ts CHANGED
@@ -102,8 +102,18 @@ export const debounce = (
102
102
  ms: number,
103
103
  ) => {
104
104
  let timeoutId: number
105
- return function (this: any, ...args: Array<any>) {
106
- targetWindow.clearTimeout(timeoutId)
107
- timeoutId = targetWindow.setTimeout(() => fn.apply(this, args), ms)
108
- }
105
+ return Object.assign(
106
+ function (this: any, ...args: Array<any>) {
107
+ targetWindow.clearTimeout(timeoutId)
108
+ timeoutId = targetWindow.setTimeout(() => fn.apply(this, args), ms)
109
+ },
110
+ {
111
+ // The handle is closure-local, so a caller that has already
112
+ // unsubscribed has no way to stop a queued call. Teardown paths use
113
+ // this to drop the pending invocation instead of letting it land.
114
+ cancel: () => {
115
+ targetWindow.clearTimeout(timeoutId)
116
+ },
117
+ },
118
+ )
109
119
  }