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:
@@ -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