@stryke/helpers
Version:
A package containing miscellaneous helper functions that are used across many different Storm Software projects.
32 lines (31 loc) • 609 B
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.Semaphore = void 0;
class Semaphore {
capacity;
available;
deferredTasks = [];
constructor(e) {
this.capacity = e, this.available = e;
}
async acquire() {
if (this.available > 0) {
this.available--;
return;
}
return new Promise(e => {
this.deferredTasks.push(e);
});
}
release() {
const e = this.deferredTasks.shift();
if (e != null) {
e();
return;
}
this.available < this.capacity && this.available++;
}
}
exports.Semaphore = Semaphore;