@stryke/helpers 0.10.10 → 0.10.11

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.
Files changed (78) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/README.md +16 -26
  3. package/dist/index.cjs +1 -21
  4. package/dist/index.d.cts +1 -10
  5. package/dist/index.d.mts +1 -10
  6. package/dist/index.mjs +1 -10
  7. package/dist/match-sorter.mjs.map +1 -1
  8. package/package.json +2 -33
  9. package/dist/debounce.cjs +0 -63
  10. package/dist/debounce.d.cts +0 -45
  11. package/dist/debounce.d.cts.map +0 -1
  12. package/dist/debounce.d.mts +0 -45
  13. package/dist/debounce.d.mts.map +0 -1
  14. package/dist/debounce.mjs +0 -63
  15. package/dist/debounce.mjs.map +0 -1
  16. package/dist/delay.cjs +0 -91
  17. package/dist/delay.d.cts +0 -75
  18. package/dist/delay.d.cts.map +0 -1
  19. package/dist/delay.d.mts +0 -75
  20. package/dist/delay.d.mts.map +0 -1
  21. package/dist/delay.mjs +0 -91
  22. package/dist/delay.mjs.map +0 -1
  23. package/dist/errors.cjs +0 -24
  24. package/dist/errors.d.cts +0 -16
  25. package/dist/errors.d.cts.map +0 -1
  26. package/dist/errors.d.mts +0 -16
  27. package/dist/errors.d.mts.map +0 -1
  28. package/dist/errors.mjs +0 -23
  29. package/dist/errors.mjs.map +0 -1
  30. package/dist/mutex.cjs +0 -80
  31. package/dist/mutex.d.cts +0 -72
  32. package/dist/mutex.d.cts.map +0 -1
  33. package/dist/mutex.d.mts +0 -72
  34. package/dist/mutex.d.mts.map +0 -1
  35. package/dist/mutex.mjs +0 -81
  36. package/dist/mutex.mjs.map +0 -1
  37. package/dist/once.cjs +0 -27
  38. package/dist/once.d.cts +0 -13
  39. package/dist/once.d.cts.map +0 -1
  40. package/dist/once.d.mts +0 -13
  41. package/dist/once.d.mts.map +0 -1
  42. package/dist/once.mjs +0 -26
  43. package/dist/once.mjs.map +0 -1
  44. package/dist/parallel.cjs +0 -39
  45. package/dist/parallel.d.cts +0 -25
  46. package/dist/parallel.d.cts.map +0 -1
  47. package/dist/parallel.d.mts +0 -25
  48. package/dist/parallel.d.mts.map +0 -1
  49. package/dist/parallel.mjs +0 -39
  50. package/dist/parallel.mjs.map +0 -1
  51. package/dist/semaphore.cjs +0 -105
  52. package/dist/semaphore.d.cts +0 -86
  53. package/dist/semaphore.d.cts.map +0 -1
  54. package/dist/semaphore.d.mts +0 -86
  55. package/dist/semaphore.d.mts.map +0 -1
  56. package/dist/semaphore.mjs +0 -105
  57. package/dist/semaphore.mjs.map +0 -1
  58. package/dist/throttle.cjs +0 -43
  59. package/dist/throttle.d.cts +0 -32
  60. package/dist/throttle.d.cts.map +0 -1
  61. package/dist/throttle.d.mts +0 -32
  62. package/dist/throttle.d.mts.map +0 -1
  63. package/dist/throttle.mjs +0 -43
  64. package/dist/throttle.mjs.map +0 -1
  65. package/dist/timeout.cjs +0 -18
  66. package/dist/timeout.d.cts +0 -12
  67. package/dist/timeout.d.cts.map +0 -1
  68. package/dist/timeout.d.mts +0 -12
  69. package/dist/timeout.d.mts.map +0 -1
  70. package/dist/timeout.mjs +0 -19
  71. package/dist/timeout.mjs.map +0 -1
  72. package/dist/with-timeout.cjs +0 -28
  73. package/dist/with-timeout.d.cts +0 -24
  74. package/dist/with-timeout.d.cts.map +0 -1
  75. package/dist/with-timeout.d.mts +0 -24
  76. package/dist/with-timeout.d.mts.map +0 -1
  77. package/dist/with-timeout.mjs +0 -29
  78. package/dist/with-timeout.mjs.map +0 -1
