trade360-nodejs-sdk
Version:
LSports Trade360 SDK for Node.js
44 lines • 1.11 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.AsyncLock = void 0;
/**
* A simple async lock implementation. This lock
* is not reentrant.
*/
class AsyncLock {
constructor() {
this.locked = false;
this.waitingResolvers = [];
}
/**
* Acquire the lock. If the lock is already
* acquired, the next caller wait until it is
* released.
* @returns a promise that resolves when the
* lock is acquired
*/
async acquire() {
if (!this.locked) {
this.locked = true;
return;
}
return new Promise((resolve) => {
this.waitingResolvers.push(resolve);
});
}
/**
* Release the lock. If there are waiting promises,
* resolve the first one.
*/
release() {
if (this.waitingResolvers.length > 0) {
const resolve = this.waitingResolvers.shift();
resolve();
}
else {
this.locked = false;
}
}
}
exports.AsyncLock = AsyncLock;
//# sourceMappingURL=async-lock.js.map