Permalink
Browse files

Make sure VirtualizedList's windowSize is greater than 0

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...
Logan Daniels authored and facebook-github-bot committed Dec 22, 2017
1 parent c547f78 commit 3559e42c55366bacd9bb5178ecab64f95e9a8ea7
@@ -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;
}
@@ -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,
@@ -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();
});
});
@@ -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.