Skip to content

Commit 60a918c

Browse files
committed
Simplify orientation cache and drop keyExtractor change
1 parent bff5f39 commit 60a918c

4 files changed

Lines changed: 15 additions & 46 deletions

File tree

‎packages/virtualized-lists/Lists/VirtualizeUtils.js‎

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -244,13 +244,11 @@ export function computeWindowedRenderLimits(
244244
}
245245

246246
export function keyExtractor(item: any, index: number): string {
247-
if (item != null && typeof item === 'object') {
248-
if (item.key != null) {
249-
return item.key;
250-
}
251-
if (item.id != null) {
252-
return item.id;
253-
}
247+
if (typeof item === 'object' && item?.key != null) {
248+
return item.key;
249+
}
250+
if (typeof item === 'object' && item?.id != null) {
251+
return item.id;
254252
}
255253
return String(index);
256254
}

‎packages/virtualized-lists/Lists/VirtualizedList.js‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1237,7 +1237,6 @@ class VirtualizedList extends StateSafePureComponent<
12371237
}
12381238

12391239
_cachedOrientation: ?ListOrientation = null;
1240-
_cachedOrientationHorizontal: ?boolean = null;
12411240
_cellRefs: {[string]: null | CellRenderer<any>} = {};
12421241
_fillRateHelper: FillRateHelper;
12431242
_listMetrics: ListMetricsAggregator = new ListMetricsAggregator();
@@ -1566,14 +1565,13 @@ class VirtualizedList extends StateSafePureComponent<
15661565
let cachedOrientation = this._cachedOrientation;
15671566
if (
15681567
cachedOrientation == null ||
1569-
this._cachedOrientationHorizontal !== horizontal
1568+
cachedOrientation.horizontal !== horizontal
15701569
) {
15711570
cachedOrientation = {
15721571
horizontal,
15731572
rtl: I18nManager.isRTL,
15741573
};
15751574
this._cachedOrientation = cachedOrientation;
1576-
this._cachedOrientationHorizontal = horizontal;
15771575
}
15781576
return cachedOrientation;
15791577
}

‎packages/virtualized-lists/Lists/__tests__/ChildListCollection-test.js‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ describe('ChildListCollection', function () {
2020
collection.add('c', 'cell2');
2121

2222
const visited = [];
23-
collection.forEach(list => visited.push(list));
23+
collection.forEach(list => {
24+
visited.push(list);
25+
});
2426
expect(visited.sort()).toEqual(['a', 'b', 'c']);
2527
expect(collection.size()).toBe(3);
2628
});
@@ -39,7 +41,9 @@ describe('ChildListCollection', function () {
3941
collection.remove('a');
4042

4143
const visited = [];
42-
collection.forEach(list => visited.push(list));
44+
collection.forEach(list => {
45+
visited.push(list);
46+
});
4347
expect(visited).toEqual([]);
4448
expect(collection.size()).toBe(0);
4549
});
@@ -50,7 +54,9 @@ describe('ChildListCollection', function () {
5054
collection.add('b', 'cell2');
5155

5256
const visited = [];
53-
collection.forEachInCell('cell1', list => visited.push(list));
57+
collection.forEachInCell('cell1', list => {
58+
visited.push(list);
59+
});
5460
expect(visited).toEqual(['a']);
5561

5662
expect(collection.anyInCell('cell2', list => list === 'b')).toBe(true);

‎packages/virtualized-lists/Lists/__tests__/VirtualizeUtils-test.js‎

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import ListMetricsAggregator from '../ListMetricsAggregator';
1616
import {
1717
computeWindowedRenderLimits,
1818
elementsThatOverlapOffsets,
19-
keyExtractor,
2019
newRangeCount,
2120
} from '../VirtualizeUtils';
2221
import {ReactNativeFeatureFlags} from 'react-native/react-private-interface';
@@ -293,35 +292,3 @@ describe('computeWindowedRenderLimits', function () {
293292
expect(result).toEqual({first: 0, last: 4});
294293
});
295294
});
296-
297-
describe('keyExtractor', function () {
298-
it('prefers item.key', function () {
299-
expect(keyExtractor({key: 'k', id: 1}, 0)).toBe('k');
300-
});
301-
302-
it('falls back to item.id when key is missing', function () {
303-
expect(keyExtractor({id: 42}, 0)).toBe(42);
304-
});
305-
306-
it('treats explicit null key as missing', function () {
307-
expect(keyExtractor({key: null, id: 9}, 0)).toBe(9);
308-
});
309-
310-
it('returns explicitly set falsy key and id values', function () {
311-
expect(keyExtractor({key: 0}, 0)).toBe(0);
312-
expect(keyExtractor({key: false}, 0)).toBe(false);
313-
expect(keyExtractor({key: null, id: 0}, 0)).toBe(0);
314-
});
315-
316-
it('falls back to the index for items without key or id', function () {
317-
expect(keyExtractor({}, 7)).toBe('7');
318-
});
319-
320-
it('falls back to the index for null, undefined, primitives and arrays', function () {
321-
expect(keyExtractor(null, 1)).toBe('1');
322-
expect(keyExtractor(undefined, 2)).toBe('2');
323-
expect(keyExtractor('str', 3)).toBe('3');
324-
expect(keyExtractor(42, 4)).toBe('4');
325-
expect(keyExtractor([], 5)).toBe('5');
326-
});
327-
});

0 commit comments

Comments
 (0)