Skip to content

Commit

Permalink
Merge pull request #31144 from barros001/serialize-events
Browse files Browse the repository at this point in the history
fix: ensure pusher events are processed in the same order they were received
  • Loading branch information
tgolen authored Nov 16, 2023
2 parents 7ec147b + cb7356e commit 1ddd581
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions src/libs/actions/OnyxUpdates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -44,11 +48,17 @@ function applyHTTPSOnyxUpdates(request: Request, response: Response) {
}

function applyPusherOnyxUpdates(updates: OnyxUpdateEvent[]) {
console.debug('[OnyxUpdateManager] Applying pusher update');
const pusherEventPromises = updates.map((update) => PusherUtils.triggerMultiEventHandler(update.eventType, update.data));
return Promise.all(pusherEventPromises).then(() => {
console.debug('[OnyxUpdateManager] Done 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');
});

return pusherEventsPromise;
}

/**
Expand Down

0 comments on commit 1ddd581

Please sign in to comment.