Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trigger recalculation on every update when not scrolling #414

Merged
merged 1 commit into from
Nov 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion packages/react-virtual/__tests__/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ test('should render given dynamic size', async () => {
expect(renderer).toHaveBeenCalledTimes(3)
})

test.only('should render given dynamic size after scroll', () => {
test('should render given dynamic size after scroll', () => {
render(<List itemSize={100} dynamic />)

expect(screen.queryByText('Row 0')).toBeInTheDocument()
Expand Down Expand Up @@ -140,3 +140,17 @@ test('should use rangeExtractor', () => {
expect(screen.queryByText('Row 1')).toBeInTheDocument()
expect(screen.queryByText('Row 2')).not.toBeInTheDocument()
})

test('should handle count change', () => {
const { rerender } = render(<List count={2} />)

expect(screen.queryByText('Row 0')).toBeInTheDocument()
expect(screen.queryByText('Row 1')).toBeInTheDocument()
expect(screen.queryByText('Row 2')).not.toBeInTheDocument()

rerender(<List count={10} />)

expect(screen.queryByText('Row 2')).toBeInTheDocument()
expect(screen.queryByText('Row 4')).toBeInTheDocument()
expect(screen.queryByText('Row 5')).not.toBeInTheDocument()
})
34 changes: 29 additions & 5 deletions packages/virtual-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,12 +261,15 @@ export interface VirtualizerOptions<
rangeExtractor?: (range: Range) => number[]
enableSmoothScroll?: boolean
scrollMargin?: number
scrollingDelay?: number
}

export class Virtualizer<TScrollElement = unknown, TItemElement = unknown> {
private unsubs: (void | (() => void))[] = []
options!: Required<VirtualizerOptions<TScrollElement, TItemElement>>
scrollElement: TScrollElement | null = null
isScrolling: boolean = false
private isScrollingTimeoutId: ReturnType<typeof setTimeout> | null = null
private measurementsCache: Item[] = []
private itemMeasurementsCache: Record<Key, number> = {}
private pendingMeasuredCacheIndexes: number[] = []
Expand All @@ -278,7 +281,7 @@ export class Virtualizer<TScrollElement = unknown, TItemElement = unknown> {
number,
(measurableItem: TItemElement | null) => void
> = {}
private range: { startIndex: number; endIndex: number } = {
range: { startIndex: number; endIndex: number } = {
startIndex: 0,
endIndex: 0,
}
Expand Down Expand Up @@ -312,6 +315,7 @@ export class Virtualizer<TScrollElement = unknown, TItemElement = unknown> {
measureElement,
initialRect: { width: 0, height: 0 },
scrollMargin: 0,
scrollingDelay: 150,
...opts,
}
}
Expand Down Expand Up @@ -353,10 +357,30 @@ export class Virtualizer<TScrollElement = unknown, TItemElement = unknown> {

this.unsubs.push(
this.options.observeElementOffset(this, (offset) => {
this.scrollOffset = offset
if (this.isScrollingTimeoutId !== null) {
clearTimeout(this.isScrollingTimeoutId)
this.isScrollingTimeoutId = null
}

if (this.scrollOffset !== offset) {
this.isScrolling = true
this.scrollOffset = offset

this.isScrollingTimeoutId = setTimeout(() => {
this.isScrollingTimeoutId = null
this.isScrolling = false

this.notify()
}, this.options.scrollingDelay)
} else {
this.isScrolling = false
}

this.calculateRange()
}),
)
} else if (!this.isScrolling) {
this.calculateRange()
}
}

Expand Down Expand Up @@ -403,7 +427,7 @@ export class Virtualizer<TScrollElement = unknown, TItemElement = unknown> {
},
)

private calculateRange = memo(
calculateRange = memo(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm ecstatic that you are trying to solve this, because my team recently just ran head first into this exact issue. My main question with this approach, I think, is why can't we just have measure (which is already public) call calculateRange as part of its implementation. At the moment, it already doesn't seem to be doing what it semantically says it should be doing (from what I can tell{).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, i think we need to re-thing measure, now it's only clears itemMeasurementsCache and trigger update so most cases it will not measure anything. When we merge #405 with adding support observing elements with ResizeObserver i think we could drop it.

() => [this.getMeasurements(), this.getSize(), this.scrollOffset],
(measurements, outerSize, scrollOffset) => {
const range = calculateRange({
Expand Down Expand Up @@ -591,12 +615,12 @@ export class Virtualizer<TScrollElement = unknown, TItemElement = unknown> {

private _scrollToOffset = (
offset: number,
{ canSmooth, sync }: { canSmooth: boolean; sync: boolean },
options: { canSmooth: boolean; sync: boolean },
) => {
clearTimeout(this.scrollCheckFrame)

this.destinationOffset = offset
this.options.scrollToFn(offset, { canSmooth, sync }, this)
this.options.scrollToFn(offset, options, this)

let scrollCheckFrame: ReturnType<typeof setTimeout>

Expand Down