Skip to content

Commit

Permalink
Merge pull request #12219 from Expensify/jasper-readPromiseSeqQueue
Browse files Browse the repository at this point in the history
Make `API.read` wait on all requests on the SequentialQueue to respond
  • Loading branch information
tgolen authored Nov 7, 2022
2 parents 391cd5f + a656dae commit e5281fc
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/libs/API.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,10 @@ function makeRequestWithSideEffects(command, apiCommandParameters = {}, onyxData
* @param {Object} [onyxData.failureData] - Onyx instructions that will be passed to Onyx.update() when the response has jsonCode !== 200.
*/
function read(command, apiCommandParameters, onyxData) {
makeRequestWithSideEffects(command, apiCommandParameters, onyxData, CONST.API_REQUEST_TYPE.READ);
// Ensure all write requests on the sequential queue have finished responding before running read requests.
// Responses from read requests can overwrite the optimistic data inserted by
// write requests that use the same Onyx keys and haven't responded yet.
SequentialQueue.waitForIdle().then(() => makeRequestWithSideEffects(command, apiCommandParameters, onyxData, CONST.API_REQUEST_TYPE.READ));
}

export {
Expand Down
9 changes: 9 additions & 0 deletions src/libs/Network/SequentialQueue.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,18 @@ function getCurrentRequest() {
return currentRequest;
}

/**
* Returns a promise that resolves when the sequential queue is done processing all persisted write requests.
* @returns {Promise}
*/
function waitForIdle() {
return isReadyPromise;
}

export {
flush,
getCurrentRequest,
isRunning,
push,
waitForIdle,
};

0 comments on commit e5281fc

Please sign in to comment.