Skip to content

Commit 3559e42

Browse files
logandanielsfacebook-github-bot
authored andcommittedDec 22, 2017
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
1 parent c547f78 commit 3559e42

File tree

4 files changed

+24
-3
lines changed

4 files changed

+24
-3
lines changed
 

‎Libraries/Lists/VirtualizeUtils.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,19 @@ function elementsThatOverlapOffsets(
2525
getFrameMetrics: (index: number) => {length: number, offset: number},
2626
): Array<number> {
2727
const out = [];
28+
let outLength = 0;
2829
for (let ii = 0; ii < itemCount; ii++) {
2930
const frame = getFrameMetrics(ii);
3031
const trailingOffset = frame.offset + frame.length;
3132
for (let kk = 0; kk < offsets.length; kk++) {
3233
if (out[kk] == null && trailingOffset >= offsets[kk]) {
3334
out[kk] = ii;
35+
outLength++;
3436
if (kk === offsets.length - 1) {
3537
invariant(
36-
out.length === offsets.length,
37-
'bad offsets input, should be in increasing order ' +
38-
JSON.stringify(offsets),
38+
outLength === offsets.length,
39+
'bad offsets input, should be in increasing order: %s',
40+
JSON.stringify(offsets),
3941
);
4042
return out;
4143
}

‎Libraries/Lists/VirtualizedList.js

+5
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,11 @@ class VirtualizedList extends React.PureComponent<Props, State> {
535535
'to support native onScroll events with useNativeDriver',
536536
);
537537

538+
invariant(
539+
props.windowSize > 0,
540+
'VirtualizedList: The windowSize prop must be present and set to a value greater than 0.',
541+
);
542+
538543
this._fillRateHelper = new FillRateHelper(this._getFrameMetrics);
539544
this._updateCellsToRenderBatcher = new Batchinator(
540545
this._updateCellsToRender,

‎Libraries/Lists/__tests__/VirtualizeUtils-test.js

+11
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,15 @@ describe('elementsThatOverlapOffsets', function() {
8181
elementsThatOverlapOffsets(offsets, frames.length, ii => frames[ii]),
8282
).toEqual([1]);
8383
});
84+
it('errors on non-increasing offsets', function() {
85+
const offsets = [150, 50];
86+
const frames = [
87+
{offset: 0, length: 50},
88+
{offset: 50, length: 150},
89+
{offset: 250, length: 100},
90+
];
91+
expect(() => {
92+
elementsThatOverlapOffsets(offsets, frames.length, ii => frames[ii]);
93+
}).toThrowErrorMatchingSnapshot();
94+
});
8495
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`elementsThatOverlapOffsets errors on non-increasing offsets 1`] = `"bad offsets input, should be in increasing order: [150,50]"`;

0 commit comments

Comments
 (0)
Please sign in to comment.