Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make API.read wait on all requests on the SequentialQueue to respond #12219

Merged
merged 27 commits into from
Nov 7, 2022
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
0bc6d78
call process/finally before makeRequestWithSideEffects
jasperhuangg Oct 28, 2022
99aa8e8
whitespace
jasperhuangg Oct 28, 2022
2b4577d
Merge branch 'main' of github.com:Expensify/App into jasper-readPromi…
jasperhuangg Oct 31, 2022
dca802b
wip
jasperhuangg Oct 31, 2022
38553d8
wip, almost working
jasperhuangg Oct 31, 2022
826e2ea
working!
jasperhuangg Oct 31, 2022
a7bd2b8
remove unused and fix style
jasperhuangg Oct 31, 2022
e84e2f5
whitespace
jasperhuangg Oct 31, 2022
8e0453d
remove unused
jasperhuangg Oct 31, 2022
6de781d
only use the isDonePromise if the SequentialQueue is running
jasperhuangg Oct 31, 2022
4714865
fix early return to be more readable
jasperhuangg Oct 31, 2022
e03b3ee
whitespace
jasperhuangg Oct 31, 2022
0052403
remove unused
jasperhuangg Oct 31, 2022
417e5fe
use single promise
jasperhuangg Nov 2, 2022
43e7635
use single promise
jasperhuangg Nov 2, 2022
306185f
remove unused
jasperhuangg Nov 2, 2022
f3f69ad
comment
jasperhuangg Nov 2, 2022
7bc24e0
Merge branch 'main' of github.com:Expensify/App into jasper-readPromi…
jasperhuangg Nov 2, 2022
132add7
rename function
jasperhuangg Nov 3, 2022
4c48dbf
directly use promise
jasperhuangg Nov 3, 2022
0984812
revert unneeded
jasperhuangg Nov 3, 2022
42e2912
Delete WorkspaceSettings.xcsettings
jasperhuangg Nov 3, 2022
2cb1fa6
Delete contents.xcworkspacedata
jasperhuangg Nov 3, 2022
c653d1a
make comment more succinct
jasperhuangg Nov 3, 2022
a7e3e6c
Merge branch 'jasper-readPromiseSeqQueue' of github.com:Expensify/App…
jasperhuangg Nov 3, 2022
0885f91
Update src/libs/Network/SequentialQueue.js
jasperhuangg Nov 3, 2022
a656dae
Merge branch 'main' of github.com:Expensify/App into jasper-readPromi…
jasperhuangg Nov 3, 2022
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
2 changes: 1 addition & 1 deletion src/libs/API.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ 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);
SequentialQueue.getIsDonePromise().then(() => makeRequestWithSideEffects(command, apiCommandParameters, onyxData, CONST.API_REQUEST_TYPE.READ));
}

export {
Expand Down
29 changes: 29 additions & 0 deletions src/libs/Network/SequentialQueue.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ let isSequentialQueueRunning = false;

let currentRequest = null;

let resolveIsDonePromise;
let isDonePromise = new Promise((resolve) => {
jasperhuangg marked this conversation as resolved.
Show resolved Hide resolved
resolveIsDonePromise = resolve;
});

let pendingRequests = [];

/**
* This method will get any persisted requests and fire them off in sequence to retry them.
*
Expand All @@ -33,6 +40,7 @@ function process() {

const task = _.reduce(persistedRequests, (previousRequest, request) => previousRequest.then(() => {
currentRequest = Request.processWithMiddleware(request, true);
pendingRequests.push(currentRequest);
jasperhuangg marked this conversation as resolved.
Show resolved Hide resolved
return currentRequest;
}), Promise.resolve());

Expand Down Expand Up @@ -68,6 +76,19 @@ function flush() {
isSequentialQueueRunning = false;
resolveIsReadyPromise();
currentRequest = null;

// Resolve the isDonePromise once all the pending requests complete,
// and reset the pendingRequests array
Promise.all(pendingRequests).then(() => {
resolveIsDonePromise();
pendingRequests = [];
});
}).then(() => {
// Reset the isDonePromise so we can use it again on the next run,
// this has to be done after the finally runs otherwise it'll have no effect
isDonePromise = new Promise((resolve) => {
resolveIsDonePromise = resolve;
});
jasperhuangg marked this conversation as resolved.
Show resolved Hide resolved
});
},
});
Expand Down Expand Up @@ -114,9 +135,17 @@ function getCurrentRequest() {
return currentRequest;
}

function getIsDonePromise() {
if (!isSequentialQueueRunning) {
return Promise.resolve();
}
jasperhuangg marked this conversation as resolved.
Show resolved Hide resolved
return isDonePromise;
}

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