@sanity/util
Version:
Utilities shared across projects of Sanity
33 lines (32 loc) • 964 B
JavaScript
class ConcurrencyLimiter {
current = 0;
resolvers = [];
max;
constructor(max) {
this.max = max;
}
/**
* Indicates when a slot for a new operation is ready.
* If under the limit, it resolves immediately; otherwise, it waits until a slot is free.
*/
ready = () => this.max === 1 / 0 ? Promise.resolve() : this.current < this.max ? (this.current++, Promise.resolve()) : new Promise((resolve) => {
this.resolvers.push(resolve);
});
/**
* Releases a slot, decrementing the current count of operations if nothing is in the queue.
* If there are operations waiting, it allows the next one in the queue to proceed.
*/
release = () => {
if (this.max === 1 / 0) return;
const nextResolver = this.resolvers.shift();
if (nextResolver) {
nextResolver();
return;
}
this.current = Math.max(0, this.current - 1);
};
}
export {
ConcurrencyLimiter
};
//# sourceMappingURL=concurrency-limiter.mjs.map