diff --git a/lib/Onyx.ts b/lib/Onyx.ts index e5df45366..9db652e3f 100644 --- a/lib/Onyx.ts +++ b/lib/Onyx.ts @@ -750,7 +750,7 @@ function exportState(): Promise>> { * @returns The current value, or `undefined` if the key has none. */ function get(key: TKey): Promise> { - return OnyxUtils.afterInit(() => Promise.resolve((onyxSubscriptionManager.getState(key) ?? undefined) as OnyxValue)); + return OnyxUtils.afterInit(() => Promise.resolve(onyxSubscriptionManager.getState(key) as OnyxValue)); } /** @@ -772,7 +772,7 @@ function get(key: TKey): Promise> { */ function multiGet(keys: Keys): Promise<{[Index in keyof Keys]: OnyxValue}> { // map() widens the key tuple to an array, so the per-slot value types survive only through the cast. - return OnyxUtils.afterInit(() => Promise.resolve(keys.map((key) => onyxSubscriptionManager.getState(key) ?? undefined) as {[Index in keyof Keys]: OnyxValue})); + return OnyxUtils.afterInit(() => Promise.resolve(keys.map((key) => onyxSubscriptionManager.getState(key)) as {[Index in keyof Keys]: OnyxValue})); } const Onyx = { diff --git a/lib/OnyxSubscriptionManager.ts b/lib/OnyxSubscriptionManager.ts index d2bb3cc7a..e4dfbc1df 100644 --- a/lib/OnyxSubscriptionManager.ts +++ b/lib/OnyxSubscriptionManager.ts @@ -19,7 +19,7 @@ type GenericListener = (value: unknown, key: OnyxKey) => void; const reportedCacheMisses = new Set(); function reportCacheMissIfStorageHasValue(key: OnyxKey): void { - if (OnyxKeys.isRamOnlyKey(key) || !cache.getAllKeys().has(key) || cache.hasNullishStorageKey(key)) { + if (!cache.getAllKeys().has(key) || OnyxKeys.isRamOnlyKey(key) || cache.hasNullishStorageKey(key)) { return; } @@ -31,7 +31,7 @@ function reportCacheMissIfStorageHasValue(key: OnyxKey): void { Storage.getItem(key) .then((value) => { - if (value === undefined || value === null) { + if (value === undefined || value === null || cache.get(key) !== undefined) { return; } diff --git a/tests/unit/OnyxSubscriptionManagerTest.ts b/tests/unit/OnyxSubscriptionManagerTest.ts index e892e412b..0dd2b9ffc 100644 --- a/tests/unit/OnyxSubscriptionManagerTest.ts +++ b/tests/unit/OnyxSubscriptionManagerTest.ts @@ -131,6 +131,10 @@ describe('OnyxSubscriptionManager', () => { expect(onyxSubscriptionManager.getState(COLLECTION)).toBe(collectionData); }); + + it('should return undefined without throwing for an undefined key', () => { + expect(onyxSubscriptionManager.getState(undefined as unknown as OnyxKey)).toBeUndefined(); + }); }); describe('collection routing on notifyKey', () => { @@ -397,6 +401,17 @@ describe('OnyxSubscriptionManager', () => { expect(logHmmmSpy).not.toHaveBeenCalled(); }); + it('should not report when the value reached the cache before storage answered', async () => { + await StorageMock.setItem(ONYXKEYS.TEST_KEY, 'from-storage'); + forgetValueButKeepKey(ONYXKEYS.TEST_KEY); + + onyxSubscriptionManager.getState(ONYXKEYS.TEST_KEY); + cache.set(ONYXKEYS.TEST_KEY, 'from-storage'); + await waitForPromisesToResolve(); + + expect(logHmmmSpy).not.toHaveBeenCalled(); + }); + it('should not report a key the index does not know, which means init has not loaded it', async () => { await StorageMock.setItem(ONYXKEYS.TEST_KEY, 'from-storage'); cache.drop(ONYXKEYS.TEST_KEY);