unpkg/server/middleware/utils/createMutex.js

22 lines
437 B
JavaScript

function createMutex(doWork) {
const mutex = {}
return function (key, payload, callback) {
if (mutex[key]) {
mutex[key].push(callback)
} else {
mutex[key] = [ function () {
delete mutex[key]
}, callback ]
doWork(payload, function (error, value) {
mutex[key].forEach(function (callback) {
callback(error, value)
})
})
}
}
}
module.exports = createMutex