es-toolkit 1.31.0-dev.999 → 1.32.0-dev.1003

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.
@@ -0,0 +1,16 @@
1
+ import { Semaphore } from './semaphore.mjs';
2
+
3
+ class Mutex {
4
+ semaphore = new Semaphore(1);
5
+ get isLocked() {
6
+ return this.semaphore.available === 0;
7
+ }
8
+ async acquire() {
9
+ return this.semaphore.acquire();
10
+ }
11
+ release() {
12
+ this.semaphore.release();
13
+ }
14
+ }
15
+
16
+ export { Mutex };
@@ -0,0 +1,81 @@
1
+ /**
2
+ * A counting semaphore for async functions that manages available permits.
3
+ * Semaphores are mainly used to limit the number of concurrent async tasks.
4
+ *
5
+ * Each `acquire` operation takes a permit or waits until one is available.
6
+ * Each `release` operation adds a permit, potentially allowing a waiting task to proceed.
7
+ *
8
+ * The semaphore ensures fairness by maintaining a FIFO (First In, First Out) order for acquirers.
9
+ *
10
+ * @example
11
+ * const sema = new Semaphore(2);
12
+ *
13
+ * async function task() {
14
+ * await sema.acquire();
15
+ * try {
16
+ * // This code can only be executed by two tasks at the same time
17
+ * } finally {
18
+ * sema.release();
19
+ * }
20
+ * }
21
+ *
22
+ * task();
23
+ * task();
24
+ * task(); // This task will wait until one of the previous tasks releases the semaphore.
25
+ */
26
+ declare class Semaphore {
27
+ /**
28
+ * The maximum number of concurrent operations allowed.
29
+ * @type {number}
30
+ */
31
+ capacity: number;
32
+ /**
33
+ * The number of available permits.
34
+ * @type {number}
35
+ */
36
+ available: number;
37
+ private deferredTasks;
38
+ /**
39
+ * Creates an instance of Semaphore.
40
+ * @param {number} capacity - The maximum number of concurrent operations allowed.
41
+ *
42
+ * @example
43
+ * const sema = new Semaphore(3); // Allows up to 3 concurrent operations.
44
+ */
45
+ constructor(capacity: number);
46
+ /**
47
+ * Acquires a semaphore, blocking if necessary until one is available.
48
+ * @returns {Promise<void>} A promise that resolves when the semaphore is acquired.
49
+ *
50
+ * @example
51
+ * const sema = new Semaphore(1);
52
+ *
53
+ * async function criticalSection() {
54
+ * await sema.acquire();
55
+ * try {
56
+ * // This code section cannot be executed simultaneously
57
+ * } finally {
58
+ * sema.release();
59
+ * }
60
+ * }
61
+ */
62
+ acquire(): Promise<void>;
63
+ /**
64
+ * Releases a semaphore, allowing one more operation to proceed.
65
+ *
66
+ * @example
67
+ * const sema = new Semaphore(1);
68
+ *
69
+ * async function task() {
70
+ * await sema.acquire();
71
+ * try {
72
+ * // This code can only be executed by two tasks at the same time
73
+ * } finally {
74
+ * sema.release(); // Allows another waiting task to proceed.
75
+ * }
76
+ * }
77
+ */
78
+ release(): void;
79
+ }
80
+
81
+ export { Semaphore };
@@ -0,0 +1,81 @@
1
+ /**
2
+ * A counting semaphore for async functions that manages available permits.
3
+ * Semaphores are mainly used to limit the number of concurrent async tasks.
4
+ *
5
+ * Each `acquire` operation takes a permit or waits until one is available.
6
+ * Each `release` operation adds a permit, potentially allowing a waiting task to proceed.
7
+ *
8
+ * The semaphore ensures fairness by maintaining a FIFO (First In, First Out) order for acquirers.
9
+ *
10
+ * @example
11
+ * const sema = new Semaphore(2);
12
+ *
13
+ * async function task() {
14
+ * await sema.acquire();
15
+ * try {
16
+ * // This code can only be executed by two tasks at the same time
17
+ * } finally {
18
+ * sema.release();
19
+ * }
20
+ * }
21
+ *
22
+ * task();
23
+ * task();
24
+ * task(); // This task will wait until one of the previous tasks releases the semaphore.
25
+ */
26
+ declare class Semaphore {
27
+ /**
28
+ * The maximum number of concurrent operations allowed.
29
+ * @type {number}
30
+ */
31
+ capacity: number;
32
+ /**
33
+ * The number of available permits.
34
+ * @type {number}
35
+ */
36
+ available: number;
37
+ private deferredTasks;
38
+ /**
39
+ * Creates an instance of Semaphore.
40
+ * @param {number} capacity - The maximum number of concurrent operations allowed.
41
+ *
42
+ * @example
43
+ * const sema = new Semaphore(3); // Allows up to 3 concurrent operations.
44
+ */
45
+ constructor(capacity: number);
46
+ /**
47
+ * Acquires a semaphore, blocking if necessary until one is available.
48
+ * @returns {Promise<void>} A promise that resolves when the semaphore is acquired.
49
+ *
50
+ * @example
51
+ * const sema = new Semaphore(1);
52
+ *
53
+ * async function criticalSection() {
54
+ * await sema.acquire();
55
+ * try {
56
+ * // This code section cannot be executed simultaneously
57
+ * } finally {
58
+ * sema.release();
59
+ * }
60
+ * }
61
+ */
62
+ acquire(): Promise<void>;
63
+ /**
64
+ * Releases a semaphore, allowing one more operation to proceed.
65
+ *
66
+ * @example
67
+ * const sema = new Semaphore(1);
68
+ *
69
+ * async function task() {
70
+ * await sema.acquire();
71
+ * try {
72
+ * // This code can only be executed by two tasks at the same time
73
+ * } finally {
74
+ * sema.release(); // Allows another waiting task to proceed.
75
+ * }
76
+ * }
77
+ */
78
+ release(): void;
79
+ }
80
+
81
+ export { Semaphore };
@@ -0,0 +1,30 @@
1
+ class Semaphore {
2
+ capacity;
3
+ available;
4
+ deferredTasks = [];
5
+ constructor(capacity) {
6
+ this.capacity = capacity;
7
+ this.available = capacity;
8
+ }
9
+ async acquire() {
10
+ if (this.available > 0) {
11
+ this.available--;
12
+ return;
13
+ }
14
+ return new Promise(resolve => {
15
+ this.deferredTasks.push(resolve);
16
+ });
17
+ }
18
+ release() {
19
+ const deferredTask = this.deferredTasks.shift();
20
+ if (deferredTask != null) {
21
+ deferredTask();
22
+ return;
23
+ }
24
+ if (this.available < this.capacity) {
25
+ this.available++;
26
+ }
27
+ }
28
+ }
29
+
30
+ export { Semaphore };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "es-toolkit",
3
3
  "description": "A state-of-the-art, high-performance JavaScript utility library with a small bundle size and strong type annotations.",
4
- "version": "1.31.0-dev.999+3aba4da5",
4
+ "version": "1.32.0-dev.1003+2241152e",
5
5
  "homepage": "https://es-toolkit.slash.page",
6
6
  "bugs": "https://github.com/toss/es-toolkit/issues",
7
7
  "repository": {