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

Defer local updates if there are missing updates and only call GetMissingOnyxMessages once #38997

Merged
Changes from 7 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
d32cd8b
defer updates if missing updates are fetched
chrispader Mar 26, 2024
e41558e
simplify
chrispader Mar 26, 2024
931527c
move deferred updates out of callback
chrispader Mar 26, 2024
5088441
simplify comments
chrispader Mar 26, 2024
9954e40
Merge branch 'main' into @chrispader/prevent-simultaneous-calls-to-Ge…
chrispader Mar 26, 2024
a314f1f
Merge branch 'main' into @chrispader/prevent-simultaneous-calls-to-Ge…
chrispader Mar 26, 2024
9d818d3
fix: apply deferred updates in order
chrispader Mar 26, 2024
f21ea78
simplify and improve applying deferred updates
chrispader Mar 27, 2024
3cc017e
check if deferred updates are older than the currently applied one
chrispader Mar 27, 2024
ce9027c
improve code
chrispader Mar 27, 2024
9889206
Merge branch 'main' into @chrispader/prevent-simultaneous-calls-to-Ge…
chrispader Mar 27, 2024
c3034e0
fix: refetching logic
chrispader Mar 29, 2024
b9afdf3
fix: comments and logic
chrispader Mar 29, 2024
6a5c922
restructure file
chrispader Apr 1, 2024
4f35d1e
fix: re-write to detect possible multiple gaps in the deferred updates
chrispader Apr 1, 2024
095f09e
simplified conditions
chrispader Apr 1, 2024
6c748ac
further improve comments
chrispader Apr 1, 2024
b5deb28
fix: minor problems
chrispader Apr 2, 2024
2d08786
remove unnecessary code
chrispader Apr 2, 2024
25c8623
improve comment
chrispader Apr 2, 2024
7769810
fix: check for queryPromise
chrispader Apr 2, 2024
47613cf
update comment
chrispader Apr 2, 2024
fb9ac39
update comment
chrispader Apr 2, 2024
b9692d1
move lines
chrispader Apr 2, 2024
af920cd
Merge branch 'main' into @chrispader/prevent-simultaneous-calls-to-Ge…
chrispader Apr 2, 2024
2514772
fix: comments
chrispader Apr 2, 2024
dab2b9f
Merge branch 'main' into @chrispader/prevent-simultaneous-calls-to-Ge…
chrispader Apr 2, 2024
c0fe364
fix: wrong early return
chrispader Apr 2, 2024
252f0ab
fix: missing return
chrispader Apr 2, 2024
c1ca79b
Revert "fix: missing return"
chrispader Apr 2, 2024
67f322e
Update OnyxUpdateManager.ts
chrispader Apr 2, 2024
cd48514
Merge branch 'main' into @chrispader/prevent-simultaneous-calls-to-Ge…
chrispader Apr 2, 2024
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
81 changes: 77 additions & 4 deletions src/libs/actions/OnyxUpdateManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Log from '@libs/Log';
import * as SequentialQueue from '@libs/Network/SequentialQueue';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {OnyxUpdatesFromServer, Response} from '@src/types/onyx';
import * as App from './App';
import * as OnyxUpdates from './OnyxUpdates';

Expand All @@ -27,6 +28,9 @@ Onyx.connect({
callback: (value) => (lastUpdateIDAppliedToClient = value),
});

let deferredUpdateBeforeReconnect: OnyxUpdatesFromServer | undefined;
let deferredUpdates: OnyxUpdatesFromServer[] = [];

