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:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user