source: node_modules/es-toolkit/dist/promise/semaphore.d.ts@ ba17441

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

Added visualizations

  • Property mode set to 100644
File size: 2.2 KB
RevLine 
[a762898]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 */
26declare 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
81export { Semaphore };
Note: See TracBrowser for help on using the repository browser.