@stryke/helpers 0.10.9 → 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.
- package/CHANGELOG.md +24 -0
- package/README.md +16 -26
- package/dist/index.cjs +1 -21
- package/dist/index.d.cts +1 -10
- package/dist/index.d.mts +1 -10
- package/dist/index.mjs +1 -10
- package/dist/match-sorter.mjs.map +1 -1
- package/package.json +2 -33
- package/dist/debounce.cjs +0 -63
- package/dist/debounce.d.cts +0 -45
- package/dist/debounce.d.cts.map +0 -1
- package/dist/debounce.d.mts +0 -45
- package/dist/debounce.d.mts.map +0 -1
- package/dist/debounce.mjs +0 -63
- package/dist/debounce.mjs.map +0 -1
- package/dist/delay.cjs +0 -91
- package/dist/delay.d.cts +0 -75
- package/dist/delay.d.cts.map +0 -1
- package/dist/delay.d.mts +0 -75
- package/dist/delay.d.mts.map +0 -1
- package/dist/delay.mjs +0 -91
- package/dist/delay.mjs.map +0 -1
- package/dist/errors.cjs +0 -24
- package/dist/errors.d.cts +0 -16
- package/dist/errors.d.cts.map +0 -1
- package/dist/errors.d.mts +0 -16
- package/dist/errors.d.mts.map +0 -1
- package/dist/errors.mjs +0 -23
- package/dist/errors.mjs.map +0 -1
- package/dist/mutex.cjs +0 -80
- package/dist/mutex.d.cts +0 -72
- package/dist/mutex.d.cts.map +0 -1
- package/dist/mutex.d.mts +0 -72
- package/dist/mutex.d.mts.map +0 -1
- package/dist/mutex.mjs +0 -81
- package/dist/mutex.mjs.map +0 -1
- package/dist/once.cjs +0 -27
- package/dist/once.d.cts +0 -13
- package/dist/once.d.cts.map +0 -1
- package/dist/once.d.mts +0 -13
- package/dist/once.d.mts.map +0 -1
- package/dist/once.mjs +0 -26
- package/dist/once.mjs.map +0 -1
- package/dist/parallel.cjs +0 -39
- package/dist/parallel.d.cts +0 -25
- package/dist/parallel.d.cts.map +0 -1
- package/dist/parallel.d.mts +0 -25
- package/dist/parallel.d.mts.map +0 -1
- package/dist/parallel.mjs +0 -39
- package/dist/parallel.mjs.map +0 -1
- package/dist/semaphore.cjs +0 -105
- package/dist/semaphore.d.cts +0 -86
- package/dist/semaphore.d.cts.map +0 -1
- package/dist/semaphore.d.mts +0 -86
- package/dist/semaphore.d.mts.map +0 -1
- package/dist/semaphore.mjs +0 -105
- package/dist/semaphore.mjs.map +0 -1
- package/dist/throttle.cjs +0 -43
- package/dist/throttle.d.cts +0 -32
- package/dist/throttle.d.cts.map +0 -1
- package/dist/throttle.d.mts +0 -32
- package/dist/throttle.d.mts.map +0 -1
- package/dist/throttle.mjs +0 -43
- package/dist/throttle.mjs.map +0 -1
- package/dist/timeout.cjs +0 -18
- package/dist/timeout.d.cts +0 -12
- package/dist/timeout.d.cts.map +0 -1
- package/dist/timeout.d.mts +0 -12
- package/dist/timeout.d.mts.map +0 -1
- package/dist/timeout.mjs +0 -19
- package/dist/timeout.mjs.map +0 -1
- package/dist/with-timeout.cjs +0 -28
- package/dist/with-timeout.d.cts +0 -24
- package/dist/with-timeout.d.cts.map +0 -1
- package/dist/with-timeout.d.mts +0 -24
- package/dist/with-timeout.d.mts.map +0 -1
- package/dist/with-timeout.mjs +0 -29
- package/dist/with-timeout.mjs.map +0 -1
package/dist/debounce.mjs
DELETED
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
//#region src/debounce.ts
|
|
2
|
-
/**
|
|
3
|
-
* Creates a debounced function that delays invoking the provided function until after `debounceMs` milliseconds
|
|
4
|
-
* have elapsed since the last time the debounced function was invoked. The debounced function also has a `cancel`
|
|
5
|
-
* method to cancel any pending execution.
|
|
6
|
-
*
|
|
7
|
-
* @example
|
|
8
|
-
* ```typescript
|
|
9
|
-
* const debouncedFunction = debounce(() => {
|
|
10
|
-
* console.log('Function executed');
|
|
11
|
-
* }, 1000);
|
|
12
|
-
*
|
|
13
|
-
* // Will log 'Function executed' after 1 second if not called again in that time
|
|
14
|
-
* debouncedFunction();
|
|
15
|
-
*
|
|
16
|
-
* // Will not log anything as the previous call is canceled
|
|
17
|
-
* debouncedFunction.cancel();
|
|
18
|
-
*
|
|
19
|
-
* // With AbortSignal
|
|
20
|
-
* const controller = new AbortController();
|
|
21
|
-
* const signal = controller.signal;
|
|
22
|
-
* const debouncedWithSignal = debounce(() => {
|
|
23
|
-
* console.log('Function executed');
|
|
24
|
-
* }, 1000, { signal });
|
|
25
|
-
*
|
|
26
|
-
* debouncedWithSignal();
|
|
27
|
-
*
|
|
28
|
-
* // Will cancel the debounced function call
|
|
29
|
-
* controller.abort();
|
|
30
|
-
* ```
|
|
31
|
-
*
|
|
32
|
-
* @param func - The function to debounce
|
|
33
|
-
* @param debounceMs - The number of milliseconds to delay
|
|
34
|
-
* @param options - Optional configuration for the debounced function, including an AbortSignal to cancel the debounce
|
|
35
|
-
* @returns A new debounced function with a `cancel` method.
|
|
36
|
-
*/
|
|
37
|
-
function debounce(func, debounceMs, options = {}) {
|
|
38
|
-
let timeoutId = null;
|
|
39
|
-
const { signal } = options;
|
|
40
|
-
const debounced = ((...args) => {
|
|
41
|
-
if (timeoutId !== null) clearTimeout(timeoutId);
|
|
42
|
-
if (signal?.aborted) return;
|
|
43
|
-
timeoutId = setTimeout(() => {
|
|
44
|
-
func(...args);
|
|
45
|
-
timeoutId = null;
|
|
46
|
-
}, debounceMs);
|
|
47
|
-
});
|
|
48
|
-
const onAbort = () => {
|
|
49
|
-
debounced.cancel();
|
|
50
|
-
};
|
|
51
|
-
debounced.cancel = () => {
|
|
52
|
-
if (timeoutId !== null) {
|
|
53
|
-
clearTimeout(timeoutId);
|
|
54
|
-
timeoutId = null;
|
|
55
|
-
}
|
|
56
|
-
};
|
|
57
|
-
signal?.addEventListener("abort", onAbort, { once: true });
|
|
58
|
-
return debounced;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
//#endregion
|
|
62
|
-
export { debounce };
|
|
63
|
-
//# sourceMappingURL=debounce.mjs.map
|
package/dist/debounce.mjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"debounce.mjs","names":["timeoutId: ReturnType<typeof setTimeout> | null"],"sources":["../src/debounce.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 DebounceOptions {\n signal?: AbortSignal;\n}\n\n/**\n * Creates a debounced function that delays invoking the provided function until after `debounceMs` milliseconds\n * have elapsed since the last time the debounced function was invoked. The debounced function also has a `cancel`\n * method to cancel any pending execution.\n *\n * @example\n * ```typescript\n * const debouncedFunction = debounce(() => {\n * console.log('Function executed');\n * }, 1000);\n *\n * // Will log 'Function executed' after 1 second if not called again in that time\n * debouncedFunction();\n *\n * // Will not log anything as the previous call is canceled\n * debouncedFunction.cancel();\n *\n * // With AbortSignal\n * const controller = new AbortController();\n * const signal = controller.signal;\n * const debouncedWithSignal = debounce(() => {\n * console.log('Function executed');\n * }, 1000, { signal });\n *\n * debouncedWithSignal();\n *\n * // Will cancel the debounced function call\n * controller.abort();\n * ```\n *\n * @param func - The function to debounce\n * @param debounceMs - The number of milliseconds to delay\n * @param options - Optional configuration for the debounced function, including an AbortSignal to cancel the debounce\n * @returns A new debounced function with a `cancel` method.\n */\nexport function debounce<F extends (...args: any[]) => void>(\n func: F,\n debounceMs: number,\n options: DebounceOptions = {}\n): F & { cancel: () => void } {\n let timeoutId: ReturnType<typeof setTimeout> | null = null;\n const { signal } = options;\n\n const debounced = ((...args: Parameters<F>) => {\n if (timeoutId !== null) {\n clearTimeout(timeoutId);\n }\n\n if (signal?.aborted) {\n return;\n }\n\n timeoutId = setTimeout(() => {\n func(...args);\n timeoutId = null;\n }, debounceMs);\n }) as F & { cancel: () => void };\n\n const onAbort = () => {\n debounced.cancel();\n };\n\n debounced.cancel = () => {\n if (timeoutId !== null) {\n clearTimeout(timeoutId);\n timeoutId = null;\n }\n };\n\n signal?.addEventListener(\"abort\", onAbort, { once: true });\n\n return debounced;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAyDA,SAAgB,SACd,MACA,YACA,UAA2B,EAAE,EACD;CAC5B,IAAIA,YAAkD;CACtD,MAAM,EAAE,WAAW;CAEnB,MAAM,cAAc,GAAG,SAAwB;AAC7C,MAAI,cAAc,KAChB,cAAa,UAAU;AAGzB,MAAI,QAAQ,QACV;AAGF,cAAY,iBAAiB;AAC3B,QAAK,GAAG,KAAK;AACb,eAAY;KACX,WAAW;;CAGhB,MAAM,gBAAgB;AACpB,YAAU,QAAQ;;AAGpB,WAAU,eAAe;AACvB,MAAI,cAAc,MAAM;AACtB,gBAAa,UAAU;AACvB,eAAY;;;AAIhB,SAAQ,iBAAiB,SAAS,SAAS,EAAE,MAAM,MAAM,CAAC;AAE1D,QAAO"}
|
package/dist/delay.cjs
DELETED
|
@@ -1,91 +0,0 @@
|
|
|
1
|
-
const require_errors = require('./errors.cjs');
|
|
2
|
-
|
|
3
|
-
//#region src/delay.ts
|
|
4
|
-
/**
|
|
5
|
-
* Delays the execution of code for a specified number of milliseconds.
|
|
6
|
-
*
|
|
7
|
-
* This function returns a Promise that resolves after the specified delay, allowing you to use it
|
|
8
|
-
* with async/await to pause execution.
|
|
9
|
-
*
|
|
10
|
-
* @example
|
|
11
|
-
* ```typescript
|
|
12
|
-
* async function foo() {
|
|
13
|
-
* console.log('Start');
|
|
14
|
-
* await delay(1000); // Delays execution for 1 second
|
|
15
|
-
* console.log('End');
|
|
16
|
-
* }
|
|
17
|
-
*
|
|
18
|
-
* foo();
|
|
19
|
-
*
|
|
20
|
-
* // With AbortSignal
|
|
21
|
-
* const controller = new AbortController();
|
|
22
|
-
* const { signal } = controller;
|
|
23
|
-
*
|
|
24
|
-
* setTimeout(() => controller.abort(), 50); // Will cancel the delay after 50ms
|
|
25
|
-
* try {
|
|
26
|
-
* await delay(100, { signal });
|
|
27
|
-
* } catch (error) {
|
|
28
|
-
* console.error(error); // Will log 'AbortError'
|
|
29
|
-
* }
|
|
30
|
-
* }
|
|
31
|
-
* ```
|
|
32
|
-
*
|
|
33
|
-
* @param ms - The number of milliseconds to delay.
|
|
34
|
-
* @param options - The options object.
|
|
35
|
-
* @returns A Promise that resolves after the specified delay.
|
|
36
|
-
*/
|
|
37
|
-
async function delay(ms, options = {}) {
|
|
38
|
-
const { signal } = options;
|
|
39
|
-
return new Promise((resolve, reject) => {
|
|
40
|
-
const abortError = () => {
|
|
41
|
-
reject(new require_errors.AbortError());
|
|
42
|
-
};
|
|
43
|
-
const abortHandler = () => {
|
|
44
|
-
clearTimeout(timeoutId);
|
|
45
|
-
abortError();
|
|
46
|
-
};
|
|
47
|
-
if (signal?.aborted) return abortError();
|
|
48
|
-
const timeoutId = setTimeout(resolve, ms);
|
|
49
|
-
signal?.addEventListener("abort", abortHandler, { once: true });
|
|
50
|
-
});
|
|
51
|
-
}
|
|
52
|
-
/**
|
|
53
|
-
* Delays the execution of code for a specified number of milliseconds.
|
|
54
|
-
*
|
|
55
|
-
* This function returns a Promise that resolves after the specified delay, allowing you to use it
|
|
56
|
-
* with async/await to pause execution.
|
|
57
|
-
*
|
|
58
|
-
* @example
|
|
59
|
-
* ```typescript
|
|
60
|
-
* async function foo() {
|
|
61
|
-
* console.log('Start');
|
|
62
|
-
* await sleep(1000); // Delays execution for 1 second
|
|
63
|
-
* console.log('End');
|
|
64
|
-
* }
|
|
65
|
-
*
|
|
66
|
-
* foo();
|
|
67
|
-
*
|
|
68
|
-
* // With AbortSignal
|
|
69
|
-
* const controller = new AbortController();
|
|
70
|
-
* const { signal } = controller;
|
|
71
|
-
*
|
|
72
|
-
* setTimeout(() => controller.abort(), 50); // Will cancel the delay after 50ms
|
|
73
|
-
* try {
|
|
74
|
-
* await sleep(100, { signal });
|
|
75
|
-
* } catch (error) {
|
|
76
|
-
* console.error(error); // Will log 'AbortError'
|
|
77
|
-
* }
|
|
78
|
-
* }
|
|
79
|
-
* ```
|
|
80
|
-
*
|
|
81
|
-
* @param ms - The number of milliseconds to sleep.
|
|
82
|
-
* @param options - The options object.
|
|
83
|
-
* @returns A Promise that resolves after the specified sleep.
|
|
84
|
-
*/
|
|
85
|
-
async function sleep(ms, options) {
|
|
86
|
-
return delay(ms, options);
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
//#endregion
|
|
90
|
-
exports.delay = delay;
|
|
91
|
-
exports.sleep = sleep;
|
package/dist/delay.d.cts
DELETED
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
//#region src/delay.d.ts
|
|
2
|
-
interface DelayOptions {
|
|
3
|
-
signal?: AbortSignal;
|
|
4
|
-
}
|
|
5
|
-
/**
|
|
6
|
-
* Delays the execution of code for a specified number of milliseconds.
|
|
7
|
-
*
|
|
8
|
-
* This function returns a Promise that resolves after the specified delay, allowing you to use it
|
|
9
|
-
* with async/await to pause execution.
|
|
10
|
-
*
|
|
11
|
-
* @example
|
|
12
|
-
* ```typescript
|
|
13
|
-
* async function foo() {
|
|
14
|
-
* console.log('Start');
|
|
15
|
-
* await delay(1000); // Delays execution for 1 second
|
|
16
|
-
* console.log('End');
|
|
17
|
-
* }
|
|
18
|
-
*
|
|
19
|
-
* foo();
|
|
20
|
-
*
|
|
21
|
-
* // With AbortSignal
|
|
22
|
-
* const controller = new AbortController();
|
|
23
|
-
* const { signal } = controller;
|
|
24
|
-
*
|
|
25
|
-
* setTimeout(() => controller.abort(), 50); // Will cancel the delay after 50ms
|
|
26
|
-
* try {
|
|
27
|
-
* await delay(100, { signal });
|
|
28
|
-
* } catch (error) {
|
|
29
|
-
* console.error(error); // Will log 'AbortError'
|
|
30
|
-
* }
|
|
31
|
-
* }
|
|
32
|
-
* ```
|
|
33
|
-
*
|
|
34
|
-
* @param ms - The number of milliseconds to delay.
|
|
35
|
-
* @param options - The options object.
|
|
36
|
-
* @returns A Promise that resolves after the specified delay.
|
|
37
|
-
*/
|
|
38
|
-
declare function delay(ms: number, options?: DelayOptions): Promise<void>;
|
|
39
|
-
/**
|
|
40
|
-
* Delays the execution of code for a specified number of milliseconds.
|
|
41
|
-
*
|
|
42
|
-
* This function returns a Promise that resolves after the specified delay, allowing you to use it
|
|
43
|
-
* with async/await to pause execution.
|
|
44
|
-
*
|
|
45
|
-
* @example
|
|
46
|
-
* ```typescript
|
|
47
|
-
* async function foo() {
|
|
48
|
-
* console.log('Start');
|
|
49
|
-
* await sleep(1000); // Delays execution for 1 second
|
|
50
|
-
* console.log('End');
|
|
51
|
-
* }
|
|
52
|
-
*
|
|
53
|
-
* foo();
|
|
54
|
-
*
|
|
55
|
-
* // With AbortSignal
|
|
56
|
-
* const controller = new AbortController();
|
|
57
|
-
* const { signal } = controller;
|
|
58
|
-
*
|
|
59
|
-
* setTimeout(() => controller.abort(), 50); // Will cancel the delay after 50ms
|
|
60
|
-
* try {
|
|
61
|
-
* await sleep(100, { signal });
|
|
62
|
-
* } catch (error) {
|
|
63
|
-
* console.error(error); // Will log 'AbortError'
|
|
64
|
-
* }
|
|
65
|
-
* }
|
|
66
|
-
* ```
|
|
67
|
-
*
|
|
68
|
-
* @param ms - The number of milliseconds to sleep.
|
|
69
|
-
* @param options - The options object.
|
|
70
|
-
* @returns A Promise that resolves after the specified sleep.
|
|
71
|
-
*/
|
|
72
|
-
declare function sleep(ms: number, options?: DelayOptions): Promise<void>;
|
|
73
|
-
//#endregion
|
|
74
|
-
export { delay, sleep };
|
|
75
|
-
//# sourceMappingURL=delay.d.cts.map
|
package/dist/delay.d.cts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"delay.d.cts","names":[],"sources":["../src/delay.ts"],"sourcesContent":[],"mappings":";UAoBU,YAAA;EAAA,MAAA,CAAA,EACC,WADW;AAqCtB;AA6DA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBA7DsB,KAAA,uBAEX,eACR;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBA0DmB,KAAA,uBAA4B,eAAe"}
|
package/dist/delay.d.mts
DELETED
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
//#region src/delay.d.ts
|
|
2
|
-
interface DelayOptions {
|
|
3
|
-
signal?: AbortSignal;
|
|
4
|
-
}
|
|
5
|
-
/**
|
|
6
|
-
* Delays the execution of code for a specified number of milliseconds.
|
|
7
|
-
*
|
|
8
|
-
* This function returns a Promise that resolves after the specified delay, allowing you to use it
|
|
9
|
-
* with async/await to pause execution.
|
|
10
|
-
*
|
|
11
|
-
* @example
|
|
12
|
-
* ```typescript
|
|
13
|
-
* async function foo() {
|
|
14
|
-
* console.log('Start');
|
|
15
|
-
* await delay(1000); // Delays execution for 1 second
|
|
16
|
-
* console.log('End');
|
|
17
|
-
* }
|
|
18
|
-
*
|
|
19
|
-
* foo();
|
|
20
|
-
*
|
|
21
|
-
* // With AbortSignal
|
|
22
|
-
* const controller = new AbortController();
|
|
23
|
-
* const { signal } = controller;
|
|
24
|
-
*
|
|
25
|
-
* setTimeout(() => controller.abort(), 50); // Will cancel the delay after 50ms
|
|
26
|
-
* try {
|
|
27
|
-
* await delay(100, { signal });
|
|
28
|
-
* } catch (error) {
|
|
29
|
-
* console.error(error); // Will log 'AbortError'
|
|
30
|
-
* }
|
|
31
|
-
* }
|
|
32
|
-
* ```
|
|
33
|
-
*
|
|
34
|
-
* @param ms - The number of milliseconds to delay.
|
|
35
|
-
* @param options - The options object.
|
|
36
|
-
* @returns A Promise that resolves after the specified delay.
|
|
37
|
-
*/
|
|
38
|
-
declare function delay(ms: number, options?: DelayOptions): Promise<void>;
|
|
39
|
-
/**
|
|
40
|
-
* Delays the execution of code for a specified number of milliseconds.
|
|
41
|
-
*
|
|
42
|
-
* This function returns a Promise that resolves after the specified delay, allowing you to use it
|
|
43
|
-
* with async/await to pause execution.
|
|
44
|
-
*
|
|
45
|
-
* @example
|
|
46
|
-
* ```typescript
|
|
47
|
-
* async function foo() {
|
|
48
|
-
* console.log('Start');
|
|
49
|
-
* await sleep(1000); // Delays execution for 1 second
|
|
50
|
-
* console.log('End');
|
|
51
|
-
* }
|
|
52
|
-
*
|
|
53
|
-
* foo();
|
|
54
|
-
*
|
|
55
|
-
* // With AbortSignal
|
|
56
|
-
* const controller = new AbortController();
|
|
57
|
-
* const { signal } = controller;
|
|
58
|
-
*
|
|
59
|
-
* setTimeout(() => controller.abort(), 50); // Will cancel the delay after 50ms
|
|
60
|
-
* try {
|
|
61
|
-
* await sleep(100, { signal });
|
|
62
|
-
* } catch (error) {
|
|
63
|
-
* console.error(error); // Will log 'AbortError'
|
|
64
|
-
* }
|
|
65
|
-
* }
|
|
66
|
-
* ```
|
|
67
|
-
*
|
|
68
|
-
* @param ms - The number of milliseconds to sleep.
|
|
69
|
-
* @param options - The options object.
|
|
70
|
-
* @returns A Promise that resolves after the specified sleep.
|
|
71
|
-
*/
|
|
72
|
-
declare function sleep(ms: number, options?: DelayOptions): Promise<void>;
|
|
73
|
-
//#endregion
|
|
74
|
-
export { delay, sleep };
|
|
75
|
-
//# sourceMappingURL=delay.d.mts.map
|
package/dist/delay.d.mts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"delay.d.mts","names":[],"sources":["../src/delay.ts"],"sourcesContent":[],"mappings":";UAoBU,YAAA;EAAA,MAAA,CAAA,EACC,WADW;AAqCtB;AA6DA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBA7DsB,KAAA,uBAEX,eACR;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBA0DmB,KAAA,uBAA4B,eAAe"}
|
package/dist/delay.mjs
DELETED
|
@@ -1,91 +0,0 @@
|
|
|
1
|
-
import { AbortError } from "./errors.mjs";
|
|
2
|
-
|
|
3
|
-
//#region src/delay.ts
|
|
4
|
-
/**
|
|
5
|
-
* Delays the execution of code for a specified number of milliseconds.
|
|
6
|
-
*
|
|
7
|
-
* This function returns a Promise that resolves after the specified delay, allowing you to use it
|
|
8
|
-
* with async/await to pause execution.
|
|
9
|
-
*
|
|
10
|
-
* @example
|
|
11
|
-
* ```typescript
|
|
12
|
-
* async function foo() {
|
|
13
|
-
* console.log('Start');
|
|
14
|
-
* await delay(1000); // Delays execution for 1 second
|
|
15
|
-
* console.log('End');
|
|
16
|
-
* }
|
|
17
|
-
*
|
|
18
|
-
* foo();
|
|
19
|
-
*
|
|
20
|
-
* // With AbortSignal
|
|
21
|
-
* const controller = new AbortController();
|
|
22
|
-
* const { signal } = controller;
|
|
23
|
-
*
|
|
24
|
-
* setTimeout(() => controller.abort(), 50); // Will cancel the delay after 50ms
|
|
25
|
-
* try {
|
|
26
|
-
* await delay(100, { signal });
|
|
27
|
-
* } catch (error) {
|
|
28
|
-
* console.error(error); // Will log 'AbortError'
|
|
29
|
-
* }
|
|
30
|
-
* }
|
|
31
|
-
* ```
|
|
32
|
-
*
|
|
33
|
-
* @param ms - The number of milliseconds to delay.
|
|
34
|
-
* @param options - The options object.
|
|
35
|
-
* @returns A Promise that resolves after the specified delay.
|
|
36
|
-
*/
|
|
37
|
-
async function delay(ms, options = {}) {
|
|
38
|
-
const { signal } = options;
|
|
39
|
-
return new Promise((resolve, reject) => {
|
|
40
|
-
const abortError = () => {
|
|
41
|
-
reject(new AbortError());
|
|
42
|
-
};
|
|
43
|
-
const abortHandler = () => {
|
|
44
|
-
clearTimeout(timeoutId);
|
|
45
|
-
abortError();
|
|
46
|
-
};
|
|
47
|
-
if (signal?.aborted) return abortError();
|
|
48
|
-
const timeoutId = setTimeout(resolve, ms);
|
|
49
|
-
signal?.addEventListener("abort", abortHandler, { once: true });
|
|
50
|
-
});
|
|
51
|
-
}
|
|
52
|
-
/**
|
|
53
|
-
* Delays the execution of code for a specified number of milliseconds.
|
|
54
|
-
*
|
|
55
|
-
* This function returns a Promise that resolves after the specified delay, allowing you to use it
|
|
56
|
-
* with async/await to pause execution.
|
|
57
|
-
*
|
|
58
|
-
* @example
|
|
59
|
-
* ```typescript
|
|
60
|
-
* async function foo() {
|
|
61
|
-
* console.log('Start');
|
|
62
|
-
* await sleep(1000); // Delays execution for 1 second
|
|
63
|
-
* console.log('End');
|
|
64
|
-
* }
|
|
65
|
-
*
|
|
66
|
-
* foo();
|
|
67
|
-
*
|
|
68
|
-
* // With AbortSignal
|
|
69
|
-
* const controller = new AbortController();
|
|
70
|
-
* const { signal } = controller;
|
|
71
|
-
*
|
|
72
|
-
* setTimeout(() => controller.abort(), 50); // Will cancel the delay after 50ms
|
|
73
|
-
* try {
|
|
74
|
-
* await sleep(100, { signal });
|
|
75
|
-
* } catch (error) {
|
|
76
|
-
* console.error(error); // Will log 'AbortError'
|
|
77
|
-
* }
|
|
78
|
-
* }
|
|
79
|
-
* ```
|
|
80
|
-
*
|
|
81
|
-
* @param ms - The number of milliseconds to sleep.
|
|
82
|
-
* @param options - The options object.
|
|
83
|
-
* @returns A Promise that resolves after the specified sleep.
|
|
84
|
-
*/
|
|
85
|
-
async function sleep(ms, options) {
|
|
86
|
-
return delay(ms, options);
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
//#endregion
|
|
90
|
-
export { delay, sleep };
|
|
91
|
-
//# sourceMappingURL=delay.mjs.map
|
package/dist/delay.mjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"delay.mjs","names":[],"sources":["../src/delay.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 { AbortError } from \"./errors\";\n\ninterface DelayOptions {\n signal?: AbortSignal;\n}\n\n/**\n * Delays the execution of code for a specified number of milliseconds.\n *\n * This function returns a Promise that resolves after the specified delay, allowing you to use it\n * with async/await to pause execution.\n *\n * @example\n * ```typescript\n * async function foo() {\n * console.log('Start');\n * await delay(1000); // Delays execution for 1 second\n * console.log('End');\n * }\n *\n * foo();\n *\n * // With AbortSignal\n * const controller = new AbortController();\n * const { signal } = controller;\n *\n * setTimeout(() => controller.abort(), 50); // Will cancel the delay after 50ms\n * try {\n * await delay(100, { signal });\n * } catch (error) {\n * console.error(error); // Will log 'AbortError'\n * }\n * }\n * ```\n *\n * @param ms - The number of milliseconds to delay.\n * @param options - The options object.\n * @returns A Promise that resolves after the specified delay.\n */\nexport async function delay(\n ms: number,\n options: DelayOptions = {}\n): Promise<void> {\n const { signal } = options;\n\n return new Promise((resolve, reject) => {\n const abortError = () => {\n reject(new AbortError());\n };\n\n const abortHandler = () => {\n // eslint-disable-next-line ts/no-use-before-define\n clearTimeout(timeoutId);\n abortError();\n };\n\n if (signal?.aborted) {\n // eslint-disable-next-line no-promise-executor-return\n return abortError();\n }\n\n const timeoutId = setTimeout(resolve, ms);\n\n signal?.addEventListener(\"abort\", abortHandler, { once: true });\n });\n}\n\n/**\n * Delays the execution of code for a specified number of milliseconds.\n *\n * This function returns a Promise that resolves after the specified delay, allowing you to use it\n * with async/await to pause execution.\n *\n * @example\n * ```typescript\n * async function foo() {\n * console.log('Start');\n * await sleep(1000); // Delays execution for 1 second\n * console.log('End');\n * }\n *\n * foo();\n *\n * // With AbortSignal\n * const controller = new AbortController();\n * const { signal } = controller;\n *\n * setTimeout(() => controller.abort(), 50); // Will cancel the delay after 50ms\n * try {\n * await sleep(100, { signal });\n * } catch (error) {\n * console.error(error); // Will log 'AbortError'\n * }\n * }\n * ```\n *\n * @param ms - The number of milliseconds to sleep.\n * @param options - The options object.\n * @returns A Promise that resolves after the specified sleep.\n */\nexport async function sleep(ms: number, options?: DelayOptions): Promise<void> {\n return delay(ms, options);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAyDA,eAAsB,MACpB,IACA,UAAwB,EAAE,EACX;CACf,MAAM,EAAE,WAAW;AAEnB,QAAO,IAAI,SAAS,SAAS,WAAW;EACtC,MAAM,mBAAmB;AACvB,UAAO,IAAI,YAAY,CAAC;;EAG1B,MAAM,qBAAqB;AAEzB,gBAAa,UAAU;AACvB,eAAY;;AAGd,MAAI,QAAQ,QAEV,QAAO,YAAY;EAGrB,MAAM,YAAY,WAAW,SAAS,GAAG;AAEzC,UAAQ,iBAAiB,SAAS,cAAc,EAAE,MAAM,MAAM,CAAC;GAC/D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAoCJ,eAAsB,MAAM,IAAY,SAAuC;AAC7E,QAAO,MAAM,IAAI,QAAQ"}
|
package/dist/errors.cjs
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
//#region src/errors.ts
|
|
3
|
-
/**
|
|
4
|
-
* An error class representing an aborted operation.
|
|
5
|
-
*/
|
|
6
|
-
var AbortError = class extends Error {
|
|
7
|
-
constructor(message = "The operation was aborted") {
|
|
8
|
-
super(message);
|
|
9
|
-
this.name = "AbortError";
|
|
10
|
-
}
|
|
11
|
-
};
|
|
12
|
-
/**
|
|
13
|
-
* An error class representing an timeout operation.
|
|
14
|
-
*/
|
|
15
|
-
var TimeoutError = class extends Error {
|
|
16
|
-
constructor(message = "The operation was timed out") {
|
|
17
|
-
super(message);
|
|
18
|
-
this.name = "TimeoutError";
|
|
19
|
-
}
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
//#endregion
|
|
23
|
-
exports.AbortError = AbortError;
|
|
24
|
-
exports.TimeoutError = TimeoutError;
|
package/dist/errors.d.cts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
//#region src/errors.d.ts
|
|
2
|
-
/**
|
|
3
|
-
* An error class representing an aborted operation.
|
|
4
|
-
*/
|
|
5
|
-
declare class AbortError extends Error {
|
|
6
|
-
constructor(message?: string);
|
|
7
|
-
}
|
|
8
|
-
/**
|
|
9
|
-
* An error class representing an timeout operation.
|
|
10
|
-
*/
|
|
11
|
-
declare class TimeoutError extends Error {
|
|
12
|
-
constructor(message?: string);
|
|
13
|
-
}
|
|
14
|
-
//#endregion
|
|
15
|
-
export { AbortError, TimeoutError };
|
|
16
|
-
//# sourceMappingURL=errors.d.cts.map
|
package/dist/errors.d.cts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"errors.d.cts","names":[],"sources":["../src/errors.ts"],"sourcesContent":[],"mappings":";;AAqBA;AAUA;cAVa,UAAA,SAAmB,KAAA;;;;;;cAUnB,YAAA,SAAqB,KAAA"}
|
package/dist/errors.d.mts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
//#region src/errors.d.ts
|
|
2
|
-
/**
|
|
3
|
-
* An error class representing an aborted operation.
|
|
4
|
-
*/
|
|
5
|
-
declare class AbortError extends Error {
|
|
6
|
-
constructor(message?: string);
|
|
7
|
-
}
|
|
8
|
-
/**
|
|
9
|
-
* An error class representing an timeout operation.
|
|
10
|
-
*/
|
|
11
|
-
declare class TimeoutError extends Error {
|
|
12
|
-
constructor(message?: string);
|
|
13
|
-
}
|
|
14
|
-
//#endregion
|
|
15
|
-
export { AbortError, TimeoutError };
|
|
16
|
-
//# sourceMappingURL=errors.d.mts.map
|
package/dist/errors.d.mts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"errors.d.mts","names":[],"sources":["../src/errors.ts"],"sourcesContent":[],"mappings":";;AAqBA;AAUA;cAVa,UAAA,SAAmB,KAAA;;;;;;cAUnB,YAAA,SAAqB,KAAA"}
|
package/dist/errors.mjs
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
//#region src/errors.ts
|
|
2
|
-
/**
|
|
3
|
-
* An error class representing an aborted operation.
|
|
4
|
-
*/
|
|
5
|
-
var AbortError = class extends Error {
|
|
6
|
-
constructor(message = "The operation was aborted") {
|
|
7
|
-
super(message);
|
|
8
|
-
this.name = "AbortError";
|
|
9
|
-
}
|
|
10
|
-
};
|
|
11
|
-
/**
|
|
12
|
-
* An error class representing an timeout operation.
|
|
13
|
-
*/
|
|
14
|
-
var TimeoutError = class extends Error {
|
|
15
|
-
constructor(message = "The operation was timed out") {
|
|
16
|
-
super(message);
|
|
17
|
-
this.name = "TimeoutError";
|
|
18
|
-
}
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
//#endregion
|
|
22
|
-
export { AbortError, TimeoutError };
|
|
23
|
-
//# sourceMappingURL=errors.mjs.map
|
package/dist/errors.mjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"errors.mjs","names":[],"sources":["../src/errors.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/**\n * An error class representing an aborted operation.\n */\nexport class AbortError extends Error {\n constructor(message = \"The operation was aborted\") {\n super(message);\n this.name = \"AbortError\";\n }\n}\n\n/**\n * An error class representing an timeout operation.\n */\nexport class TimeoutError extends Error {\n constructor(message = \"The operation was timed out\") {\n super(message);\n this.name = \"TimeoutError\";\n }\n}\n"],"mappings":";;;;AAqBA,IAAa,aAAb,cAAgC,MAAM;CACpC,YAAY,UAAU,6BAA6B;AACjD,QAAM,QAAQ;AACd,OAAK,OAAO;;;;;;AAOhB,IAAa,eAAb,cAAkC,MAAM;CACtC,YAAY,UAAU,+BAA+B;AACnD,QAAM,QAAQ;AACd,OAAK,OAAO"}
|
package/dist/mutex.cjs
DELETED
|
@@ -1,80 +0,0 @@
|
|
|
1
|
-
const require_semaphore = require('./semaphore.cjs');
|
|
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 require_semaphore.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
|
-
exports.Mutex = Mutex;
|
package/dist/mutex.d.cts
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.cts.map
|
package/dist/mutex.d.cts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"mutex.d.cts","names":[],"sources":["../src/mutex.ts"],"sourcesContent":[],"mappings":";;AAyCA;;;;;;;;;;;;;;;;;;;;cAAa,KAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;aAkCa"}
|