-
Notifications
You must be signed in to change notification settings - Fork 74
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
add setCollection method #594
Changes from 1 commit
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 |
---|---|---|
|
@@ -725,6 +725,56 @@ function update(data: OnyxUpdate[]): Promise<void> { | |
.then(() => undefined); | ||
} | ||
|
||
/** | ||
* Sets a collection by replacing all existing collection members with new values. | ||
* Any existing collection members not included in the new data will be removed. | ||
* | ||
* @example | ||
* Onyx.setCollection(ONYXKEYS.COLLECTION.REPORT, { | ||
* [`${ONYXKEYS.COLLECTION.REPORT}1`]: report1, | ||
* [`${ONYXKEYS.COLLECTION.REPORT}2`]: report2, | ||
* }); | ||
* | ||
* @param collectionKey e.g. `ONYXKEYS.COLLECTION.REPORT` | ||
* @param collection Object collection keyed by individual collection member keys and values | ||
*/ | ||
function setCollection<TKey extends CollectionKeyBase, TMap>(collectionKey: TKey, collection: OnyxMergeCollectionInput<TKey, TMap>): Promise<void> { | ||
if (!OnyxUtils.isValidNonEmptyCollectionForMerge(collection)) { | ||
Logger.logInfo('setCollection() called with invalid or empty value. Skipping this update.'); | ||
return Promise.resolve(); | ||
} | ||
|
||
const newCollectionKeys = Object.keys(collection); | ||
if (!OnyxUtils.doAllCollectionItemsBelongToSameParent(collectionKey, newCollectionKeys)) { | ||
return Promise.resolve(); | ||
zirgulis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
return OnyxUtils.getAllKeys().then((persistedKeys) => { | ||
// Find existing collection members | ||
const existingCollectionKeys = Array.from(persistedKeys).filter((key) => key.startsWith(collectionKey)); | ||
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. Isn't this creating a potentially huge array from the set only to select a few entries from it and discard the set? Why not iterate the set and get the entries we want, reducing the memory footprint? |
||
|
||
// Create removal object with null values for keys to be removed | ||
const removalCollection = existingCollectionKeys | ||
.filter((key) => !newCollectionKeys.includes(key)) | ||
.reduce( | ||
(obj, key) => ({ | ||
...obj, | ||
[key]: null, | ||
}), | ||
{}, | ||
); | ||
|
||
// Combine removals with new collection data | ||
const finalCollection = { | ||
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. Similarly here, isn't this creating a new variable with the combination of both |
||
...removalCollection, | ||
...collection, | ||
}; | ||
|
||
// Use multiSet to handle all updates atomically | ||
return multiSet(finalCollection); | ||
}); | ||
} | ||
|
||
const Onyx = { | ||
METHOD: OnyxUtils.METHOD, | ||
connect, | ||
|
@@ -733,6 +783,7 @@ const Onyx = { | |
multiSet, | ||
merge, | ||
mergeCollection, | ||
setCollection, | ||
update, | ||
clear, | ||
init, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's totally valid to set a collection to empty if you want to clear it, not sure why we would prevent that