@sentclose/sentc-nodejs
Version:
End-to-end encryption sdk
30 lines (29 loc) • 733 B
JavaScript
;
/**
* @author Jörn Heinemann <joernheinemann@gmx.de>
* @since 2021/09/16
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.Mutex = void 0;
/**
* Mix of:
* - https://stackoverflow.com/a/51086893/12177973
* - https://github.com/mgtitimoli/await-mutex/blob/master/src/mutex.js
*/
class Mutex {
constructor() {
this.current = Promise.resolve();
}
lock() {
let unlockNext;
const p = new Promise((resolve) => {
unlockNext = () => {
return resolve();
};
});
const unlock = this.current.then(() => { return unlockNext; });
this.current = p;
return unlock;
}
}
exports.Mutex = Mutex;