Skip to content

Commit

Permalink
fix: reset pendingMeasuredCacheIndexes when options change (#511)
Browse files Browse the repository at this point in the history
  • Loading branch information
piecyk authored Feb 20, 2023
1 parent 41199b2 commit 2ea2f5a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
33 changes: 28 additions & 5 deletions packages/virtual-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -430,15 +430,30 @@ export class Virtualizer<
return this.scrollRect[this.options.horizontal ? 'width' : 'height']
}

private getMeasurements = memo(
private memoOptions = memo(
() => [
this.options.count,
this.options.paddingStart,
this.options.scrollMargin,
this.options.getItemKey,
this.itemSizeCache,
],
(count, paddingStart, scrollMargin, getItemKey, itemSizeCache) => {
(count, paddingStart, scrollMargin, getItemKey) => {
this.pendingMeasuredCacheIndexes = []
return {
count,
paddingStart,
scrollMargin,
getItemKey,
}
},
{
key: false,
},
)

private getMeasurements = memo(
() => [this.memoOptions(), this.itemSizeCache],
({ count, paddingStart, scrollMargin, getItemKey }, itemSizeCache) => {
const min =
this.pendingMeasuredCacheIndexes.length > 0
? Math.min(...this.pendingMeasuredCacheIndexes)
Expand Down Expand Up @@ -487,14 +502,22 @@ export class Virtualizer<
)

private maybeNotify = memo(
() => [...Object.values(this.calculateRange()), this.isScrolling],
() => {
const range = this.calculateRange()

return [range.startIndex, range.endIndex, this.isScrolling]
},
() => {
this.notify()
},
{
key: process.env.NODE_ENV !== 'production' && 'maybeNotify',
debug: () => this.options.debug,
initialDeps: [...Object.values(this.range), this.isScrolling],
initialDeps: [
this.range.startIndex,
this.range.endIndex,
this.isScrolling,
],
},
)

Expand Down
2 changes: 1 addition & 1 deletion packages/virtual-core/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export function memo<TDeps extends readonly any[], TResult>(
getDeps: () => [...TDeps],
fn: (...args: NoInfer<[...TDeps]>) => TResult,
opts: {
key: any
key: false | string
debug?: () => any
onChange?: (result: TResult) => void
initialDeps?: TDeps
Expand Down

0 comments on commit 2ea2f5a

Please sign in to comment.