Skip to content

Commit 1764d2c

Browse files
committed
Address PR #793 review comments
- Simplify hasCacheForKey comment in multiGet - Drop PR refs (#787) from the pre-warm .catch() comment in mergeCollectionWithPatches; keep the why (preserve cache-first invariant on transient storage read failures) - Add blank line between the warm/cold parity test and the Storage.multiGet-rejects regression test, per Fabio's suggestion - Simplify the Storage.multiGet-rejects test's intro comment by removing the codex review ID and PR number references; keep the what (subscribers must still see the merge) and the why (otherwise Onyx.mergeCollection rejects)
1 parent 7fb447d commit 1764d2c

2 files changed

Lines changed: 9 additions & 14 deletions

File tree

lib/OnyxUtils.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -347,9 +347,8 @@ function multiGet<TKey extends OnyxKey>(keys: CollectionKeyBase[]): Promise<Map<
347347
continue;
348348
}
349349

350-
// Use hasCacheForKey, not a truthy check on the value — otherwise cached falsy values
351-
// (0, '', false, null) get treated as cache misses and re-fetched from storage, which
352-
// can overwrite the warm cached value with a stale storage value via cache.merge().
350+
// hasCacheForKey catches cached falsy values (0, '', false, null) as cache hits, which
351+
// a truthy check on the value would miss.
353352
if (cache.hasCacheForKey(key)) {
354353
dataMap.set(key, cache.get(key) as OnyxValue<TKey>);
355354
continue;
@@ -1611,11 +1610,9 @@ function mergeCollectionWithPatches<TKey extends CollectionKeyBase>(
16111610
// missing keys into a single Storage.multiGet call (vs. N parallel get() invocations) and
16121611
// writes the storage values back to cache before resolving.
16131612
const hasColdExistingKey = existingKeys.some((key) => !cache.hasCacheForKey(key));
1614-
// Swallow pre-warm read failures the same way the previous get()-based pre-warm did
1615-
// (see get() catch at the bottom of its definition). Without this, a transient
1616-
// Storage.multiGet rejection would skip cache.merge() + keysChanged() below and
1617-
// regress the cache-first invariant established in #787 — subscribers would miss
1618-
// the merge and Onyx.mergeCollection would reject up to the caller.
1613+
// Swallow pre-warm read failures so a transient Storage.multiGet rejection doesn't
1614+
// skip the cache.merge() + keysChanged() below. Subscribers still see the merge even
1615+
// when storage reads fail.
16191616
const prewarmPromise = hasColdExistingKey
16201617
? multiGet(existingKeys).catch((err) => Logger.logInfo(`mergeCollectionWithPatches pre-warm failed; proceeding with cache-only merge. Error: ${err}`))
16211618
: Promise.resolve();

tests/unit/onyxUtilsTest.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1089,13 +1089,11 @@ describe('OnyxUtils', () => {
10891089
expect(warmResult).toEqual(coldResult);
10901090
expect(coldResult).toEqual({value: 'after', extra: 'kept'});
10911091
});
1092+
10921093
it('preserves cache-first invariant when Storage.multiGet rejects on the slow path', async () => {
1093-
// Regression for codex review #3302454987 / PR #793. Before the .catch() at the
1094-
// pre-warm call site, a Storage.multiGet rejection would propagate up and skip
1095-
// cache.merge() + keysChanged() entirely — subscribers would miss the merge and
1096-
// Onyx.mergeCollection would reject. The previous get()-based pre-warm swallowed
1097-
// these errors per-key inside get()'s own .catch(), so the cache-first invariant
1098-
// from #787 held even on a flaky read.
1094+
// A Storage.multiGet rejection during pre-warm must not skip the cache.merge() +
1095+
// keysChanged() that follow. Without the .catch() at the pre-warm call site,
1096+
// subscribers would miss the merge and Onyx.mergeCollection would reject.
10991097
const collectionKey = ONYXKEYS.COLLECTION.TEST_KEY;
11001098
const coldMemberKey = `${collectionKey}1`;
11011099
const newMemberKey = `${collectionKey}2`;

0 commit comments

Comments
 (0)