Skip to content

Commit 5ed3b79

Browse files
authored
Merge pull request #630 from callstack-internal/task/877-refactor-onyx-to-use-for-of-loops
Task/877 refactor onyx to use for of loops
2 parents b4b42fa + d326ac4 commit 5ed3b79

9 files changed

Lines changed: 513 additions & 660 deletions

File tree

lib/Onyx.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ function init({
4040
initialKeyStates = {},
4141
safeEvictionKeys = [],
4242
maxCachedKeysCount = 1000,
43-
shouldSyncMultipleInstances = Boolean(global.localStorage),
43+
shouldSyncMultipleInstances = !!global.localStorage,
4444
debugSetState = false,
4545
enablePerformanceMetrics = false,
4646
skippableCollectionMemberIDs = [],

lib/OnyxCache.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -231,10 +231,9 @@ class OnyxCache {
231231
numKeysToRemove--;
232232
}
233233

234-
// eslint-disable-next-line @typescript-eslint/prefer-for-of
235-
for (let i = 0; i < temp.length; ++i) {
236-
delete this.storageMap[temp[i]];
237-
this.recentKeys.delete(temp[i]);
234+
for (const key of temp) {
235+
delete this.storageMap[key];
236+
this.recentKeys.delete(key);
238237
}
239238
}
240239

lib/OnyxUtils.ts

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
/* eslint-disable @typescript-eslint/prefer-for-of */
21
/* eslint-disable no-continue */
32
import {deepEqual} from 'fast-equals';
43
import lodashClone from 'lodash/clone';
@@ -666,8 +665,9 @@ function keysChanged<TKey extends CollectionKeyBase>(
666665
// and does not represent all of the combined keys and values for a collection key. It is just the "new" data that was merged in via mergeCollection().
667666
const stateMappingKeys = Object.keys(callbackToStateMapping);
668667
const collectionKeyLength = collectionKey.length;
669-
for (let i = 0; i < stateMappingKeys.length; i++) {
670-
const subscriber = callbackToStateMapping[stateMappingKeys[i]];
668+
669+
for (const stateMappingKey of stateMappingKeys) {
670+
const subscriber = callbackToStateMapping[stateMappingKey];
671671
if (!subscriber) {
672672
continue;
673673
}
@@ -704,9 +704,7 @@ function keysChanged<TKey extends CollectionKeyBase>(
704704
// If they are not using waitForCollectionCallback then we notify the subscriber with
705705
// the new merged data but only for any keys in the partial collection.
706706
const dataKeys = Object.keys(partialCollection ?? {});
707-
for (let j = 0; j < dataKeys.length; j++) {
708-
const dataKey = dataKeys[j];
709-
707+
for (const dataKey of dataKeys) {
710708
if (deepEqual(cachedCollection[dataKey], previousCollection[dataKey])) {
711709
continue;
712710
}
@@ -763,8 +761,7 @@ function keysChanged<TKey extends CollectionKeyBase>(
763761
const prevCollection = prevState?.[subscriber.statePropertyName] ?? {};
764762
const finalCollection = lodashClone(prevCollection);
765763
const dataKeys = Object.keys(partialCollection ?? {});
766-
for (let j = 0; j < dataKeys.length; j++) {
767-
const dataKey = dataKeys[j];
764+
for (const dataKey of dataKeys) {
768765
finalCollection[dataKey] = cachedCollection[dataKey];
769766
}
770767

@@ -883,8 +880,8 @@ function keyChanged<TKey extends OnyxKey>(
883880

884881
const cachedCollections: Record<string, ReturnType<typeof getCachedCollection>> = {};
885882

886-
for (let i = 0; i < stateMappingKeys.length; i++) {
887-
const subscriber = callbackToStateMapping[stateMappingKeys[i]];
883+
for (const stateMappingKey of stateMappingKeys) {
884+
const subscriber = callbackToStateMapping[stateMappingKey];
888885
if (!subscriber || !isKeyMatch(subscriber.key, key) || !canUpdateSubscriber(subscriber)) {
889886
continue;
890887
}
@@ -1348,7 +1345,7 @@ function subscribeToKey<TKey extends OnyxKey>(connectOptions: ConnectOptions<TKe
13481345
// Performance improvement
13491346
// If the mapping is connected to an onyx key that is not a collection
13501347
// we can skip the call to getAllKeys() and return an array with a single item
1351-
if (Boolean(mapping.key) && typeof mapping.key === 'string' && !isCollectionKey(mapping.key) && cache.getAllKeys().has(mapping.key)) {
1348+
if (!!mapping.key && typeof mapping.key === 'string' && !isCollectionKey(mapping.key) && cache.getAllKeys().has(mapping.key)) {
13521349
return new Set([mapping.key]);
13531350
}
13541351
return getAllKeys();

lib/utils.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
/* eslint-disable @typescript-eslint/prefer-for-of */
2-
31
import type {ConnectOptions, OnyxInput, OnyxKey} from './types';
42

53
type EmptyObject = Record<string, never>;
@@ -154,16 +152,15 @@ function filterObject<TValue>(obj: Record<string, TValue>, condition: string | s
154152
const result: Record<string, TValue> = {};
155153
const entries = Object.entries(obj);
156154

157-
for (let i = 0; i < entries.length; i++) {
158-
const [key, value] = entries[i];
155+
for (const [key, value] of entries) {
159156
let shouldInclude: boolean;
160157

161158
if (Array.isArray(condition)) {
162159
shouldInclude = condition.includes(key);
163160
} else if (typeof condition === 'string') {
164161
shouldInclude = key === condition;
165162
} else {
166-
shouldInclude = condition(entries[i]);
163+
shouldInclude = condition([key, value]);
167164
}
168165

169166
if (include ? shouldInclude : !shouldInclude) {

0 commit comments

Comments
 (0)