@tanstack/virtual-core 3.0.0-beta.47 → 3.0.0-beta.49

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
@@ -430,15 +430,30 @@ export class Virtualizer<
430
430
  return this.scrollRect[this.options.horizontal ? 'width' : 'height']
431
431
  }
432
432
 
433
- private getMeasurements = memo(
433
+ private memoOptions = memo(
434
434
  () => [
435
435
  this.options.count,
436
436
  this.options.paddingStart,
437
437
  this.options.scrollMargin,
438
438
  this.options.getItemKey,
439
- this.itemSizeCache,
440
439
  ],
441
- (count, paddingStart, scrollMargin, getItemKey, itemSizeCache) => {
440
+ (count, paddingStart, scrollMargin, getItemKey) => {
441
+ this.pendingMeasuredCacheIndexes = []
442
+ return {
443
+ count,
444
+ paddingStart,
445
+ scrollMargin,
446
+ getItemKey,
447
+ }
448
+ },
449
+ {
450
+ key: false,
451
+ },
452
+ )
453
+
454
+ private getMeasurements = memo(
455
+ () => [this.memoOptions(), this.itemSizeCache],
456
+ ({ count, paddingStart, scrollMargin, getItemKey }, itemSizeCache) => {
442
457
  const min =
443
458
  this.pendingMeasuredCacheIndexes.length > 0
444
459
  ? Math.min(...this.pendingMeasuredCacheIndexes)
@@ -487,14 +502,22 @@ export class Virtualizer<
487
502
  )
488
503
 
489
504
  private maybeNotify = memo(
490
- () => [...Object.values(this.calculateRange()), this.isScrolling],
505
+ () => {
506
+ const range = this.calculateRange()
507
+
508
+ return [range.startIndex, range.endIndex, this.isScrolling]
509
+ },
491
510
  () => {
492
511
  this.notify()
493
512
  },
494
513
  {
495
514
  key: process.env.NODE_ENV !== 'production' && 'maybeNotify',
496
515
  debug: () => this.options.debug,
497
- initialDeps: [...Object.values(this.range), this.isScrolling],
516
+ initialDeps: [
517
+ this.range.startIndex,
518
+ this.range.endIndex,
519
+ this.isScrolling,
520
+ ],
498
521
  },
499
522
  )
500
523
 
@@ -617,6 +640,21 @@ export class Virtualizer<
617
640
  },
618
641
  )
619
642
 
643
+ getVirtualItemForOffset = (offset: number) => {
644
+ const measurements = this.getMeasurements()
645
+
646
+ return notUndefined(
647
+ measurements[
648
+ findNearestBinarySearch(
649
+ 0,
650
+ measurements.length - 1,
651
+ (index: number) => notUndefined(measurements[index]).start,
652
+ offset,
653
+ )
654
+ ],
655
+ )
656
+ }
657
+
620
658
  getOffsetForAlignment = (toOffset: number, align: ScrollAlignment) => {
621
659
  const size = this.getSize()
622
660
 
package/src/utils.ts CHANGED
@@ -6,7 +6,7 @@ export function memo<TDeps extends readonly any[], TResult>(
6
6
  getDeps: () => [...TDeps],
7
7
  fn: (...args: NoInfer<[...TDeps]>) => TResult,
8
8
  opts: {
9
- key: any
9
+ key: false | string
10
10
  debug?: () => any
11
11
  onChange?: (result: TResult) => void
12
12
  initialDeps?: TDeps