@chriscdn/promise-semaphore 3.0.1 → 3.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +39 -53
- package/__tests__/semaphore.test.ts +27 -0
- package/lib/promise-semaphore.cjs +1 -1
- package/lib/promise-semaphore.cjs.map +1 -1
- package/lib/promise-semaphore.modern.js +1 -1
- package/lib/promise-semaphore.modern.js.map +1 -1
- package/lib/promise-semaphore.module.js +1 -1
- package/lib/promise-semaphore.module.js.map +1 -1
- package/lib/semaphore.d.ts +14 -7
- package/package.json +2 -2
- package/src/semaphore.ts +53 -26
package/README.md
CHANGED
|
@@ -21,9 +21,7 @@ yarn add @chriscdn/promise-semaphore
|
|
|
21
21
|
|
|
22
22
|
Version 3 introduces two main changes:
|
|
23
23
|
|
|
24
|
-
- A new `GroupSemaphore` class has been added. It allows multiple tasks within
|
|
25
|
-
the same group (identified by a key) to run concurrently while ensuring that
|
|
26
|
-
only one group's tasks are active at a time. See below for documentation.
|
|
24
|
+
- A new `GroupSemaphore` class has been added. It allows multiple tasks within the same group (identified by a key) to run concurrently while ensuring that only one group's tasks are active at a time. See below for documentation.
|
|
27
25
|
- The default export has been replaced with a named export.
|
|
28
26
|
|
|
29
27
|
Change:
|
|
@@ -42,47 +40,47 @@ import { Semaphore } from "@chriscdn/promise-semaphore";
|
|
|
42
40
|
|
|
43
41
|
### Create an instance
|
|
44
42
|
|
|
45
|
-
```
|
|
43
|
+
```ts
|
|
46
44
|
import { Semaphore } from "@chriscdn/promise-semaphore";
|
|
47
45
|
const semaphore = new Semaphore([maxConcurrent]);
|
|
48
46
|
```
|
|
49
47
|
|
|
50
|
-
The `maxConcurrent` parameter is optional and defaults to `1` (making it an
|
|
51
|
-
exclusive lock or _binary semaphore_). An integer greater than `1` can be used
|
|
52
|
-
to allow multiple concurrent executions from separate iterations of the event
|
|
53
|
-
loop.
|
|
48
|
+
The `maxConcurrent` parameter is optional and defaults to `1` (making it an exclusive lock or _binary semaphore_). An integer greater than `1` can be used to allow multiple concurrent executions from separate iterations of the event loop.
|
|
54
49
|
|
|
55
50
|
### Acquire a lock
|
|
56
51
|
|
|
57
|
-
```
|
|
58
|
-
semaphore.acquire([
|
|
52
|
+
```ts
|
|
53
|
+
semaphore.acquire([options]);
|
|
59
54
|
```
|
|
60
55
|
|
|
61
|
-
This returns a `Promise` that resolves
|
|
62
|
-
|
|
63
|
-
|
|
56
|
+
This method returns a `Promise` that resolves when the lock is acquired.
|
|
57
|
+
|
|
58
|
+
The `options` parameter is optional and can be:
|
|
59
|
+
|
|
60
|
+
- A **key** (`string` or `number`): This lets a single `Semaphore` instance manage locks in different contexts (see the second example for `key` usage).
|
|
61
|
+
- An **object** with the following properties (all optional):
|
|
62
|
+
- `key` (`string` or `number`): Functions the same as above.
|
|
63
|
+
- `priority` (`number`): Determines the order in which queued requests are processed. Higher values are processed first.
|
|
64
64
|
|
|
65
65
|
### Release a lock
|
|
66
66
|
|
|
67
|
-
```
|
|
67
|
+
```ts
|
|
68
68
|
semaphore.release([key]);
|
|
69
69
|
```
|
|
70
70
|
|
|
71
|
-
The `release` method should be called within a `finally` block (whether using
|
|
72
|
-
promises or a `try/catch` block) to ensure the lock is released.
|
|
71
|
+
The `release` method should be called within a `finally` block (whether using promises or a `try/catch` block) to ensure the lock is released. It's crucial to call `release` with the same `key` the lock was acquired with.
|
|
73
72
|
|
|
74
73
|
### Check if a lock can be acquired
|
|
75
74
|
|
|
76
|
-
```
|
|
75
|
+
```ts
|
|
77
76
|
semaphore.canAcquire([key]);
|
|
78
77
|
```
|
|
79
78
|
|
|
80
|
-
This synchronous method returns `true` if a lock can be immediately acquired,
|
|
81
|
-
and `false` otherwise.
|
|
79
|
+
This synchronous method returns `true` if a lock can be immediately acquired, and `false` otherwise.
|
|
82
80
|
|
|
83
81
|
### `count`
|
|
84
82
|
|
|
85
|
-
```
|
|
83
|
+
```ts
|
|
86
84
|
semaphore.count([key]);
|
|
87
85
|
```
|
|
88
86
|
|
|
@@ -90,16 +88,15 @@ This function is synchronous, and returns the current number of locks.
|
|
|
90
88
|
|
|
91
89
|
### `request` method
|
|
92
90
|
|
|
93
|
-
```
|
|
94
|
-
const results = await semaphore.request(fn [,
|
|
91
|
+
```ts
|
|
92
|
+
const results = await semaphore.request(fn [, options]);
|
|
95
93
|
```
|
|
96
94
|
|
|
97
|
-
This function reduces boilerplate when using `acquire` and `release`. It returns
|
|
98
|
-
a promise that resolves when `fn` completes. It is functionally equivalent to:
|
|
95
|
+
This function reduces boilerplate when using `acquire` and `release`. It returns a promise that resolves when `fn` completes. It is functionally equivalent to:
|
|
99
96
|
|
|
100
|
-
```
|
|
97
|
+
```ts
|
|
101
98
|
try {
|
|
102
|
-
await semaphore.acquire([
|
|
99
|
+
await semaphore.acquire([options]);
|
|
103
100
|
return await fn();
|
|
104
101
|
} finally {
|
|
105
102
|
semaphore.release([key]);
|
|
@@ -108,23 +105,23 @@ try {
|
|
|
108
105
|
|
|
109
106
|
### `requestIfAvailable` method
|
|
110
107
|
|
|
111
|
-
```
|
|
112
|
-
const results = await semaphore.requestIfAvailable(fn [,
|
|
108
|
+
```ts
|
|
109
|
+
const results = await semaphore.requestIfAvailable(fn [, options]);
|
|
113
110
|
```
|
|
114
111
|
|
|
115
112
|
This is functionally equivalent to:
|
|
116
113
|
|
|
117
|
-
```
|
|
118
|
-
return semaphore.canAcquire([key])
|
|
114
|
+
```ts
|
|
115
|
+
return semaphore.canAcquire([key])
|
|
116
|
+
? await semaphore.request(fn, [options])
|
|
117
|
+
: null;
|
|
119
118
|
```
|
|
120
119
|
|
|
121
|
-
This is useful in scenarios where only one instance of a function block should
|
|
122
|
-
run while discarding additional attempts. For example, handling repeated button
|
|
123
|
-
clicks.
|
|
120
|
+
This is useful in scenarios where only one instance of a function block should run while discarding additional attempts. For example, handling repeated button clicks.
|
|
124
121
|
|
|
125
122
|
## Example 1
|
|
126
123
|
|
|
127
|
-
```
|
|
124
|
+
```ts
|
|
128
125
|
import { Semaphore } from "@chriscdn/promise-semaphore";
|
|
129
126
|
const semaphore = new Semaphore();
|
|
130
127
|
|
|
@@ -161,7 +158,7 @@ await semaphore.request(() => {
|
|
|
161
158
|
|
|
162
159
|
Consider an asynchronous function that downloads a file and saves it to disk:
|
|
163
160
|
|
|
164
|
-
```
|
|
161
|
+
```ts
|
|
165
162
|
const downloadAndSave = async (url) => {
|
|
166
163
|
const filePath = urlToFilePath(url);
|
|
167
164
|
|
|
@@ -175,14 +172,11 @@ const downloadAndSave = async (url) => {
|
|
|
175
172
|
};
|
|
176
173
|
```
|
|
177
174
|
|
|
178
|
-
This approach works as expected until `downloadAndSave()` is called multiple
|
|
179
|
-
times with the same `url` in quick succession. Without control, it could
|
|
180
|
-
initiate simultaneous downloads that attempt to write to the same file at the
|
|
181
|
-
same time.
|
|
175
|
+
This approach works as expected until `downloadAndSave()` is called multiple times in quick succession with the same `url`. Without control, it could initiate simultaneous downloads that attempt to write to the same file at the same time.
|
|
182
176
|
|
|
183
177
|
This issue can be resolved by using a `Semaphore` with the `key` parameter:
|
|
184
178
|
|
|
185
|
-
```
|
|
179
|
+
```ts
|
|
186
180
|
import { Semaphore } from "@chriscdn/promise-semaphore";
|
|
187
181
|
const semaphore = new Semaphore();
|
|
188
182
|
|
|
@@ -208,9 +202,9 @@ const downloadAndSave = async (url) => {
|
|
|
208
202
|
};
|
|
209
203
|
```
|
|
210
204
|
|
|
211
|
-
The same outcome can be achieved using the `request` function:
|
|
205
|
+
The same outcome can be achieved by using the `request` function:
|
|
212
206
|
|
|
213
|
-
```
|
|
207
|
+
```ts
|
|
214
208
|
const downloadAndSave = (url) => {
|
|
215
209
|
return semaphore.request(async () => {
|
|
216
210
|
const filePath = urlToFilePath(url);
|
|
@@ -227,13 +221,9 @@ const downloadAndSave = (url) => {
|
|
|
227
221
|
|
|
228
222
|
## API - GroupSemaphore
|
|
229
223
|
|
|
230
|
-
The `GroupSemaphore` class manages a semaphore for different
|
|
231
|
-
group is identified by a key, and the semaphore ensures that only one group can
|
|
232
|
-
run its tasks at a time. The tasks within a group can run concurrently.
|
|
224
|
+
The `GroupSemaphore` class manages a semaphore for different _groups_ of tasks. A group is identified by a key, and the semaphore ensures that only one group can run its tasks at a time. The tasks within a group can run concurrently.
|
|
233
225
|
|
|
234
|
-
The `GroupSemaphore` class exposes `acquire` and `release` methods, which have
|
|
235
|
-
the same interface as `Semaphore`. The only difference is that the `key`
|
|
236
|
-
parameter is required.
|
|
226
|
+
The `GroupSemaphore` class exposes `acquire` and `release` methods, which have the same interface as `Semaphore`. The only difference is that the `key` parameter is required.
|
|
237
227
|
|
|
238
228
|
### Example
|
|
239
229
|
|
|
@@ -263,11 +253,7 @@ const RunB = async () => {
|
|
|
263
253
|
};
|
|
264
254
|
```
|
|
265
255
|
|
|
266
|
-
This setup allows `RunA` to be called multiple times, and will run concurrently.
|
|
267
|
-
However, calling `RunB` will wait until all `GroupA` tasks are completed before
|
|
268
|
-
acquiring the lock for `GroupB`. As soon as `GroupB` acquires the lock, any
|
|
269
|
-
subsequent calls to `RunA` will wait until `GroupB` releases the lock before it
|
|
270
|
-
executes.
|
|
256
|
+
This setup allows `RunA` to be called multiple times, and will run concurrently. However, calling `RunB` will wait until all `GroupA` tasks are completed before acquiring the lock for `GroupB`. As soon as `GroupB` acquires the lock, any subsequent calls to `RunA` will wait until `GroupB` releases the lock before it executes.
|
|
271
257
|
|
|
272
258
|
## License
|
|
273
259
|
|
|
@@ -145,3 +145,30 @@ describe("All test", () => {
|
|
|
145
145
|
});
|
|
146
146
|
});
|
|
147
147
|
});
|
|
148
|
+
|
|
149
|
+
describe("Priority", () => {
|
|
150
|
+
it("Priority", async () => {
|
|
151
|
+
const semaphore = new Semaphore(1);
|
|
152
|
+
let tester = 0;
|
|
153
|
+
let key = "yyz";
|
|
154
|
+
|
|
155
|
+
// this one is executed immediately since it's the first object
|
|
156
|
+
const p1 = semaphore.request(async () => {
|
|
157
|
+
tester = 5;
|
|
158
|
+
}, { key, priority: 0 });
|
|
159
|
+
|
|
160
|
+
// this one gets queued with priority 20
|
|
161
|
+
const p2 = semaphore.request(async () => {
|
|
162
|
+
tester = 10;
|
|
163
|
+
}, { key, priority: 20 });
|
|
164
|
+
|
|
165
|
+
// this one gets queued with priority 30, which should execute before p2
|
|
166
|
+
const p3 = semaphore.request(async () => {
|
|
167
|
+
expect(tester).toBe(5);
|
|
168
|
+
}, { key, priority: 30 });
|
|
169
|
+
|
|
170
|
+
semaphore.acquire({ priority: 5 });
|
|
171
|
+
|
|
172
|
+
await Promise.all([p1, p2, p3]);
|
|
173
|
+
});
|
|
174
|
+
});
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
function e(e){var t=function(e){if("object"!=typeof e||!e)return e;var t=e[Symbol.toPrimitive];if(void 0!==t){var
|
|
1
|
+
function e(e){var t=function(e){if("object"!=typeof e||!e)return e;var t=e[Symbol.toPrimitive];if(void 0!==t){var r=t.call(e,"string");if("object"!=typeof r)return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(e)}(e);return"symbol"==typeof t?t:t+""}var t="_default",r=function(e){return["string","number"].includes(typeof e)},n=function(e){var n;return null!=(n=r(e)?e:e.key)?n:t},i=/*#__PURE__*/function(){function t(e){this.queue=void 0,this.maxConcurrent=void 0,this.count=void 0,this.queue=[],this.maxConcurrent=e,this.count=0}var r,n,i=t.prototype;return i.incrementCount=function(){this.count++},i.decrementCount=function(){this.count--},i.acquire=function(e){var t=this;return this.canAcquire?(this.incrementCount(),Promise.resolve()):new Promise(function(r){t.queue.push({resolve:r,priority:e}),t.queue.sort(function(e,t){return t.priority-e.priority})})},i.release=function(){var e=this.queue.shift();e?setTimeout(e.resolve,0):this.decrementCount()},r=t,(n=[{key:"canAcquire",get:function(){return this.count<this.maxConcurrent}}])&&function(t,r){for(var n=0;n<r.length;n++){var i=r[n];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(t,e(i.key),i)}}(r.prototype,n),Object.defineProperty(r,"prototype",{writable:!1}),r}(),o=/*#__PURE__*/function(){function e(e){void 0===e&&(e=1),this.semaphoreInstances=void 0,this.maxConcurrent=void 0,this.semaphoreInstances={},this.maxConcurrent=e}var o=e.prototype;return o.hasSemaphoreInstance=function(e){return void 0===e&&(e=t),Boolean(this.semaphoreInstances[e])},o.getSemaphoreInstance=function(e){return void 0===e&&(e=t),this.hasSemaphoreInstance(e)||(this.semaphoreInstances[e]=new i(this.maxConcurrent)),this.semaphoreInstances[e]},o.tidy=function(e){void 0===e&&(e=t),this.hasSemaphoreInstance(e)&&0===this.getSemaphoreInstance(e).count&&delete this.semaphoreInstances[e]},o.canAcquire=function(e){void 0===e&&(e=t);var r=n(e);return!this.hasSemaphoreInstance(r)||this.getSemaphoreInstance(r).canAcquire},o.acquire=function(e){void 0===e&&(e=t);var i,o,s=n(e),u=null!=(o=r(i=e)?0:i.priority)?o:0;return this.getSemaphoreInstance(s).acquire(u)},o.release=function(e){void 0===e&&(e=t);var r=n(e);this.getSemaphoreInstance(r).release(),this.tidy(r)},o.count=function(e){void 0===e&&(e=t);var r=n(e);return this.hasSemaphoreInstance(r)?this.getSemaphoreInstance(r).count:0},o.hasTasks=function(e){return void 0===e&&(e=t),this.count(e)>0},o.request=function(e,r){void 0===r&&(r=t);try{var n=this;return Promise.resolve(function(t,i){try{var o=Promise.resolve(n.acquire(r)).then(function(){return Promise.resolve(e())})}catch(e){return i(!0,e)}return o&&o.then?o.then(i.bind(null,!1),i.bind(null,!0)):i(!1,o)}(0,function(e,t){if(n.release(r),e)throw t;return t}))}catch(e){return Promise.reject(e)}},o.requestIfAvailable=function(e,r){void 0===r&&(r=t);try{return this.canAcquire(r)?Promise.resolve(this.request(e,r)):Promise.resolve(null)}catch(e){return Promise.reject(e)}},e}();exports.GroupSemaphore=/*#__PURE__*/function(){function e(){this._semaphore=new o,this._activeCounts={},this._groupWaiters={}}var t=e.prototype;return t.acquire=function(e){try{var t,r,n=this,i=null!=(t=n._activeCounts[e])?t:0;n._activeCounts[e]=i+1;var o=null!=(r=n._groupWaiters[e])?r:n._semaphore.acquire();return n._groupWaiters[e]=o,Promise.resolve(o).then(function(){})}catch(e){return Promise.reject(e)}},t.release=function(e){var t=this._activeCounts[e];1===t?(this._semaphore.release(),delete this._activeCounts[e],delete this._groupWaiters[e]):this._activeCounts[e]=t-1},e}(),exports.Semaphore=o;
|
|
2
2
|
//# sourceMappingURL=promise-semaphore.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"promise-semaphore.cjs","sources":["../src/semaphore.ts","../src/group-semaphore.ts"],"sourcesContent":["class SemaphoreItem {\n private queue: Function[];\n private maxConcurrent: number;\n\n /**\n * The number of locks.\n */\n public count: number;\n\n constructor(maxConcurrent: number) {\n this.queue = [];\n this.maxConcurrent = maxConcurrent;\n this.count = 0;\n }\n\n get canAcquire(): boolean {\n return this.count < this.maxConcurrent;\n }\n\n private incrementCount() {\n this.count++;\n }\n\n private decrementCount() {\n this.count--;\n }\n\n acquire(): Promise<void> {\n if (this.canAcquire) {\n this.incrementCount();\n return Promise.resolve();\n } else {\n return new Promise((resolve) => this.queue.push(resolve));\n }\n }\n\n release(): void {\n const resolveFunc = this.queue.shift();\n\n if (resolveFunc) {\n // Give the micro task queue a small break instead of calling resolveFunc() directly\n setTimeout(resolveFunc, 0);\n } else {\n this.decrementCount();\n }\n }\n}\n\nconst defaultKey = \"_default\";\n\nclass Semaphore {\n private semaphoreInstances: Record<string | number, SemaphoreItem>;\n private maxConcurrent: number;\n\n /**\n * @param {number} [maxConcurrent] The maximum number of concurrent locks.\n */\n constructor(maxConcurrent: number = 1) {\n this.semaphoreInstances = {};\n this.maxConcurrent = maxConcurrent;\n }\n\n private hasSemaphoreInstance(key: string | number = defaultKey) {\n return Boolean(this.semaphoreInstances[key]);\n }\n\n private getSemaphoreInstance(key: string | number = defaultKey) {\n if (!this.hasSemaphoreInstance(key)) {\n this.semaphoreInstances[key] = new SemaphoreItem(\n this.maxConcurrent,\n );\n }\n return this.semaphoreInstances[key];\n }\n\n /**\n * @param {string | number} [key]- Optional, the semaphore key.\n */\n private tidy(key: string | number = defaultKey): void {\n if (\n this.hasSemaphoreInstance(key) &&\n this.getSemaphoreInstance(key).count === 0\n ) {\n delete this.semaphoreInstances[key];\n }\n }\n\n /**\n * A synchronous function to determine whether a lock can be acquired.\n *\n * @param {string | number} [key]- Optional, the semaphore key.\n * @returns {boolean} Returns true if the lock on `key` can be acquired, false\n * otherwise.\n */\n canAcquire(key: string | number = defaultKey): boolean {\n return !this.hasSemaphoreInstance(key) ||\n this.getSemaphoreInstance(key).canAcquire;\n }\n\n /**\n * @param {string | number} [key]- Optional, the semaphore key.\n */\n acquire(key: string | number = defaultKey) {\n return this.getSemaphoreInstance(key).acquire();\n }\n\n /**\n * @param {string | number} [key]- Optional, the semaphore key.\n */\n release(key: string | number = defaultKey): void {\n this.getSemaphoreInstance(key).release();\n this.tidy(key);\n }\n\n /**\n * The number of active locks. Will always be less or equal to `max`.\n *\n * @param {string | number} [key]- Optional, the semaphore key.\n */\n count(key: string | number = defaultKey): number {\n if (this.hasSemaphoreInstance(key)) {\n return this.getSemaphoreInstance(key).count;\n } else {\n return 0;\n }\n }\n\n /**\n * @param {string | number} [key]- Optional, the semaphore key.\n * @returns {boolean} True if the semaphore and key has locks, false otherwise.\n */\n hasTasks(key: string | number = defaultKey): boolean {\n return this.count(key) > 0;\n }\n\n /**\n * @param {Function<T>} fn The function to execute.\n * @param {string | number} [key]- Optional, the semaphore key.\n * @returns {Promise<T>}\n */\n async request<T>(\n fn: Function,\n key: string | number = defaultKey,\n ): Promise<T> {\n try {\n await this.acquire(key);\n return await fn();\n } finally {\n this.release(key);\n }\n }\n\n /**\n * Asynchronously executes `fn` if a lock can be immediately acquired.\n * Otherwise, returns null.\n *\n * @param {Function<T>} fn The function to execute.\n * @param {string | number} [key]- Optional, the semaphore key.\n * @returns {Promise<T>}\n */\n async requestIfAvailable<T>(\n fn: Function,\n key: string | number = defaultKey,\n ): Promise<T | null> {\n if (this.canAcquire(key)) {\n return this.request(fn, key);\n } else {\n return null;\n }\n }\n}\n\nexport { Semaphore };\n","import { Semaphore } from \"./semaphore\";\n\n/**\n * GroupSemaphore manages a shared semaphore for different groups of tasks. Each\n * group is identified by a unique key, and the semaphore ensures only one group\n * can run its tasks concurrently.\n *\n * - acquire(key): Increments the active count for the given group. If it's the\n * first task for the group (active count is 0), it acquires the global\n * semaphore, ensuring only one group's tasks can proceed at a time.\n * Subsequent calls in the group increment the count and are permitted to run.\n * - release(key): Decrements the active count for the group. If the last task\n * for that group is released, it releases the global semaphore, allowing\n * other groups to proceed.\n *\n * This ensures that only one group can execute concurrently, but multiple tasks\n * within the same group can run as long as no other tasks from different groups\n * are active.\n */\nclass GroupSemaphore {\n private _semaphore = new Semaphore();\n private _activeCounts: Record<string, number> = {};\n private _groupWaiters: Record<string, Promise<void>> = {};\n\n async acquire(key: string) {\n const activeCount = this._activeCounts[key] ?? 0;\n this._activeCounts[key] = activeCount + 1;\n const waiter = this._groupWaiters[key] ?? this._semaphore.acquire();\n this._groupWaiters[key] = waiter;\n await waiter;\n }\n\n release(key: string) {\n const activeCount = this._activeCounts[key];\n\n if (activeCount === 1) {\n this._semaphore.release();\n delete this._activeCounts[key];\n delete this._groupWaiters[key];\n } else {\n this._activeCounts[key] = activeCount - 1;\n }\n }\n}\n\nexport { GroupSemaphore };\n"],"names":["SemaphoreItem","maxConcurrent","queue","this","count","_proto","prototype","incrementCount","decrementCount","acquire","_this","canAcquire","Promise","resolve","push","release","resolveFunc","shift","setTimeout","key","get","defaultKey","Semaphore","semaphoreInstances","_proto2","hasSemaphoreInstance","Boolean","getSemaphoreInstance","tidy","hasTasks","request","fn","_this2","then","_finallyRethrows","_wasThrown","_result","e","reject","requestIfAvailable","GroupSemaphore","_semaphore","_activeCounts","_groupWaiters","_this$_activeCounts$k","_this$_groupWaiters$k","activeCount","waiter"],"mappings":"mSAAMA,eAAa,WASf,SAAAA,EAAYC,GARJ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OApBwDnC,EAEnDI,iBAAQU,GAAW,QAAAyB,EAAAC,EAAAnC,EACDP,KAAd2C,EAAqC,OAA1BF,EAAGlC,EAAKgC,cAAcvB,IAAIyB,EAAI,EAC/ClC,EAAKgC,cAAcvB,GAAO2B,EAAc,EACxC,IAAMC,EAAgCF,OAA1BA,EAAGnC,EAAKiC,cAAcxB,IAAI0B,EAAInC,EAAK+B,WAAWhC,UACzB,OAAjCC,EAAKiC,cAAcxB,GAAO4B,EAAOnC,QAAAC,QAC3BkC,GAAMd,KAChB,WAAA,EAAA,CAAC,MAAAI,GAAAzB,OAAAA,QAAA0B,OAAAD,EAAAhC,CAAAA,EAAAA,EAEDU,QAAA,SAAQI,GACJ,IAAM2B,EAAc3C,KAAKuC,cAAcvB,GAEnB,IAAhB2B,GACA3C,KAAKsC,WAAW1B,iBACLZ,KAACuC,cAAcvB,UACnBhB,KAAKwC,cAAcxB,IAE1BhB,KAAKuC,cAAcvB,GAAO2B,EAAc,CAEhD,EAACN,CAAA"}
|
|
1
|
+
{"version":3,"file":"promise-semaphore.cjs","sources":["../src/semaphore.ts","../src/group-semaphore.ts"],"sourcesContent":["const defaultKey = \"_default\";\n\ntype KeyPrimitive = string | number;\n\ntype Key = KeyPrimitive | { key?: KeyPrimitive };\ntype KeyOptions = Key & { priority?: number };\n\nconst _isPrimitiveKey = (item: Key): item is KeyPrimitive =>\n [\"string\", \"number\"].includes(typeof item);\n\nconst resolveKey = (item: Key): KeyPrimitive =>\n (_isPrimitiveKey(item) ? item : item.key) ?? defaultKey;\n\nconst resolvePriority = (item: KeyOptions) =>\n (_isPrimitiveKey(item) ? 0 : item.priority) ?? 0;\n\nclass SemaphoreItem {\n private queue: Array<{\n resolve: Function;\n priority: number;\n }>;\n private maxConcurrent: number;\n\n /**\n * The number of locks.\n */\n public count: number;\n\n constructor(maxConcurrent: number) {\n this.queue = [];\n this.maxConcurrent = maxConcurrent;\n this.count = 0;\n }\n\n get canAcquire(): boolean {\n return this.count < this.maxConcurrent;\n }\n\n private incrementCount() {\n this.count++;\n }\n\n private decrementCount() {\n this.count--;\n }\n\n acquire(priority: number): Promise<void> {\n if (this.canAcquire) {\n this.incrementCount();\n return Promise.resolve();\n } else {\n return new Promise((resolve) => {\n this.queue.push({ resolve, priority });\n this.queue.sort((a, b) => b.priority - a.priority);\n });\n }\n }\n\n release(): void {\n const resolveFunc = this.queue.shift();\n\n if (resolveFunc) {\n // Give the micro task queue a small break instead of calling resolveFunc() directly\n setTimeout(resolveFunc.resolve, 0);\n } else {\n this.decrementCount();\n }\n }\n}\n\nclass Semaphore {\n private semaphoreInstances: Record<string | number, SemaphoreItem>;\n private maxConcurrent: number;\n\n /**\n * @param {number} [maxConcurrent] The maximum number of concurrent locks.\n */\n constructor(maxConcurrent: number = 1) {\n this.semaphoreInstances = {};\n this.maxConcurrent = maxConcurrent;\n }\n\n private hasSemaphoreInstance(key: KeyPrimitive = defaultKey) {\n return Boolean(this.semaphoreInstances[key]);\n }\n\n private getSemaphoreInstance(key: KeyPrimitive = defaultKey) {\n if (!this.hasSemaphoreInstance(key)) {\n this.semaphoreInstances[key] = new SemaphoreItem(\n this.maxConcurrent,\n );\n }\n return this.semaphoreInstances[key];\n }\n\n /**\n * @param {string | number} [key]- Optional, the semaphore key.\n */\n private tidy(key: KeyPrimitive = defaultKey): void {\n if (\n this.hasSemaphoreInstance(key) &&\n this.getSemaphoreInstance(key).count === 0\n ) {\n delete this.semaphoreInstances[key];\n }\n }\n\n /**\n * A synchronous function to determine whether a lock can be acquired.\n *\n * @param {string | number} [key]- Optional, the semaphore key.\n * @returns {boolean} Returns true if the lock on `key` can be acquired, false\n * otherwise.\n */\n canAcquire(key: Key = defaultKey): boolean {\n const _key = resolveKey(key);\n\n return !this.hasSemaphoreInstance(_key) ||\n this.getSemaphoreInstance(_key).canAcquire;\n }\n\n /**\n * @param {string | number} [key]- Optional, the semaphore key.\n */\n acquire(key: KeyOptions = defaultKey) {\n const _key = resolveKey(key);\n const _priority = resolvePriority(key);\n\n return this.getSemaphoreInstance(_key).acquire(_priority);\n }\n\n /**\n * @param {string | number} [key]- Optional, the semaphore key.\n */\n release(key: Key = defaultKey): void {\n const _key = resolveKey(key);\n\n this.getSemaphoreInstance(_key).release();\n this.tidy(_key);\n }\n\n /**\n * The number of active locks. Will always be less or equal to `max`.\n *\n * @param {string | number} [key]- Optional, the semaphore key.\n */\n count(key: Key = defaultKey): number {\n const _key = resolveKey(key);\n\n return (this.hasSemaphoreInstance(_key))\n ? this.getSemaphoreInstance(_key).count\n : 0;\n }\n\n /**\n * @param {string | number} [key]- Optional, the semaphore key.\n * @returns {boolean} True if the semaphore and key has locks, false otherwise.\n */\n hasTasks(key: Key = defaultKey): boolean {\n return this.count(key) > 0;\n }\n\n /**\n * @param {Function<T>} fn The function to execute.\n * @param {string | number} [key]- Optional, the semaphore key.\n * @returns {Promise<T>}\n */\n async request<T>(\n fn: Function,\n key: KeyOptions = defaultKey,\n ): Promise<T> {\n try {\n await this.acquire(key);\n return await fn();\n } finally {\n this.release(key);\n }\n }\n\n /**\n * Asynchronously executes `fn` if a lock can be immediately acquired.\n * Otherwise, returns null.\n *\n * @param {Function<T>} fn The function to execute.\n * @param {string | number} [key]- Optional, the semaphore key.\n * @returns {Promise<T>}\n */\n async requestIfAvailable<T>(\n fn: Function,\n key: KeyOptions = defaultKey,\n ): Promise<T | null> {\n if (this.canAcquire(key)) {\n return this.request(fn, key);\n } else {\n return null;\n }\n }\n}\n\nexport { Semaphore };\n","import { Semaphore } from \"./semaphore\";\n\n/**\n * GroupSemaphore manages a shared semaphore for different groups of tasks. Each\n * group is identified by a unique key, and the semaphore ensures only one group\n * can run its tasks concurrently.\n *\n * - acquire(key): Increments the active count for the given group. If it's the\n * first task for the group (active count is 0), it acquires the global\n * semaphore, ensuring only one group's tasks can proceed at a time.\n * Subsequent calls in the group increment the count and are permitted to run.\n * - release(key): Decrements the active count for the group. If the last task\n * for that group is released, it releases the global semaphore, allowing\n * other groups to proceed.\n *\n * This ensures that only one group can execute concurrently, but multiple tasks\n * within the same group can run as long as no other tasks from different groups\n * are active.\n */\nclass GroupSemaphore {\n private _semaphore = new Semaphore();\n private _activeCounts: Record<string, number> = {};\n private _groupWaiters: Record<string, Promise<void>> = {};\n\n async acquire(key: string) {\n const activeCount = this._activeCounts[key] ?? 0;\n this._activeCounts[key] = activeCount + 1;\n const waiter = this._groupWaiters[key] ?? this._semaphore.acquire();\n this._groupWaiters[key] = waiter;\n await waiter;\n }\n\n release(key: string) {\n const activeCount = this._activeCounts[key];\n\n if (activeCount === 1) {\n this._semaphore.release();\n delete this._activeCounts[key];\n delete this._groupWaiters[key];\n } else {\n this._activeCounts[key] = activeCount - 1;\n }\n }\n}\n\nexport { GroupSemaphore };\n"],"names":["defaultKey","_isPrimitiveKey","item","includes","resolveKey","_ref","key","SemaphoreItem","maxConcurrent","queue","count","this","_proto","prototype","incrementCount","decrementCount","acquire","priority","_this","canAcquire","Promise","resolve","push","sort","a","b","release","resolveFunc","shift","setTimeout","get","Semaphore","semaphoreInstances","_proto2","hasSemaphoreInstance","Boolean","getSemaphoreInstance","tidy","_key","_ref2","_priority","hasTasks","request","fn","_this2","then","_finallyRethrows","_wasThrown","_result","e","reject","requestIfAvailable","GroupSemaphore","_semaphore","_activeCounts","_groupWaiters","_this$_activeCounts$k","_this$_groupWaiters$k","activeCount","waiter"],"mappings":"+RAAA,IAAMA,EAAa,WAObC,EAAkB,SAACC,GAAS,MAC9B,CAAC,SAAU,UAAUC,gBAAgBD,EAAK,EAExCE,EAAa,SAACF,OAASG,EAAA,OACeA,OADfA,EACxBJ,EAAgBC,GAAQA,EAAOA,EAAKI,KAAGD,EAAKL,CAAU,EAKrDO,eAYF,WAAA,SAAAA,EAAYC,GAXJC,KAAAA,WAIAD,EAAAA,KAAAA,mBAKDE,EAAAA,KAAAA,aAGHC,KAAKF,MAAQ,GACbE,KAAKH,cAAgBA,EACrBG,KAAKD,MAAQ,CACjB,CAAC,QAAAE,EAAAL,EAAAM,iBA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WAAWb,GAChBc,QAAAC,QADAV,KACY+B,QAAQC,EAAIrC,IAExBc,QAAAC,QAAO,KAEf,CAAC,MAAA4B,GAAA7B,OAAAA,QAAA8B,OAAAD,EAAAlB,CAAAA,EAAAA,CAAA,CA9HU,2DCnDKqB,IAAAzC,KACR0C,WAAa,IAAItB,EAAWpB,KAC5B2C,cAAwC,CAAE,EAAA3C,KAC1C4C,cAA+C,EAAE,CAAA,IAAA3C,EAAAwC,EAAAvC,UAoBxDuC,OApBwDxC,EAEnDI,iBAAQV,GAAW,QAAAkD,EAAAC,EAAAvC,EACDP,KAAd+C,EAAqC,OAA1BF,EAAGtC,EAAKoC,cAAchD,IAAIkD,EAAI,EAC/CtC,EAAKoC,cAAchD,GAAOoD,EAAc,EACxC,IAAMC,EAAgCF,OAA1BA,EAAGvC,EAAKqC,cAAcjD,IAAImD,EAAIvC,EAAKmC,WAAWrC,UACzB,OAAjCE,EAAKqC,cAAcjD,GAAOqD,EAAOvC,QAAAC,QAC3BsC,GAAMd,KAChB,WAAA,EAAA,CAAC,MAAAI,GAAA7B,OAAAA,QAAA8B,OAAAD,EAAArC,CAAAA,EAAAA,EAEDc,QAAA,SAAQpB,GACJ,IAAMoD,EAAc/C,KAAK2C,cAAchD,GAEnB,IAAhBoD,GACA/C,KAAK0C,WAAW3B,iBACLf,KAAC2C,cAAchD,UACnBK,KAAK4C,cAAcjD,IAE1BK,KAAK2C,cAAchD,GAAOoD,EAAc,CAEhD,EAACN,CAAA"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
|
|
1
|
+
const e="_default",t=e=>["string","number"].includes(typeof e),s=s=>{var n;return null!=(n=t(s)?s:s.key)?n:e};class n{constructor(e){this.queue=void 0,this.maxConcurrent=void 0,this.count=void 0,this.queue=[],this.maxConcurrent=e,this.count=0}get canAcquire(){return this.count<this.maxConcurrent}incrementCount(){this.count++}decrementCount(){this.count--}acquire(e){return this.canAcquire?(this.incrementCount(),Promise.resolve()):new Promise(t=>{this.queue.push({resolve:t,priority:e}),this.queue.sort((e,t)=>t.priority-e.priority)})}release(){const e=this.queue.shift();e?setTimeout(e.resolve,0):this.decrementCount()}}class r{constructor(e=1){this.semaphoreInstances=void 0,this.maxConcurrent=void 0,this.semaphoreInstances={},this.maxConcurrent=e}hasSemaphoreInstance(t=e){return Boolean(this.semaphoreInstances[t])}getSemaphoreInstance(t=e){return this.hasSemaphoreInstance(t)||(this.semaphoreInstances[t]=new n(this.maxConcurrent)),this.semaphoreInstances[t]}tidy(t=e){this.hasSemaphoreInstance(t)&&0===this.getSemaphoreInstance(t).count&&delete this.semaphoreInstances[t]}canAcquire(t=e){const n=s(t);return!this.hasSemaphoreInstance(n)||this.getSemaphoreInstance(n).canAcquire}acquire(n=e){const r=s(n),i=null!=(o=t(a=n)?0:a.priority)?o:0;var a,o;return this.getSemaphoreInstance(r).acquire(i)}release(t=e){const n=s(t);this.getSemaphoreInstance(n).release(),this.tidy(n)}count(t=e){const n=s(t);return this.hasSemaphoreInstance(n)?this.getSemaphoreInstance(n).count:0}hasTasks(t=e){return this.count(t)>0}async request(t,s=e){try{return await this.acquire(s),await t()}finally{this.release(s)}}async requestIfAvailable(t,s=e){return this.canAcquire(s)?this.request(t,s):null}}class i{constructor(){this._semaphore=new r,this._activeCounts={},this._groupWaiters={}}async acquire(e){var t,s;const n=null!=(t=this._activeCounts[e])?t:0;this._activeCounts[e]=n+1;const r=null!=(s=this._groupWaiters[e])?s:this._semaphore.acquire();this._groupWaiters[e]=r,await r}release(e){const t=this._activeCounts[e];1===t?(this._semaphore.release(),delete this._activeCounts[e],delete this._groupWaiters[e]):this._activeCounts[e]=t-1}}export{i as GroupSemaphore,r as Semaphore};
|
|
2
2
|
//# sourceMappingURL=promise-semaphore.modern.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"promise-semaphore.modern.js","sources":["../src/semaphore.ts","../src/group-semaphore.ts"],"sourcesContent":["class SemaphoreItem {\n private queue: Function[];\n private maxConcurrent: number;\n\n /**\n * The number of locks.\n */\n public count: number;\n\n constructor(maxConcurrent: number) {\n this.queue = [];\n this.maxConcurrent = maxConcurrent;\n this.count = 0;\n }\n\n get canAcquire(): boolean {\n return this.count < this.maxConcurrent;\n }\n\n private incrementCount() {\n this.count++;\n }\n\n private decrementCount() {\n this.count--;\n }\n\n acquire(): Promise<void> {\n if (this.canAcquire) {\n this.incrementCount();\n return Promise.resolve();\n } else {\n return new Promise((resolve) => this.queue.push(resolve));\n }\n }\n\n release(): void {\n const resolveFunc = this.queue.shift();\n\n if (resolveFunc) {\n // Give the micro task queue a small break instead of calling resolveFunc() directly\n setTimeout(resolveFunc, 0);\n } else {\n this.decrementCount();\n }\n }\n}\n\nconst defaultKey = \"_default\";\n\nclass Semaphore {\n private semaphoreInstances: Record<string | number, SemaphoreItem>;\n private maxConcurrent: number;\n\n /**\n * @param {number} [maxConcurrent] The maximum number of concurrent locks.\n */\n constructor(maxConcurrent: number = 1) {\n this.semaphoreInstances = {};\n this.maxConcurrent = maxConcurrent;\n }\n\n private hasSemaphoreInstance(key: string | number = defaultKey) {\n return Boolean(this.semaphoreInstances[key]);\n }\n\n private getSemaphoreInstance(key: string | number = defaultKey) {\n if (!this.hasSemaphoreInstance(key)) {\n this.semaphoreInstances[key] = new SemaphoreItem(\n this.maxConcurrent,\n );\n }\n return this.semaphoreInstances[key];\n }\n\n /**\n * @param {string | number} [key]- Optional, the semaphore key.\n */\n private tidy(key: string | number = defaultKey): void {\n if (\n this.hasSemaphoreInstance(key) &&\n this.getSemaphoreInstance(key).count === 0\n ) {\n delete this.semaphoreInstances[key];\n }\n }\n\n /**\n * A synchronous function to determine whether a lock can be acquired.\n *\n * @param {string | number} [key]- Optional, the semaphore key.\n * @returns {boolean} Returns true if the lock on `key` can be acquired, false\n * otherwise.\n */\n canAcquire(key: string | number = defaultKey): boolean {\n return !this.hasSemaphoreInstance(key) ||\n this.getSemaphoreInstance(key).canAcquire;\n }\n\n /**\n * @param {string | number} [key]- Optional, the semaphore key.\n */\n acquire(key: string | number = defaultKey) {\n return this.getSemaphoreInstance(key).acquire();\n }\n\n /**\n * @param {string | number} [key]- Optional, the semaphore key.\n */\n release(key: string | number = defaultKey): void {\n this.getSemaphoreInstance(key).release();\n this.tidy(key);\n }\n\n /**\n * The number of active locks. Will always be less or equal to `max`.\n *\n * @param {string | number} [key]- Optional, the semaphore key.\n */\n count(key: string | number = defaultKey): number {\n if (this.hasSemaphoreInstance(key)) {\n return this.getSemaphoreInstance(key).count;\n } else {\n return 0;\n }\n }\n\n /**\n * @param {string | number} [key]- Optional, the semaphore key.\n * @returns {boolean} True if the semaphore and key has locks, false otherwise.\n */\n hasTasks(key: string | number = defaultKey): boolean {\n return this.count(key) > 0;\n }\n\n /**\n * @param {Function<T>} fn The function to execute.\n * @param {string | number} [key]- Optional, the semaphore key.\n * @returns {Promise<T>}\n */\n async request<T>(\n fn: Function,\n key: string | number = defaultKey,\n ): Promise<T> {\n try {\n await this.acquire(key);\n return await fn();\n } finally {\n this.release(key);\n }\n }\n\n /**\n * Asynchronously executes `fn` if a lock can be immediately acquired.\n * Otherwise, returns null.\n *\n * @param {Function<T>} fn The function to execute.\n * @param {string | number} [key]- Optional, the semaphore key.\n * @returns {Promise<T>}\n */\n async requestIfAvailable<T>(\n fn: Function,\n key: string | number = defaultKey,\n ): Promise<T | null> {\n if (this.canAcquire(key)) {\n return this.request(fn, key);\n } else {\n return null;\n }\n }\n}\n\nexport { Semaphore };\n","import { Semaphore } from \"./semaphore\";\n\n/**\n * GroupSemaphore manages a shared semaphore for different groups of tasks. Each\n * group is identified by a unique key, and the semaphore ensures only one group\n * can run its tasks concurrently.\n *\n * - acquire(key): Increments the active count for the given group. If it's the\n * first task for the group (active count is 0), it acquires the global\n * semaphore, ensuring only one group's tasks can proceed at a time.\n * Subsequent calls in the group increment the count and are permitted to run.\n * - release(key): Decrements the active count for the group. If the last task\n * for that group is released, it releases the global semaphore, allowing\n * other groups to proceed.\n *\n * This ensures that only one group can execute concurrently, but multiple tasks\n * within the same group can run as long as no other tasks from different groups\n * are active.\n */\nclass GroupSemaphore {\n private _semaphore = new Semaphore();\n private _activeCounts: Record<string, number> = {};\n private _groupWaiters: Record<string, Promise<void>> = {};\n\n async acquire(key: string) {\n const activeCount = this._activeCounts[key] ?? 0;\n this._activeCounts[key] = activeCount + 1;\n const waiter = this._groupWaiters[key] ?? this._semaphore.acquire();\n this._groupWaiters[key] = waiter;\n await waiter;\n }\n\n release(key: string) {\n const activeCount = this._activeCounts[key];\n\n if (activeCount === 1) {\n this._semaphore.release();\n delete this._activeCounts[key];\n delete this._groupWaiters[key];\n } else {\n this._activeCounts[key] = activeCount - 1;\n }\n }\n}\n\nexport { GroupSemaphore };\n"],"names":["SemaphoreItem","constructor","maxConcurrent","this","queue","count","canAcquire","incrementCount","decrementCount","acquire","Promise","resolve","push","release","resolveFunc","shift","setTimeout","defaultKey","Semaphore","semaphoreInstances","hasSemaphoreInstance","key","Boolean","getSemaphoreInstance","tidy","hasTasks","request","fn","requestIfAvailable","GroupSemaphore","_semaphore","_activeCounts","_groupWaiters","_this$_activeCounts$k","_this$_groupWaiters$k","activeCount","waiter"],"mappings":"AAAA,MAAMA,EASFC,WAAAA,CAAYC,GAAqBC,KARzBC,WAAK,EAAAD,KACLD,mBAAa,EAAAC,KAKdE,WAGH,EAAAF,KAAKC,MAAQ,GACbD,KAAKD,cAAgBA,EACrBC,KAAKE,MAAQ,CACjB,CAEA,cAAIC,GACA,OAAOH,KAAKE,MAAQF,KAAKD,aAC7B,CAEQK,cAAAA,GACJJ,KAAKE,OACT,CAEQG,cAAAA,GACJL,KAAKE,OACT,CAEAI,OAAAA,GACI,OAAIN,KAAKG,YACLH,KAAKI,iBACEG,QAAQC,WAER,IAAID,QAASC,GAAYR,KAAKC,MAAMQ,KAAKD,GAExD,CAEAE,OAAAA,GACI,MAAMC,EAAcX,KAAKC,MAAMW,QAE3BD,EAEAE,WAAWF,EAAa,GAExBX,KAAKK,gBAEb,EAGJ,MA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MQ,EAAc5B,WAAAA,GAAAE,KACR2B,WAAa,IAAIZ,OACjBa,cAAwC,CAAE,OAC1CC,cAA+C,CAAA,CAAE,CAEzD,aAAMvB,CAAQY,GAAW,IAAAY,EAAAC,EACrB,MAAMC,EAAqCF,OAA1BA,EAAG9B,KAAK4B,cAAcV,IAAIY,EAAI,EAC/C9B,KAAK4B,cAAcV,GAAOc,EAAc,EACxC,MAAMC,EAAgC,OAA1BF,EAAG/B,KAAK6B,cAAcX,IAAIa,EAAI/B,KAAK2B,WAAWrB,UAC1DN,KAAK6B,cAAcX,GAAOe,QACpBA,CACV,CAEAvB,OAAAA,CAAQQ,GACJ,MAAMc,EAAchC,KAAK4B,cAAcV,GAEnB,IAAhBc,GACAhC,KAAK2B,WAAWjB,iBACTV,KAAK4B,cAAcV,UACnBlB,KAAK6B,cAAcX,IAE1BlB,KAAK4B,cAAcV,GAAOc,EAAc,CAEhD"}
|
|
1
|
+
{"version":3,"file":"promise-semaphore.modern.js","sources":["../src/semaphore.ts","../src/group-semaphore.ts"],"sourcesContent":["const defaultKey = \"_default\";\n\ntype KeyPrimitive = string | number;\n\ntype Key = KeyPrimitive | { key?: KeyPrimitive };\ntype KeyOptions = Key & { priority?: number };\n\nconst _isPrimitiveKey = (item: Key): item is KeyPrimitive =>\n [\"string\", \"number\"].includes(typeof item);\n\nconst resolveKey = (item: Key): KeyPrimitive =>\n (_isPrimitiveKey(item) ? item : item.key) ?? defaultKey;\n\nconst resolvePriority = (item: KeyOptions) =>\n (_isPrimitiveKey(item) ? 0 : item.priority) ?? 0;\n\nclass SemaphoreItem {\n private queue: Array<{\n resolve: Function;\n priority: number;\n }>;\n private maxConcurrent: number;\n\n /**\n * The number of locks.\n */\n public count: number;\n\n constructor(maxConcurrent: number) {\n this.queue = [];\n this.maxConcurrent = maxConcurrent;\n this.count = 0;\n }\n\n get canAcquire(): boolean {\n return this.count < this.maxConcurrent;\n }\n\n private incrementCount() {\n this.count++;\n }\n\n private decrementCount() {\n this.count--;\n }\n\n acquire(priority: number): Promise<void> {\n if (this.canAcquire) {\n this.incrementCount();\n return Promise.resolve();\n } else {\n return new Promise((resolve) => {\n this.queue.push({ resolve, priority });\n this.queue.sort((a, b) => b.priority - a.priority);\n });\n }\n }\n\n release(): void {\n const resolveFunc = this.queue.shift();\n\n if (resolveFunc) {\n // Give the micro task queue a small break instead of calling resolveFunc() directly\n setTimeout(resolveFunc.resolve, 0);\n } else {\n this.decrementCount();\n }\n }\n}\n\nclass Semaphore {\n private semaphoreInstances: Record<string | number, SemaphoreItem>;\n private maxConcurrent: number;\n\n /**\n * @param {number} [maxConcurrent] The maximum number of concurrent locks.\n */\n constructor(maxConcurrent: number = 1) {\n this.semaphoreInstances = {};\n this.maxConcurrent = maxConcurrent;\n }\n\n private hasSemaphoreInstance(key: KeyPrimitive = defaultKey) {\n return Boolean(this.semaphoreInstances[key]);\n }\n\n private getSemaphoreInstance(key: KeyPrimitive = defaultKey) {\n if (!this.hasSemaphoreInstance(key)) {\n this.semaphoreInstances[key] = new SemaphoreItem(\n this.maxConcurrent,\n );\n }\n return this.semaphoreInstances[key];\n }\n\n /**\n * @param {string | number} [key]- Optional, the semaphore key.\n */\n private tidy(key: KeyPrimitive = defaultKey): void {\n if (\n this.hasSemaphoreInstance(key) &&\n this.getSemaphoreInstance(key).count === 0\n ) {\n delete this.semaphoreInstances[key];\n }\n }\n\n /**\n * A synchronous function to determine whether a lock can be acquired.\n *\n * @param {string | number} [key]- Optional, the semaphore key.\n * @returns {boolean} Returns true if the lock on `key` can be acquired, false\n * otherwise.\n */\n canAcquire(key: Key = defaultKey): boolean {\n const _key = resolveKey(key);\n\n return !this.hasSemaphoreInstance(_key) ||\n this.getSemaphoreInstance(_key).canAcquire;\n }\n\n /**\n * @param {string | number} [key]- Optional, the semaphore key.\n */\n acquire(key: KeyOptions = defaultKey) {\n const _key = resolveKey(key);\n const _priority = resolvePriority(key);\n\n return this.getSemaphoreInstance(_key).acquire(_priority);\n }\n\n /**\n * @param {string | number} [key]- Optional, the semaphore key.\n */\n release(key: Key = defaultKey): void {\n const _key = resolveKey(key);\n\n this.getSemaphoreInstance(_key).release();\n this.tidy(_key);\n }\n\n /**\n * The number of active locks. Will always be less or equal to `max`.\n *\n * @param {string | number} [key]- Optional, the semaphore key.\n */\n count(key: Key = defaultKey): number {\n const _key = resolveKey(key);\n\n return (this.hasSemaphoreInstance(_key))\n ? this.getSemaphoreInstance(_key).count\n : 0;\n }\n\n /**\n * @param {string | number} [key]- Optional, the semaphore key.\n * @returns {boolean} True if the semaphore and key has locks, false otherwise.\n */\n hasTasks(key: Key = defaultKey): boolean {\n return this.count(key) > 0;\n }\n\n /**\n * @param {Function<T>} fn The function to execute.\n * @param {string | number} [key]- Optional, the semaphore key.\n * @returns {Promise<T>}\n */\n async request<T>(\n fn: Function,\n key: KeyOptions = defaultKey,\n ): Promise<T> {\n try {\n await this.acquire(key);\n return await fn();\n } finally {\n this.release(key);\n }\n }\n\n /**\n * Asynchronously executes `fn` if a lock can be immediately acquired.\n * Otherwise, returns null.\n *\n * @param {Function<T>} fn The function to execute.\n * @param {string | number} [key]- Optional, the semaphore key.\n * @returns {Promise<T>}\n */\n async requestIfAvailable<T>(\n fn: Function,\n key: KeyOptions = defaultKey,\n ): Promise<T | null> {\n if (this.canAcquire(key)) {\n return this.request(fn, key);\n } else {\n return null;\n }\n }\n}\n\nexport { Semaphore };\n","import { Semaphore } from \"./semaphore\";\n\n/**\n * GroupSemaphore manages a shared semaphore for different groups of tasks. Each\n * group is identified by a unique key, and the semaphore ensures only one group\n * can run its tasks concurrently.\n *\n * - acquire(key): Increments the active count for the given group. If it's the\n * first task for the group (active count is 0), it acquires the global\n * semaphore, ensuring only one group's tasks can proceed at a time.\n * Subsequent calls in the group increment the count and are permitted to run.\n * - release(key): Decrements the active count for the group. If the last task\n * for that group is released, it releases the global semaphore, allowing\n * other groups to proceed.\n *\n * This ensures that only one group can execute concurrently, but multiple tasks\n * within the same group can run as long as no other tasks from different groups\n * are active.\n */\nclass GroupSemaphore {\n private _semaphore = new Semaphore();\n private _activeCounts: Record<string, number> = {};\n private _groupWaiters: Record<string, Promise<void>> = {};\n\n async acquire(key: string) {\n const activeCount = this._activeCounts[key] ?? 0;\n this._activeCounts[key] = activeCount + 1;\n const waiter = this._groupWaiters[key] ?? this._semaphore.acquire();\n this._groupWaiters[key] = waiter;\n await waiter;\n }\n\n release(key: string) {\n const activeCount = this._activeCounts[key];\n\n if (activeCount === 1) {\n this._semaphore.release();\n delete this._activeCounts[key];\n delete this._groupWaiters[key];\n } else {\n this._activeCounts[key] = activeCount - 1;\n }\n }\n}\n\nexport { GroupSemaphore };\n"],"names":["defaultKey","_isPrimitiveKey","item","includes","resolveKey","_ref","key","SemaphoreItem","constructor","maxConcurrent","this","queue","count","canAcquire","incrementCount","decrementCount","acquire","priority","Promise","resolve","push","sort","a","b","release","resolveFunc","shift","setTimeout","Semaphore","semaphoreInstances","hasSemaphoreInstance","Boolean","getSemaphoreInstance","tidy","_key","_priority","_ref2","hasTasks","request","fn","requestIfAvailable","GroupSemaphore","_semaphore","_activeCounts","_groupWaiters","_this$_activeCounts$k","_this$_groupWaiters$k","activeCount","waiter"],"mappings":"AAAA,MAAMA,EAAa,WAObC,EAAmBC,GACrB,CAAC,SAAU,UAAUC,gBAAgBD,GAEnCE,EAAcF,IAASG,IAAAA,EAAAA,OACe,OADfA,EACxBJ,EAAgBC,GAAQA,EAAOA,EAAKI,KAAGD,EAAKL,GAKjD,MAAMO,EAYFC,WAAAA,CAAYC,GAAqBC,KAXzBC,WAAK,EAAAD,KAILD,mBAKDG,EAAAA,KAAAA,WAGH,EAAAF,KAAKC,MAAQ,GACbD,KAAKD,cAAgBA,EACrBC,KAAKE,MAAQ,CACjB,CAEA,cAAIC,GACA,OAAWH,KAACE,MAAQF,KAAKD,aAC7B,CAEQK,cAAAA,GACJJ,KAAKE,OACT,CAEQG,cAAAA,GACJL,KAAKE,OACT,CAEAI,OAAAA,CAAQC,GACJ,OAAIP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AMV,UAChCd,KAAKuB,KAAKC,EACd,CAOAtB,KAAAA,CAAMN,EAAWN,GACb,MAAMkC,EAAO9B,EAAWE,GAExB,OAAYI,KAACoB,qBAAqBI,GAC5BxB,KAAKsB,qBAAqBE,GAAMtB,MAChC,CACV,CAMAyB,QAAAA,CAAS/B,EAAWN,GAChB,OAAWU,KAACE,MAAMN,GAAO,CAC7B,CAOA,aAAMgC,CACFC,EACAjC,EAAkBN,GAElB,IAEI,aADMU,KAAKM,QAAQV,SACNiC,GAChB,CAAA,QACG7B,KAAKc,QAAQlB,EAChB,CACL,CAUA,wBAAMkC,CACFD,EACAjC,EAAkBN,GAElB,OAAIU,KAAKG,WAAWP,GACTI,KAAK4B,QAAQC,EAAIjC,GAG3B,IACL,ECjLJ,MAAMmC,EAAcjC,WAAAA,GAAAE,KACRgC,WAAa,IAAId,OACjBe,cAAwC,CAAE,OAC1CC,cAA+C,CAAA,CAAE,CAEzD,aAAM5B,CAAQV,GAAW,IAAAuC,EAAAC,EACrB,MAAMC,EAAqCF,OAA1BA,EAAGnC,KAAKiC,cAAcrC,IAAIuC,EAAI,EAC/CnC,KAAKiC,cAAcrC,GAAOyC,EAAc,EACxC,MAAMC,EAAgC,OAA1BF,EAAGpC,KAAKkC,cAActC,IAAIwC,EAAIpC,KAAKgC,WAAW1B,UAC1DN,KAAKkC,cAActC,GAAO0C,QACpBA,CACV,CAEAxB,OAAAA,CAAQlB,GACJ,MAAMyC,EAAcrC,KAAKiC,cAAcrC,GAEnB,IAAhByC,GACArC,KAAKgC,WAAWlB,iBACTd,KAAKiC,cAAcrC,UACnBI,KAAKkC,cAActC,IAE1BI,KAAKiC,cAAcrC,GAAOyC,EAAc,CAEhD"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
function e(e){var t=function(e){if("object"!=typeof e||!e)return e;var t=e[Symbol.toPrimitive];if(void 0!==t){var
|
|
1
|
+
function e(e){var t=function(e){if("object"!=typeof e||!e)return e;var t=e[Symbol.toPrimitive];if(void 0!==t){var r=t.call(e,"string");if("object"!=typeof r)return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(e)}(e);return"symbol"==typeof t?t:t+""}var t="_default",r=function(e){return["string","number"].includes(typeof e)},n=function(e){var n;return null!=(n=r(e)?e:e.key)?n:t},i=/*#__PURE__*/function(){function t(e){this.queue=void 0,this.maxConcurrent=void 0,this.count=void 0,this.queue=[],this.maxConcurrent=e,this.count=0}var r,n,i=t.prototype;return i.incrementCount=function(){this.count++},i.decrementCount=function(){this.count--},i.acquire=function(e){var t=this;return this.canAcquire?(this.incrementCount(),Promise.resolve()):new Promise(function(r){t.queue.push({resolve:r,priority:e}),t.queue.sort(function(e,t){return t.priority-e.priority})})},i.release=function(){var e=this.queue.shift();e?setTimeout(e.resolve,0):this.decrementCount()},r=t,(n=[{key:"canAcquire",get:function(){return this.count<this.maxConcurrent}}])&&function(t,r){for(var n=0;n<r.length;n++){var i=r[n];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(t,e(i.key),i)}}(r.prototype,n),Object.defineProperty(r,"prototype",{writable:!1}),r}(),o=/*#__PURE__*/function(){function e(e){void 0===e&&(e=1),this.semaphoreInstances=void 0,this.maxConcurrent=void 0,this.semaphoreInstances={},this.maxConcurrent=e}var o=e.prototype;return o.hasSemaphoreInstance=function(e){return void 0===e&&(e=t),Boolean(this.semaphoreInstances[e])},o.getSemaphoreInstance=function(e){return void 0===e&&(e=t),this.hasSemaphoreInstance(e)||(this.semaphoreInstances[e]=new i(this.maxConcurrent)),this.semaphoreInstances[e]},o.tidy=function(e){void 0===e&&(e=t),this.hasSemaphoreInstance(e)&&0===this.getSemaphoreInstance(e).count&&delete this.semaphoreInstances[e]},o.canAcquire=function(e){void 0===e&&(e=t);var r=n(e);return!this.hasSemaphoreInstance(r)||this.getSemaphoreInstance(r).canAcquire},o.acquire=function(e){void 0===e&&(e=t);var i,o,s=n(e),u=null!=(o=r(i=e)?0:i.priority)?o:0;return this.getSemaphoreInstance(s).acquire(u)},o.release=function(e){void 0===e&&(e=t);var r=n(e);this.getSemaphoreInstance(r).release(),this.tidy(r)},o.count=function(e){void 0===e&&(e=t);var r=n(e);return this.hasSemaphoreInstance(r)?this.getSemaphoreInstance(r).count:0},o.hasTasks=function(e){return void 0===e&&(e=t),this.count(e)>0},o.request=function(e,r){void 0===r&&(r=t);try{var n=this;return Promise.resolve(function(t,i){try{var o=Promise.resolve(n.acquire(r)).then(function(){return Promise.resolve(e())})}catch(e){return i(!0,e)}return o&&o.then?o.then(i.bind(null,!1),i.bind(null,!0)):i(!1,o)}(0,function(e,t){if(n.release(r),e)throw t;return t}))}catch(e){return Promise.reject(e)}},o.requestIfAvailable=function(e,r){void 0===r&&(r=t);try{return this.canAcquire(r)?Promise.resolve(this.request(e,r)):Promise.resolve(null)}catch(e){return Promise.reject(e)}},e}(),s=/*#__PURE__*/function(){function e(){this._semaphore=new o,this._activeCounts={},this._groupWaiters={}}var t=e.prototype;return t.acquire=function(e){try{var t,r,n=this,i=null!=(t=n._activeCounts[e])?t:0;n._activeCounts[e]=i+1;var o=null!=(r=n._groupWaiters[e])?r:n._semaphore.acquire();return n._groupWaiters[e]=o,Promise.resolve(o).then(function(){})}catch(e){return Promise.reject(e)}},t.release=function(e){var t=this._activeCounts[e];1===t?(this._semaphore.release(),delete this._activeCounts[e],delete this._groupWaiters[e]):this._activeCounts[e]=t-1},e}();export{s as GroupSemaphore,o as Semaphore};
|
|
2
2
|
//# sourceMappingURL=promise-semaphore.module.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"promise-semaphore.module.js","sources":["../src/semaphore.ts","../src/group-semaphore.ts"],"sourcesContent":["class SemaphoreItem {\n private queue: Function[];\n private maxConcurrent: number;\n\n /**\n * The number of locks.\n */\n public count: number;\n\n constructor(maxConcurrent: number) {\n this.queue = [];\n this.maxConcurrent = maxConcurrent;\n this.count = 0;\n }\n\n get canAcquire(): boolean {\n return this.count < this.maxConcurrent;\n }\n\n private incrementCount() {\n this.count++;\n }\n\n private decrementCount() {\n this.count--;\n }\n\n acquire(): Promise<void> {\n if (this.canAcquire) {\n this.incrementCount();\n return Promise.resolve();\n } else {\n return new Promise((resolve) => this.queue.push(resolve));\n }\n }\n\n release(): void {\n const resolveFunc = this.queue.shift();\n\n if (resolveFunc) {\n // Give the micro task queue a small break instead of calling resolveFunc() directly\n setTimeout(resolveFunc, 0);\n } else {\n this.decrementCount();\n }\n }\n}\n\nconst defaultKey = \"_default\";\n\nclass Semaphore {\n private semaphoreInstances: Record<string | number, SemaphoreItem>;\n private maxConcurrent: number;\n\n /**\n * @param {number} [maxConcurrent] The maximum number of concurrent locks.\n */\n constructor(maxConcurrent: number = 1) {\n this.semaphoreInstances = {};\n this.maxConcurrent = maxConcurrent;\n }\n\n private hasSemaphoreInstance(key: string | number = defaultKey) {\n return Boolean(this.semaphoreInstances[key]);\n }\n\n private getSemaphoreInstance(key: string | number = defaultKey) {\n if (!this.hasSemaphoreInstance(key)) {\n this.semaphoreInstances[key] = new SemaphoreItem(\n this.maxConcurrent,\n );\n }\n return this.semaphoreInstances[key];\n }\n\n /**\n * @param {string | number} [key]- Optional, the semaphore key.\n */\n private tidy(key: string | number = defaultKey): void {\n if (\n this.hasSemaphoreInstance(key) &&\n this.getSemaphoreInstance(key).count === 0\n ) {\n delete this.semaphoreInstances[key];\n }\n }\n\n /**\n * A synchronous function to determine whether a lock can be acquired.\n *\n * @param {string | number} [key]- Optional, the semaphore key.\n * @returns {boolean} Returns true if the lock on `key` can be acquired, false\n * otherwise.\n */\n canAcquire(key: string | number = defaultKey): boolean {\n return !this.hasSemaphoreInstance(key) ||\n this.getSemaphoreInstance(key).canAcquire;\n }\n\n /**\n * @param {string | number} [key]- Optional, the semaphore key.\n */\n acquire(key: string | number = defaultKey) {\n return this.getSemaphoreInstance(key).acquire();\n }\n\n /**\n * @param {string | number} [key]- Optional, the semaphore key.\n */\n release(key: string | number = defaultKey): void {\n this.getSemaphoreInstance(key).release();\n this.tidy(key);\n }\n\n /**\n * The number of active locks. Will always be less or equal to `max`.\n *\n * @param {string | number} [key]- Optional, the semaphore key.\n */\n count(key: string | number = defaultKey): number {\n if (this.hasSemaphoreInstance(key)) {\n return this.getSemaphoreInstance(key).count;\n } else {\n return 0;\n }\n }\n\n /**\n * @param {string | number} [key]- Optional, the semaphore key.\n * @returns {boolean} True if the semaphore and key has locks, false otherwise.\n */\n hasTasks(key: string | number = defaultKey): boolean {\n return this.count(key) > 0;\n }\n\n /**\n * @param {Function<T>} fn The function to execute.\n * @param {string | number} [key]- Optional, the semaphore key.\n * @returns {Promise<T>}\n */\n async request<T>(\n fn: Function,\n key: string | number = defaultKey,\n ): Promise<T> {\n try {\n await this.acquire(key);\n return await fn();\n } finally {\n this.release(key);\n }\n }\n\n /**\n * Asynchronously executes `fn` if a lock can be immediately acquired.\n * Otherwise, returns null.\n *\n * @param {Function<T>} fn The function to execute.\n * @param {string | number} [key]- Optional, the semaphore key.\n * @returns {Promise<T>}\n */\n async requestIfAvailable<T>(\n fn: Function,\n key: string | number = defaultKey,\n ): Promise<T | null> {\n if (this.canAcquire(key)) {\n return this.request(fn, key);\n } else {\n return null;\n }\n }\n}\n\nexport { Semaphore };\n","import { Semaphore } from \"./semaphore\";\n\n/**\n * GroupSemaphore manages a shared semaphore for different groups of tasks. Each\n * group is identified by a unique key, and the semaphore ensures only one group\n * can run its tasks concurrently.\n *\n * - acquire(key): Increments the active count for the given group. If it's the\n * first task for the group (active count is 0), it acquires the global\n * semaphore, ensuring only one group's tasks can proceed at a time.\n * Subsequent calls in the group increment the count and are permitted to run.\n * - release(key): Decrements the active count for the group. If the last task\n * for that group is released, it releases the global semaphore, allowing\n * other groups to proceed.\n *\n * This ensures that only one group can execute concurrently, but multiple tasks\n * within the same group can run as long as no other tasks from different groups\n * are active.\n */\nclass GroupSemaphore {\n private _semaphore = new Semaphore();\n private _activeCounts: Record<string, number> = {};\n private _groupWaiters: Record<string, Promise<void>> = {};\n\n async acquire(key: string) {\n const activeCount = this._activeCounts[key] ?? 0;\n this._activeCounts[key] = activeCount + 1;\n const waiter = this._groupWaiters[key] ?? this._semaphore.acquire();\n this._groupWaiters[key] = waiter;\n await waiter;\n }\n\n release(key: string) {\n const activeCount = this._activeCounts[key];\n\n if (activeCount === 1) {\n this._semaphore.release();\n delete this._activeCounts[key];\n delete this._groupWaiters[key];\n } else {\n this._activeCounts[key] = activeCount - 1;\n }\n }\n}\n\nexport { GroupSemaphore };\n"],"names":["SemaphoreItem","maxConcurrent","queue","this","count","_proto","prototype","incrementCount","decrementCount","acquire","_this","canAcquire","Promise","resolve","push","release","resolveFunc","shift","setTimeout","key","get","defaultKey","Semaphore","semaphoreInstances","_proto2","hasSemaphoreInstance","Boolean","getSemaphoreInstance","tidy","hasTasks","request","fn","_this2","then","_finallyRethrows","_wasThrown","_result","e","reject","requestIfAvailable","GroupSemaphore","_semaphore","_activeCounts","_groupWaiters","_this$_activeCounts$k","_this$_groupWaiters$k","activeCount","waiter"],"mappings":"mSAAMA,eAAa,WASf,SAAAA,EAAYC,GARJC,KAAAA,kBACAD,mBAAa,EAAAE,KAKdC,WAGH,EAAAD,KAAKD,MAAQ,GACbC,KAAKF,cAAgBA,EACrBE,KAAKC,MAAQ,CACjB,CAAC,QAAAC,EAAAL,EAAAM,iBAAAD,EAMOE,eAAA,WACJJ,KAAKC,OACT,EAACC,EAEOG,eAAA,WACJL,KAAKC,OACT,EAACC,EAEDI,QAAA,WAAOC,IAAAA,OACH,OAAIP,KAAKQ,YACLR,KAAKI,iBACEK,QAAQC,eAEJD,QAAQ,SAACC,UAAYH,EAAKR,MAAMY,KAAKD,EAAQ,EAEhE,EAACR,EAEDU,QAAA,WACI,IAAMC,EAAcb,KAAKD,MAAMe,QAE3BD,EAEAE,WAAWF,EAAa,GAExBb,KAAKK,gBAEb,IAACR,KAAAmB,CAAAA,CAAAA,iBAAAC,IA9BD,WACI,OAAOjB,KAAKC,MAAQD,KAAKF,aAC7B,i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wC,cAAcxB,IAE1BhB,KAAKuC,cAAcvB,GAAO2B,EAAc,CAEhD,EAACN,CAAA"}
|
|
1
|
+
{"version":3,"file":"promise-semaphore.module.js","sources":["../src/semaphore.ts","../src/group-semaphore.ts"],"sourcesContent":["const defaultKey = \"_default\";\n\ntype KeyPrimitive = string | number;\n\ntype Key = KeyPrimitive | { key?: KeyPrimitive };\ntype KeyOptions = Key & { priority?: number };\n\nconst _isPrimitiveKey = (item: Key): item is KeyPrimitive =>\n [\"string\", \"number\"].includes(typeof item);\n\nconst resolveKey = (item: Key): KeyPrimitive =>\n (_isPrimitiveKey(item) ? item : item.key) ?? defaultKey;\n\nconst resolvePriority = (item: KeyOptions) =>\n (_isPrimitiveKey(item) ? 0 : item.priority) ?? 0;\n\nclass SemaphoreItem {\n private queue: Array<{\n resolve: Function;\n priority: number;\n }>;\n private maxConcurrent: number;\n\n /**\n * The number of locks.\n */\n public count: number;\n\n constructor(maxConcurrent: number) {\n this.queue = [];\n this.maxConcurrent = maxConcurrent;\n this.count = 0;\n }\n\n get canAcquire(): boolean {\n return this.count < this.maxConcurrent;\n }\n\n private incrementCount() {\n this.count++;\n }\n\n private decrementCount() {\n this.count--;\n }\n\n acquire(priority: number): Promise<void> {\n if (this.canAcquire) {\n this.incrementCount();\n return Promise.resolve();\n } else {\n return new Promise((resolve) => {\n this.queue.push({ resolve, priority });\n this.queue.sort((a, b) => b.priority - a.priority);\n });\n }\n }\n\n release(): void {\n const resolveFunc = this.queue.shift();\n\n if (resolveFunc) {\n // Give the micro task queue a small break instead of calling resolveFunc() directly\n setTimeout(resolveFunc.resolve, 0);\n } else {\n this.decrementCount();\n }\n }\n}\n\nclass Semaphore {\n private semaphoreInstances: Record<string | number, SemaphoreItem>;\n private maxConcurrent: number;\n\n /**\n * @param {number} [maxConcurrent] The maximum number of concurrent locks.\n */\n constructor(maxConcurrent: number = 1) {\n this.semaphoreInstances = {};\n this.maxConcurrent = maxConcurrent;\n }\n\n private hasSemaphoreInstance(key: KeyPrimitive = defaultKey) {\n return Boolean(this.semaphoreInstances[key]);\n }\n\n private getSemaphoreInstance(key: KeyPrimitive = defaultKey) {\n if (!this.hasSemaphoreInstance(key)) {\n this.semaphoreInstances[key] = new SemaphoreItem(\n this.maxConcurrent,\n );\n }\n return this.semaphoreInstances[key];\n }\n\n /**\n * @param {string | number} [key]- Optional, the semaphore key.\n */\n private tidy(key: KeyPrimitive = defaultKey): void {\n if (\n this.hasSemaphoreInstance(key) &&\n this.getSemaphoreInstance(key).count === 0\n ) {\n delete this.semaphoreInstances[key];\n }\n }\n\n /**\n * A synchronous function to determine whether a lock can be acquired.\n *\n * @param {string | number} [key]- Optional, the semaphore key.\n * @returns {boolean} Returns true if the lock on `key` can be acquired, false\n * otherwise.\n */\n canAcquire(key: Key = defaultKey): boolean {\n const _key = resolveKey(key);\n\n return !this.hasSemaphoreInstance(_key) ||\n this.getSemaphoreInstance(_key).canAcquire;\n }\n\n /**\n * @param {string | number} [key]- Optional, the semaphore key.\n */\n acquire(key: KeyOptions = defaultKey) {\n const _key = resolveKey(key);\n const _priority = resolvePriority(key);\n\n return this.getSemaphoreInstance(_key).acquire(_priority);\n }\n\n /**\n * @param {string | number} [key]- Optional, the semaphore key.\n */\n release(key: Key = defaultKey): void {\n const _key = resolveKey(key);\n\n this.getSemaphoreInstance(_key).release();\n this.tidy(_key);\n }\n\n /**\n * The number of active locks. Will always be less or equal to `max`.\n *\n * @param {string | number} [key]- Optional, the semaphore key.\n */\n count(key: Key = defaultKey): number {\n const _key = resolveKey(key);\n\n return (this.hasSemaphoreInstance(_key))\n ? this.getSemaphoreInstance(_key).count\n : 0;\n }\n\n /**\n * @param {string | number} [key]- Optional, the semaphore key.\n * @returns {boolean} True if the semaphore and key has locks, false otherwise.\n */\n hasTasks(key: Key = defaultKey): boolean {\n return this.count(key) > 0;\n }\n\n /**\n * @param {Function<T>} fn The function to execute.\n * @param {string | number} [key]- Optional, the semaphore key.\n * @returns {Promise<T>}\n */\n async request<T>(\n fn: Function,\n key: KeyOptions = defaultKey,\n ): Promise<T> {\n try {\n await this.acquire(key);\n return await fn();\n } finally {\n this.release(key);\n }\n }\n\n /**\n * Asynchronously executes `fn` if a lock can be immediately acquired.\n * Otherwise, returns null.\n *\n * @param {Function<T>} fn The function to execute.\n * @param {string | number} [key]- Optional, the semaphore key.\n * @returns {Promise<T>}\n */\n async requestIfAvailable<T>(\n fn: Function,\n key: KeyOptions = defaultKey,\n ): Promise<T | null> {\n if (this.canAcquire(key)) {\n return this.request(fn, key);\n } else {\n return null;\n }\n }\n}\n\nexport { Semaphore };\n","import { Semaphore } from \"./semaphore\";\n\n/**\n * GroupSemaphore manages a shared semaphore for different groups of tasks. Each\n * group is identified by a unique key, and the semaphore ensures only one group\n * can run its tasks concurrently.\n *\n * - acquire(key): Increments the active count for the given group. If it's the\n * first task for the group (active count is 0), it acquires the global\n * semaphore, ensuring only one group's tasks can proceed at a time.\n * Subsequent calls in the group increment the count and are permitted to run.\n * - release(key): Decrements the active count for the group. If the last task\n * for that group is released, it releases the global semaphore, allowing\n * other groups to proceed.\n *\n * This ensures that only one group can execute concurrently, but multiple tasks\n * within the same group can run as long as no other tasks from different groups\n * are active.\n */\nclass GroupSemaphore {\n private _semaphore = new Semaphore();\n private _activeCounts: Record<string, number> = {};\n private _groupWaiters: Record<string, Promise<void>> = {};\n\n async acquire(key: string) {\n const activeCount = this._activeCounts[key] ?? 0;\n this._activeCounts[key] = activeCount + 1;\n const waiter = this._groupWaiters[key] ?? this._semaphore.acquire();\n this._groupWaiters[key] = waiter;\n await waiter;\n }\n\n release(key: string) {\n const activeCount = this._activeCounts[key];\n\n if (activeCount === 1) {\n this._semaphore.release();\n delete this._activeCounts[key];\n delete this._groupWaiters[key];\n } else {\n this._activeCounts[key] = activeCount - 1;\n }\n }\n}\n\nexport { GroupSemaphore };\n"],"names":["defaultKey","_isPrimitiveKey","item","includes","resolveKey","_ref","key","SemaphoreItem","maxConcurrent","queue","count","this","_proto","prototype","incrementCount","decrementCount","acquire","priority","_this","canAcquire","Promise","resolve","push","sort","a","b","release","resolveFunc","shift","setTimeout","get","Semaphore","semaphoreInstances","_proto2","hasSemaphoreInstance","Boolean","getSemaphoreInstance","tidy","_key","_ref2","_priority","hasTasks","request","fn","_this2","then","_finallyRethrows","_wasThrown","_result","e","reject","requestIfAvailable","GroupSemaphore","_semaphore","_activeCounts","_groupWaiters","_this$_activeCounts$k","_this$_groupWaiters$k","activeCount","waiter"],"mappings":"+RAAA,IAAMA,EAAa,WAObC,EAAkB,SAACC,GAAS,MAC9B,CAAC,SAAU,UAAUC,gBAAgBD,EAAK,EAExCE,EAAa,SAACF,OAASG,EAAA,OACeA,OADfA,EACxBJ,EAAgBC,GAAQA,EAAOA,EAAKI,KAAGD,EAAKL,CAAU,EAKrDO,eAYF,WAAA,SAAAA,EAAYC,GAXJC,KAAAA,WAIAD,EAAAA,KAAAA,mBAKDE,EAAAA,KAAAA,aAGHC,KAAKF,MAAQ,GACbE,KAAKH,cAAgBA,EACrBG,KAAKD,MAAQ,CACjB,CAAC,QAAAE,EAAAL,EAAAM,iBAAAD,EAMOE,eAAA,WACJH,KAAKD,OACT,EAACE,EAEOG,eAAA,WACJJ,KAAKD,OACT,EAACE,EAEDI,QAAA,SAAQC,GAAgBC,IAAAA,EACpBP,KAAA,OAAIA,KAAKQ,YACLR,KAAKG,iBACEM,QAAQC,WAER,IAAID,QAAQ,SAACC,GAChBH,EAAKT,MAAMa,KAAK,CAAED,QAAAA,EAASJ,SAAAA,IAC3BC,EAAKT,MAAMc,KAAK,SAACC,EAAGC,GAAC,OAAKA,EAAER,SAAWO,EAAEP,QAAQ,EACrD,EAER,EAACL,EAEDc,QAAA,WACI,IAAMC,EAAchB,KAAKF,MAAMmB,QAE3BD,EAEAE,WAAWF,EAAYN,QAAS,GAEhCV,KAAKI,gBAEb,IAACR,KAAA,CAAA,CAAAD,IAAA,aAAAwB,IAjCD,WACI,YAAYpB,MAAQC,KAAKH,aAC7B,iPAkCE,C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mC,WAAWrC,UACzB,OAAjCE,EAAKqC,cAAcjD,GAAOqD,EAAOvC,QAAAC,QAC3BsC,GAAMd,KAChB,WAAA,EAAA,CAAC,MAAAI,GAAA7B,OAAAA,QAAA8B,OAAAD,EAAArC,CAAAA,EAAAA,EAEDc,QAAA,SAAQpB,GACJ,IAAMoD,EAAc/C,KAAK2C,cAAchD,GAEnB,IAAhBoD,GACA/C,KAAK0C,WAAW3B,iBACLf,KAAC2C,cAAchD,UACnBK,KAAK4C,cAAcjD,IAE1BK,KAAK2C,cAAchD,GAAOoD,EAAc,CAEhD,EAACN,CAAA"}
|
package/lib/semaphore.d.ts
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
type KeyPrimitive = string | number;
|
|
2
|
+
type Key = KeyPrimitive | {
|
|
3
|
+
key?: KeyPrimitive;
|
|
4
|
+
};
|
|
5
|
+
type KeyOptions = Key & {
|
|
6
|
+
priority?: number;
|
|
7
|
+
};
|
|
1
8
|
declare class Semaphore {
|
|
2
9
|
private semaphoreInstances;
|
|
3
10
|
private maxConcurrent;
|
|
@@ -18,32 +25,32 @@ declare class Semaphore {
|
|
|
18
25
|
* @returns {boolean} Returns true if the lock on `key` can be acquired, false
|
|
19
26
|
* otherwise.
|
|
20
27
|
*/
|
|
21
|
-
canAcquire(key?:
|
|
28
|
+
canAcquire(key?: Key): boolean;
|
|
22
29
|
/**
|
|
23
30
|
* @param {string | number} [key]- Optional, the semaphore key.
|
|
24
31
|
*/
|
|
25
|
-
acquire(key?:
|
|
32
|
+
acquire(key?: KeyOptions): Promise<void>;
|
|
26
33
|
/**
|
|
27
34
|
* @param {string | number} [key]- Optional, the semaphore key.
|
|
28
35
|
*/
|
|
29
|
-
release(key?:
|
|
36
|
+
release(key?: Key): void;
|
|
30
37
|
/**
|
|
31
38
|
* The number of active locks. Will always be less or equal to `max`.
|
|
32
39
|
*
|
|
33
40
|
* @param {string | number} [key]- Optional, the semaphore key.
|
|
34
41
|
*/
|
|
35
|
-
count(key?:
|
|
42
|
+
count(key?: Key): number;
|
|
36
43
|
/**
|
|
37
44
|
* @param {string | number} [key]- Optional, the semaphore key.
|
|
38
45
|
* @returns {boolean} True if the semaphore and key has locks, false otherwise.
|
|
39
46
|
*/
|
|
40
|
-
hasTasks(key?:
|
|
47
|
+
hasTasks(key?: Key): boolean;
|
|
41
48
|
/**
|
|
42
49
|
* @param {Function<T>} fn The function to execute.
|
|
43
50
|
* @param {string | number} [key]- Optional, the semaphore key.
|
|
44
51
|
* @returns {Promise<T>}
|
|
45
52
|
*/
|
|
46
|
-
request<T>(fn: Function, key?:
|
|
53
|
+
request<T>(fn: Function, key?: KeyOptions): Promise<T>;
|
|
47
54
|
/**
|
|
48
55
|
* Asynchronously executes `fn` if a lock can be immediately acquired.
|
|
49
56
|
* Otherwise, returns null.
|
|
@@ -52,6 +59,6 @@ declare class Semaphore {
|
|
|
52
59
|
* @param {string | number} [key]- Optional, the semaphore key.
|
|
53
60
|
* @returns {Promise<T>}
|
|
54
61
|
*/
|
|
55
|
-
requestIfAvailable<T>(fn: Function, key?:
|
|
62
|
+
requestIfAvailable<T>(fn: Function, key?: KeyOptions): Promise<T | null>;
|
|
56
63
|
}
|
|
57
64
|
export { Semaphore };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@chriscdn/promise-semaphore",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.1.1",
|
|
4
4
|
"description": "Limit or throttle the simultaneous execution of asynchronous code in separate iterations of the event loop.",
|
|
5
5
|
"repository": "https://github.com/chriscdn/promise-semaphore",
|
|
6
6
|
"author": "Christopher Meyer <chris@schwiiz.org>",
|
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
},
|
|
24
24
|
"devDependencies": {
|
|
25
25
|
"microbundle": "^0.15.1",
|
|
26
|
-
"vitest": "^3.
|
|
26
|
+
"vitest": "^3.2.4"
|
|
27
27
|
},
|
|
28
28
|
"keywords": [
|
|
29
29
|
"promise",
|
package/src/semaphore.ts
CHANGED
|
@@ -1,5 +1,24 @@
|
|
|
1
|
+
const defaultKey = "_default";
|
|
2
|
+
|
|
3
|
+
type KeyPrimitive = string | number;
|
|
4
|
+
|
|
5
|
+
type Key = KeyPrimitive | { key?: KeyPrimitive };
|
|
6
|
+
type KeyOptions = Key & { priority?: number };
|
|
7
|
+
|
|
8
|
+
const _isPrimitiveKey = (item: Key): item is KeyPrimitive =>
|
|
9
|
+
["string", "number"].includes(typeof item);
|
|
10
|
+
|
|
11
|
+
const resolveKey = (item: Key): KeyPrimitive =>
|
|
12
|
+
(_isPrimitiveKey(item) ? item : item.key) ?? defaultKey;
|
|
13
|
+
|
|
14
|
+
const resolvePriority = (item: KeyOptions) =>
|
|
15
|
+
(_isPrimitiveKey(item) ? 0 : item.priority) ?? 0;
|
|
16
|
+
|
|
1
17
|
class SemaphoreItem {
|
|
2
|
-
private queue:
|
|
18
|
+
private queue: Array<{
|
|
19
|
+
resolve: Function;
|
|
20
|
+
priority: number;
|
|
21
|
+
}>;
|
|
3
22
|
private maxConcurrent: number;
|
|
4
23
|
|
|
5
24
|
/**
|
|
@@ -25,12 +44,15 @@ class SemaphoreItem {
|
|
|
25
44
|
this.count--;
|
|
26
45
|
}
|
|
27
46
|
|
|
28
|
-
acquire(): Promise<void> {
|
|
47
|
+
acquire(priority: number): Promise<void> {
|
|
29
48
|
if (this.canAcquire) {
|
|
30
49
|
this.incrementCount();
|
|
31
50
|
return Promise.resolve();
|
|
32
51
|
} else {
|
|
33
|
-
return new Promise((resolve) =>
|
|
52
|
+
return new Promise((resolve) => {
|
|
53
|
+
this.queue.push({ resolve, priority });
|
|
54
|
+
this.queue.sort((a, b) => b.priority - a.priority);
|
|
55
|
+
});
|
|
34
56
|
}
|
|
35
57
|
}
|
|
36
58
|
|
|
@@ -39,15 +61,13 @@ class SemaphoreItem {
|
|
|
39
61
|
|
|
40
62
|
if (resolveFunc) {
|
|
41
63
|
// Give the micro task queue a small break instead of calling resolveFunc() directly
|
|
42
|
-
setTimeout(resolveFunc, 0);
|
|
64
|
+
setTimeout(resolveFunc.resolve, 0);
|
|
43
65
|
} else {
|
|
44
66
|
this.decrementCount();
|
|
45
67
|
}
|
|
46
68
|
}
|
|
47
69
|
}
|
|
48
70
|
|
|
49
|
-
const defaultKey = "_default";
|
|
50
|
-
|
|
51
71
|
class Semaphore {
|
|
52
72
|
private semaphoreInstances: Record<string | number, SemaphoreItem>;
|
|
53
73
|
private maxConcurrent: number;
|
|
@@ -60,11 +80,11 @@ class Semaphore {
|
|
|
60
80
|
this.maxConcurrent = maxConcurrent;
|
|
61
81
|
}
|
|
62
82
|
|
|
63
|
-
private hasSemaphoreInstance(key:
|
|
83
|
+
private hasSemaphoreInstance(key: KeyPrimitive = defaultKey) {
|
|
64
84
|
return Boolean(this.semaphoreInstances[key]);
|
|
65
85
|
}
|
|
66
86
|
|
|
67
|
-
private getSemaphoreInstance(key:
|
|
87
|
+
private getSemaphoreInstance(key: KeyPrimitive = defaultKey) {
|
|
68
88
|
if (!this.hasSemaphoreInstance(key)) {
|
|
69
89
|
this.semaphoreInstances[key] = new SemaphoreItem(
|
|
70
90
|
this.maxConcurrent,
|
|
@@ -76,7 +96,7 @@ class Semaphore {
|
|
|
76
96
|
/**
|
|
77
97
|
* @param {string | number} [key]- Optional, the semaphore key.
|
|
78
98
|
*/
|
|
79
|
-
private tidy(key:
|
|
99
|
+
private tidy(key: KeyPrimitive = defaultKey): void {
|
|
80
100
|
if (
|
|
81
101
|
this.hasSemaphoreInstance(key) &&
|
|
82
102
|
this.getSemaphoreInstance(key).count === 0
|
|
@@ -92,24 +112,31 @@ class Semaphore {
|
|
|
92
112
|
* @returns {boolean} Returns true if the lock on `key` can be acquired, false
|
|
93
113
|
* otherwise.
|
|
94
114
|
*/
|
|
95
|
-
canAcquire(key:
|
|
96
|
-
|
|
97
|
-
|
|
115
|
+
canAcquire(key: Key = defaultKey): boolean {
|
|
116
|
+
const _key = resolveKey(key);
|
|
117
|
+
|
|
118
|
+
return !this.hasSemaphoreInstance(_key) ||
|
|
119
|
+
this.getSemaphoreInstance(_key).canAcquire;
|
|
98
120
|
}
|
|
99
121
|
|
|
100
122
|
/**
|
|
101
123
|
* @param {string | number} [key]- Optional, the semaphore key.
|
|
102
124
|
*/
|
|
103
|
-
acquire(key:
|
|
104
|
-
|
|
125
|
+
acquire(key: KeyOptions = defaultKey) {
|
|
126
|
+
const _key = resolveKey(key);
|
|
127
|
+
const _priority = resolvePriority(key);
|
|
128
|
+
|
|
129
|
+
return this.getSemaphoreInstance(_key).acquire(_priority);
|
|
105
130
|
}
|
|
106
131
|
|
|
107
132
|
/**
|
|
108
133
|
* @param {string | number} [key]- Optional, the semaphore key.
|
|
109
134
|
*/
|
|
110
|
-
release(key:
|
|
111
|
-
|
|
112
|
-
|
|
135
|
+
release(key: Key = defaultKey): void {
|
|
136
|
+
const _key = resolveKey(key);
|
|
137
|
+
|
|
138
|
+
this.getSemaphoreInstance(_key).release();
|
|
139
|
+
this.tidy(_key);
|
|
113
140
|
}
|
|
114
141
|
|
|
115
142
|
/**
|
|
@@ -117,19 +144,19 @@ class Semaphore {
|
|
|
117
144
|
*
|
|
118
145
|
* @param {string | number} [key]- Optional, the semaphore key.
|
|
119
146
|
*/
|
|
120
|
-
count(key:
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
147
|
+
count(key: Key = defaultKey): number {
|
|
148
|
+
const _key = resolveKey(key);
|
|
149
|
+
|
|
150
|
+
return (this.hasSemaphoreInstance(_key))
|
|
151
|
+
? this.getSemaphoreInstance(_key).count
|
|
152
|
+
: 0;
|
|
126
153
|
}
|
|
127
154
|
|
|
128
155
|
/**
|
|
129
156
|
* @param {string | number} [key]- Optional, the semaphore key.
|
|
130
157
|
* @returns {boolean} True if the semaphore and key has locks, false otherwise.
|
|
131
158
|
*/
|
|
132
|
-
hasTasks(key:
|
|
159
|
+
hasTasks(key: Key = defaultKey): boolean {
|
|
133
160
|
return this.count(key) > 0;
|
|
134
161
|
}
|
|
135
162
|
|
|
@@ -140,7 +167,7 @@ class Semaphore {
|
|
|
140
167
|
*/
|
|
141
168
|
async request<T>(
|
|
142
169
|
fn: Function,
|
|
143
|
-
key:
|
|
170
|
+
key: KeyOptions = defaultKey,
|
|
144
171
|
): Promise<T> {
|
|
145
172
|
try {
|
|
146
173
|
await this.acquire(key);
|
|
@@ -160,7 +187,7 @@ class Semaphore {
|
|
|
160
187
|
*/
|
|
161
188
|
async requestIfAvailable<T>(
|
|
162
189
|
fn: Function,
|
|
163
|
-
key:
|
|
190
|
+
key: KeyOptions = defaultKey,
|
|
164
191
|
): Promise<T | null> {
|
|
165
192
|
if (this.canAcquire(key)) {
|
|
166
193
|
return this.request(fn, key);
|