package/dist/mutex.d.mts DELETED
@@ -1,72 +0,0 @@
1
- //#region src/mutex.d.ts
2
- /**
3
- * A Mutex (mutual exclusion lock) for async functions.
4
- * It allows only one async task to access a critical section at a time.
5
- *
6
- * @example
7
- * ```typescript
8
- * const mutex = new Mutex();
9
- *
10
- * async function criticalSection() {
11
- * await mutex.acquire();
12
- * try {
13
- * // This code section cannot be executed simultaneously
14
- * } finally {
15
- * mutex.release();
16
- * }
17
- * }
18
- *
19
- * criticalSection();
20
- * criticalSection(); // This call will wait until the first call releases the mutex.
21
- * ```
22
- */
23
- declare class Mutex {
24
- private semaphore;
25
- /**
26
- * Checks if the mutex is currently locked.
27
- * @returns True if the mutex is locked, false otherwise.
28
- *
29
- * @example
30
- * const mutex = new Mutex();
31
- * console.log(mutex.isLocked); // false
32
- * await mutex.acquire();
33
- * console.log(mutex.isLocked); // true
34
- * mutex.release();
35
- * console.log(mutex.isLocked); // false
36
- */
37
- get isLocked(): boolean;
38
- /**
39
- * Acquires the mutex, blocking if necessary until it is available.
40
- * @returns A promise that resolves when the mutex is acquired.
41
- *
42
- * @example
43
- * ```typescript
44
- * const mutex = new Mutex();
45
- * await mutex.acquire();
46
- * try {
47
- * // This code section cannot be executed simultaneously
48
- * } finally {
49
- * mutex.release();
50
- * }
51
- * ```
52
- */
53
- acquire(): Promise<void>;
54
- /**
55
- * Releases the mutex, allowing another waiting task to proceed.
56
- *
57
- * @example
58
- * ```typescript
59
- * const mutex = new Mutex();
60
- * await mutex.acquire();
61
- * try {
62
- * // This code section cannot be executed simultaneously
63
- * } finally {
64
- * mutex.release(); // Allows another waiting task to proceed.
65
- * }
66
- * ```
67
- */
68
- release(): void;
69
- }
70
- //#endregion
71
- export { Mutex };
72
- //# sourceMappingURL=mutex.d.mts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"mutex.d.mts","names":[],"sources":["../src/mutex.ts"],"sourcesContent":[],"mappings":";;AAyCA;;;;;;;;;;;;;;;;;;;;cAAa,KAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;aAkCa"}
package/dist/mutex.mjs DELETED
@@ -1,81 +0,0 @@
1
- import { Semaphore } from "./semaphore.mjs";
2
-
3
- //#region src/mutex.ts
4
- /**
5
- * A Mutex (mutual exclusion lock) for async functions.
6
- * It allows only one async task to access a critical section at a time.
7
- *
8
- * @example
9
- * ```typescript
10
- * const mutex = new Mutex();
11
- *
12
- * async function criticalSection() {
13
- * await mutex.acquire();
14
- * try {
15
- * // This code section cannot be executed simultaneously
16
- * } finally {
17
- * mutex.release();
18
- * }
19
- * }
20
- *
21
- * criticalSection();
22
- * criticalSection(); // This call will wait until the first call releases the mutex.
23
- * ```
24
- */
25
- var Mutex = class {
26
- semaphore = new Semaphore(1);
27
- /**
28
- * Checks if the mutex is currently locked.
29
- * @returns True if the mutex is locked, false otherwise.
30
- *
31
- * @example
32
- * const mutex = new Mutex();
33
- * console.log(mutex.isLocked); // false
34
- * await mutex.acquire();
35
- * console.log(mutex.isLocked); // true
36
- * mutex.release();
37
- * console.log(mutex.isLocked); // false
38
- */
39
- get isLocked() {
40
- return this.semaphore.available === 0;
41
- }
42
- /**
43
- * Acquires the mutex, blocking if necessary until it is available.
44
- * @returns A promise that resolves when the mutex is acquired.
45
- *
46
- * @example
47
- * ```typescript
48
- * const mutex = new Mutex();
49
- * await mutex.acquire();
50
- * try {
51
- * // This code section cannot be executed simultaneously
52
- * } finally {
53
- * mutex.release();
54
- * }
55
- * ```
56
- */
57
- async acquire() {
58
- return this.semaphore.acquire();
59
- }
60
- /**
61
- * Releases the mutex, allowing another waiting task to proceed.
62
- *
63
- * @example
64
- * ```typescript
65
- * const mutex = new Mutex();
66
- * await mutex.acquire();
67
- * try {
68
- * // This code section cannot be executed simultaneously
69
- * } finally {
70
- * mutex.release(); // Allows another waiting task to proceed.
71
- * }
72
- * ```
73
- */
74
- release() {
75
- this.semaphore.release();
76
- }
77
- };
78
-
79
- //#endregion
80
- export { Mutex };
81
- //# sourceMappingURL=mutex.mjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"mutex.mjs","names":[],"sources":["../src/mutex.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/stryke.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/stryke\n Documentation: https://docs.stormsoftware.com/projects/stryke\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nimport { Semaphore } from \"./semaphore\";\n\n/**\n * A Mutex (mutual exclusion lock) for async functions.\n * It allows only one async task to access a critical section at a time.\n *\n * @example\n * ```typescript\n * const mutex = new Mutex();\n *\n * async function criticalSection() {\n * await mutex.acquire();\n * try {\n * // This code section cannot be executed simultaneously\n * } finally {\n * mutex.release();\n * }\n * }\n *\n * criticalSection();\n * criticalSection(); // This call will wait until the first call releases the mutex.\n * ```\n */\nexport class Mutex {\n private semaphore = new Semaphore(1);\n\n /**\n * Checks if the mutex is currently locked.\n * @returns True if the mutex is locked, false otherwise.\n *\n * @example\n * const mutex = new Mutex();\n * console.log(mutex.isLocked); // false\n * await mutex.acquire();\n * console.log(mutex.isLocked); // true\n * mutex.release();\n * console.log(mutex.isLocked); // false\n */\n public get isLocked(): boolean {\n return this.semaphore.available === 0;\n }\n\n /**\n * Acquires the mutex, blocking if necessary until it is available.\n * @returns A promise that resolves when the mutex is acquired.\n *\n * @example\n * ```typescript\n * const mutex = new Mutex();\n * await mutex.acquire();\n * try {\n * // This code section cannot be executed simultaneously\n * } finally {\n * mutex.release();\n * }\n * ```\n */\n public async acquire(): Promise<void> {\n return this.semaphore.acquire();\n }\n\n /**\n * Releases the mutex, allowing another waiting task to proceed.\n *\n * @example\n * ```typescript\n * const mutex = new Mutex();\n * await mutex.acquire();\n * try {\n * // This code section cannot be executed simultaneously\n * } finally {\n * mutex.release(); // Allows another waiting task to proceed.\n * }\n * ```\n */\n public release(): void {\n this.semaphore.release();\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAyCA,IAAa,QAAb,MAAmB;CACjB,AAAQ,YAAY,IAAI,UAAU,EAAE;;;;;;;;;;;;;CAcpC,IAAW,WAAoB;AAC7B,SAAO,KAAK,UAAU,cAAc;;;;;;;;;;;;;;;;;CAkBtC,MAAa,UAAyB;AACpC,SAAO,KAAK,UAAU,SAAS;;;;;;;;;;;;;;;;CAiBjC,AAAO,UAAgB;AACrB,OAAK,UAAU,SAAS"}
package/dist/once.cjs DELETED
@@ -1,27 +0,0 @@
1
-
2
- //#region src/once.ts
3
- /** silence "error TS2322" but practically this is safer and more convenient than resorting to `any` in a random place */
4
- function safeFunctionCast(fn) {
5
- return fn;
6
- }
7
- /**
8
- * Executes a function only once.
9
- *
10
- * @param fn - The function to be executed only once.
11
- * @returns A function that, when called, will execute the original function only once.
12
- */
13
- function once(fn) {
14
- let result;
15
- let called = false;
16
- return safeFunctionCast(function(...args) {
17
- if (!called) {
18
- result = fn.apply(this, args);
19
- called = true;
20
- }
21
- return result;
22
- });
23
- }
24
-
25
- //#endregion
26
- exports.once = once;
27
- exports.safeFunctionCast = safeFunctionCast;
package/dist/once.d.cts DELETED
@@ -1,13 +0,0 @@
1
- //#region src/once.d.ts
2
- /** silence "error TS2322" but practically this is safer and more convenient than resorting to `any` in a random place */
3
- declare function safeFunctionCast<TFunc extends (...args: any[]) => any>(fn: (...args: Parameters<TFunc>) => ReturnType<TFunc>): TFunc;
4
- /**
5
- * Executes a function only once.
6
- *
7
- * @param fn - The function to be executed only once.
8
- * @returns A function that, when called, will execute the original function only once.
9
- */
10
- declare function once<TFunc extends (...args: any[]) => any>(fn: TFunc): TFunc;
11
- //#endregion
12
- export { once, safeFunctionCast };
13
- //# sourceMappingURL=once.d.cts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"once.d.cts","names":[],"sources":["../src/once.ts"],"sourcesContent":[],"mappings":";;AAmBgB,iBAAA,gBAAgB,CAAA,cAAA,CAAA,GAAA,IAAA,EAAA,GAAA,EAAA,EAAA,GAAA,GAAA,CAAA,CAAA,EAAA,EAAA,CAAA,GAAA,IAAA,EAChB,UADgB,CACL,KADK,CAAA,EAAA,GACM,UADN,CACiB,KADjB,CAAA,CAAA,EAE7B,KAF6B;;;;;;;AAehB,iBAAA,IAAgD,CAAA,cAAa,CAAA,GAAA,IAAA,EAAA,GAAA,EAAA,EAAA,GAAA,GAAA,CAAA,CAAA,EAAA,EAAb,KAAa,CAAA,EAAL,KAAK"}
package/dist/once.d.mts DELETED
@@ -1,13 +0,0 @@
1
- //#region src/once.d.ts
2
- /** silence "error TS2322" but practically this is safer and more convenient than resorting to `any` in a random place */
3
- declare function safeFunctionCast<TFunc extends (...args: any[]) => any>(fn: (...args: Parameters<TFunc>) => ReturnType<TFunc>): TFunc;
4
- /**
5
- * Executes a function only once.
6
- *
7
- * @param fn - The function to be executed only once.
8
- * @returns A function that, when called, will execute the original function only once.
9
- */
10
- declare function once<TFunc extends (...args: any[]) => any>(fn: TFunc): TFunc;
11
- //#endregion
12
- export { once, safeFunctionCast };
13
- //# sourceMappingURL=once.d.mts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"once.d.mts","names":[],"sources":["../src/once.ts"],"sourcesContent":[],"mappings":";;AAmBgB,iBAAA,gBAAgB,CAAA,cAAA,CAAA,GAAA,IAAA,EAAA,GAAA,EAAA,EAAA,GAAA,GAAA,CAAA,CAAA,EAAA,EAAA,CAAA,GAAA,IAAA,EAChB,UADgB,CACL,KADK,CAAA,EAAA,GACM,UADN,CACiB,KADjB,CAAA,CAAA,EAE7B,KAF6B;;;;;;;AAehB,iBAAA,IAAgD,CAAA,cAAa,CAAA,GAAA,IAAA,EAAA,GAAA,EAAA,EAAA,GAAA,GAAA,CAAA,CAAA,EAAA,EAAb,KAAa,CAAA,EAAL,KAAK"}
package/dist/once.mjs DELETED
@@ -1,26 +0,0 @@
1
- //#region src/once.ts
2
- /** silence "error TS2322" but practically this is safer and more convenient than resorting to `any` in a random place */
3
- function safeFunctionCast(fn) {
4
- return fn;
5
- }
6
- /**
7
- * Executes a function only once.
8
- *
9
- * @param fn - The function to be executed only once.
10
- * @returns A function that, when called, will execute the original function only once.
11
- */
12
- function once(fn) {
13
- let result;
14
- let called = false;
15
- return safeFunctionCast(function(...args) {
16
- if (!called) {
17
- result = fn.apply(this, args);
18
- called = true;
19
- }
20
- return result;
21
- });
22
- }
23
-
24
- //#endregion
25
- export { once, safeFunctionCast };
26
- //# sourceMappingURL=once.mjs.map
package/dist/once.mjs.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"once.mjs","names":["result: ReturnType<TFunc>"],"sources":["../src/once.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/stryke.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/stryke\n Documentation: https://docs.stormsoftware.com/projects/stryke\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\n/** silence \"error TS2322\" but practically this is safer and more convenient than resorting to `any` in a random place */\nexport function safeFunctionCast<TFunc extends (...args: any[]) => any>(\n fn: (...args: Parameters<TFunc>) => ReturnType<TFunc>\n): TFunc {\n // @ts-expect-error\n // error TS2322: Type '(...args: Parameters<F>) => ReturnType<F>' is not assignable to type 'F'.\n // '(...args: Parameters<F>) => ReturnType<F>' is assignable to the constraint of type 'F', but 'F' could be instantiated with a different subtype of constraint '(...args: any[]) => any'.\n return fn;\n}\n\n/**\n * Executes a function only once.\n *\n * @param fn - The function to be executed only once.\n * @returns A function that, when called, will execute the original function only once.\n */\nexport function once<TFunc extends (...args: any[]) => any>(fn: TFunc): TFunc {\n let result: ReturnType<TFunc>;\n let called = false;\n\n // eslint-disable-next-line func-names\n return safeFunctionCast<TFunc>(function (this: unknown, ...args) {\n if (!called) {\n result = fn.apply(this, args);\n called = true;\n }\n return result;\n });\n}\n"],"mappings":";;AAmBA,SAAgB,iBACd,IACO;AAIP,QAAO;;;;;;;;AAST,SAAgB,KAA4C,IAAkB;CAC5E,IAAIA;CACJ,IAAI,SAAS;AAGb,QAAO,iBAAwB,SAAyB,GAAG,MAAM;AAC/D,MAAI,CAAC,QAAQ;AACX,YAAS,GAAG,MAAM,MAAM,KAAK;AAC7B,YAAS;;AAEX,SAAO;GACP"}
package/dist/parallel.cjs DELETED
@@ -1,39 +0,0 @@
1
-
2
- //#region src/parallel.ts
3
- /**
4
- * Executes a callback function on a set of inputs in parallel, with a specified concurrency limit and optional interval between task executions.
5
- *
6
- * @param inputs - A set of inputs to process.
7
- * @param cb - A callback function that takes an input and returns a value or a promise. This function will be executed for each input in the set.
8
- * @param options - An object containing options for the parallel execution
9
- * @returns A promise that resolves to an object containing an array of errors that occurred during the execution of the tasks. If no errors occurred, the array will be empty.
10
- */
11
- async function runParallel(inputs, cb, options) {
12
- const errors = [];
13
- const tasks = /* @__PURE__ */ new Set();
14
- function queueNext() {
15
- const route = inputs.values().next().value;
16
- if (!route) return;
17
- inputs.delete(route);
18
- const task = (options.interval ? new Promise((resolve) => {
19
- setTimeout(resolve, options.interval);
20
- }) : Promise.resolve()).then(() => cb(route)).catch((error) => {
21
- console.error(error);
22
- errors.push(error);
23
- });
24
- tasks.add(task);
25
- return task.then(async () => {
26
- tasks.delete(task);
27
- if (inputs.size > 0) return refillQueue();
28
- });
29
- }
30
- async function refillQueue() {
31
- const workers = Math.min(options.concurrency - tasks.size, inputs.size);
32
- return Promise.all(Array.from({ length: workers }, async () => queueNext()));
33
- }
34
- await refillQueue();
35
- return { errors };
36
- }
37
-
38
- //#endregion
39
- exports.runParallel = runParallel;
@@ -1,25 +0,0 @@
1
- //#region src/parallel.d.ts
2
- interface RunParallelOptions {
3
- /**
4
- * The maximum number of tasks to run concurrently. This option is required and must be a positive integer.
5
- */
6
- concurrency: number;
7
- /**
8
- * An optional interval (in milliseconds) to wait between the execution of each task. If specified, the function will wait for the specified interval before starting the next task. If not specified, tasks will be executed immediately one after another without any delay.
9
- */
10
- interval?: number;
11
- }
12
- /**
13
- * Executes a callback function on a set of inputs in parallel, with a specified concurrency limit and optional interval between task executions.
14
- *
15
- * @param inputs - A set of inputs to process.
16
- * @param cb - A callback function that takes an input and returns a value or a promise. This function will be executed for each input in the set.
17
- * @param options - An object containing options for the parallel execution
18
- * @returns A promise that resolves to an object containing an array of errors that occurred during the execution of the tasks. If no errors occurred, the array will be empty.
19
- */
20
- declare function runParallel<T>(inputs: Set<T>, cb: (input: T) => unknown | Promise<unknown>, options: RunParallelOptions): Promise<{
21
- errors: unknown[];
22
- }>;
23
- //#endregion
24
- export { RunParallelOptions, runParallel };
25
- //# sourceMappingURL=parallel.d.cts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"parallel.d.cts","names":[],"sources":["../src/parallel.ts"],"sourcesContent":[],"mappings":";UAkBiB,kBAAA;EAAA;AAmBjB;;EACU,WAAA,EAAA,MAAA;EACI;;;EAEX,QAAA,CAAA,EAAA,MAAA;;;;;;;;;;iBAJmB,uBACZ,IAAI,gBACA,gBAAgB,2BACnB,qBACR"}
@@ -1,25 +0,0 @@
1
- //#region src/parallel.d.ts
2
- interface RunParallelOptions {
3
- /**
4
- * The maximum number of tasks to run concurrently. This option is required and must be a positive integer.
5
- */
6
- concurrency: number;
7
- /**
8
- * An optional interval (in milliseconds) to wait between the execution of each task. If specified, the function will wait for the specified interval before starting the next task. If not specified, tasks will be executed immediately one after another without any delay.
9
- */
10
- interval?: number;
11
- }
12
- /**
13
- * Executes a callback function on a set of inputs in parallel, with a specified concurrency limit and optional interval between task executions.
14
- *
15
- * @param inputs - A set of inputs to process.
16
- * @param cb - A callback function that takes an input and returns a value or a promise. This function will be executed for each input in the set.
17
- * @param options - An object containing options for the parallel execution
18
- * @returns A promise that resolves to an object containing an array of errors that occurred during the execution of the tasks. If no errors occurred, the array will be empty.
19
- */
20
- declare function runParallel<T>(inputs: Set<T>, cb: (input: T) => unknown | Promise<unknown>, options: RunParallelOptions): Promise<{
21
- errors: unknown[];
22
- }>;
23
- //#endregion
24
- export { RunParallelOptions, runParallel };
25
- //# sourceMappingURL=parallel.d.mts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"parallel.d.mts","names":[],"sources":["../src/parallel.ts"],"sourcesContent":[],"mappings":";UAkBiB,kBAAA;EAAA;AAmBjB;;EACU,WAAA,EAAA,MAAA;EACI;;;EAEX,QAAA,CAAA,EAAA,MAAA;;;;;;;;;;iBAJmB,uBACZ,IAAI,gBACA,gBAAgB,2BACnB,qBACR"}
package/dist/parallel.mjs DELETED
@@ -1,39 +0,0 @@
1
- //#region src/parallel.ts
2
- /**
3
- * Executes a callback function on a set of inputs in parallel, with a specified concurrency limit and optional interval between task executions.
4
- *
5
- * @param inputs - A set of inputs to process.
6
- * @param cb - A callback function that takes an input and returns a value or a promise. This function will be executed for each input in the set.
7
- * @param options - An object containing options for the parallel execution
8
- * @returns A promise that resolves to an object containing an array of errors that occurred during the execution of the tasks. If no errors occurred, the array will be empty.
9
- */
10
- async function runParallel(inputs, cb, options) {
11
- const errors = [];
12
- const tasks = /* @__PURE__ */ new Set();
13
- function queueNext() {
14
- const route = inputs.values().next().value;
15
- if (!route) return;
16
- inputs.delete(route);
17
- const task = (options.interval ? new Promise((resolve) => {
18
- setTimeout(resolve, options.interval);
19
- }) : Promise.resolve()).then(() => cb(route)).catch((error) => {
20
- console.error(error);
21
- errors.push(error);
22
- });
23
- tasks.add(task);
24
- return task.then(async () => {
25
- tasks.delete(task);
26
- if (inputs.size > 0) return refillQueue();
27
- });
28
- }
29
- async function refillQueue() {
30
- const workers = Math.min(options.concurrency - tasks.size, inputs.size);
31
- return Promise.all(Array.from({ length: workers }, async () => queueNext()));
32
- }
33
- await refillQueue();
34
- return { errors };
35
- }
36
-
37
- //#endregion
38
- export { runParallel };
39
- //# sourceMappingURL=parallel.mjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"parallel.mjs","names":["errors: unknown[]"],"sources":["../src/parallel.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/stryke.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/stryke\n Documentation: https://docs.stormsoftware.com/projects/stryke\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nexport interface RunParallelOptions {\n /**\n * The maximum number of tasks to run concurrently. This option is required and must be a positive integer.\n */\n concurrency: number;\n /**\n * An optional interval (in milliseconds) to wait between the execution of each task. If specified, the function will wait for the specified interval before starting the next task. If not specified, tasks will be executed immediately one after another without any delay.\n */\n interval?: number;\n}\n\n/**\n * Executes a callback function on a set of inputs in parallel, with a specified concurrency limit and optional interval between task executions.\n *\n * @param inputs - A set of inputs to process.\n * @param cb - A callback function that takes an input and returns a value or a promise. This function will be executed for each input in the set.\n * @param options - An object containing options for the parallel execution\n * @returns A promise that resolves to an object containing an array of errors that occurred during the execution of the tasks. If no errors occurred, the array will be empty.\n */\nexport async function runParallel<T>(\n inputs: Set<T>,\n cb: (input: T) => unknown | Promise<unknown>,\n options: RunParallelOptions\n): Promise<{ errors: unknown[] }> {\n const errors: unknown[] = [];\n const tasks = new Set<Promise<unknown>>();\n\n function queueNext(): undefined | Promise<unknown> {\n const route = inputs.values().next().value;\n if (!route) {\n return;\n }\n\n inputs.delete(route);\n const task = (\n options.interval\n ? new Promise(resolve => {\n setTimeout(resolve, options.interval);\n })\n : Promise.resolve()\n )\n .then(() => cb(route))\n .catch(error => {\n // eslint-disable-next-line no-console\n console.error(error);\n errors.push(error);\n });\n\n tasks.add(task);\n return task.then(async () => {\n tasks.delete(task);\n if (inputs.size > 0) {\n return refillQueue();\n }\n\n return void 0;\n });\n }\n\n async function refillQueue(): Promise<unknown> {\n const workers = Math.min(options.concurrency - tasks.size, inputs.size);\n\n return Promise.all(\n Array.from({ length: workers }, async () => queueNext())\n );\n }\n\n await refillQueue();\n return { errors };\n}\n"],"mappings":";;;;;;;;;AAqCA,eAAsB,YACpB,QACA,IACA,SACgC;CAChC,MAAMA,SAAoB,EAAE;CAC5B,MAAM,wBAAQ,IAAI,KAAuB;CAEzC,SAAS,YAA0C;EACjD,MAAM,QAAQ,OAAO,QAAQ,CAAC,MAAM,CAAC;AACrC,MAAI,CAAC,MACH;AAGF,SAAO,OAAO,MAAM;EACpB,MAAM,QACJ,QAAQ,WACJ,IAAI,SAAQ,YAAW;AACrB,cAAW,SAAS,QAAQ,SAAS;IACrC,GACF,QAAQ,SAAS,EAEpB,WAAW,GAAG,MAAM,CAAC,CACrB,OAAM,UAAS;AAEd,WAAQ,MAAM,MAAM;AACpB,UAAO,KAAK,MAAM;IAClB;AAEJ,QAAM,IAAI,KAAK;AACf,SAAO,KAAK,KAAK,YAAY;AAC3B,SAAM,OAAO,KAAK;AAClB,OAAI,OAAO,OAAO,EAChB,QAAO,aAAa;IAItB;;CAGJ,eAAe,cAAgC;EAC7C,MAAM,UAAU,KAAK,IAAI,QAAQ,cAAc,MAAM,MAAM,OAAO,KAAK;AAEvE,SAAO,QAAQ,IACb,MAAM,KAAK,EAAE,QAAQ,SAAS,EAAE,YAAY,WAAW,CAAC,CACzD;;AAGH,OAAM,aAAa;AACnB,QAAO,EAAE,QAAQ"}
@@ -1,105 +0,0 @@
1
-
2
- //#region src/semaphore.ts
3
- /**
4
- * A counting semaphore for async functions that manages available permits.
5
- * Semaphores are mainly used to limit the number of concurrent async tasks.
6
- *
7
- * Each `acquire` operation takes a permit or waits until one is available.
8
- * Each `release` operation adds a permit, potentially allowing a waiting task to proceed.
9
- *
10
- * The semaphore ensures fairness by maintaining a FIFO (First In, First Out) order for acquirers.
11
- *
12
- * @example
13
- * const sema = new Semaphore(2);
14
- *
15
- * async function task() \{
16
- * await sema.acquire();
17
- * try \{
18
- * // This code can only be executed by two tasks at the same time
19
- * \} finally \{
20
- * sema.release();
21
- * \}
22
- * \}
23
- *
24
- * task();
25
- * task();
26
- * task(); // This task will wait until one of the previous tasks releases the semaphore.
27
- */
28
- var Semaphore = class {
29
- /**
30
- * The maximum number of concurrent operations allowed.
31
- */
32
- capacity;
33
- /**
34
- * The number of available permits.
35
- */
36
- available;
37
- deferredTasks = [];
38
- /**
39
- * Creates an instance of Semaphore.
40
- * @param capacity - The maximum number of concurrent operations allowed.
41
- *
42
- * @example
43
- * ```ts
44
- * const sema = new Semaphore(3); // Allows up to 3 concurrent operations.
45
- * ```
46
- */
47
- constructor(capacity) {
48
- this.capacity = capacity;
49
- this.available = capacity;
50
- }
51
- /**
52
- * Acquires a semaphore, blocking if necessary until one is available.
53
- *
54
- * @example
55
- * ```ts
56
- * const sema = new Semaphore(1);
57
- *
58
- * async function criticalSection() {
59
- * await sema.acquire();
60
- * try {
61
- * // This code section cannot be executed simultaneously
62
- * } finally {
63
- * sema.release();
64
- * }
65
- * }
66
- * ```
67
- *
68
- * @returns A promise that resolves when the semaphore is acquired.
69
- */
70
- async acquire() {
71
- if (this.available > 0) {
72
- this.available--;
73
- return;
74
- }
75
- return new Promise((resolve) => {
76
- this.deferredTasks.push(resolve);
77
- });
78
- }
79
- /**
80
- * Releases a semaphore, allowing one more operation to proceed.
81
- *
82
- * @example
83
- * const sema = new Semaphore(1);
84
- *
85
- * async function task() \{
86
- * await sema.acquire();
87
- * try \{
88
- * // This code can only be executed by two tasks at the same time
89
- * \} finally \{
90
- * sema.release(); // Allows another waiting task to proceed.
91
- * \}
92
- * \}
93
- */
94
- release() {
95
- const deferredTask = this.deferredTasks.shift();
96
- if (deferredTask != null) {
97
- deferredTask();
98
- return;
99
- }
100
- if (this.available < this.capacity) this.available++;
101
- }
102
- };
103
-
104
- //#endregion
105
- exports.Semaphore = Semaphore;
@@ -1,86 +0,0 @@
1
- //#region src/semaphore.d.ts
2
- /**
3
- * A counting semaphore for async functions that manages available permits.
4
- * Semaphores are mainly used to limit the number of concurrent async tasks.
5
- *
6
- * Each `acquire` operation takes a permit or waits until one is available.
7
- * Each `release` operation adds a permit, potentially allowing a waiting task to proceed.
8
- *
9
- * The semaphore ensures fairness by maintaining a FIFO (First In, First Out) order for acquirers.
10
- *
11
- * @example
12
- * const sema = new Semaphore(2);
13
- *
14
- * async function task() \{
15
- * await sema.acquire();
16
- * try \{
17
- * // This code can only be executed by two tasks at the same time
18
- * \} finally \{
19
- * sema.release();
20
- * \}
21
- * \}
22
- *
23
- * task();
24
- * task();
25
- * task(); // This task will wait until one of the previous tasks releases the semaphore.
26
- */
27
- declare class Semaphore {
28
- /**
29
- * The maximum number of concurrent operations allowed.
30
- */
31
- capacity: number;
32
- /**
33
- * The number of available permits.
34
- */
35
- available: number;
36
- private deferredTasks;
37
- /**
38
- * Creates an instance of Semaphore.
39
- * @param capacity - The maximum number of concurrent operations allowed.
40
- *
41
- * @example
42
- * ```ts
43
- * const sema = new Semaphore(3); // Allows up to 3 concurrent operations.
44
- * ```
45
- */
46
- constructor(capacity: number);
47
- /**
48
- * Acquires a semaphore, blocking if necessary until one is available.
49
- *
50
- * @example
51
- * ```ts
52
- * const sema = new Semaphore(1);
53
- *
54
- * async function criticalSection() {
55
- * await sema.acquire();
56
- * try {
57
- * // This code section cannot be executed simultaneously
58
- * } finally {
59
- * sema.release();
60
- * }
61
- * }
62
- * ```
63
- *
64
- * @returns A promise that resolves when the semaphore is acquired.
65
- */
66
- acquire(): Promise<void>;
67
- /**
68
- * Releases a semaphore, allowing one more operation to proceed.
69
- *
70
- * @example
71
- * const sema = new Semaphore(1);
72
- *
73
- * async function task() \{
74
- * await sema.acquire();
75
- * try \{
76
- * // This code can only be executed by two tasks at the same time
77
- * \} finally \{
78
- * sema.release(); // Allows another waiting task to proceed.
79
- * \}
80
- * \}
81
- */
82
- release(): void;
83
- }
84
- //#endregion
85
- export { Semaphore };
86
- //# sourceMappingURL=semaphore.d.cts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"semaphore.d.cts","names":[],"sources":["../src/semaphore.ts"],"sourcesContent":[],"mappings":";;AA2CA;;;;;;;;;;;;;;;;;;;;;;;;cAAa,SAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;aA8CM"}