export default () => {
console.debug('[OnyxUpdateManager] Listening for updates from the server');
Onyx.connect({
Expand Down Expand Up @@ -66,32 +70,101 @@ export default () => {
// applied in their correct and specific order. If this queue was not paused, then there would be a lot of
// onyx data being applied while we are fetching the missing updates and that would put them all out of order.
SequentialQueue.pause();
let canUnpauseQueuePromise;
let canUnpauseQueuePromise: Promise<Response | void>;

// The flow below is setting the promise to a reconnect app to address flow (1) explained above.
if (!lastUpdateIDAppliedToClient) {
Log.info('Client has not gotten reliable updates before so reconnecting the app to start the process');

deferredUpdateBeforeReconnect = updateParams;

// Since this is a full reconnectApp, we'll not apply the updates we received - those will come in the reconnect app request.
canUnpauseQueuePromise = App.finalReconnectAppAfterActivatingReliableUpdates();
} else {
// The flow below is setting the promise to a getMissingOnyxUpdates to address flow (2) explained above.

// Get the number of deferred updates before adding the new one
const existingDeferredUpdatesCount = deferredUpdates.length;
// Add the new update to the deferred updates
chrispader marked this conversation as resolved.
Show resolved Hide resolved
deferredUpdates.push(updateParams);
chrispader marked this conversation as resolved.
Show resolved Hide resolved

// If there are already deferred updates, then we don't need to fetch the missing updates again
if (existingDeferredUpdatesCount > 0) {
return;
}

console.debug(`[OnyxUpdateManager] Client is behind the server by ${Number(previousUpdateIDFromServer) - lastUpdateIDAppliedToClient} so fetching incremental updates`);
Log.info('Gap detected in update IDs from server so fetching incremental updates', true, {
lastUpdateIDFromServer,
previousUpdateIDFromServer,
lastUpdateIDAppliedToClient,
});

// Get the missing Onyx updates from the server
canUnpauseQueuePromise = App.getMissingOnyxUpdates(lastUpdateIDAppliedToClient, previousUpdateIDFromServer);
}

canUnpauseQueuePromise.finally(() => {
OnyxUpdates.apply(updateParams).finally(() => {
// This function will apply the deferred updates in order after the missing updates are fetched
function applyDeferredUpdates() {
// If lastUpdateIDAppliedToClient is null, then no updates were applied,
// therefore something must have gone wrong and we should handle the problem.
if (!lastUpdateIDAppliedToClient) {
// TODO: Handle this case
return;
}

function unpauseQueueAndReset() {
console.debug('[OnyxUpdateManager] Done applying all updates');
Onyx.set(ONYXKEYS.ONYX_UPDATES_FROM_SERVER, null);
SequentialQueue.unpause();
}

// If "updateAfterReconnectApp" is set, it means that case (1) was triggered and we only need to apply the one update, not the deferred ones.
if (deferredUpdateBeforeReconnect) {
OnyxUpdates.apply(deferredUpdateBeforeReconnect).finally(unpauseQueueAndReset);
deferredUpdateBeforeReconnect = undefined;
return;
chrispader marked this conversation as resolved.
Show resolved Hide resolved
}

// If case (2) was triggered, then we need to sort the deferred updates by lastUpdateID and apply them in order.
const sortedDeferredUpdates = deferredUpdates.sort((a, b) => {
const aLastUpdateID = Number(a.lastUpdateID) || 0;
const bLastUpdateID = Number(b.lastUpdateID) || 0;

return aLastUpdateID - bLastUpdateID;
});
chrispader marked this conversation as resolved.
Show resolved Hide resolved
});

// In order for the deferred updates to be applied correctly in order,
// we need to check if there are any gaps deferred updates.
function validateUpdatesAreChained(): boolean {
for (let i = 0; i < sortedDeferredUpdates.length - 1; i++) {
const reversedDeferredUpdates = sortedDeferredUpdates.toReversed();
const currentUpdate = reversedDeferredUpdates[i];
const previousUpdate = reversedDeferredUpdates[i + 1];

if (Number(previousUpdate.lastUpdateID) !== Number(currentUpdate.previousUpdateID)) {
return false;
}
}
chrispader marked this conversation as resolved.
Show resolved Hide resolved

return true;
}

// If we detect a gap in the fetched + deferred updates, re-fetch the missing updates
if (!validateUpdatesAreChained()) {
const lastDeferedUpdateId = Number(sortedDeferredUpdates.at(-1)?.lastUpdateID) ?? 0;

canUnpauseQueuePromise = App.getMissingOnyxUpdates(lastUpdateIDAppliedToClient, lastDeferedUpdateId).finally(applyDeferredUpdates);
return;
}

Promise.all(sortedDeferredUpdates.map((update) => OnyxUpdates.apply(update))).finally(() => {
unpauseQueueAndReset();
deferredUpdates = [];
});
}

canUnpauseQueuePromise.finally(applyDeferredUpdates);
},
});
};
Loading