From 774a19678a0c6a8747fd6876f88f6e2b67757a84 Mon Sep 17 00:00:00 2001 From: Daniel Silva Date: Tue, 11 Jun 2024 13:51:44 +0200 Subject: [PATCH 1/3] subscribe to onyx for lastUpdateID key --- src/libs/API/index.ts | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/libs/API/index.ts b/src/libs/API/index.ts index bfa1b95836f8..427a77fee261 100644 --- a/src/libs/API/index.ts +++ b/src/libs/API/index.ts @@ -11,6 +11,7 @@ import type OnyxRequest from '@src/types/onyx/Request'; import type Response from '@src/types/onyx/Response'; import pkg from '../../../package.json'; import type {ApiRequest, ApiRequestCommandParameters, ReadCommand, SideEffectRequestCommand, WriteCommand} from './types'; +import ONYXKEYS from '@src/ONYXKEYS'; // Setup API middlewares. Each request made will pass through a series of middleware functions that will get called in sequence (each one passing the result of the previous to the next). // Note: The ordering here is intentional as we want to Log, Recheck Connection, Reauthenticate, and Save the Response in Onyx. Errors thrown in one middleware will bubble to the next. @@ -39,6 +40,14 @@ type OnyxData = { finallyData?: OnyxUpdate[]; }; +// For all write requests, we'll send the lastUpdateID that is applied to this client. This will +// allow us to calculate previousUpdateID faster. +let lastUpdateIDAppliedToClient = 0; +Onyx.connect({ + key: ONYXKEYS.ONYX_UPDATES_LAST_UPDATE_ID_APPLIED_TO_CLIENT, + callback: (value) => (lastUpdateIDAppliedToClient = value ?? 0), +}); + /** * All calls to API.write() will be persisted to disk as JSON with the params, successData, and failureData (or finallyData, if included in place of the former two values). * This is so that if the network is unavailable or the app is closed, we can send the WRITE request later. From dd6bbb22817c85d7a39076d470686a13e7bcf4aa Mon Sep 17 00:00:00 2001 From: Daniel Silva Date: Tue, 11 Jun 2024 13:52:39 +0200 Subject: [PATCH 2/3] adding parameter on write and makeRequestWithSideEffects --- src/libs/API/index.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/libs/API/index.ts b/src/libs/API/index.ts index 427a77fee261..20d0a47a09f8 100644 --- a/src/libs/API/index.ts +++ b/src/libs/API/index.ts @@ -91,6 +91,7 @@ function write(command: TCommand, apiCommandParam // This should be removed once we are no longer using deprecatedAPI https://github.com/Expensify/Expensify/issues/215650 shouldRetry: true, canCancel: true, + clientUpdateID: lastUpdateIDAppliedToClient, }, ...onyxDataWithoutOptimisticData, }; @@ -139,6 +140,7 @@ function makeRequestWithSideEffects Date: Tue, 11 Jun 2024 13:59:50 +0200 Subject: [PATCH 3/3] prettier --- src/libs/API/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/API/index.ts b/src/libs/API/index.ts index 20d0a47a09f8..fc4585b9ef68 100644 --- a/src/libs/API/index.ts +++ b/src/libs/API/index.ts @@ -7,11 +7,11 @@ import * as Pusher from '@libs/Pusher/pusher'; import * as Request from '@libs/Request'; import * as PersistedRequests from '@userActions/PersistedRequests'; import CONST from '@src/CONST'; +import ONYXKEYS from '@src/ONYXKEYS'; import type OnyxRequest from '@src/types/onyx/Request'; import type Response from '@src/types/onyx/Response'; import pkg from '../../../package.json'; import type {ApiRequest, ApiRequestCommandParameters, ReadCommand, SideEffectRequestCommand, WriteCommand} from './types'; -import ONYXKEYS from '@src/ONYXKEYS'; // Setup API middlewares. Each request made will pass through a series of middleware functions that will get called in sequence (each one passing the result of the previous to the next). // Note: The ordering here is intentional as we want to Log, Recheck Connection, Reauthenticate, and Save the Response in Onyx. Errors thrown in one middleware will bubble to the next.