@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.
- package/CHANGELOG.md +16 -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/semaphore.d.mts
DELETED
|
@@ -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.mts.map
|
package/dist/semaphore.d.mts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"semaphore.d.mts","names":[],"sources":["../src/semaphore.ts"],"sourcesContent":[],"mappings":";;AA2CA;;;;;;;;;;;;;;;;;;;;;;;;cAAa,SAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;aA8CM"}
|
package/dist/semaphore.mjs
DELETED
|
@@ -1,105 +0,0 @@
|
|
|
1
|
-
//#region src/semaphore.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
|
-
var Semaphore = class {
|
|
28
|
-
/**
|
|
29
|
-
* The maximum number of concurrent operations allowed.
|
|
30
|
-
*/
|
|
31
|
-
capacity;
|
|
32
|
-
/**
|
|
33
|
-
* The number of available permits.
|
|
34
|
-
*/
|
|
35
|
-
available;
|
|
36
|
-
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) {
|
|
47
|
-
this.capacity = capacity;
|
|
48
|
-
this.available = capacity;
|
|
49
|
-
}
|
|
50
|
-
/**
|
|
51
|
-
* Acquires a semaphore, blocking if necessary until one is available.
|
|
52
|
-
*
|
|
53
|
-
* @example
|
|
54
|
-
* ```ts
|
|
55
|
-
* const sema = new Semaphore(1);
|
|
56
|
-
*
|
|
57
|
-
* async function criticalSection() {
|
|
58
|
-
* await sema.acquire();
|
|
59
|
-
* try {
|
|
60
|
-
* // This code section cannot be executed simultaneously
|
|
61
|
-
* } finally {
|
|
62
|
-
* sema.release();
|
|
63
|
-
* }
|
|
64
|
-
* }
|
|
65
|
-
* ```
|
|
66
|
-
*
|
|
67
|
-
* @returns A promise that resolves when the semaphore is acquired.
|
|
68
|
-
*/
|
|
69
|
-
async acquire() {
|
|
70
|
-
if (this.available > 0) {
|
|
71
|
-
this.available--;
|
|
72
|
-
return;
|
|
73
|
-
}
|
|
74
|
-
return new Promise((resolve) => {
|
|
75
|
-
this.deferredTasks.push(resolve);
|
|
76
|
-
});
|
|
77
|
-
}
|
|
78
|
-
/**
|
|
79
|
-
* Releases a semaphore, allowing one more operation to proceed.
|
|
80
|
-
*
|
|
81
|
-
* @example
|
|
82
|
-
* const sema = new Semaphore(1);
|
|
83
|
-
*
|
|
84
|
-
* async function task() \{
|
|
85
|
-
* await sema.acquire();
|
|
86
|
-
* try \{
|
|
87
|
-
* // This code can only be executed by two tasks at the same time
|
|
88
|
-
* \} finally \{
|
|
89
|
-
* sema.release(); // Allows another waiting task to proceed.
|
|
90
|
-
* \}
|
|
91
|
-
* \}
|
|
92
|
-
*/
|
|
93
|
-
release() {
|
|
94
|
-
const deferredTask = this.deferredTasks.shift();
|
|
95
|
-
if (deferredTask != null) {
|
|
96
|
-
deferredTask();
|
|
97
|
-
return;
|
|
98
|
-
}
|
|
99
|
-
if (this.available < this.capacity) this.available++;
|
|
100
|
-
}
|
|
101
|
-
};
|
|
102
|
-
|
|
103
|
-
//#endregion
|
|
104
|
-
export { Semaphore };
|
|
105
|
-
//# sourceMappingURL=semaphore.mjs.map
|
package/dist/semaphore.mjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"semaphore.mjs","names":[],"sources":["../src/semaphore.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 * A counting semaphore for async functions that manages available permits.\n * Semaphores are mainly used to limit the number of concurrent async tasks.\n *\n * Each `acquire` operation takes a permit or waits until one is available.\n * Each `release` operation adds a permit, potentially allowing a waiting task to proceed.\n *\n * The semaphore ensures fairness by maintaining a FIFO (First In, First Out) order for acquirers.\n *\n * @example\n * const sema = new Semaphore(2);\n *\n * async function task() \\{\n * await sema.acquire();\n * try \\{\n * // This code can only be executed by two tasks at the same time\n * \\} finally \\{\n * sema.release();\n * \\}\n * \\}\n *\n * task();\n * task();\n * task(); // This task will wait until one of the previous tasks releases the semaphore.\n */\nexport class Semaphore {\n /**\n * The maximum number of concurrent operations allowed.\n */\n public capacity: number;\n\n /**\n * The number of available permits.\n */\n public available: number;\n\n private deferredTasks: Array<() => void> = [];\n\n /**\n * Creates an instance of Semaphore.\n * @param capacity - The maximum number of concurrent operations allowed.\n *\n * @example\n * ```ts\n * const sema = new Semaphore(3); // Allows up to 3 concurrent operations.\n * ```\n */\n constructor(capacity: number) {\n this.capacity = capacity;\n this.available = capacity;\n }\n\n /**\n * Acquires a semaphore, blocking if necessary until one is available.\n *\n * @example\n * ```ts\n * const sema = new Semaphore(1);\n *\n * async function criticalSection() {\n * await sema.acquire();\n * try {\n * // This code section cannot be executed simultaneously\n * } finally {\n * sema.release();\n * }\n * }\n * ```\n *\n * @returns A promise that resolves when the semaphore is acquired.\n */\n async acquire(): Promise<void> {\n if (this.available > 0) {\n this.available--;\n return;\n }\n\n return new Promise<void>(resolve => {\n this.deferredTasks.push(resolve);\n });\n }\n\n /**\n * Releases a semaphore, allowing one more operation to proceed.\n *\n * @example\n * const sema = new Semaphore(1);\n *\n * async function task() \\{\n * await sema.acquire();\n * try \\{\n * // This code can only be executed by two tasks at the same time\n * \\} finally \\{\n * sema.release(); // Allows another waiting task to proceed.\n * \\}\n * \\}\n */\n release(): void {\n const deferredTask = this.deferredTasks.shift();\n\n if (deferredTask != null) {\n deferredTask();\n return;\n }\n\n if (this.available < this.capacity) {\n this.available++;\n }\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AA2CA,IAAa,YAAb,MAAuB;;;;CAIrB,AAAO;;;;CAKP,AAAO;CAEP,AAAQ,gBAAmC,EAAE;;;;;;;;;;CAW7C,YAAY,UAAkB;AAC5B,OAAK,WAAW;AAChB,OAAK,YAAY;;;;;;;;;;;;;;;;;;;;;CAsBnB,MAAM,UAAyB;AAC7B,MAAI,KAAK,YAAY,GAAG;AACtB,QAAK;AACL;;AAGF,SAAO,IAAI,SAAc,YAAW;AAClC,QAAK,cAAc,KAAK,QAAQ;IAChC;;;;;;;;;;;;;;;;;CAkBJ,UAAgB;EACd,MAAM,eAAe,KAAK,cAAc,OAAO;AAE/C,MAAI,gBAAgB,MAAM;AACxB,iBAAc;AACd;;AAGF,MAAI,KAAK,YAAY,KAAK,SACxB,MAAK"}
|
package/dist/throttle.cjs
DELETED
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
//#region src/throttle.ts
|
|
3
|
-
/**
|
|
4
|
-
* Creates a throttled function that only invokes the provided function at most once
|
|
5
|
-
* per every `throttleMs` milliseconds. Subsequent calls to the throttled function
|
|
6
|
-
* within the wait time will not trigger the execution of the original function.
|
|
7
|
-
*
|
|
8
|
-
* @example
|
|
9
|
-
* ```typescript
|
|
10
|
-
* const throttledFunction = throttle(() => {
|
|
11
|
-
* console.log('Function executed');
|
|
12
|
-
* }, 1000);
|
|
13
|
-
*
|
|
14
|
-
* // Will log 'Function executed' immediately
|
|
15
|
-
* throttledFunction();
|
|
16
|
-
*
|
|
17
|
-
* // Will not log anything as it is within the throttle time
|
|
18
|
-
* throttledFunction();
|
|
19
|
-
*
|
|
20
|
-
* // After 1 second
|
|
21
|
-
* setTimeout(() => {
|
|
22
|
-
* throttledFunction(); // Will log 'Function executed'
|
|
23
|
-
* }, 1000);
|
|
24
|
-
* ```
|
|
25
|
-
*
|
|
26
|
-
* @param func - The function to throttle.
|
|
27
|
-
* @param throttleMs - The number of milliseconds to throttle executions to.
|
|
28
|
-
* @returns A new throttled function that accepts the same parameters as the original function.
|
|
29
|
-
*/
|
|
30
|
-
function throttle(func, throttleMs) {
|
|
31
|
-
let lastCallTime;
|
|
32
|
-
const throttledFunction = ((...args) => {
|
|
33
|
-
const now = Date.now();
|
|
34
|
-
if (lastCallTime == null || now - lastCallTime >= throttleMs) {
|
|
35
|
-
lastCallTime = now;
|
|
36
|
-
func(...args);
|
|
37
|
-
}
|
|
38
|
-
});
|
|
39
|
-
return throttledFunction;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
//#endregion
|
|
43
|
-
exports.throttle = throttle;
|
package/dist/throttle.d.cts
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
//#region src/throttle.d.ts
|
|
2
|
-
/**
|
|
3
|
-
* Creates a throttled function that only invokes the provided function at most once
|
|
4
|
-
* per every `throttleMs` milliseconds. Subsequent calls to the throttled function
|
|
5
|
-
* within the wait time will not trigger the execution of the original function.
|
|
6
|
-
*
|
|
7
|
-
* @example
|
|
8
|
-
* ```typescript
|
|
9
|
-
* const throttledFunction = throttle(() => {
|
|
10
|
-
* console.log('Function executed');
|
|
11
|
-
* }, 1000);
|
|
12
|
-
*
|
|
13
|
-
* // Will log 'Function executed' immediately
|
|
14
|
-
* throttledFunction();
|
|
15
|
-
*
|
|
16
|
-
* // Will not log anything as it is within the throttle time
|
|
17
|
-
* throttledFunction();
|
|
18
|
-
*
|
|
19
|
-
* // After 1 second
|
|
20
|
-
* setTimeout(() => {
|
|
21
|
-
* throttledFunction(); // Will log 'Function executed'
|
|
22
|
-
* }, 1000);
|
|
23
|
-
* ```
|
|
24
|
-
*
|
|
25
|
-
* @param func - The function to throttle.
|
|
26
|
-
* @param throttleMs - The number of milliseconds to throttle executions to.
|
|
27
|
-
* @returns A new throttled function that accepts the same parameters as the original function.
|
|
28
|
-
*/
|
|
29
|
-
declare function throttle<F extends (...args: any[]) => void>(func: F, throttleMs: number): F;
|
|
30
|
-
//#endregion
|
|
31
|
-
export { throttle };
|
|
32
|
-
//# sourceMappingURL=throttle.d.cts.map
|
package/dist/throttle.d.cts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"throttle.d.cts","names":[],"sources":["../src/throttle.ts"],"sourcesContent":[],"mappings":";;AA6CA;;;;;;;;;;;;;;;;;;;;;;;;;;iBAAgB,mDACR,wBAEL"}
|
package/dist/throttle.d.mts
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
//#region src/throttle.d.ts
|
|
2
|
-
/**
|
|
3
|
-
* Creates a throttled function that only invokes the provided function at most once
|
|
4
|
-
* per every `throttleMs` milliseconds. Subsequent calls to the throttled function
|
|
5
|
-
* within the wait time will not trigger the execution of the original function.
|
|
6
|
-
*
|
|
7
|
-
* @example
|
|
8
|
-
* ```typescript
|
|
9
|
-
* const throttledFunction = throttle(() => {
|
|
10
|
-
* console.log('Function executed');
|
|
11
|
-
* }, 1000);
|
|
12
|
-
*
|
|
13
|
-
* // Will log 'Function executed' immediately
|
|
14
|
-
* throttledFunction();
|
|
15
|
-
*
|
|
16
|
-
* // Will not log anything as it is within the throttle time
|
|
17
|
-
* throttledFunction();
|
|
18
|
-
*
|
|
19
|
-
* // After 1 second
|
|
20
|
-
* setTimeout(() => {
|
|
21
|
-
* throttledFunction(); // Will log 'Function executed'
|
|
22
|
-
* }, 1000);
|
|
23
|
-
* ```
|
|
24
|
-
*
|
|
25
|
-
* @param func - The function to throttle.
|
|
26
|
-
* @param throttleMs - The number of milliseconds to throttle executions to.
|
|
27
|
-
* @returns A new throttled function that accepts the same parameters as the original function.
|
|
28
|
-
*/
|
|
29
|
-
declare function throttle<F extends (...args: any[]) => void>(func: F, throttleMs: number): F;
|
|
30
|
-
//#endregion
|
|
31
|
-
export { throttle };
|
|
32
|
-
//# sourceMappingURL=throttle.d.mts.map
|
package/dist/throttle.d.mts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"throttle.d.mts","names":[],"sources":["../src/throttle.ts"],"sourcesContent":[],"mappings":";;AA6CA;;;;;;;;;;;;;;;;;;;;;;;;;;iBAAgB,mDACR,wBAEL"}
|
package/dist/throttle.mjs
DELETED
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
//#region src/throttle.ts
|
|
2
|
-
/**
|
|
3
|
-
* Creates a throttled function that only invokes the provided function at most once
|
|
4
|
-
* per every `throttleMs` milliseconds. Subsequent calls to the throttled function
|
|
5
|
-
* within the wait time will not trigger the execution of the original function.
|
|
6
|
-
*
|
|
7
|
-
* @example
|
|
8
|
-
* ```typescript
|
|
9
|
-
* const throttledFunction = throttle(() => {
|
|
10
|
-
* console.log('Function executed');
|
|
11
|
-
* }, 1000);
|
|
12
|
-
*
|
|
13
|
-
* // Will log 'Function executed' immediately
|
|
14
|
-
* throttledFunction();
|
|
15
|
-
*
|
|
16
|
-
* // Will not log anything as it is within the throttle time
|
|
17
|
-
* throttledFunction();
|
|
18
|
-
*
|
|
19
|
-
* // After 1 second
|
|
20
|
-
* setTimeout(() => {
|
|
21
|
-
* throttledFunction(); // Will log 'Function executed'
|
|
22
|
-
* }, 1000);
|
|
23
|
-
* ```
|
|
24
|
-
*
|
|
25
|
-
* @param func - The function to throttle.
|
|
26
|
-
* @param throttleMs - The number of milliseconds to throttle executions to.
|
|
27
|
-
* @returns A new throttled function that accepts the same parameters as the original function.
|
|
28
|
-
*/
|
|
29
|
-
function throttle(func, throttleMs) {
|
|
30
|
-
let lastCallTime;
|
|
31
|
-
const throttledFunction = ((...args) => {
|
|
32
|
-
const now = Date.now();
|
|
33
|
-
if (lastCallTime == null || now - lastCallTime >= throttleMs) {
|
|
34
|
-
lastCallTime = now;
|
|
35
|
-
func(...args);
|
|
36
|
-
}
|
|
37
|
-
});
|
|
38
|
-
return throttledFunction;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
//#endregion
|
|
42
|
-
export { throttle };
|
|
43
|
-
//# sourceMappingURL=throttle.mjs.map
|
package/dist/throttle.mjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"throttle.mjs","names":["lastCallTime: number | null"],"sources":["../src/throttle.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 * Creates a throttled function that only invokes the provided function at most once\n * per every `throttleMs` milliseconds. Subsequent calls to the throttled function\n * within the wait time will not trigger the execution of the original function.\n *\n * @example\n * ```typescript\n * const throttledFunction = throttle(() => {\n * console.log('Function executed');\n * }, 1000);\n *\n * // Will log 'Function executed' immediately\n * throttledFunction();\n *\n * // Will not log anything as it is within the throttle time\n * throttledFunction();\n *\n * // After 1 second\n * setTimeout(() => {\n * throttledFunction(); // Will log 'Function executed'\n * }, 1000);\n * ```\n *\n * @param func - The function to throttle.\n * @param throttleMs - The number of milliseconds to throttle executions to.\n * @returns A new throttled function that accepts the same parameters as the original function.\n */\nexport function throttle<F extends (...args: any[]) => void>(\n func: F,\n throttleMs: number\n): F {\n let lastCallTime: number | null;\n\n const throttledFunction = ((...args: Parameters<F>) => {\n const now = Date.now();\n\n if (lastCallTime == null || now - lastCallTime >= throttleMs) {\n lastCallTime = now;\n func(...args);\n }\n }) as F;\n\n return throttledFunction;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AA6CA,SAAgB,SACd,MACA,YACG;CACH,IAAIA;CAEJ,MAAM,sBAAsB,GAAG,SAAwB;EACrD,MAAM,MAAM,KAAK,KAAK;AAEtB,MAAI,gBAAgB,QAAQ,MAAM,gBAAgB,YAAY;AAC5D,kBAAe;AACf,QAAK,GAAG,KAAK;;;AAIjB,QAAO"}
|
package/dist/timeout.cjs
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
const require_errors = require('./errors.cjs');
|
|
2
|
-
const require_delay = require('./delay.cjs');
|
|
3
|
-
|
|
4
|
-
//#region src/timeout.ts
|
|
5
|
-
/**
|
|
6
|
-
* Returns a promise that rejects with a `TimeoutError` after a specified delay.
|
|
7
|
-
*
|
|
8
|
-
* @param ms - The delay duration in milliseconds.
|
|
9
|
-
* @returns A promise that rejects with a `TimeoutError` after the specified delay.
|
|
10
|
-
* @throws Throws a `TimeoutError` after the specified delay.
|
|
11
|
-
*/
|
|
12
|
-
async function timeout(ms) {
|
|
13
|
-
await require_delay.delay(ms);
|
|
14
|
-
throw new require_errors.TimeoutError();
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
//#endregion
|
|
18
|
-
exports.timeout = timeout;
|
package/dist/timeout.d.cts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
//#region src/timeout.d.ts
|
|
2
|
-
/**
|
|
3
|
-
* Returns a promise that rejects with a `TimeoutError` after a specified delay.
|
|
4
|
-
*
|
|
5
|
-
* @param ms - The delay duration in milliseconds.
|
|
6
|
-
* @returns A promise that rejects with a `TimeoutError` after the specified delay.
|
|
7
|
-
* @throws Throws a `TimeoutError` after the specified delay.
|
|
8
|
-
*/
|
|
9
|
-
declare function timeout(ms: number): Promise<never>;
|
|
10
|
-
//#endregion
|
|
11
|
-
export { timeout };
|
|
12
|
-
//# sourceMappingURL=timeout.d.cts.map
|
package/dist/timeout.d.cts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"timeout.d.cts","names":[],"sources":["../src/timeout.ts"],"sourcesContent":[],"mappings":";;AA4BA;;;;;;iBAAsB,OAAA,cAAqB"}
|
package/dist/timeout.d.mts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
//#region src/timeout.d.ts
|
|
2
|
-
/**
|
|
3
|
-
* Returns a promise that rejects with a `TimeoutError` after a specified delay.
|
|
4
|
-
*
|
|
5
|
-
* @param ms - The delay duration in milliseconds.
|
|
6
|
-
* @returns A promise that rejects with a `TimeoutError` after the specified delay.
|
|
7
|
-
* @throws Throws a `TimeoutError` after the specified delay.
|
|
8
|
-
*/
|
|
9
|
-
declare function timeout(ms: number): Promise<never>;
|
|
10
|
-
//#endregion
|
|
11
|
-
export { timeout };
|
|
12
|
-
//# sourceMappingURL=timeout.d.mts.map
|
package/dist/timeout.d.mts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"timeout.d.mts","names":[],"sources":["../src/timeout.ts"],"sourcesContent":[],"mappings":";;AA4BA;;;;;;iBAAsB,OAAA,cAAqB"}
|
package/dist/timeout.mjs
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { TimeoutError } from "./errors.mjs";
|
|
2
|
-
import { delay } from "./delay.mjs";
|
|
3
|
-
|
|
4
|
-
//#region src/timeout.ts
|
|
5
|
-
/**
|
|
6
|
-
* Returns a promise that rejects with a `TimeoutError` after a specified delay.
|
|
7
|
-
*
|
|
8
|
-
* @param ms - The delay duration in milliseconds.
|
|
9
|
-
* @returns A promise that rejects with a `TimeoutError` after the specified delay.
|
|
10
|
-
* @throws Throws a `TimeoutError` after the specified delay.
|
|
11
|
-
*/
|
|
12
|
-
async function timeout(ms) {
|
|
13
|
-
await delay(ms);
|
|
14
|
-
throw new TimeoutError();
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
//#endregion
|
|
18
|
-
export { timeout };
|
|
19
|
-
//# sourceMappingURL=timeout.mjs.map
|
package/dist/timeout.mjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"timeout.mjs","names":[],"sources":["../src/timeout.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 { delay } from \"./delay\";\nimport { TimeoutError } from \"./errors\";\n\n/**\n * Returns a promise that rejects with a `TimeoutError` after a specified delay.\n *\n * @param ms - The delay duration in milliseconds.\n * @returns A promise that rejects with a `TimeoutError` after the specified delay.\n * @throws Throws a `TimeoutError` after the specified delay.\n */\nexport async function timeout(ms: number): Promise<never> {\n await delay(ms);\n throw new TimeoutError();\n}\n"],"mappings":";;;;;;;;;;;AA4BA,eAAsB,QAAQ,IAA4B;AACxD,OAAM,MAAM,GAAG;AACf,OAAM,IAAI,cAAc"}
|
package/dist/with-timeout.cjs
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
const require_timeout = require('./timeout.cjs');
|
|
2
|
-
|
|
3
|
-
//#region src/with-timeout.ts
|
|
4
|
-
/**
|
|
5
|
-
* Executes an async function and enforces a timeout.
|
|
6
|
-
*
|
|
7
|
-
* If the promise does not resolve within the specified time,
|
|
8
|
-
* the timeout will trigger and the returned promise will be rejected.
|
|
9
|
-
*
|
|
10
|
-
* @example
|
|
11
|
-
* ```typescript
|
|
12
|
-
* try {
|
|
13
|
-
* await withTimeout(() => {}, 1000); // Timeout exception after 1 second
|
|
14
|
-
* } catch (error) {
|
|
15
|
-
* console.error(error); // Will log 'TimeoutError'
|
|
16
|
-
* }
|
|
17
|
-
* ```
|
|
18
|
-
*
|
|
19
|
-
* @param run - A function that returns a promise to be executed.
|
|
20
|
-
* @param ms - The timeout duration in milliseconds.
|
|
21
|
-
* @returns A promise that resolves with the result of the `run` function or rejects if the timeout is reached.
|
|
22
|
-
*/
|
|
23
|
-
async function withTimeout(run, ms) {
|
|
24
|
-
return Promise.race([run(), require_timeout.timeout(ms)]);
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
//#endregion
|
|
28
|
-
exports.withTimeout = withTimeout;
|
package/dist/with-timeout.d.cts
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
//#region src/with-timeout.d.ts
|
|
2
|
-
/**
|
|
3
|
-
* Executes an async function and enforces a timeout.
|
|
4
|
-
*
|
|
5
|
-
* If the promise does not resolve within the specified time,
|
|
6
|
-
* the timeout will trigger and the returned promise will be rejected.
|
|
7
|
-
*
|
|
8
|
-
* @example
|
|
9
|
-
* ```typescript
|
|
10
|
-
* try {
|
|
11
|
-
* await withTimeout(() => {}, 1000); // Timeout exception after 1 second
|
|
12
|
-
* } catch (error) {
|
|
13
|
-
* console.error(error); // Will log 'TimeoutError'
|
|
14
|
-
* }
|
|
15
|
-
* ```
|
|
16
|
-
*
|
|
17
|
-
* @param run - A function that returns a promise to be executed.
|
|
18
|
-
* @param ms - The timeout duration in milliseconds.
|
|
19
|
-
* @returns A promise that resolves with the result of the `run` function or rejects if the timeout is reached.
|
|
20
|
-
*/
|
|
21
|
-
declare function withTimeout<T>(run: () => Promise<T>, ms: number): Promise<T>;
|
|
22
|
-
//#endregion
|
|
23
|
-
export { withTimeout };
|
|
24
|
-
//# sourceMappingURL=with-timeout.d.cts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"with-timeout.d.cts","names":[],"sources":["../src/with-timeout.ts"],"sourcesContent":[],"mappings":";;AAuCA;;;;;;;;;;;;;;;;;;iBAAsB,0BACT,QAAQ,iBAElB,QAAQ"}
|
package/dist/with-timeout.d.mts
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
//#region src/with-timeout.d.ts
|
|
2
|
-
/**
|
|
3
|
-
* Executes an async function and enforces a timeout.
|
|
4
|
-
*
|
|
5
|
-
* If the promise does not resolve within the specified time,
|
|
6
|
-
* the timeout will trigger and the returned promise will be rejected.
|
|
7
|
-
*
|
|
8
|
-
* @example
|
|
9
|
-
* ```typescript
|
|
10
|
-
* try {
|
|
11
|
-
* await withTimeout(() => {}, 1000); // Timeout exception after 1 second
|
|
12
|
-
* } catch (error) {
|
|
13
|
-
* console.error(error); // Will log 'TimeoutError'
|
|
14
|
-
* }
|
|
15
|
-
* ```
|
|
16
|
-
*
|
|
17
|
-
* @param run - A function that returns a promise to be executed.
|
|
18
|
-
* @param ms - The timeout duration in milliseconds.
|
|
19
|
-
* @returns A promise that resolves with the result of the `run` function or rejects if the timeout is reached.
|
|
20
|
-
*/
|
|
21
|
-
declare function withTimeout<T>(run: () => Promise<T>, ms: number): Promise<T>;
|
|
22
|
-
//#endregion
|
|
23
|
-
export { withTimeout };
|
|
24
|
-
//# sourceMappingURL=with-timeout.d.mts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"with-timeout.d.mts","names":[],"sources":["../src/with-timeout.ts"],"sourcesContent":[],"mappings":";;AAuCA;;;;;;;;;;;;;;;;;;iBAAsB,0BACT,QAAQ,iBAElB,QAAQ"}
|
package/dist/with-timeout.mjs
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import { timeout } from "./timeout.mjs";
|
|
2
|
-
|
|
3
|
-
//#region src/with-timeout.ts
|
|
4
|
-
/**
|
|
5
|
-
* Executes an async function and enforces a timeout.
|
|
6
|
-
*
|
|
7
|
-
* If the promise does not resolve within the specified time,
|
|
8
|
-
* the timeout will trigger and the returned promise will be rejected.
|
|
9
|
-
*
|
|
10
|
-
* @example
|
|
11
|
-
* ```typescript
|
|
12
|
-
* try {
|
|
13
|
-
* await withTimeout(() => {}, 1000); // Timeout exception after 1 second
|
|
14
|
-
* } catch (error) {
|
|
15
|
-
* console.error(error); // Will log 'TimeoutError'
|
|
16
|
-
* }
|
|
17
|
-
* ```
|
|
18
|
-
*
|
|
19
|
-
* @param run - A function that returns a promise to be executed.
|
|
20
|
-
* @param ms - The timeout duration in milliseconds.
|
|
21
|
-
* @returns A promise that resolves with the result of the `run` function or rejects if the timeout is reached.
|
|
22
|
-
*/
|
|
23
|
-
async function withTimeout(run, ms) {
|
|
24
|
-
return Promise.race([run(), timeout(ms)]);
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
//#endregion
|
|
28
|
-
export { withTimeout };
|
|
29
|
-
//# sourceMappingURL=with-timeout.mjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"with-timeout.mjs","names":[],"sources":["../src/with-timeout.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 { timeout } from \"./timeout\";\n\n/**\n * Executes an async function and enforces a timeout.\n *\n * If the promise does not resolve within the specified time,\n * the timeout will trigger and the returned promise will be rejected.\n *\n * @example\n * ```typescript\n * try {\n * await withTimeout(() => {}, 1000); // Timeout exception after 1 second\n * } catch (error) {\n * console.error(error); // Will log 'TimeoutError'\n * }\n * ```\n *\n * @param run - A function that returns a promise to be executed.\n * @param ms - The timeout duration in milliseconds.\n * @returns A promise that resolves with the result of the `run` function or rejects if the timeout is reached.\n */\nexport async function withTimeout<T>(\n run: () => Promise<T>,\n ms: number\n): Promise<T> {\n return Promise.race([run(), timeout(ms) as T]);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAuCA,eAAsB,YACpB,KACA,IACY;AACZ,QAAO,QAAQ,KAAK,CAAC,KAAK,EAAE,QAAQ,GAAG,CAAM,CAAC"}
|