Skip to content

Commit

Permalink
Make sure VirtualizedList's windowSize is greater than 0
Browse files Browse the repository at this point in the history
Summary:
Setting `windowSize = 0` doesn't make sense. Let's make sure we catch this problem in the constructor so that it doesn't cause inexplicable list behavior.

Also fixed an invariant in `VirtualizeUtils` that is meant to prohibit non-monotonically-increasing offset arrays. As written, the invariant condition can never actually be violated.

Reviewed By: sahrens

Differential Revision: D6625302

fbshipit-source-id: b2a983cbe7bb5fbe0aed7c5d59e69a8a00672993
  • Loading branch information
logandaniels authored and facebook-github-bot committed Dec 22, 2017
1 parent c547f78 commit 3559e42
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 3 deletions.
8 changes: 5 additions & 3 deletions Libraries/Lists/VirtualizeUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,19 @@ function elementsThatOverlapOffsets(
getFrameMetrics: (index: number) => {length: number, offset: number},
): Array<number> {
const out = [];
let outLength = 0;
for (let ii = 0; ii < itemCount; ii++) {
const frame = getFrameMetrics(ii);
const trailingOffset = frame.offset + frame.length;
for (let kk = 0; kk < offsets.length; kk++) {
if (out[kk] == null && trailingOffset >= offsets[kk]) {
out[kk] = ii;
outLength++;
if (kk === offsets.length - 1) {
invariant(
out.length === offsets.length,
'bad offsets input, should be in increasing order ' +
JSON.stringify(offsets),
outLength === offsets.length,
'bad offsets input, should be in increasing order: %s',
JSON.stringify(offsets),
);
return out;
}
Expand Down
5 changes: 5 additions & 0 deletions Libraries/Lists/VirtualizedList.js
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,11 @@ class VirtualizedList extends React.PureComponent<Props, State> {
'to support native onScroll events with useNativeDriver',
);

invariant(
props.windowSize > 0,
'VirtualizedList: The windowSize prop must be present and set to a value greater than 0.',
);

this._fillRateHelper = new FillRateHelper(this._getFrameMetrics);
this._updateCellsToRenderBatcher = new Batchinator(
this._updateCellsToRender,
Expand Down
11 changes: 11 additions & 0 deletions Libraries/Lists/__tests__/VirtualizeUtils-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,15 @@ describe('elementsThatOverlapOffsets', function() {
elementsThatOverlapOffsets(offsets, frames.length, ii => frames[ii]),
).toEqual([1]);
});
it('errors on non-increasing offsets', function() {
const offsets = [150, 50];
const frames = [
{offset: 0, length: 50},
{offset: 50, length: 150},
{offset: 250, length: 100},
];
expect(() => {
elementsThatOverlapOffsets(offsets, frames.length, ii => frames[ii]);
}).toThrowErrorMatchingSnapshot();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`elementsThatOverlapOffsets errors on non-increasing offsets 1`] = `"bad offsets input, should be in increasing order: [150,50]"`;

0 comments on commit 3559e42

Please sign in to comment.