mirror of
https://github.com/FranP-code/Baileys.git
synced 2025-10-13 00:32:22 +00:00
Added mutex + fixed rare duplicate chats bug + fixed connect bug
The mutex will prevent duplicate functions from being called and throwing funky errors.
This commit is contained in:
24
src/WAConnection/Mutex.ts
Normal file
24
src/WAConnection/Mutex.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
/**
|
||||
* A simple mutex that can be used as a decorator. For examples, see Tests.Mutex.ts
|
||||
* @param keyGetter if you want to lock functions based on certain arguments, specify the key for the function based on the arguments
|
||||
*/
|
||||
export function Mutex (keyGetter?: (...args: any[]) => string) {
|
||||
let tasks: { [k: string]: Promise<void> } = {}
|
||||
return function (_, __, descriptor: PropertyDescriptor) {
|
||||
const originalMethod = descriptor.value
|
||||
descriptor.value = function (this: Object, ...args) {
|
||||
const key = (keyGetter && keyGetter.call(this, ...args)) || 'undefined'
|
||||
|
||||
tasks[key] = (async () => {
|
||||
try {
|
||||
tasks[key] && await tasks[key]
|
||||
} catch {
|
||||
|
||||
}
|
||||
const result = await originalMethod.call(this, ...args)
|
||||
return result
|
||||
})()
|
||||
return tasks[key]
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user