-
Notifications
You must be signed in to change notification settings - Fork 80
Implemented partialSetCollection #677
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
99287ca
2cc2352
a86719f
b31ab1f
7039e81
c8e41fd
b06e249
d230066
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1616,6 +1616,60 @@ function mergeCollectionWithPatches<TKey extends CollectionKeyBase, TMap>( | |
.then(() => undefined); | ||
} | ||
|
||
/** | ||
* Sets keys in a collection by replacing all targeted collection members with new values. | ||
* Any existing collection members not included in the new data will not be removed. | ||
* | ||
* @param collectionKey e.g. `ONYXKEYS.COLLECTION.REPORT` | ||
* @param collection Object collection keyed by individual collection member keys and values | ||
*/ | ||
function partialSetCollection<TKey extends CollectionKeyBase, TMap>(collectionKey: TKey, collection: OnyxMergeCollectionInput<TKey, TMap>): Promise<void> { | ||
let resultCollection: OnyxInputKeyValueMapping = collection; | ||
let resultCollectionKeys = Object.keys(resultCollection); | ||
|
||
// Confirm all the collection keys belong to the same parent | ||
if (!doAllCollectionItemsBelongToSameParent(collectionKey, resultCollectionKeys)) { | ||
Logger.logAlert(`setCollection called with keys that do not belong to the same parent ${collectionKey}. Skipping this update.`); | ||
return Promise.resolve(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we should reject this and throw an error back. This is a development mistake, so they should not proceed with such code. Silencing this error could give false positive. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the standard implementation across different methods. We are logging an alert. If we throw an error here, it will cause issues (crash) if we get some bad updates from BE. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok. Talking about backend updates where are we handling the backend updates for this new method. |
||
} | ||
|
||
if (skippableCollectionMemberIDs.size) { | ||
resultCollection = resultCollectionKeys.reduce((result: OnyxInputKeyValueMapping, key) => { | ||
try { | ||
const [, collectionMemberID] = splitCollectionMemberKey(key, collectionKey); | ||
// If the collection member key is a skippable one we set its value to null. | ||
// eslint-disable-next-line no-param-reassign | ||
result[key] = !skippableCollectionMemberIDs.has(collectionMemberID) ? resultCollection[key] : null; | ||
} catch { | ||
// Something went wrong during split, so we assign the data to result anyway. | ||
// eslint-disable-next-line no-param-reassign | ||
result[key] = resultCollection[key]; | ||
} | ||
|
||
return result; | ||
}, {}); | ||
} | ||
resultCollectionKeys = Object.keys(resultCollection); | ||
|
||
return getAllKeys().then((persistedKeys) => { | ||
const mutableCollection: OnyxInputKeyValueMapping = {...resultCollection}; | ||
const existingKeys = resultCollectionKeys.filter((key) => persistedKeys.has(key)); | ||
const previousCollection = getCachedCollection(collectionKey, existingKeys); | ||
const keyValuePairs = prepareKeyValuePairsForStorage(mutableCollection, true); | ||
|
||
keyValuePairs.forEach(([key, value]) => cache.set(key, value)); | ||
|
||
const updatePromise = scheduleNotifyCollectionSubscribers(collectionKey, mutableCollection, previousCollection); | ||
|
||
return Storage.multiSet(keyValuePairs) | ||
.catch((error) => evictStorageAndRetry(error, partialSetCollection, collectionKey, collection)) | ||
.then(() => { | ||
sendActionToDevTools(METHOD.SET_COLLECTION, undefined, mutableCollection); | ||
return updatePromise; | ||
}); | ||
}); | ||
} | ||
|
||
function logKeyChanged(onyxMethod: Extract<OnyxMethod, 'set' | 'merge'>, key: OnyxKey, value: unknown, hasChanged: boolean) { | ||
Logger.logInfo(`${onyxMethod} called for key: ${key}${_.isObject(value) ? ` properties: ${_.keys(value).join(',')}` : ''} hasChanged: ${hasChanged}`); | ||
} | ||
|
@@ -1686,6 +1740,7 @@ const OnyxUtils = { | |
reduceCollectionWithSelector, | ||
updateSnapshots, | ||
mergeCollectionWithPatches, | ||
partialSetCollection, | ||
logKeyChanged, | ||
logKeyRemoved, | ||
}; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,8 @@ import Onyx from '../../lib'; | |
import OnyxUtils from '../../lib/OnyxUtils'; | ||
import type {GenericDeepRecord} from '../types'; | ||
import utils from '../../lib/utils'; | ||
import type {Collection} from '../../lib/types'; | ||
import type {Collection, OnyxCollection} from '../../lib/types'; | ||
import type GenericCollection from '../utils/GenericCollection'; | ||
|
||
const testObject: GenericDeepRecord = { | ||
a: 'a', | ||
|
@@ -71,6 +72,7 @@ const ONYXKEYS = { | |
TEST_KEY: 'test_', | ||
TEST_LEVEL_KEY: 'test_level_', | ||
TEST_LEVEL_LAST_KEY: 'test_level_last_', | ||
ROUTES: 'routes_', | ||
}, | ||
}; | ||
|
||
|
@@ -123,6 +125,102 @@ describe('OnyxUtils', () => { | |
}); | ||
}); | ||
|
||
describe('partialSetCollection', () => { | ||
beforeEach(() => { | ||
Onyx.clear(); | ||
}); | ||
|
||
afterEach(() => { | ||
Onyx.clear(); | ||
}); | ||
it('should replace all existing collection members with new values and keep old ones intact', async () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This tests does not to match with function description.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you think should happen here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oops, I just saw that "not be removed". |
||
let result: OnyxCollection<unknown>; | ||
const routeA = `${ONYXKEYS.COLLECTION.ROUTES}A`; | ||
const routeB = `${ONYXKEYS.COLLECTION.ROUTES}B`; | ||
const routeB1 = `${ONYXKEYS.COLLECTION.ROUTES}B1`; | ||
const routeC = `${ONYXKEYS.COLLECTION.ROUTES}C`; | ||
|
||
const connection = Onyx.connect({ | ||
key: ONYXKEYS.COLLECTION.ROUTES, | ||
initWithStoredValues: false, | ||
callback: (value) => (result = value), | ||
waitForCollectionCallback: true, | ||
}); | ||
|
||
// Set initial collection state | ||
await Onyx.setCollection(ONYXKEYS.COLLECTION.ROUTES, { | ||
[routeA]: {name: 'Route A'}, | ||
[routeB1]: {name: 'Route B1'}, | ||
[routeC]: {name: 'Route C'}, | ||
} as GenericCollection); | ||
|
||
// Replace with new collection data | ||
await OnyxUtils.partialSetCollection(ONYXKEYS.COLLECTION.ROUTES, { | ||
[routeA]: {name: 'New Route A'}, | ||
[routeB]: {name: 'New Route B'}, | ||
[routeC]: {name: 'New Route C'}, | ||
} as GenericCollection); | ||
|
||
expect(result).toEqual({ | ||
[routeA]: {name: 'New Route A'}, | ||
[routeB]: {name: 'New Route B'}, | ||
[routeB1]: {name: 'Route B1'}, | ||
[routeC]: {name: 'New Route C'}, | ||
}); | ||
await Onyx.disconnect(connection); | ||
}); | ||
|
||
it('should not replace anything in the collection with empty values', async () => { | ||
let result: OnyxCollection<unknown>; | ||
const routeA = `${ONYXKEYS.COLLECTION.ROUTES}A`; | ||
|
||
const connection = Onyx.connect({ | ||
key: ONYXKEYS.COLLECTION.ROUTES, | ||
initWithStoredValues: false, | ||
callback: (value) => (result = value), | ||
waitForCollectionCallback: true, | ||
}); | ||
|
||
await Onyx.mergeCollection(ONYXKEYS.COLLECTION.ROUTES, { | ||
[routeA]: {name: 'Route A'}, | ||
} as GenericCollection); | ||
|
||
await OnyxUtils.partialSetCollection(ONYXKEYS.COLLECTION.ROUTES, {} as GenericCollection); | ||
|
||
expect(result).toEqual({ | ||
[routeA]: {name: 'Route A'}, | ||
}); | ||
await Onyx.disconnect(connection); | ||
}); | ||
|
||
it('should reject collection items with invalid keys', async () => { | ||
let result: OnyxCollection<unknown>; | ||
const routeA = `${ONYXKEYS.COLLECTION.ROUTES}A`; | ||
const invalidRoute = 'invalid_route'; | ||
|
||
const connection = Onyx.connect({ | ||
key: ONYXKEYS.COLLECTION.ROUTES, | ||
initWithStoredValues: false, | ||
callback: (value) => (result = value), | ||
waitForCollectionCallback: true, | ||
}); | ||
|
||
await Onyx.mergeCollection(ONYXKEYS.COLLECTION.ROUTES, { | ||
[routeA]: {name: 'Route A'}, | ||
} as GenericCollection); | ||
|
||
await OnyxUtils.partialSetCollection(ONYXKEYS.COLLECTION.ROUTES, { | ||
[invalidRoute]: {name: 'Invalid Route'}, | ||
} as GenericCollection); | ||
|
||
expect(result).toEqual({ | ||
[routeA]: {name: 'Route A'}, | ||
}); | ||
|
||
await Onyx.disconnect(connection); | ||
}); | ||
}); | ||
|
||
describe('keysChanged', () => { | ||
beforeEach(() => { | ||
Onyx.clear(); | ||
|
Uh oh!
There was an error while loading. Please reload this page.