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

Simplify the isReady state for the sequential queue #9133

Merged
merged 1 commit into from
May 23, 2022
Merged
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
21 changes: 14 additions & 7 deletions src/libs/Network/SequentialQueue.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ import * as NetworkStore from './NetworkStore';
import ONYXKEYS from '../../ONYXKEYS';
import * as ActiveClientManager from '../ActiveClientManager';
import * as Request from '../Request';
import createOnReadyTask from '../createOnReadyTask';

// Create a deferred task to hook into and immediately set it as ready
const queueProcessTask = createOnReadyTask();
queueProcessTask.setIsReady();
let resolveIsReadyPromise;
let isReadyPromise = new Promise((resolve) => {
resolveIsReadyPromise = resolve;
});

// Resolve the isReadyPromise immediately so that the queue starts working as soon as the page loads
resolveIsReadyPromise();

let isSequentialQueueRunning = false;

Expand Down Expand Up @@ -44,7 +47,11 @@ function flush() {
}

isSequentialQueueRunning = true;
queueProcessTask.reset();

// Reset the isReadyPromise so that the queue will be flushed as soon as the request is finished
isReadyPromise = new Promise((resolve) => {
resolveIsReadyPromise = resolve;
});

// Ensure persistedRequests are read from storage before proceeding with the queue
const connectionID = Onyx.connect({
Expand All @@ -54,7 +61,7 @@ function flush() {
process()
.finally(() => {
isSequentialQueueRunning = false;
queueProcessTask.setIsReady();
resolveIsReadyPromise();
});
},
});
Expand Down Expand Up @@ -84,7 +91,7 @@ function push(request) {

// If the queue is running this request will run once it has finished processing the current batch
if (isSequentialQueueRunning) {
queueProcessTask.isReady().then(flush);
isReadyPromise.then(flush);
return;
}

Expand Down