Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
MR414N-ID
GitHub Repository: MR414N-ID/botku2
Path: blob/master/node_modules/@adiwajshing/baileys/lib/Utils/make-mutex.js
1129 views
1
"use strict";
2
Object.defineProperty(exports, "__esModule", { value: true });
3
exports.makeKeyedMutex = exports.makeMutex = void 0;
4
const makeMutex = () => {
5
let task = Promise.resolve();
6
return {
7
mutex(code) {
8
task = (async () => {
9
// wait for the previous task to complete
10
// if there is an error, we swallow so as to not block the queue
11
try {
12
await task;
13
}
14
catch (_a) { }
15
// execute the current task
16
return code();
17
})();
18
// we replace the existing task, appending the new piece of execution to it
19
// so the next task will have to wait for this one to finish
20
return task;
21
},
22
};
23
};
24
exports.makeMutex = makeMutex;
25
const makeKeyedMutex = () => {
26
const map = {};
27
return {
28
mutex(key, task) {
29
if (!map[key]) {
30
map[key] = (0, exports.makeMutex)();
31
}
32
return map[key].mutex(task);
33
}
34
};
35
};
36
exports.makeKeyedMutex = makeKeyedMutex;
37
38