Skip to content

Commit 5764771

Browse files
authored
Merge pull request Expensify#626 from nkdengineer/fix/53265
fix: snapshot data is updated after the loading state
2 parents 0a3c5fc + 2f06807 commit 5764771

2 files changed

Lines changed: 10 additions & 6 deletions

File tree

lib/Onyx.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -622,7 +622,7 @@ function clear(keysToPreserve: OnyxKey[] = []): Promise<void> {
622622

623623
function updateSnapshots(data: OnyxUpdate[]) {
624624
const snapshotCollectionKey = OnyxUtils.getSnapshotKey();
625-
if (!snapshotCollectionKey) return;
625+
if (!snapshotCollectionKey) return [];
626626

627627
const promises: Array<() => Promise<void>> = [];
628628

@@ -676,7 +676,7 @@ function updateSnapshots(data: OnyxUpdate[]) {
676676
promises.push(() => merge(snapshotKey, {data: updatedData}));
677677
});
678678

679-
return Promise.all(promises.map((p) => p()));
679+
return promises;
680680
}
681681

682682
/**
@@ -804,10 +804,12 @@ function update(data: OnyxUpdate[]): Promise<void> {
804804
}
805805
});
806806

807-
return clearPromise
808-
.then(() => Promise.all(promises.map((p) => p())))
809-
.then(() => updateSnapshots(data))
810-
.then(() => undefined);
807+
const snapshotPromises = updateSnapshots(data);
808+
809+
// We need to run the snapshot updates before the other updates so the snapshot data can be updated before the loading state in the snapshot
810+
const finalPromises = snapshotPromises.concat(promises);
811+
812+
return clearPromise.then(() => Promise.all(finalPromises.map((p) => p()))).then(() => undefined);
811813
}
812814

813815
/**

tests/unit/onyxTest.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1460,6 +1460,8 @@ describe('Onyx', () => {
14601460
callback,
14611461
});
14621462

1463+
await waitForPromisesToResolve();
1464+
14631465
await Onyx.update([{key: cat, value: finalValue, onyxMethod: Onyx.METHOD.MERGE}]);
14641466

14651467
expect(callback).toBeCalledTimes(2);

0 commit comments

Comments
 (0)