perf: eliminates a delay in message delivery between phone→app + cache device-list

perf: eliminates a delay in message delivery between phone→app + cache device-list
This commit is contained in:
Renato Alcara
2026-02-24 07:40:18 -03:00
committed by GitHub
parent d34d3d8791
commit f906eca124
4 changed files with 52 additions and 7 deletions
+22
View File
@@ -1330,6 +1330,7 @@ export const makeChatsSocket = (config: SocketConfig) => {
if (syncState === SyncState.Syncing) {
// All collections will be synced, so clear any blocked ones
blockedCollections.clear()
logger.info('Doing app state sync')
await resyncAppState(ALL_WA_PATCH_NAMES, true)
@@ -1430,6 +1431,27 @@ export const makeChatsSocket = (config: SocketConfig) => {
logger.info('Connection is now AwaitingInitialSync, buffering events')
ev.buffer()
// On reconnections (accountSyncCounter > 0), app state was already synced in a previous
// session. Skip the 20s AwaitingInitialSync wait and go directly to Online so that
// live incoming messages are not held in the buffer for up to 20-60 seconds.
// History messages that arrive later are still processed via processMessage regardless
// of the state machine phase (see the Syncing → Online path below).
const isReconnection = (authState.creds.accountSyncCounter ?? 0) > 0
if (isReconnection) {
logger.info('Reconnection detected (accountSyncCounter > 0), skipping AwaitingInitialSync wait. Transitioning to Online immediately.')
blockedCollections.clear()
syncState = SyncState.Online
const accountSyncCounter = (authState.creds.accountSyncCounter || 0) + 1
ev.emit('creds.update', { accountSyncCounter })
// Fire-and-forget: pick up patches missed during downtime (mute/archive/pin/read state).
// Runs in background so live incoming messages are not blocked.
resyncAppState(ALL_WA_PATCH_NAMES, true).catch(err =>
logger.warn({ err }, 'Background app state resync failed (non-critical on reconnection)')
)
setTimeout(() => ev.flush(), 0)
return
}
const willSyncHistory = shouldSyncHistoryMessage(
proto.Message.HistorySyncNotification.create({
syncType: proto.HistorySync.HistorySyncType.RECENT