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

[VirtualList] Fix off-by-one errors when rendering window-sized items. #14559

Closed
wants to merge 4 commits into from
Closed
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
6 changes: 4 additions & 2 deletions Libraries/Lists/VirtualizeUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function elementsThatOverlapOffsets(
let outLength = 0;
for (let ii = 0; ii < itemCount; ii++) {
const frame = getFrameMetrics(ii);
const trailingOffset = frame.offset + frame.length;
const trailingOffset = frame.offset + Math.max(0, frame.length - 1);
for (let kk = 0; kk < offsets.length; kk++) {
if (out[kk] == null && trailingOffset >= offsets[kk]) {
out[kk] = ii;
Expand Down Expand Up @@ -99,7 +99,7 @@ function computeWindowedRenderLimits(
// in the direction of scroll. Total overscan area is capped, which should cap memory consumption
// too.
const visibleBegin = Math.max(0, offset);
const visibleEnd = visibleBegin + visibleLength;
const visibleEnd = visibleBegin + Math.max(0, visibleLength - 1);
const overscanLength = (windowSize - 1) * visibleLength;

// Considering velocity seems to introduce more churn than it's worth.
Expand All @@ -124,6 +124,8 @@ function computeWindowedRenderLimits(
}

// Find the indices that correspond to the items at the render boundaries we're targeting.
// visibleBegin and visibleEnd represent the first and last visible pixel,
// so first and last represent the first and last visible component, as well.
let [overscanFirst, first, last, overscanLast] = elementsThatOverlapOffsets(
[overscanBegin, visibleBegin, visibleEnd, overscanEnd],
props.getItemCount(props.data),
Expand Down
15 changes: 13 additions & 2 deletions Libraries/Lists/__tests__/VirtualizeUtils-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,14 @@ describe('elementsThatOverlapOffsets', function() {
const offsets = [150, 250, 900];
const frames = [
{offset: 0, length: 50},
{offset: 50, length: 200},
{offset: 50, length: 200}, // Doesn't actually hit the 250 offset, since it only goes to pixel 249
{offset: 250, length: 600},
{offset: 850, length: 100},
{offset: 950, length: 150},
];
expect(
elementsThatOverlapOffsets(offsets, frames.length, ii => frames[ii]),
).toEqual([1, 1, 3]);
).toEqual([1, 2, 3]);
});
it('handles out of bounds', function() {
const offsets = [150, 900];
Expand All @@ -90,4 +90,15 @@ describe('elementsThatOverlapOffsets', function() {
elementsThatOverlapOffsets(offsets, frames.length, ii => frames[ii]);
}).toThrowErrorMatchingSnapshot();
});
it('handles off-by-one edge case', function() {
const offsets = [100, 199, 200];
const frames = [
{offset: 0, length: 100},
{offset: 100, length: 100},
{offset: 200, length: 100},
];
expect(
elementsThatOverlapOffsets(offsets, frames.length, (ii) => frames[ii])
).toEqual([1, 1, 2]);
});
});