Skip to content
Draft
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
6 changes: 6 additions & 0 deletions lib/Onyx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ function init({

cache.setRamOnlyKeys(new Set<OnyxKey>(ramOnlyKeys));

// Remove any previously persisted values for keys that are now RAM-only.
// This handles the case where a key was once a regular key and later changed to RAM-only.
if (ramOnlyKeys.length > 0) {
Storage.removeItems(ramOnlyKeys);
}
Comment on lines +62 to +64
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it safe to execute this asynchoronous in Onyx.init?


if (shouldSyncMultipleInstances) {
Storage.keepInstancesSync?.((key, value) => {
cache.set(key, value);
Expand Down
48 changes: 48 additions & 0 deletions tests/unit/onyxTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3023,6 +3023,54 @@ describe('Onyx.init', () => {
return Onyx.clear();
});

describe('should remove RAM-only keys from storage during init', () => {
it('should remove a previously persisted key from storage when it becomes RAM-only', async () => {
// Simulate a key that was previously persisted in storage as a regular key
await StorageMock.setItem(ONYX_KEYS.RAM_ONLY_TEST_KEY, 'previously persisted value');
expect(await StorageMock.getItem(ONYX_KEYS.RAM_ONLY_TEST_KEY)).toEqual('previously persisted value');

Onyx.init({
keys: ONYX_KEYS,
ramOnlyKeys: [ONYX_KEYS.RAM_ONLY_TEST_KEY],
});
await act(async () => waitForPromisesToResolve());

// The key should have been removed from storage
expect(await StorageMock.getItem(ONYX_KEYS.RAM_ONLY_TEST_KEY)).toBeNull();
});

it('should remove multiple previously persisted keys from storage when they become RAM-only', async () => {
const collectionMemberKey = `${ONYX_KEYS.COLLECTION.RAM_ONLY_COLLECTION}1`;

// Simulate multiple keys that were previously persisted in storage
await StorageMock.setItem(ONYX_KEYS.RAM_ONLY_TEST_KEY, 'old value 1');
await StorageMock.setItem(collectionMemberKey, 'old value 2');
expect(await StorageMock.getItem(ONYX_KEYS.RAM_ONLY_TEST_KEY)).toEqual('old value 1');
expect(await StorageMock.getItem(collectionMemberKey)).toEqual('old value 2');

Onyx.init({
keys: ONYX_KEYS,
ramOnlyKeys: [ONYX_KEYS.RAM_ONLY_TEST_KEY, ONYX_KEYS.COLLECTION.RAM_ONLY_COLLECTION, collectionMemberKey],
});
await act(async () => waitForPromisesToResolve());

expect(await StorageMock.getItem(ONYX_KEYS.RAM_ONLY_TEST_KEY)).toBeNull();
expect(await StorageMock.getItem(collectionMemberKey)).toBeNull();
});

it('should not call removeItems when there are no RAM-only keys', async () => {
jest.spyOn(StorageMock, 'removeItems').mockClear();

Onyx.init({
keys: ONYX_KEYS,
ramOnlyKeys: [],
});
await act(async () => waitForPromisesToResolve());

expect(StorageMock.removeItems).not.toHaveBeenCalled();
});
});

describe('should only execute Onyx methods after initialization', () => {
it('set', async () => {
Onyx.set(ONYX_KEYS.TEST_KEY, 'test');
Expand Down