Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions lib/Onyx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ function init({
debugSetState = false,
enablePerformanceMetrics = false,
skippableCollectionMemberIDs = [],
fullyMergedSnapshotKeys = [],
}: InitOptions): void {
if (enablePerformanceMetrics) {
GlobalSettings.setPerformanceMetricsEnabled(true);
Expand All @@ -71,7 +70,7 @@ function init({
cache.setRecentKeysLimit(maxCachedKeysCount);
}

OnyxUtils.initStoreValues(keys, initialKeyStates, evictableKeys, fullyMergedSnapshotKeys);
OnyxUtils.initStoreValues(keys, initialKeyStates, evictableKeys);

// Initialize all of our keys with data provided then give green light to any pending connections
Promise.all([cache.addEvictableKeysToRecentlyAccessedList(OnyxUtils.isCollectionKey, OnyxUtils.getAllKeys), OnyxUtils.initializeWithDefaultKeyStates()]).then(
Expand Down
16 changes: 2 additions & 14 deletions lib/OnyxUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,6 @@ let lastConnectionCallbackData = new Map<number, OnyxValue<OnyxKey>>();

let snapshotKey: OnyxKey | null = null;

let fullyMergedSnapshotKeys: Set<string> | undefined;

// Keeps track of the last subscriptionID that was used so we can keep incrementing it
let lastSubscriptionID = 0;

Expand Down Expand Up @@ -141,9 +139,8 @@ function setSkippableCollectionMemberIDs(ids: Set<string>): void {
* @param keys - `ONYXKEYS` constants object from Onyx.init()
* @param initialKeyStates - initial data to set when `init()` and `clear()` are called
* @param evictableKeys - This is an array of keys (individual or collection patterns) that when provided to Onyx are flagged as "safe" for removal.
* @param fullyMergedSnapshotKeys - Array of snapshot collection keys where full merge is supported and data structure can be changed after merge.
*/
function initStoreValues(keys: DeepRecord<string, OnyxKey>, initialKeyStates: Partial<KeyValueMapping>, evictableKeys: OnyxKey[], fullyMergedSnapshotKeysParam?: string[]): void {
function initStoreValues(keys: DeepRecord<string, OnyxKey>, initialKeyStates: Partial<KeyValueMapping>, evictableKeys: OnyxKey[]): void {
// We need the value of the collection keys later for checking if a
// key is a collection. We store it in a map for faster lookup.
const collectionValues = Object.values(keys.COLLECTION ?? {}) as string[];
Expand All @@ -165,7 +162,6 @@ function initStoreValues(keys: DeepRecord<string, OnyxKey>, initialKeyStates: Pa

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

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

const oldValue = updatedData[key] || {};
let collectionKey: string | undefined;
try {
collectionKey = getCollectionKey(key);
} catch (e) {
// If getCollectionKey() throws an error it means the key is not a collection key.
collectionKey = undefined;
}
const shouldFullyMerge = fullyMergedSnapshotKeys?.has(collectionKey || key);
const newValue = shouldFullyMerge ? value : lodashPick(value, Object.keys(snapshotData[key]));
const newValue = lodashPick(value, Object.keys(snapshotData[key]));

updatedData = {...updatedData, [key]: Object.assign(oldValue, newValue)};
});
Expand Down
8 changes: 0 additions & 8 deletions lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -489,14 +489,6 @@ type InitOptions = {
* Additionally, any subscribers from these keys to won't receive any data from Onyx.
*/
skippableCollectionMemberIDs?: string[];

/**
* Array of snapshot collection keys where full merge is supported and data structure can be changed after merge.
* For e.g. if oldSnapshotData is {report_1: {name 'Fitsum'}} and BE update is {report_1: {name:'Fitsum2', nickName:'Fitse'}}
* if it is fullyMergedSnapshotkey the `nickName` prop that didn't exist in the previous data will be merged
* otherwise only existing prop will be picked from the BE update and merged (in this case only name).
*/
fullyMergedSnapshotKeys?: string[];
};

// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down
21 changes: 5 additions & 16 deletions tests/unit/onyxTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ Onyx.init({
[ONYX_KEYS.KEY_WITH_UNDERSCORE]: 'default',
},
skippableCollectionMemberIDs: ['skippable-id'],
fullyMergedSnapshotKeys: [ONYX_KEYS.COLLECTION.ANIMALS, ONYX_KEYS.OTHER_TEST],
});

describe('Onyx', () => {
Expand Down Expand Up @@ -1447,19 +1446,13 @@ describe('Onyx', () => {

it('should update Snapshot when its data changed', async () => {
const cat = `${ONYX_KEYS.COLLECTION.ANIMALS}cat`;
const people = `${ONYX_KEYS.COLLECTION.PEOPLE}1`;
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'};
const finalValue = {name: 'Kitty'};

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

const callback = jest.fn();

Expand All @@ -1470,15 +1463,11 @@ describe('Onyx', () => {

await waitForPromisesToResolve();

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},
]);
await Onyx.update([{key: cat, value: finalValue, onyxMethod: Onyx.METHOD.MERGE}]);

expect(callback).toBeCalledTimes(2);
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);
expect(callback).toHaveBeenNthCalledWith(1, {data: {[cat]: initialValue}}, snapshot1);
expect(callback).toHaveBeenNthCalledWith(2, {data: {[cat]: finalValue}}, snapshot1);
});

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