From 6c6303f0fc39ffcec719819faf1c70ff538fc29c Mon Sep 17 00:00:00 2001 From: Marc Glasser Date: Thu, 17 Aug 2023 08:12:37 -1000 Subject: [PATCH] Add and improve a comment --- lib/Onyx.js | 2 +- lib/withOnyx.js | 13 ++++++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/Onyx.js b/lib/Onyx.js index 6e66250a..5d04c96d 100644 --- a/lib/Onyx.js +++ b/lib/Onyx.js @@ -901,7 +901,6 @@ function broadcastUpdate(key, value, hasChanged, method) { } /** - * @private * @param {String} key * @returns {Boolean} */ @@ -1409,6 +1408,7 @@ const Onyx = { METHOD, setMemoryOnlyKeys, tryGetCachedValue, + hasPendingMergeForKey, }; /** diff --git a/lib/withOnyx.js b/lib/withOnyx.js index 6ec0dcf0..a250e025 100644 --- a/lib/withOnyx.js +++ b/lib/withOnyx.js @@ -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; }