Skip to content
Merged
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
5 changes: 5 additions & 0 deletions lib/Onyx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,11 @@ function multiSet(data: OnyxMultiSetInput): Promise<void> {

const updatePromises = keyValuePairsToSet.map(([key, value]) => {
const prevValue = cache.get(key, false);
// When we use multiSet to set a key we want to clear the current delta changes from Onyx.merge that were queued
// before the value was set. If Onyx.merge is currently reading the old value from storage, it will then not apply the changes.
if (OnyxUtils.hasPendingMergeForKey(key)) {
delete OnyxUtils.getMergeQueue()[key];
}

// Update cache and optimistically inform subscribers on the next tick
cache.set(key, value);
Expand Down
20 changes: 20 additions & 0 deletions tests/unit/onyxTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2029,5 +2029,25 @@ describe('Onyx', () => {
[`${ONYX_KEYS.COLLECTION.TEST_KEY}entry2`]: {id: 'entry2_id', name: 'entry2_name'},
});
});
it('should clear pending merge for a key during multiSet()', async () => {
const testKey = `${ONYX_KEYS.COLLECTION.TEST_KEY}entry1`;

// Mock the merge queue with the correct type
const mockMergeQueue: Record<string, unknown[]> = {
[testKey]: [{some: 'mergeData'}],
};

// Mock the utility functions
jest.spyOn(OnyxUtils, 'hasPendingMergeForKey').mockImplementation((key) => key === testKey);
jest.spyOn(OnyxUtils, 'getMergeQueue').mockImplementation(() => mockMergeQueue);

await Onyx.multiSet({
[testKey]: {id: 'entry1_id', name: 'entry1_name'},
});

expect(mockMergeQueue[testKey]).toBeUndefined();

jest.restoreAllMocks();
});
});
});