diff --git a/src/Socket/chats.ts b/src/Socket/chats.ts index 3ae52791..639ebd4d 100644 --- a/src/Socket/chats.ts +++ b/src/Socket/chats.ts @@ -72,8 +72,18 @@ export const makeChatsSocket = (config: SocketConfig) => { let privacySettings: { [_: string]: string } | undefined let syncState: SyncState = SyncState.Connecting - /** this mutex ensures that the notifications (receipts, messages etc.) are processed in order */ - const processingMutex = makeMutex() + + /** this mutex ensures that messages are processed in order */ + const messageMutex = makeMutex() + + /** this mutex ensures that receipts are processed in order */ + const receiptMutex = makeMutex() + + /** this mutex ensures that app state patches are processed in order */ + const appStatePatchMutex = makeMutex() + + /** this mutex ensures that notifications are processed in order */ + const notificationMutex = makeMutex() // Timeout for AwaitingInitialSync state let awaitingSyncTimeout: NodeJS.Timeout | undefined @@ -748,7 +758,7 @@ export const makeChatsSocket = (config: SocketConfig) => { let initial: LTHashState let encodeResult: { patch: proto.ISyncdPatch; state: LTHashState } - await processingMutex.mutex(async () => { + await appStatePatchMutex.mutex(async () => { await authState.keys.transaction(async () => { logger.debug({ patch: patchCreate }, 'applying app patch') @@ -1180,7 +1190,10 @@ export const makeChatsSocket = (config: SocketConfig) => { ...sock, createCallLink, getBotListV2, - processingMutex, + messageMutex, + receiptMutex, + appStatePatchMutex, + notificationMutex, fetchPrivacySettings, upsertMessage, appPatch, diff --git a/src/Socket/messages-recv.ts b/src/Socket/messages-recv.ts index bb7307d8..7f787c6d 100644 --- a/src/Socket/messages-recv.ts +++ b/src/Socket/messages-recv.ts @@ -71,7 +71,9 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => { ev, authState, ws, - processingMutex, + messageMutex, + notificationMutex, + receiptMutex, signalRepository, query, upsertMessage, @@ -1067,7 +1069,7 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => { try { await Promise.all([ - processingMutex.mutex(async () => { + receiptMutex.mutex(async () => { const status = getStatusFromReceiptType(attrs.type) if ( typeof status !== 'undefined' && @@ -1141,7 +1143,7 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => { try { await Promise.all([ - processingMutex.mutex(async () => { + notificationMutex.mutex(async () => { const msg = await processNotification(node) if (msg) { const fromMe = areJidsSameUser(node.attrs.participant || remoteJid, authState.creds.me!.id) @@ -1218,7 +1220,7 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => { } try { - await processingMutex.mutex(async () => { + await messageMutex.mutex(async () => { await decrypt() // message failed to decrypt if (msg.messageStubType === proto.WebMessageInfo.StubType.CIPHERTEXT && msg.category !== 'peer') { diff --git a/src/Socket/messages-send.ts b/src/Socket/messages-send.ts index adfc5a7a..9762c268 100644 --- a/src/Socket/messages-send.ts +++ b/src/Socket/messages-send.ts @@ -71,7 +71,7 @@ export const makeMessagesSocket = (config: SocketConfig) => { const { ev, authState, - processingMutex, + messageMutex, signalRepository, upsertMessage, query, @@ -1199,7 +1199,7 @@ export const makeMessagesSocket = (config: SocketConfig) => { }) if (config.emitOwnEvents) { process.nextTick(async () => { - await processingMutex.mutex(() => upsertMessage(fullMsg, 'append')) + await messageMutex.mutex(() => upsertMessage(fullMsg, 'append')) }) }