Skip to content

Commit 6301dfd

Browse files
committed
Improve comments in Onyx.merge()
1 parent df65c33 commit 6301dfd

1 file changed

Lines changed: 10 additions & 10 deletions

File tree

lib/Onyx.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -345,28 +345,28 @@ function merge<TKey extends OnyxKey>(key: TKey, changes: OnyxMergeInput<TKey>):
345345
return Promise.resolve();
346346
}
347347

348-
// The "preMergedValue" will be directly "set" in storage instead of being merged, so we merge
349-
// the batched changes with the existing value to get the final merged value that will be stored.
350-
// We can remove null values from the "preMergedValue", because "null" implicates that the user wants to remove a value from storage.
351-
// Additionally, we will signal OnyxUtils.applyMerge() to replace any nested properties previously marked in "batchedDeltaChanges"
352-
// by our custom merging strategy.
353-
const preMergedValue = OnyxUtils.applyMerge(shouldSetValue ? undefined : existingValue, [batchedDeltaChanges]);
348+
// If "shouldSetValue" is true, it means that we want to completely replace the existing value with the batched changes,
349+
// so we pass `undefined` to OnyxUtils.applyMerge() first parameter to make it use "batchedDeltaChanges" to
350+
// create a new object for us.
351+
// If "shouldSetValue" is false, it means that we want to merge the batched changes into the existing value,
352+
// so we pass "existingValue" to the first parameter.
353+
const resultValue = OnyxUtils.applyMerge(shouldSetValue ? undefined : existingValue, [batchedDeltaChanges]);
354354

355355
// In cache, we don't want to remove the key if it's null to improve performance and speed up the next merge.
356-
const hasChanged = cache.hasValueChanged(key, preMergedValue);
356+
const hasChanged = cache.hasValueChanged(key, resultValue);
357357

358358
logMergeCall(hasChanged);
359359

360360
// This approach prioritizes fast UI changes without waiting for data to be stored in device storage.
361-
const updatePromise = OnyxUtils.broadcastUpdate(key, preMergedValue as OnyxValue<TKey>, hasChanged);
361+
const updatePromise = OnyxUtils.broadcastUpdate(key, resultValue as OnyxValue<TKey>, hasChanged);
362362

363363
// If the value has not changed, calling Storage.setItem() would be redundant and a waste of performance, so return early instead.
364364
if (!hasChanged) {
365365
return updatePromise;
366366
}
367367

368-
return Storage.mergeItem(key, preMergedValue as OnyxValue<TKey>).then(() => {
369-
OnyxUtils.sendActionToDevTools(OnyxUtils.METHOD.MERGE, key, changes, preMergedValue);
368+
return Storage.mergeItem(key, resultValue as OnyxValue<TKey>).then(() => {
369+
OnyxUtils.sendActionToDevTools(OnyxUtils.METHOD.MERGE, key, changes, resultValue);
370370
return updatePromise;
371371
});
372372
} catch (error) {

0 commit comments

Comments
 (0)