Skip to content

Commit

Permalink
serialize execution of entire apply method instead of only applyPushe…
Browse files Browse the repository at this point in the history
…rOnyxUpdates
  • Loading branch information
barros001 committed Nov 14, 2023
1 parent 39508d3 commit 82fb44b
Showing 1 changed file with 27 additions and 21 deletions.
48 changes: 27 additions & 21 deletions src/libs/actions/OnyxUpdates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ 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
Expand Down Expand Up @@ -48,19 +44,22 @@ function applyHTTPSOnyxUpdates(request: Request, response: Response) {
}

function applyPusherOnyxUpdates(updates: OnyxUpdateEvent[]) {
pusherEventsPromise = pusherEventsPromise.then(() => {
console.debug('[OnyxUpdateManager] Applying pusher update');
});

pusherEventsPromise = updates
.reduce((promise, update) => promise.then(() => PusherUtils.triggerMultiEventHandler(update.eventType, update.data)), pusherEventsPromise)
return updates
.reduce(
(promise, update) => promise.then(() => PusherUtils.triggerMultiEventHandler(update.eventType, update.data)),
Promise.resolve().then(() => {
console.debug('[OnyxUpdateManager] Applying pusher update');
}),
)
.then(() => {
console.debug('[OnyxUpdateManager] Done applying Pusher update');
});

return pusherEventsPromise;
}

// 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<void | Response> = Promise.resolve();

/**
* @param [updateParams.request] Exists if updateParams.type === 'https'
* @param [updateParams.response] Exists if updateParams.type === 'https'
Expand All @@ -69,21 +68,28 @@ function applyPusherOnyxUpdates(updates: OnyxUpdateEvent[]) {
function apply({lastUpdateID, type, request, response, updates}: Merge<OnyxUpdatesFromServer, {updates: OnyxUpdateEvent[]; type: 'pusher'}>): Promise<void>;
function apply({lastUpdateID, type, request, response, updates}: Merge<OnyxUpdatesFromServer, {request: Request; response: Response; type: 'https'}>): Promise<Response>;
function apply({lastUpdateID, type, request, response, updates}: OnyxUpdatesFromServer): Promise<void | Response> | undefined {
console.debug(`[OnyxUpdateManager] Applying update type: ${type} with lastUpdateID: ${lastUpdateID}`, {request, response, updates});
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));
}

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) {
return applyHTTPSOnyxUpdates(request, response);
applyPromise = applyPromise.then(() => applyHTTPSOnyxUpdates(request, response));
}
if (type === CONST.ONYX_UPDATE_TYPES.PUSHER && updates) {
return applyPusherOnyxUpdates(updates);
applyPromise = applyPromise.then(() => applyPusherOnyxUpdates(updates));
}

return applyPromise;
}

/**
Expand Down

0 comments on commit 82fb44b

Please sign in to comment.