react-native-onyx 3.0.79 → 3.0.81

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -42,7 +42,7 @@ declare class OnyxSnapshotCache {
42
42
  * Other options like `reuseConnection` don't affect the data transformation
43
43
  * or timing behavior of getSnapshot, so they're excluded from the cache key for better cache hit rates.
44
44
  */
45
- registerConsumer<TKey extends OnyxKey, TReturnValue>(options: Pick<UseOnyxOptions<TKey, TReturnValue>, 'selector'>): string;
45
+ registerConsumer<TKey extends OnyxKey, TReturnValue>(key: TKey, options: Pick<UseOnyxOptions<TKey, TReturnValue>, 'selector'>): string;
46
46
  /**
47
47
  * Deregister a consumer for a cache key.
48
48
  * Decrements reference counter and removes cache entry if no consumers remain.
@@ -39,9 +39,9 @@ class OnyxSnapshotCache {
39
39
  * Other options like `reuseConnection` don't affect the data transformation
40
40
  * or timing behavior of getSnapshot, so they're excluded from the cache key for better cache hit rates.
41
41
  */
42
- registerConsumer(options) {
42
+ registerConsumer(key, options) {
43
43
  const selectorID = (options === null || options === void 0 ? void 0 : options.selector) ? this.getSelectorID(options.selector) : 'no_selector';
44
- const cacheKey = String(selectorID);
44
+ const cacheKey = `${key}_${selectorID}`;
45
45
  // Increment reference count for this cache key
46
46
  const currentCount = this.cacheKeyRefCounts.get(cacheKey) || 0;
47
47
  this.cacheKeyRefCounts.set(cacheKey, currentCount + 1);
package/dist/OnyxUtils.js CHANGED
@@ -279,9 +279,10 @@ function multiGet(keys) {
279
279
  }
280
280
  continue;
281
281
  }
282
- const cacheValue = OnyxCache_1.default.get(key);
283
- if (cacheValue) {
284
- dataMap.set(key, cacheValue);
282
+ // hasCacheForKey catches cached falsy values (0, '', false, null) as cache hits, which
283
+ // a truthy check on the value would miss.
284
+ if (OnyxCache_1.default.hasCacheForKey(key)) {
285
+ dataMap.set(key, OnyxCache_1.default.get(key));
285
286
  continue;
286
287
  }
287
288
  const pendingKey = `${OnyxCache_1.TASK.GET}:${key}`;
@@ -328,6 +329,12 @@ function multiGet(keys) {
328
329
  // The key is not a collection one or something went wrong during split, so we proceed with the function's logic.
329
330
  }
330
331
  }
332
+ // Prefer cache over stale storage if a concurrent write populated it during
333
+ // the read — otherwise cache.merge(temp) below would resurrect dropped fields.
334
+ if (OnyxCache_1.default.hasCacheForKey(key)) {
335
+ dataMap.set(key, OnyxCache_1.default.get(key));
336
+ continue;
337
+ }
331
338
  dataMap.set(key, value);
332
339
  temp[key] = value;
333
340
  }
@@ -1338,12 +1345,17 @@ function mergeCollectionWithPatches({ collectionKey, collection, mergeReplaceNul
1338
1345
  const keyValuePairsForNewCollection = prepareKeyValuePairsForStorage(newCollection, true);
1339
1346
  // finalMergedCollection contains all the keys that were merged, without the keys of incompatible updates
1340
1347
  const finalMergedCollection = Object.assign(Object.assign({}, existingKeyCollection), newCollection);
1341
- // Pre-warm cache for any existing storage keys that aren't yet in cache. get() is a no-op
1342
- // (sync-resolved) for cache hits, and on a cache miss it reads from storage and writes the
1343
- // value back to cache. This is required so the subsequent cache.merge() merges the new delta
1344
- // into the real previous storage value (rather than starting from `undefined` and dropping
1345
- // the existing keys).
1346
- return Promise.all(existingKeys.map((key) => get(key))).then(() => {
1348
+ // Pre-warm cache for cache-miss existingKeys so cache.merge() merges the new delta into
1349
+ // the real previous storage value. Fast path (all warm) skips the pre-warm to preserve
1350
+ // promise-chain depth; slow path batches the misses into one Storage.multiGet.
1351
+ const hasColdExistingKey = existingKeys.some((key) => !OnyxCache_1.default.hasCacheForKey(key));
1352
+ // Swallow pre-warm read failures so a transient Storage.multiGet rejection doesn't
1353
+ // skip the cache.merge() + keysChanged() below. Subscribers still see the merge even
1354
+ // when storage reads fail.
1355
+ const prewarmPromise = hasColdExistingKey
1356
+ ? multiGet(existingKeys).catch((err) => Logger.logInfo(`mergeCollectionWithPatches pre-warm failed; proceeding with cache-only merge. Error: ${err}`))
1357
+ : Promise.resolve();
1358
+ return prewarmPromise.then(() => {
1347
1359
  // Snapshot previous values from the (now-warm) cache for keysChanged's diff, then update
1348
1360
  // cache and notify subscribers synchronously BEFORE issuing storage writes. This matches
1349
1361
  // the cache-first / storage-second invariant followed by every other Onyx write method
package/dist/useOnyx.js CHANGED
@@ -107,9 +107,9 @@ function useOnyx(key, options, dependencies = []) {
107
107
  // Inside useOnyx.ts, we need to track the sourceValue separately
108
108
  const sourceValueRef = (0, react_1.useRef)(undefined);
109
109
  // Cache the options key to avoid regenerating it every getSnapshot call
110
- const cacheKey = (0, react_1.useMemo)(() => OnyxSnapshotCache_1.default.registerConsumer({
110
+ const cacheKey = (0, react_1.useMemo)(() => OnyxSnapshotCache_1.default.registerConsumer(key, {
111
111
  selector: options === null || options === void 0 ? void 0 : options.selector,
112
- }), [options === null || options === void 0 ? void 0 : options.selector]);
112
+ }), [key, options === null || options === void 0 ? void 0 : options.selector]);
113
113
  (0, react_1.useEffect)(() => () => OnyxSnapshotCache_1.default.deregisterConsumer(key, cacheKey), [key, cacheKey]);
114
114
  // Track previous dependencies to prevent infinite loops
115
115
  const previousDependenciesRef = (0, react_1.useRef)([]);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-onyx",
3
- "version": "3.0.79",
3
+ "version": "3.0.81",
4
4
  "author": "Expensify, Inc.",
5
5
  "homepage": "https://expensify.com",
6
6
  "description": "State management for React Native",