Skip to content

Commit fdedf73

Browse files
committed
refactored to use Set
1 parent 65e2c65 commit fdedf73

2 files changed

Lines changed: 19 additions & 8 deletions

File tree

lib/OnyxUtils.ts

Lines changed: 12 additions & 4 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[] = [];
75+
let fullyMergedSnapshotKeys: Set<string> | undefined;
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[] {
90+
function getFullyMergedSnapshotKeys(): Set<string> | undefined {
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 = new Set(fullyMergedSnapshotKeysParam ?? []);
164164
}
165165
}
166166

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

14231423
const oldValue = updatedData[key] || {};
14241424
const fullyMergedKeys = getFullyMergedSnapshotKeys();
1425-
const newValue = fullyMergedKeys.some((collectionKey) => isCollectionMemberKey(collectionKey, key)) ? value : lodashPick(value, Object.keys(snapshotData[key]));
1425+
let collectionKey: string | undefined;
1426+
try {
1427+
collectionKey = getCollectionKey(key);
1428+
} catch (e) {
1429+
// If getCollectionKey() throws an error it means the key is not a collection key.
1430+
collectionKey = undefined;
1431+
}
1432+
const shouldFullyMerge = fullyMergedKeys?.has(collectionKey || key);
1433+
const newValue = shouldFullyMerge ? value : lodashPick(value, Object.keys(snapshotData[key]));
14261434

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

tests/unit/onyxTest.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Onyx.init({
3131
[ONYX_KEYS.KEY_WITH_UNDERSCORE]: 'default',
3232
},
3333
skippableCollectionMemberIDs: ['skippable-id'],
34-
fullyMergedSnapshotKeys: [ONYX_KEYS.COLLECTION.ANIMALS],
34+
fullyMergedSnapshotKeys: [ONYX_KEYS.COLLECTION.ANIMALS, ONYX_KEYS.OTHER_TEST],
3535
});
3636

3737
describe('Onyx', () => {
@@ -1450,13 +1450,15 @@ describe('Onyx', () => {
14501450
const snapshot1 = `${ONYX_KEYS.COLLECTION.SNAPSHOT}1`;
14511451

14521452
const initialValue = {name: 'Fluffy'};
1453+
const initialValueOtherTest = {1: {name: 'Kitty'}};
14531454
const finalValuePeople = {name: 'Kitty'};
1455+
const finalValueOtherTest = {1: {name: 'First person'}, 2: {name: 'Second person'}};
14541456
const finalValueCat = {name: 'Kitty', nickName: 'Fitse'};
14551457
const onyxUpdate = {name: 'Kitty', nickName: 'Fitse'};
14561458

14571459
await Onyx.set(cat, initialValue);
14581460
await Onyx.set(people, initialValue);
1459-
await Onyx.set(snapshot1, {data: {[cat]: initialValue, [people]: initialValue}});
1461+
await Onyx.set(snapshot1, {data: {[ONYX_KEYS.OTHER_TEST]: initialValueOtherTest, [cat]: initialValue, [people]: initialValue}});
14601462

14611463
const callback = jest.fn();
14621464

@@ -1470,11 +1472,12 @@ describe('Onyx', () => {
14701472
await Onyx.update([
14711473
{key: cat, value: onyxUpdate, onyxMethod: Onyx.METHOD.MERGE},
14721474
{key: people, value: onyxUpdate, onyxMethod: Onyx.METHOD.MERGE},
1475+
{key: ONYX_KEYS.OTHER_TEST, value: finalValueOtherTest, onyxMethod: Onyx.METHOD.MERGE},
14731476
]);
14741477

14751478
expect(callback).toBeCalledTimes(2);
1476-
expect(callback).toHaveBeenNthCalledWith(1, {data: {[cat]: initialValue, [people]: initialValue}}, snapshot1);
1477-
expect(callback).toHaveBeenNthCalledWith(2, {data: {[cat]: finalValueCat, [people]: finalValuePeople}}, snapshot1);
1479+
expect(callback).toHaveBeenNthCalledWith(1, {data: {[cat]: initialValue, [ONYX_KEYS.OTHER_TEST]: initialValueOtherTest, [people]: initialValue}}, snapshot1);
1480+
expect(callback).toHaveBeenNthCalledWith(2, {data: {[cat]: finalValueCat, [ONYX_KEYS.OTHER_TEST]: finalValueOtherTest, [people]: finalValuePeople}}, snapshot1);
14781481
});
14791482

14801483
describe('update', () => {

0 commit comments

Comments
 (0)