parallel-universe
Version:
The set of async flow control structures and promise utils.
40 lines (37 loc) • 1.1 kB
JavaScript
;
/**
* Promise-based lock implementation.
*
* When someone tries to acquire a {@link Lock} they receive a promise for a release callback that is fulfilled as soon
* as previous lock owner invokes their release callback.
*
* @see https://en.wikipedia.org/wiki/Lock_(computer_science) Lock (computer science)
*/
class Lock {
/**
* `true` if {@link Lock} was acquired and wasn't released yet.
*/
get isLocked() {
return this._promise !== undefined;
}
/**
* Waits for the {@link Lock} to become available and fulfills it with the callback that releases the lock.
*/
acquire() {
const { _promise } = this;
let promise;
const release = () => {
if (this._promise === promise) {
this._promise = undefined;
}
};
if (_promise !== undefined) {
this._promise = promise = _promise.then(() => release);
}
else {
this._promise = promise = Promise.resolve(release);
}
return promise;
}
}
exports.Lock = Lock;