diff --git a/lib/Onyx.ts b/lib/Onyx.ts index 4503744d2..46170cdbf 100644 --- a/lib/Onyx.ts +++ b/lib/Onyx.ts @@ -238,6 +238,11 @@ function multiSet(data: OnyxMultiSetInput): Promise { 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); diff --git a/tests/unit/onyxTest.ts b/tests/unit/onyxTest.ts index 1bca6b377..dda151511 100644 --- a/tests/unit/onyxTest.ts +++ b/tests/unit/onyxTest.ts @@ -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 = { + [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(); + }); }); });