diff --git a/lib/tunnelbroker/peer-to-peer-context.js b/lib/tunnelbroker/peer-to-peer-context.js --- a/lib/tunnelbroker/peer-to-peer-context.js +++ b/lib/tunnelbroker/peer-to-peer-context.js @@ -290,8 +290,10 @@ [allPeerDevices], ); - const processOutboundMessages = React.useCallback( - ( + const processOutboundMessagesLatestVersionRef = React.useRef(-1); + const processOutboundMessages = React.useMemo(() => { + const currentVersion = ++processOutboundMessagesLatestVersionRef.current; + return ( outboundMessageIDs: ?$ReadOnlyArray, dmOpID: ?string, notificationsCreationData: ?NotificationsCreationData, @@ -306,6 +308,11 @@ promiseRunning.current = true; void (async () => { do { + if ( + currentVersion !== processOutboundMessagesLatestVersionRef.current + ) { + break; + } const queueFront = processingQueue.current.shift(); try { const [result] = await Promise.all([ @@ -343,15 +350,14 @@ promiseRunning.current = false; })(); } - }, - [ - allPeerDevicesSet, - sendMessageToDevice, - identityContext, - peerOlmSessionsCreator, - sendPushNotifs, - ], - ); + }; + }, [ + allPeerDevicesSet, + sendMessageToDevice, + identityContext, + peerOlmSessionsCreator, + sendPushNotifs, + ]); const broadcastEphemeralMessage = React.useCallback( async (