Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/Onyx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -750,7 +750,7 @@ function exportState(): Promise<Record<OnyxKey, OnyxValue<OnyxKey>>> {
* @returns The current value, or `undefined` if the key has none.
*/
function get<TKey extends OnyxKey>(key: TKey): Promise<OnyxValue<TKey>> {
return OnyxUtils.afterInit(() => Promise.resolve((onyxSubscriptionManager.getState(key) ?? undefined) as OnyxValue<TKey>));
return OnyxUtils.afterInit(() => Promise.resolve(onyxSubscriptionManager.getState(key) as OnyxValue<TKey>));
}

/**
Expand All @@ -772,7 +772,7 @@ function get<TKey extends OnyxKey>(key: TKey): Promise<OnyxValue<TKey>> {
*/
function multiGet<const Keys extends readonly OnyxKey[]>(keys: Keys): Promise<{[Index in keyof Keys]: OnyxValue<Keys[Index]>}> {
// 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<Keys[Index]>}));
return OnyxUtils.afterInit(() => Promise.resolve(keys.map((key) => onyxSubscriptionManager.getState(key)) as {[Index in keyof Keys]: OnyxValue<Keys[Index]>}));
}

const Onyx = {
Expand Down
4 changes: 2 additions & 2 deletions lib/OnyxSubscriptionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type GenericListener = (value: unknown, key: OnyxKey) => void;
const reportedCacheMisses = new Set<OnyxKey>();

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;
}

Expand All @@ -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;
}

Expand Down
15 changes: 15 additions & 0 deletions tests/unit/OnyxSubscriptionManagerTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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);
Expand Down
Loading