Skip to content
16 changes: 12 additions & 4 deletions lib/OnyxUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ const lastConnectionCallbackData = new Map<number, OnyxValue<OnyxKey>>();

let snapshotKey: OnyxKey | null = null;

let fullyMergedSnapshotKeys: string[] = [];
let fullyMergedSnapshotKeys: Set<string> | undefined;

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

function getFullyMergedSnapshotKeys(): string[] {
function getFullyMergedSnapshotKeys(): Set<string> | undefined {
return fullyMergedSnapshotKeys;
}

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

if (typeof keys.COLLECTION === 'object' && typeof keys.COLLECTION.SNAPSHOT === 'string') {
snapshotKey = keys.COLLECTION.SNAPSHOT;
fullyMergedSnapshotKeys = fullyMergedSnapshotKeysParam ?? [];
fullyMergedSnapshotKeys = new Set(fullyMergedSnapshotKeysParam ?? []);
}
}

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

const oldValue = updatedData[key] || {};
const fullyMergedKeys = getFullyMergedSnapshotKeys();
const newValue = fullyMergedKeys.some((collectionKey) => isCollectionMemberKey(collectionKey, key)) ? value : lodashPick(value, Object.keys(snapshotData[key]));
let collectionKey: string | undefined;
try {
collectionKey = getCollectionKey(key);
} catch (e) {
Comment thread
FitseTLT marked this conversation as resolved.
// If getCollectionKey() throws an error it means the key is not a collection key.
collectionKey = undefined;
}
const shouldFullyMerge = fullyMergedKeys?.has(collectionKey || key);
const newValue = shouldFullyMerge ? value : lodashPick(value, Object.keys(snapshotData[key]));

updatedData = {...updatedData, [key]: Object.assign(oldValue, newValue)};
});
Expand Down
11 changes: 7 additions & 4 deletions tests/unit/onyxTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Onyx.init({
[ONYX_KEYS.KEY_WITH_UNDERSCORE]: 'default',
},
skippableCollectionMemberIDs: ['skippable-id'],
fullyMergedSnapshotKeys: [ONYX_KEYS.COLLECTION.ANIMALS],
fullyMergedSnapshotKeys: [ONYX_KEYS.COLLECTION.ANIMALS, ONYX_KEYS.OTHER_TEST],
});

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

const initialValue = {name: 'Fluffy'};
const initialValueOtherTest = {1: {name: 'Kitty'}};
const finalValuePeople = {name: 'Kitty'};
const finalValueOtherTest = {1: {name: 'First person'}, 2: {name: 'Second person'}};
const finalValueCat = {name: 'Kitty', nickName: 'Fitse'};
const onyxUpdate = {name: 'Kitty', nickName: 'Fitse'};

await Onyx.set(cat, initialValue);
await Onyx.set(people, initialValue);
await Onyx.set(snapshot1, {data: {[cat]: initialValue, [people]: initialValue}});
await Onyx.set(snapshot1, {data: {[ONYX_KEYS.OTHER_TEST]: initialValueOtherTest, [cat]: initialValue, [people]: initialValue}});

const callback = jest.fn();

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

expect(callback).toBeCalledTimes(2);
expect(callback).toHaveBeenNthCalledWith(1, {data: {[cat]: initialValue, [people]: initialValue}}, snapshot1);
expect(callback).toHaveBeenNthCalledWith(2, {data: {[cat]: finalValueCat, [people]: finalValuePeople}}, snapshot1);
expect(callback).toHaveBeenNthCalledWith(1, {data: {[cat]: initialValue, [ONYX_KEYS.OTHER_TEST]: initialValueOtherTest, [people]: initialValue}}, snapshot1);
expect(callback).toHaveBeenNthCalledWith(2, {data: {[cat]: finalValueCat, [ONYX_KEYS.OTHER_TEST]: finalValueOtherTest, [people]: finalValuePeople}}, snapshot1);
});

describe('update', () => {
Expand Down
Loading