fix: mutex app state sync to prevent race conditions that log connection out

This commit is contained in:
Adhiraj Singh
2021-11-18 11:30:35 +05:30
parent 40280d777d
commit bb5f13d188
2 changed files with 84 additions and 49 deletions

19
src/Utils/make-mutex.ts Normal file
View File

@@ -0,0 +1,19 @@
export default () => {
let task = Promise.resolve() as Promise<any>
return {
mutex<T>(code: () => Promise<T>):Promise<T> {
task = (async () => {
// wait for the previous task to complete
// if there is an error, we swallow so as to not block the queue
try { await task } catch { }
// execute the current task
return code()
})()
// we replace the existing task, appending the new piece of execution to it
// so the next task will have to wait for this one to finish
return task
},
}
}