Skip to content
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

fix: avoid using multiSet in setCollection #609

Merged
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
14 changes: 13 additions & 1 deletion lib/Onyx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -767,7 +767,19 @@ function setCollection<TKey extends CollectionKeyBase, TMap extends string>(coll
mutableCollection[key] = null;
});

return multiSet(mutableCollection);
const keyValuePairs = OnyxUtils.prepareKeyValuePairsForStorage(mutableCollection, true);
const previousCollection = OnyxUtils.getCachedCollection(collectionKey);

keyValuePairs.forEach(([key, value]) => cache.set(key, value));

const updatePromise = OnyxUtils.scheduleNotifyCollectionSubscribers(collectionKey, mutableCollection, previousCollection);

return Storage.multiSet(keyValuePairs)
.catch((error) => OnyxUtils.evictStorageAndRetry(error, setCollection, collectionKey, collection))
.then(() => {
OnyxUtils.sendActionToDevTools(OnyxUtils.METHOD.SET_COLLECTION, undefined, mutableCollection);
return updatePromise;
});
});
}

Expand Down
6 changes: 3 additions & 3 deletions lib/OnyxUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,13 +165,13 @@ function initStoreValues(keys: DeepRecord<string, OnyxKey>, initialKeyStates: Pa
* @param mergedValue - (optional) value that was written in the storage after a merge method was executed.
*/
function sendActionToDevTools(
method: typeof METHOD.MERGE_COLLECTION | typeof METHOD.MULTI_SET,
method: typeof METHOD.MERGE_COLLECTION | typeof METHOD.MULTI_SET | typeof METHOD.SET_COLLECTION,
key: undefined,
value: OnyxCollection<KeyValueMapping[OnyxKey]>,
mergedValue?: undefined,
): void;
function sendActionToDevTools(
method: Exclude<OnyxMethod, typeof METHOD.MERGE_COLLECTION | typeof METHOD.MULTI_SET>,
method: Exclude<OnyxMethod, typeof METHOD.MERGE_COLLECTION | typeof METHOD.MULTI_SET | typeof METHOD.SET_COLLECTION>,
key: OnyxKey,
value: OnyxEntry<KeyValueMapping[OnyxKey]>,
mergedValue?: OnyxEntry<KeyValueMapping[OnyxKey]>,
Expand Down Expand Up @@ -1088,7 +1088,7 @@ function reportStorageQuota(): Promise<void> {
* evicting some data from Onyx and then retrying to do
* whatever it is we attempted to do.
*/
function evictStorageAndRetry<TMethod extends typeof Onyx.set | typeof Onyx.multiSet | typeof Onyx.mergeCollection>(
function evictStorageAndRetry<TMethod extends typeof Onyx.set | typeof Onyx.multiSet | typeof Onyx.mergeCollection | typeof Onyx.setCollection>(
error: Error,
onyxMethod: TMethod,
...args: Parameters<TMethod>
Expand Down
Loading