source: node_modules/es-toolkit/dist/promise/semaphore.js

Last change on this file was a762898, checked in by istevanoska <ilinastevanoska@…>, 5 months ago

Added visualizations

  • Property mode set to 100644
File size: 776 bytes
RevLine 
[a762898]1'use strict';
2
3Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
5class Semaphore {
6 capacity;
7 available;
8 deferredTasks = [];
9 constructor(capacity) {
10 this.capacity = capacity;
11 this.available = capacity;
12 }
13 async acquire() {
14 if (this.available > 0) {
15 this.available--;
16 return;
17 }
18 return new Promise(resolve => {
19 this.deferredTasks.push(resolve);
20 });
21 }
22 release() {
23 const deferredTask = this.deferredTasks.shift();
24 if (deferredTask != null) {
25 deferredTask();
26 return;
27 }
28 if (this.available < this.capacity) {
29 this.available++;
30 }
31 }
32}
33
34exports.Semaphore = Semaphore;
Note: See TracBrowser for help on using the repository browser.