MultiLoader-Template/node_modules/es-toolkit/dist/promise/semaphore.mjs
3944Realms 768f38fc97 feat: 可使用的构建模板
修改了脚本,使其可以推给Maven仓库
2026-05-03 13:02:19 +08:00

31 lines
678 B
JavaScript

class Semaphore {
capacity;
available;
deferredTasks = [];
constructor(capacity) {
this.capacity = capacity;
this.available = capacity;
}
async acquire() {
if (this.available > 0) {
this.available--;
return;
}
return new Promise(resolve => {
this.deferredTasks.push(resolve);
});
}
release() {
const deferredTask = this.deferredTasks.shift();
if (deferredTask != null) {
deferredTask();
return;
}
if (this.available < this.capacity) {
this.available++;
}
}
}
export { Semaphore };