mirror of
https://github.com/FranP-code/Baileys.git
synced 2025-10-13 00:32:22 +00:00
refactor: impl counter based event buffer
1. counter based event buffer keeps track of the number of blocks that request event processing in buffer
2. event buffer only releases events when the last block completes (i.e. counter = 0)
this approach is far simpler than the promised based garbled crap I wrote, should also prevent the deadlock issues it introduced 🙏
This commit is contained in:
@@ -822,7 +822,8 @@ export const makeChatsSocket = (config: SocketConfig) => {
|
||||
// we keep buffering events until we finally have
|
||||
// the key and can sync the messages
|
||||
if(!authState.creds?.myAppStateKeyId) {
|
||||
needToFlushWithAppStateSync = ev.buffer()
|
||||
ev.buffer()
|
||||
needToFlushWithAppStateSync = true
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
@@ -645,16 +645,9 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
|
||||
identifier: string,
|
||||
exec: (node: BinaryNode) => Promise<any>
|
||||
) => {
|
||||
const started = ev.buffer()
|
||||
if(started) {
|
||||
await execTask()
|
||||
if(started) {
|
||||
await ev.flush()
|
||||
}
|
||||
} else {
|
||||
const task = execTask()
|
||||
ev.processInBuffer(task)
|
||||
}
|
||||
ev.buffer()
|
||||
await execTask()
|
||||
ev.flush()
|
||||
|
||||
function execTask() {
|
||||
return exec(node)
|
||||
@@ -662,17 +655,6 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
|
||||
}
|
||||
}
|
||||
|
||||
// called when all offline notifs are handled
|
||||
ws.on('CB:ib,,offline', async(node: BinaryNode) => {
|
||||
const child = getBinaryNodeChild(node, 'offline')
|
||||
const offlineNotifs = +(child?.attrs.count || 0)
|
||||
|
||||
logger.info(`handled ${offlineNotifs} offline messages/notifications`)
|
||||
await ev.flush()
|
||||
|
||||
ev.emit('connection.update', { receivedPendingNotifications: true })
|
||||
})
|
||||
|
||||
// recv a message
|
||||
ws.on('CB:message', (node: BinaryNode) => {
|
||||
processNodeWithBuffer(node, 'processing message', handleMessage)
|
||||
|
||||
@@ -532,15 +532,32 @@ export const makeSocket = ({
|
||||
end(new Boom('Multi-device beta not joined', { statusCode: DisconnectReason.multideviceMismatch }))
|
||||
})
|
||||
|
||||
let didStartBuffer = false
|
||||
process.nextTick(() => {
|
||||
if(creds.me?.id) {
|
||||
// start buffering important events
|
||||
// if we're logged in
|
||||
ev.buffer()
|
||||
didStartBuffer = true
|
||||
}
|
||||
|
||||
ev.emit('connection.update', { connection: 'connecting', receivedPendingNotifications: false, qr: undefined })
|
||||
})
|
||||
|
||||
// called when all offline notifs are handled
|
||||
ws.on('CB:ib,,offline', (node: BinaryNode) => {
|
||||
const child = getBinaryNodeChild(node, 'offline')
|
||||
const offlineNotifs = +(child?.attrs.count || 0)
|
||||
|
||||
logger.info(`handled ${offlineNotifs} offline messages/notifications`)
|
||||
if(didStartBuffer) {
|
||||
ev.flush()
|
||||
logger.trace('flushed events for initial buffer')
|
||||
}
|
||||
|
||||
ev.emit('connection.update', { receivedPendingNotifications: true })
|
||||
})
|
||||
|
||||
// update credentials when required
|
||||
ev.on('creds.update', update => {
|
||||
const name = update.me?.name
|
||||
|
||||
Reference in New Issue
Block a user