Skip to content

Commit 73c0e98

Browse files
Merge branch 'main' of github.com:Expensify/react-native-onyx into test-for-keys-eviction-implementation
2 parents 1dbd8ac + 44e189e commit 73c0e98

9 files changed

Lines changed: 515 additions & 661 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
evictableKeys = [],
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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,10 +260,10 @@ class OnyxCache {
260260
iterResult = iterator.next();
261261
}
262262

263-
keysToRemove.forEach((key) => {
263+
for (const key of keysToRemove) {
264264
delete this.storageMap[key];
265265
this.recentKeys.delete(key);
266-
});
266+
}
267267
}
268268

269269
/** Set the recent keys list size */

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';
@@ -602,8 +601,9 @@ function keysChanged<TKey extends CollectionKeyBase>(
602601
// 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().
603602
const stateMappingKeys = Object.keys(callbackToStateMapping);
604603
const collectionKeyLength = collectionKey.length;
605-
for (let i = 0; i < stateMappingKeys.length; i++) {
606-
const subscriber = callbackToStateMapping[stateMappingKeys[i]];
604+
605+
for (const stateMappingKey of stateMappingKeys) {
606+
const subscriber = callbackToStateMapping[stateMappingKey];
607607
if (!subscriber) {
608608
continue;
609609
}
@@ -640,9 +640,7 @@ function keysChanged<TKey extends CollectionKeyBase>(
640640
// If they are not using waitForCollectionCallback then we notify the subscriber with
641641
// the new merged data but only for any keys in the partial collection.
642642
const dataKeys = Object.keys(partialCollection ?? {});
643-
for (let j = 0; j < dataKeys.length; j++) {
644-
const dataKey = dataKeys[j];
645-
643+
for (const dataKey of dataKeys) {
646644
if (deepEqual(cachedCollection[dataKey], previousCollection[dataKey])) {
647645
continue;
648646
}
@@ -699,8 +697,7 @@ function keysChanged<TKey extends CollectionKeyBase>(
699697
const prevCollection = prevState?.[subscriber.statePropertyName] ?? {};
700698
const finalCollection = lodashClone(prevCollection);
701699
const dataKeys = Object.keys(partialCollection ?? {});
702-
for (let j = 0; j < dataKeys.length; j++) {
703-
const dataKey = dataKeys[j];
700+
for (const dataKey of dataKeys) {
704701
finalCollection[dataKey] = cachedCollection[dataKey];
705702
}
706703

@@ -819,8 +816,8 @@ function keyChanged<TKey extends OnyxKey>(
819816

820817
const cachedCollections: Record<string, ReturnType<typeof getCachedCollection>> = {};
821818

822-
for (let i = 0; i < stateMappingKeys.length; i++) {
823-
const subscriber = callbackToStateMapping[stateMappingKeys[i]];
819+
for (const stateMappingKey of stateMappingKeys) {
820+
const subscriber = callbackToStateMapping[stateMappingKey];
824821
if (!subscriber || !isKeyMatch(subscriber.key, key) || !canUpdateSubscriber(subscriber)) {
825822
continue;
826823
}
@@ -1287,7 +1284,7 @@ function subscribeToKey<TKey extends OnyxKey>(connectOptions: ConnectOptions<TKe
12871284
// Performance improvement
12881285
// If the mapping is connected to an onyx key that is not a collection
12891286
// we can skip the call to getAllKeys() and return an array with a single item
1290-
if (Boolean(mapping.key) && typeof mapping.key === 'string' && !isCollectionKey(mapping.key) && cache.getAllKeys().has(mapping.key)) {
1287+
if (!!mapping.key && typeof mapping.key === 'string' && !isCollectionKey(mapping.key) && cache.getAllKeys().has(mapping.key)) {
12911288
return new Set([mapping.key]);
12921289
}
12931290
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)