From cb7356e22acf5e399b18a95fe179c7fb4da47b23 Mon Sep 17 00:00:00 2001 From: Carlos Barros <765936+barros001@users.noreply.github.com> Date: Tue, 14 Nov 2023 14:14:28 -0500 Subject: [PATCH] Revert "serialize execution of entire apply method instead of only applyPusherOnyxUpdates" This reverts commit 82fb44bdfd00d84ddc4abcb1e19ba686c7ed9fed. --- src/libs/actions/OnyxUpdates.ts | 48 +++++++++++++++------------------ 1 file changed, 21 insertions(+), 27 deletions(-) diff --git a/src/libs/actions/OnyxUpdates.ts b/src/libs/actions/OnyxUpdates.ts index 852fc907a920..ce673fa6aaaf 100644 --- a/src/libs/actions/OnyxUpdates.ts +++ b/src/libs/actions/OnyxUpdates.ts @@ -15,6 +15,10 @@ Onyx.connect({ callback: (val) => (lastUpdateIDAppliedToClient = val), }); +// This promise is used to ensure pusher events are always processed in the order they are received, +// even when such events are received over multiple separate pusher updates. +let pusherEventsPromise = Promise.resolve(); + function applyHTTPSOnyxUpdates(request: Request, response: Response) { console.debug('[OnyxUpdateManager] Applying https update'); // For most requests we can immediately update Onyx. For write requests we queue the updates and apply them after the sequential queue has flushed to prevent a replay effect in @@ -44,21 +48,18 @@ function applyHTTPSOnyxUpdates(request: Request, response: Response) { } function applyPusherOnyxUpdates(updates: OnyxUpdateEvent[]) { - return updates - .reduce( - (promise, update) => promise.then(() => PusherUtils.triggerMultiEventHandler(update.eventType, update.data)), - Promise.resolve().then(() => { - console.debug('[OnyxUpdateManager] Applying pusher update'); - }), - ) + pusherEventsPromise = pusherEventsPromise.then(() => { + console.debug('[OnyxUpdateManager] Applying pusher update'); + }); + + pusherEventsPromise = updates + .reduce((promise, update) => promise.then(() => PusherUtils.triggerMultiEventHandler(update.eventType, update.data)), pusherEventsPromise) .then(() => { console.debug('[OnyxUpdateManager] Done applying Pusher update'); }); -} -// This promise is used to ensure pusher events are always processed in the order they are received, -// even when such events are received over multiple separate pusher updates. -let applyPromise: Promise = Promise.resolve(); + return pusherEventsPromise; +} /** * @param [updateParams.request] Exists if updateParams.type === 'https' @@ -68,28 +69,21 @@ let applyPromise: Promise = Promise.resolve(); function apply({lastUpdateID, type, request, response, updates}: Merge): Promise; function apply({lastUpdateID, type, request, response, updates}: Merge): Promise; function apply({lastUpdateID, type, request, response, updates}: OnyxUpdatesFromServer): Promise | undefined { - applyPromise = applyPromise.then(() => { - console.debug(`[OnyxUpdateManager] Applying update type: ${type} with lastUpdateID: ${lastUpdateID}`, {request, response, updates}); - - if (lastUpdateID && lastUpdateIDAppliedToClient && Number(lastUpdateID) < lastUpdateIDAppliedToClient) { - console.debug('[OnyxUpdateManager] Update received was older than current state, returning without applying the updates'); - return Promise.resolve(); - } - if (lastUpdateID && (lastUpdateIDAppliedToClient === null || Number(lastUpdateID) > lastUpdateIDAppliedToClient)) { - return Onyx.merge(ONYXKEYS.ONYX_UPDATES_LAST_UPDATE_ID_APPLIED_TO_CLIENT, Number(lastUpdateID)); - } + console.debug(`[OnyxUpdateManager] Applying update type: ${type} with lastUpdateID: ${lastUpdateID}`, {request, response, updates}); + if (lastUpdateID && lastUpdateIDAppliedToClient && Number(lastUpdateID) < lastUpdateIDAppliedToClient) { + console.debug('[OnyxUpdateManager] Update received was older than current state, returning without applying the updates'); return Promise.resolve(); - }); - + } + if (lastUpdateID && (lastUpdateIDAppliedToClient === null || Number(lastUpdateID) > lastUpdateIDAppliedToClient)) { + Onyx.merge(ONYXKEYS.ONYX_UPDATES_LAST_UPDATE_ID_APPLIED_TO_CLIENT, Number(lastUpdateID)); + } if (type === CONST.ONYX_UPDATE_TYPES.HTTPS && request && response) { - applyPromise = applyPromise.then(() => applyHTTPSOnyxUpdates(request, response)); + return applyHTTPSOnyxUpdates(request, response); } if (type === CONST.ONYX_UPDATE_TYPES.PUSHER && updates) { - applyPromise = applyPromise.then(() => applyPusherOnyxUpdates(updates)); + return applyPusherOnyxUpdates(updates); } - - return applyPromise; } /**