Skip to content

Commit 4c77804

Browse files
committed
minor update
1 parent ed2076f commit 4c77804

1 file changed

Lines changed: 10 additions & 17 deletions

File tree

lib/OnyxUtils.ts

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ const lastConnectionCallbackData = new Map<number, OnyxValue<OnyxKey>>();
7272

7373
let snapshotKey: OnyxKey | null = null;
7474

75-
let fullyMergedSnapshotKeys: string[] | undefined = [];
75+
let fullyMergedSnapshotKeys: string[] = [];
7676

7777
// Keeps track of the last subscriptionID that was used so we can keep incrementing it
7878
let lastSubscriptionID = 0;
@@ -87,7 +87,7 @@ function getSnapshotKey(): OnyxKey | null {
8787
return snapshotKey;
8888
}
8989

90-
function getFullyMergedSnapshotKeys(): string[] | undefined {
90+
function getFullyMergedSnapshotKeys(): string[] {
9191
return fullyMergedSnapshotKeys;
9292
}
9393

@@ -160,7 +160,7 @@ function initStoreValues(keys: DeepRecord<string, OnyxKey>, initialKeyStates: Pa
160160

161161
if (typeof keys.COLLECTION === 'object' && typeof keys.COLLECTION.SNAPSHOT === 'string') {
162162
snapshotKey = keys.COLLECTION.SNAPSHOT;
163-
fullyMergedSnapshotKeys = fullyMergedSnapshotKeysParam;
163+
fullyMergedSnapshotKeys = fullyMergedSnapshotKeysParam ?? [];
164164
}
165165
}
166166

@@ -455,8 +455,8 @@ function isCollectionKey(key: OnyxKey): key is CollectionKeyBase {
455455
return onyxCollectionKeySet.has(key);
456456
}
457457

458-
function isCollectionMemberKey<TCollectionKey extends CollectionKeyBase>(collectionKey: TCollectionKey, key: string, collectionKeyLength: number): key is `${TCollectionKey}${string}` {
459-
return key.startsWith(collectionKey) && key.length > collectionKeyLength;
458+
function isCollectionMemberKey<TCollectionKey extends CollectionKeyBase>(collectionKey: TCollectionKey, key: string): key is `${TCollectionKey}${string}` {
459+
return key.startsWith(collectionKey) && key.length > collectionKey.length;
460460
}
461461

462462
/**
@@ -470,7 +470,7 @@ function splitCollectionMemberKey<TKey extends CollectionKey, CollectionKeyType
470470
key: TKey,
471471
collectionKey?: string,
472472
): [CollectionKeyType, string] {
473-
if (collectionKey && !isCollectionMemberKey(collectionKey, key, collectionKey.length)) {
473+
if (collectionKey && !isCollectionMemberKey(collectionKey, key)) {
474474
throw new Error(`Invalid '${collectionKey}' collection key provided, it isn't compatible with '${key}' key.`);
475475
}
476476

@@ -565,14 +565,12 @@ function getCachedCollection<TKey extends CollectionKeyBase>(collectionKey: TKey
565565
const allKeys = collectionMemberKeys || cache.getAllKeys();
566566
const collection: OnyxCollection<KeyValueMapping[TKey]> = {};
567567

568-
const collectionKeyLength = collectionKey.length;
569-
570568
// forEach exists on both Set and Array
571569
allKeys.forEach((key) => {
572570
// If we don't have collectionMemberKeys array then we have to check whether a key is a collection member key.
573571
// Because in that case the keys will be coming from `cache.getAllKeys()` and we need to filter out the keys that
574572
// are not part of the collection.
575-
if (!collectionMemberKeys && !isCollectionMemberKey(collectionKey, key, collectionKeyLength)) {
573+
if (!collectionMemberKeys && !isCollectionMemberKey(collectionKey, key)) {
576574
return;
577575
}
578576

@@ -608,7 +606,6 @@ function keysChanged<TKey extends CollectionKeyBase>(
608606
// individual collection key member for the collection that is being updated. It is important to note that the collection parameter cane be a PARTIAL collection
609607
// 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().
610608
const stateMappingKeys = Object.keys(callbackToStateMapping);
611-
const collectionKeyLength = collectionKey.length;
612609

613610
for (const stateMappingKey of stateMappingKeys) {
614611
const subscriber = callbackToStateMapping[stateMappingKey];
@@ -629,7 +626,7 @@ function keysChanged<TKey extends CollectionKeyBase>(
629626
/**
630627
* e.g. Onyx.connect({key: `${ONYXKEYS.COLLECTION.REPORT}{reportID}`, callback: ...});
631628
*/
632-
const isSubscribedToCollectionMemberKey = isCollectionMemberKey(collectionKey, subscriber.key, collectionKeyLength);
629+
const isSubscribedToCollectionMemberKey = isCollectionMemberKey(collectionKey, subscriber.key);
633630

634631
// Regular Onyx.connect() subscriber found.
635632
if (typeof subscriber.callback === 'function') {
@@ -1389,7 +1386,6 @@ function updateSnapshots(data: OnyxUpdate[], mergeFn: typeof Onyx.merge): Array<
13891386
const promises: Array<() => Promise<void>> = [];
13901387

13911388
const snapshotCollection = OnyxUtils.getCachedCollection(snapshotCollectionKey);
1392-
const snapshotCollectionKeyLength = snapshotCollectionKey.length;
13931389

13941390
Object.entries(snapshotCollection).forEach(([snapshotEntryKey, snapshotEntryValue]) => {
13951391
// Snapshots may not be present in cache. We don't know how to update them so we skip.
@@ -1401,7 +1397,7 @@ function updateSnapshots(data: OnyxUpdate[], mergeFn: typeof Onyx.merge): Array<
14011397

14021398
data.forEach(({key, value}) => {
14031399
// snapshots are normal keys so we want to skip update if they are written to Onyx
1404-
if (OnyxUtils.isCollectionMemberKey(snapshotCollectionKey, key, snapshotCollectionKeyLength)) {
1400+
if (OnyxUtils.isCollectionMemberKey(snapshotCollectionKey, key)) {
14051401
return;
14061402
}
14071403

@@ -1426,10 +1422,7 @@ function updateSnapshots(data: OnyxUpdate[], mergeFn: typeof Onyx.merge): Array<
14261422

14271423
const oldValue = updatedData[key] || {};
14281424
const fullyMergedKeys = getFullyMergedSnapshotKeys();
1429-
const newValue =
1430-
fullyMergedKeys && fullyMergedKeys.some((collectionKey) => isCollectionMemberKey(collectionKey, key, collectionKey.length))
1431-
? value
1432-
: lodashPick(value, Object.keys(snapshotData[key]));
1425+
const newValue = fullyMergedKeys.some((collectionKey) => isCollectionMemberKey(collectionKey, key)) ? value : lodashPick(value, Object.keys(snapshotData[key]));
14331426

14341427
updatedData = {...updatedData, [key]: Object.assign(oldValue, newValue)};
14351428
});

0 commit comments

Comments
 (0)