Skip to content

Commit

Permalink
Merge pull request #303 from Expensify/marcaaron-skipCacheWhileMergeP…
Browse files Browse the repository at this point in the history
…ending

Only use cache value when merge is not pending for key
  • Loading branch information
marcaaron authored Aug 17, 2023
2 parents a626324 + 6c6303f commit 09391ce
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/Onyx.js
Original file line number Diff line number Diff line change
Expand Up @@ -901,7 +901,6 @@ function broadcastUpdate(key, value, hasChanged, method) {
}

/**
* @private
* @param {String} key
* @returns {Boolean}
*/
Expand Down Expand Up @@ -1409,6 +1408,7 @@ const Onyx = {
METHOD,
setMemoryOnlyKeys,
tryGetCachedValue,
hasPendingMergeForKey,
};

/**
Expand Down
13 changes: 12 additions & 1 deletion lib/withOnyx.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,18 @@ export default function (mapOnyxToState) {
const key = Str.result(mapping.key, props);
const value = Onyx.tryGetCachedValue(key, mapping);

if (value !== undefined) {
/**
* If we have a pending merge for a key it could mean that data is being set via Onyx.merge() and someone expects a component to have this data immediately.
*
* @example
*
* Onyx.merge('report_123', value);
* Navigation.navigate(route); // Where "route" expects the "value" to be available immediately once rendered.
*
* In reality, Onyx.merge() will only update the subscriber after all merges have been batched and the previous value is retrieved via a get() (returns a promise).
* So, we won't use the cache optimization here as it will lead us to arbitrarily defer various actions in the application code.
*/
if (value !== undefined && !Onyx.hasPendingMergeForKey(key)) {
// eslint-disable-next-line no-param-reassign
resultObj[propertyName] = value;
}
Expand Down

0 comments on commit 09391ce

Please sign in to comment.