diff --git a/src/libs/API/index.ts b/src/libs/API/index.ts index bfa1b95836f8..fc4585b9ef68 100644 --- a/src/libs/API/index.ts +++ b/src/libs/API/index.ts @@ -7,6 +7,7 @@ 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'; @@ -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. @@ -82,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, }; @@ -130,6 +140,7 @@ function makeRequestWithSideEffects