Skip to content

Commit 6d9b430

Browse files
author
sedat.ciftci
committed
Simplify orientation cache and drop keyExtractor change
1 parent 5c43c72 commit 6d9b430

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
@@ -1236,7 +1236,6 @@ class VirtualizedList extends StateSafePureComponent<
12361236
}
12371237

12381238
_cachedOrientation: ?ListOrientation = null;
1239-
_cachedOrientationHorizontal: ?boolean = null;
12401239
_cellRefs: {[string]: null | CellRenderer<any>} = {};
12411240
_fillRateHelper: FillRateHelper;
12421241
_listMetrics: ListMetricsAggregator = new ListMetricsAggregator();
@@ -1565,14 +1564,13 @@ class VirtualizedList extends StateSafePureComponent<
15651564
let cachedOrientation = this._cachedOrientation;
15661565
if (
15671566
cachedOrientation == null ||
1568-
this._cachedOrientationHorizontal !== horizontal
1567+
cachedOrientation.horizontal !== horizontal
15691568
) {
15701569
cachedOrientation = {
15711570
horizontal,
15721571
rtl: I18nManager.isRTL,
15731572
};
15741573
this._cachedOrientation = cachedOrientation;
1575-
this._cachedOrientationHorizontal = horizontal;
15761574
}
15771575
return cachedOrientation;
15781576
}

‎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 * as ReactNativeFeatureFlags from 'react-native/src/private/featureflags/ReactNativeFeatureFlags';
@@ -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)