@tanstack/pacer 0.1.0 → 0.2.0
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/dist/cjs/async-debouncer.cjs +60 -44
- package/dist/cjs/async-debouncer.cjs.map +1 -1
- package/dist/cjs/async-debouncer.d.cts +37 -24
- package/dist/cjs/async-queuer.cjs +149 -125
- package/dist/cjs/async-queuer.cjs.map +1 -1
- package/dist/cjs/async-queuer.d.cts +65 -48
- package/dist/cjs/async-rate-limiter.cjs +63 -46
- package/dist/cjs/async-rate-limiter.cjs.map +1 -1
- package/dist/cjs/async-rate-limiter.d.cts +39 -27
- package/dist/cjs/async-throttler.cjs +70 -47
- package/dist/cjs/async-throttler.cjs.map +1 -1
- package/dist/cjs/async-throttler.d.cts +43 -25
- package/dist/cjs/debouncer.cjs +46 -22
- package/dist/cjs/debouncer.cjs.map +1 -1
- package/dist/cjs/debouncer.d.cts +25 -11
- package/dist/cjs/index.cjs +2 -0
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.cts +2 -0
- package/dist/cjs/queuer.cjs +114 -104
- package/dist/cjs/queuer.cjs.map +1 -1
- package/dist/cjs/queuer.d.cts +53 -40
- package/dist/cjs/rate-limiter.cjs +54 -42
- package/dist/cjs/rate-limiter.cjs.map +1 -1
- package/dist/cjs/rate-limiter.d.cts +37 -45
- package/dist/cjs/throttler.cjs +61 -41
- package/dist/cjs/throttler.cjs.map +1 -1
- package/dist/cjs/throttler.d.cts +35 -22
- package/dist/cjs/types.d.cts +12 -0
- package/dist/cjs/utils.cjs +13 -0
- package/dist/cjs/utils.cjs.map +1 -0
- package/dist/cjs/utils.d.cts +1 -0
- package/dist/esm/async-debouncer.d.ts +37 -24
- package/dist/esm/async-debouncer.js +60 -44
- package/dist/esm/async-debouncer.js.map +1 -1
- package/dist/esm/async-queuer.d.ts +65 -48
- package/dist/esm/async-queuer.js +149 -125
- package/dist/esm/async-queuer.js.map +1 -1
- package/dist/esm/async-rate-limiter.d.ts +39 -27
- package/dist/esm/async-rate-limiter.js +63 -46
- package/dist/esm/async-rate-limiter.js.map +1 -1
- package/dist/esm/async-throttler.d.ts +43 -25
- package/dist/esm/async-throttler.js +70 -47
- package/dist/esm/async-throttler.js.map +1 -1
- package/dist/esm/debouncer.d.ts +25 -11
- package/dist/esm/debouncer.js +46 -22
- package/dist/esm/debouncer.js.map +1 -1
- package/dist/esm/index.d.ts +2 -0
- package/dist/esm/index.js +2 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/queuer.d.ts +53 -40
- package/dist/esm/queuer.js +114 -104
- package/dist/esm/queuer.js.map +1 -1
- package/dist/esm/rate-limiter.d.ts +37 -45
- package/dist/esm/rate-limiter.js +54 -42
- package/dist/esm/rate-limiter.js.map +1 -1
- package/dist/esm/throttler.d.ts +35 -22
- package/dist/esm/throttler.js +61 -41
- package/dist/esm/throttler.js.map +1 -1
- package/dist/esm/types.d.ts +12 -0
- package/dist/esm/utils.d.ts +1 -0
- package/dist/esm/utils.js +13 -0
- package/dist/esm/utils.js.map +1 -0
- package/package.json +8 -1
- package/src/async-debouncer.ts +90 -62
- package/src/async-queuer.ts +178 -145
- package/src/async-rate-limiter.ts +93 -67
- package/src/async-throttler.ts +98 -63
- package/src/debouncer.ts +71 -35
- package/src/index.ts +2 -0
- package/src/queuer.ts +135 -118
- package/src/rate-limiter.ts +79 -81
- package/src/throttler.ts +87 -61
- package/src/types.ts +17 -0
- package/src/utils.ts +13 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.cjs","sources":["../../src/utils.ts"],"sourcesContent":["export function bindInstanceMethods<T extends Record<string, any>>(\n instance: T,\n) {\n return Object.getOwnPropertyNames(Object.getPrototypeOf(instance))\n .filter((key) => typeof instance[key as keyof T] === 'function')\n .reduce((acc: any, key) => {\n const method = instance[key as keyof T]\n if (typeof method === 'function') {\n acc[key] = method.bind(instance)\n }\n return acc\n }, {} as T)\n}\n"],"names":[],"mappings":";;AAAO,SAAS,oBACd,UACA;AACA,SAAO,OAAO,oBAAoB,OAAO,eAAe,QAAQ,CAAC,EAC9D,OAAO,CAAC,QAAQ,OAAO,SAAS,GAAc,MAAM,UAAU,EAC9D,OAAO,CAAC,KAAU,QAAQ;AACnB,UAAA,SAAS,SAAS,GAAc;AAClC,QAAA,OAAO,WAAW,YAAY;AAChC,UAAI,GAAG,IAAI,OAAO,KAAK,QAAQ;AAAA,IAAA;AAE1B,WAAA;AAAA,EACT,GAAG,EAAO;AACd;;"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function bindInstanceMethods<T extends Record<string, any>>(instance: T): any;
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
+
import { AnyAsyncFunction } from './types.js';
|
|
1
2
|
/**
|
|
2
3
|
* Options for configuring an async debounced function
|
|
3
4
|
*/
|
|
4
|
-
export interface AsyncDebouncerOptions {
|
|
5
|
+
export interface AsyncDebouncerOptions<TFn extends AnyAsyncFunction, TArgs extends Parameters<TFn>> {
|
|
5
6
|
/**
|
|
6
7
|
* Whether the debouncer is enabled. When disabled, maybeExecute will not trigger any executions.
|
|
7
8
|
* Defaults to true.
|
|
@@ -12,6 +13,14 @@ export interface AsyncDebouncerOptions {
|
|
|
12
13
|
* Defaults to false.
|
|
13
14
|
*/
|
|
14
15
|
leading?: boolean;
|
|
16
|
+
/**
|
|
17
|
+
* Optional error handler for when the debounced function throws
|
|
18
|
+
*/
|
|
19
|
+
onError?: (error: unknown) => void;
|
|
20
|
+
/**
|
|
21
|
+
* Optional function to call when the debounced function is executed
|
|
22
|
+
*/
|
|
23
|
+
onExecute?: (debouncer: AsyncDebouncer<TFn, TArgs>) => void;
|
|
15
24
|
/**
|
|
16
25
|
* Whether to execute on the trailing edge of the timeout.
|
|
17
26
|
* Defaults to true.
|
|
@@ -22,10 +31,6 @@ export interface AsyncDebouncerOptions {
|
|
|
22
31
|
* Defaults to 0ms
|
|
23
32
|
*/
|
|
24
33
|
wait: number;
|
|
25
|
-
/**
|
|
26
|
-
* Optional error handler for when the debounced function throws
|
|
27
|
-
*/
|
|
28
|
-
onError?: (error: unknown) => void;
|
|
29
34
|
}
|
|
30
35
|
/**
|
|
31
36
|
* A class that creates an async debounced function.
|
|
@@ -39,45 +44,53 @@ export interface AsyncDebouncerOptions {
|
|
|
39
44
|
*
|
|
40
45
|
* @example
|
|
41
46
|
* ```ts
|
|
42
|
-
* const
|
|
47
|
+
* const asyncDebouncer = new AsyncDebouncer(async (value: string) => {
|
|
43
48
|
* await searchAPI(value);
|
|
44
49
|
* }, { wait: 500 });
|
|
45
50
|
*
|
|
46
51
|
* // Called on each keystroke but only executes after 500ms of no typing
|
|
47
52
|
* inputElement.addEventListener('input', () => {
|
|
48
|
-
*
|
|
53
|
+
* asyncDebouncer.maybeExecute(inputElement.value);
|
|
49
54
|
* });
|
|
50
55
|
* ```
|
|
51
56
|
*/
|
|
52
|
-
export declare class AsyncDebouncer<TFn extends
|
|
57
|
+
export declare class AsyncDebouncer<TFn extends AnyAsyncFunction, TArgs extends Parameters<TFn>> {
|
|
53
58
|
private fn;
|
|
54
|
-
private
|
|
55
|
-
private
|
|
56
|
-
private
|
|
57
|
-
private
|
|
58
|
-
private
|
|
59
|
-
private
|
|
60
|
-
private
|
|
61
|
-
constructor(fn: TFn, initialOptions: AsyncDebouncerOptions);
|
|
59
|
+
private _abortController;
|
|
60
|
+
private _executionCount;
|
|
61
|
+
private _isExecuting;
|
|
62
|
+
private _lastArgs;
|
|
63
|
+
private _options;
|
|
64
|
+
private _timeoutId;
|
|
65
|
+
private _canLeadingExecute;
|
|
66
|
+
constructor(fn: TFn, initialOptions: AsyncDebouncerOptions<TFn, TArgs>);
|
|
62
67
|
/**
|
|
63
68
|
* Updates the debouncer options
|
|
64
69
|
* Returns the new options state
|
|
65
70
|
*/
|
|
66
|
-
setOptions(newOptions: Partial<AsyncDebouncerOptions
|
|
67
|
-
/**
|
|
68
|
-
* Returns the number of times the function has been executed
|
|
69
|
-
*/
|
|
70
|
-
getExecutionCount(): number;
|
|
71
|
+
setOptions(newOptions: Partial<AsyncDebouncerOptions<TFn, TArgs>>): Required<AsyncDebouncerOptions<TFn, TArgs>>;
|
|
71
72
|
/**
|
|
72
|
-
*
|
|
73
|
+
* Returns the current debouncer options
|
|
73
74
|
*/
|
|
74
|
-
|
|
75
|
+
getOptions(): Required<AsyncDebouncerOptions<TFn, TArgs>>;
|
|
75
76
|
/**
|
|
76
77
|
* Attempts to execute the debounced function
|
|
77
78
|
* If a call is already in progress, it will be queued
|
|
78
79
|
*/
|
|
79
80
|
maybeExecute(...args: TArgs): Promise<void>;
|
|
80
81
|
private executeFunction;
|
|
82
|
+
/**
|
|
83
|
+
* Cancels any pending execution
|
|
84
|
+
*/
|
|
85
|
+
cancel(): void;
|
|
86
|
+
/**
|
|
87
|
+
* Returns the number of times the function has been executed
|
|
88
|
+
*/
|
|
89
|
+
getExecutionCount(): number;
|
|
90
|
+
/**
|
|
91
|
+
* Returns `true` if there is a pending execution
|
|
92
|
+
*/
|
|
93
|
+
getIsPending(): boolean;
|
|
81
94
|
}
|
|
82
95
|
/**
|
|
83
96
|
* Creates an async debounced function that delays execution until after a specified wait time.
|
|
@@ -96,4 +109,4 @@ export declare class AsyncDebouncer<TFn extends (...args: Array<any>) => Promise
|
|
|
96
109
|
* await debounced("third"); // Executes after 1s
|
|
97
110
|
* ```
|
|
98
111
|
*/
|
|
99
|
-
export declare function asyncDebounce<TFn extends
|
|
112
|
+
export declare function asyncDebounce<TFn extends AnyAsyncFunction, TArgs extends Parameters<TFn>>(fn: TFn, initialOptions: Omit<AsyncDebouncerOptions<TFn, TArgs>, 'enabled'>): (...args: TArgs) => Promise<void>;
|
|
@@ -4,17 +4,19 @@ const defaultOptions = {
|
|
|
4
4
|
trailing: true,
|
|
5
5
|
onError: () => {
|
|
6
6
|
},
|
|
7
|
+
onExecute: () => {
|
|
8
|
+
},
|
|
7
9
|
wait: 0
|
|
8
10
|
};
|
|
9
11
|
class AsyncDebouncer {
|
|
10
12
|
constructor(fn, initialOptions) {
|
|
11
13
|
this.fn = fn;
|
|
12
|
-
this.
|
|
13
|
-
this.
|
|
14
|
-
this.
|
|
15
|
-
this.
|
|
16
|
-
this.
|
|
17
|
-
this.
|
|
14
|
+
this._abortController = null;
|
|
15
|
+
this._executionCount = 0;
|
|
16
|
+
this._isExecuting = false;
|
|
17
|
+
this._timeoutId = null;
|
|
18
|
+
this._canLeadingExecute = true;
|
|
19
|
+
this._options = {
|
|
18
20
|
...defaultOptions,
|
|
19
21
|
...initialOptions
|
|
20
22
|
};
|
|
@@ -24,32 +26,17 @@ class AsyncDebouncer {
|
|
|
24
26
|
* Returns the new options state
|
|
25
27
|
*/
|
|
26
28
|
setOptions(newOptions) {
|
|
27
|
-
this.
|
|
28
|
-
...this.
|
|
29
|
+
this._options = {
|
|
30
|
+
...this._options,
|
|
29
31
|
...newOptions
|
|
30
32
|
};
|
|
31
|
-
return this.
|
|
33
|
+
return this._options;
|
|
32
34
|
}
|
|
33
35
|
/**
|
|
34
|
-
* Returns the
|
|
36
|
+
* Returns the current debouncer options
|
|
35
37
|
*/
|
|
36
|
-
|
|
37
|
-
return this.
|
|
38
|
-
}
|
|
39
|
-
/**
|
|
40
|
-
* Cancels any pending execution
|
|
41
|
-
*/
|
|
42
|
-
cancel() {
|
|
43
|
-
if (this.timeoutId) {
|
|
44
|
-
clearTimeout(this.timeoutId);
|
|
45
|
-
this.timeoutId = null;
|
|
46
|
-
}
|
|
47
|
-
if (this.abortController) {
|
|
48
|
-
this.abortController.abort();
|
|
49
|
-
this.abortController = null;
|
|
50
|
-
}
|
|
51
|
-
this.lastArgs = void 0;
|
|
52
|
-
this.canLeadingExecute = true;
|
|
38
|
+
getOptions() {
|
|
39
|
+
return this._options;
|
|
53
40
|
}
|
|
54
41
|
/**
|
|
55
42
|
* Attempts to execute the debounced function
|
|
@@ -57,45 +44,74 @@ class AsyncDebouncer {
|
|
|
57
44
|
*/
|
|
58
45
|
async maybeExecute(...args) {
|
|
59
46
|
this.cancel();
|
|
60
|
-
this.
|
|
61
|
-
if (this.
|
|
62
|
-
this.
|
|
47
|
+
this._lastArgs = args;
|
|
48
|
+
if (this._options.leading && this._canLeadingExecute) {
|
|
49
|
+
this._canLeadingExecute = false;
|
|
63
50
|
await this.executeFunction(...args);
|
|
64
51
|
}
|
|
65
52
|
return new Promise((resolve) => {
|
|
66
|
-
this.
|
|
67
|
-
if (this.
|
|
53
|
+
this._timeoutId = setTimeout(async () => {
|
|
54
|
+
if (this._isExecuting) {
|
|
68
55
|
resolve();
|
|
69
56
|
return;
|
|
70
57
|
}
|
|
71
|
-
this.
|
|
72
|
-
if (this.
|
|
73
|
-
this.
|
|
58
|
+
this._canLeadingExecute = true;
|
|
59
|
+
if (this._options.trailing) {
|
|
60
|
+
this._abortController = new AbortController();
|
|
74
61
|
try {
|
|
75
|
-
this.
|
|
76
|
-
if (this.
|
|
77
|
-
await this.executeFunction(...this.
|
|
62
|
+
this._isExecuting = true;
|
|
63
|
+
if (this._lastArgs) {
|
|
64
|
+
await this.executeFunction(...this._lastArgs);
|
|
78
65
|
}
|
|
79
66
|
} catch (error) {
|
|
80
67
|
try {
|
|
81
|
-
this.
|
|
68
|
+
this._options.onError(error);
|
|
82
69
|
} catch {
|
|
70
|
+
console.error("Error in error handler", error);
|
|
83
71
|
}
|
|
84
72
|
} finally {
|
|
85
|
-
this.
|
|
86
|
-
this.
|
|
73
|
+
this._isExecuting = false;
|
|
74
|
+
this._abortController = null;
|
|
87
75
|
resolve();
|
|
88
76
|
}
|
|
89
77
|
} else {
|
|
90
78
|
resolve();
|
|
91
79
|
}
|
|
92
|
-
}, this.
|
|
80
|
+
}, this._options.wait);
|
|
93
81
|
});
|
|
94
82
|
}
|
|
95
83
|
async executeFunction(...args) {
|
|
96
|
-
if (!this.
|
|
97
|
-
this.
|
|
84
|
+
if (!this._options.enabled) return;
|
|
85
|
+
this._executionCount++;
|
|
98
86
|
await this.fn(...args);
|
|
87
|
+
this._options.onExecute(this);
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Cancels any pending execution
|
|
91
|
+
*/
|
|
92
|
+
cancel() {
|
|
93
|
+
if (this._timeoutId) {
|
|
94
|
+
clearTimeout(this._timeoutId);
|
|
95
|
+
this._timeoutId = null;
|
|
96
|
+
}
|
|
97
|
+
if (this._abortController) {
|
|
98
|
+
this._abortController.abort();
|
|
99
|
+
this._abortController = null;
|
|
100
|
+
}
|
|
101
|
+
this._lastArgs = void 0;
|
|
102
|
+
this._canLeadingExecute = true;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Returns the number of times the function has been executed
|
|
106
|
+
*/
|
|
107
|
+
getExecutionCount() {
|
|
108
|
+
return this._executionCount;
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Returns `true` if there is a pending execution
|
|
112
|
+
*/
|
|
113
|
+
getIsPending() {
|
|
114
|
+
return this._options.enabled && (this._timeoutId !== null || this._isExecuting);
|
|
99
115
|
}
|
|
100
116
|
}
|
|
101
117
|
function asyncDebounce(fn, initialOptions) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"async-debouncer.js","sources":["../../src/async-debouncer.ts"],"sourcesContent":["/**\n * Options for configuring an async debounced function\n */\nexport interface AsyncDebouncerOptions {\n /**\n * Whether the debouncer is enabled. When disabled, maybeExecute will not trigger any executions.\n * Defaults to true.\n */\n enabled?: boolean\n /**\n * Whether to execute on the leading edge of the timeout.\n * Defaults to false.\n */\n leading?: boolean\n /**\n * Whether to execute on the trailing edge of the timeout.\n * Defaults to true.\n */\n trailing?: boolean\n /**\n * Delay in milliseconds to wait after the last call before executing\n * Defaults to 0ms\n */\n wait: number\n
|
|
1
|
+
{"version":3,"file":"async-debouncer.js","sources":["../../src/async-debouncer.ts"],"sourcesContent":["import type { AnyAsyncFunction } from './types'\n\n/**\n * Options for configuring an async debounced function\n */\nexport interface AsyncDebouncerOptions<\n TFn extends AnyAsyncFunction,\n TArgs extends Parameters<TFn>,\n> {\n /**\n * Whether the debouncer is enabled. When disabled, maybeExecute will not trigger any executions.\n * Defaults to true.\n */\n enabled?: boolean\n /**\n * Whether to execute on the leading edge of the timeout.\n * Defaults to false.\n */\n leading?: boolean\n /**\n * Optional error handler for when the debounced function throws\n */\n onError?: (error: unknown) => void\n /**\n * Optional function to call when the debounced function is executed\n */\n onExecute?: (debouncer: AsyncDebouncer<TFn, TArgs>) => void\n /**\n * Whether to execute on the trailing edge of the timeout.\n * Defaults to true.\n */\n trailing?: boolean\n /**\n * Delay in milliseconds to wait after the last call before executing\n * Defaults to 0ms\n */\n wait: number\n}\n\nconst defaultOptions: Required<AsyncDebouncerOptions<any, any>> = {\n enabled: true,\n leading: false,\n trailing: true,\n onError: () => {},\n onExecute: () => {},\n wait: 0,\n}\n\n/**\n * A class that creates an async debounced function.\n *\n * Debouncing ensures that a function is only executed after a specified delay has passed since its last invocation.\n * Each new invocation resets the delay timer. This is useful for handling frequent events like window resizing\n * or input changes where you only want to execute the handler after the events have stopped occurring.\n *\n * Unlike throttling which allows execution at regular intervals, debouncing prevents any execution until\n * the function stops being called for the specified delay period.\n *\n * @example\n * ```ts\n * const asyncDebouncer = new AsyncDebouncer(async (value: string) => {\n * await searchAPI(value);\n * }, { wait: 500 });\n *\n * // Called on each keystroke but only executes after 500ms of no typing\n * inputElement.addEventListener('input', () => {\n * asyncDebouncer.maybeExecute(inputElement.value);\n * });\n * ```\n */\nexport class AsyncDebouncer<\n TFn extends AnyAsyncFunction,\n TArgs extends Parameters<TFn>,\n> {\n private _abortController: AbortController | null = null\n private _executionCount = 0\n private _isExecuting = false\n private _lastArgs: TArgs | undefined\n private _options: Required<AsyncDebouncerOptions<TFn, TArgs>>\n private _timeoutId: ReturnType<typeof setTimeout> | null = null\n private _canLeadingExecute = true\n\n constructor(\n private fn: TFn,\n initialOptions: AsyncDebouncerOptions<TFn, TArgs>,\n ) {\n this._options = {\n ...defaultOptions,\n ...initialOptions,\n }\n }\n\n /**\n * Updates the debouncer options\n * Returns the new options state\n */\n setOptions(\n newOptions: Partial<AsyncDebouncerOptions<TFn, TArgs>>,\n ): Required<AsyncDebouncerOptions<TFn, TArgs>> {\n this._options = {\n ...this._options,\n ...newOptions,\n }\n return this._options\n }\n\n /**\n * Returns the current debouncer options\n */\n getOptions(): Required<AsyncDebouncerOptions<TFn, TArgs>> {\n return this._options\n }\n\n /**\n * Attempts to execute the debounced function\n * If a call is already in progress, it will be queued\n */\n async maybeExecute(...args: TArgs): Promise<void> {\n this.cancel()\n this._lastArgs = args\n\n // Handle leading execution\n if (this._options.leading && this._canLeadingExecute) {\n this._canLeadingExecute = false\n await this.executeFunction(...args)\n }\n\n return new Promise((resolve) => {\n this._timeoutId = setTimeout(async () => {\n if (this._isExecuting) {\n resolve()\n return\n }\n\n this._canLeadingExecute = true\n // Execute trailing only if enabled\n if (this._options.trailing) {\n this._abortController = new AbortController()\n try {\n this._isExecuting = true\n if (this._lastArgs) {\n await this.executeFunction(...this._lastArgs)\n }\n } catch (error) {\n try {\n this._options.onError(error)\n } catch {\n console.error('Error in error handler', error)\n }\n } finally {\n this._isExecuting = false\n this._abortController = null\n resolve()\n }\n } else {\n resolve()\n }\n }, this._options.wait)\n })\n }\n\n private async executeFunction(...args: TArgs): Promise<void> {\n if (!this._options.enabled) return\n this._executionCount++\n await this.fn(...args)\n this._options.onExecute(this)\n }\n\n /**\n * Cancels any pending execution\n */\n cancel(): void {\n if (this._timeoutId) {\n clearTimeout(this._timeoutId)\n this._timeoutId = null\n }\n if (this._abortController) {\n this._abortController.abort()\n this._abortController = null\n }\n this._lastArgs = undefined\n this._canLeadingExecute = true\n }\n\n /**\n * Returns the number of times the function has been executed\n */\n getExecutionCount(): number {\n return this._executionCount\n }\n\n /**\n * Returns `true` if there is a pending execution\n */\n getIsPending(): boolean {\n return (\n this._options.enabled && (this._timeoutId !== null || this._isExecuting)\n )\n }\n}\n\n/**\n * Creates an async debounced function that delays execution until after a specified wait time.\n * The debounced function will only execute once the wait period has elapsed without any new calls.\n * If called again during the wait period, the timer resets and a new wait period begins.\n *\n * @example\n * ```ts\n * const debounced = asyncDebounce(async (value: string) => {\n * await saveToAPI(value);\n * }, { wait: 1000 });\n *\n * // Will only execute once, 1 second after the last call\n * await debounced(\"first\"); // Cancelled\n * await debounced(\"second\"); // Cancelled\n * await debounced(\"third\"); // Executes after 1s\n * ```\n */\nexport function asyncDebounce<\n TFn extends AnyAsyncFunction,\n TArgs extends Parameters<TFn>,\n>(fn: TFn, initialOptions: Omit<AsyncDebouncerOptions<TFn, TArgs>, 'enabled'>) {\n const asyncDebouncer = new AsyncDebouncer(fn, initialOptions)\n return asyncDebouncer.maybeExecute.bind(asyncDebouncer)\n}\n"],"names":[],"mappings":"AAuCA,MAAM,iBAA4D;AAAA,EAChE,SAAS;AAAA,EACT,SAAS;AAAA,EACT,UAAU;AAAA,EACV,SAAS,MAAM;AAAA,EAAC;AAAA,EAChB,WAAW,MAAM;AAAA,EAAC;AAAA,EAClB,MAAM;AACR;AAwBO,MAAM,eAGX;AAAA,EASA,YACU,IACR,gBACA;AAFQ,SAAA,KAAA;AATV,SAAQ,mBAA2C;AACnD,SAAQ,kBAAkB;AAC1B,SAAQ,eAAe;AAGvB,SAAQ,aAAmD;AAC3D,SAAQ,qBAAqB;AAM3B,SAAK,WAAW;AAAA,MACd,GAAG;AAAA,MACH,GAAG;AAAA,IACL;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOF,WACE,YAC6C;AAC7C,SAAK,WAAW;AAAA,MACd,GAAG,KAAK;AAAA,MACR,GAAG;AAAA,IACL;AACA,WAAO,KAAK;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA,EAMd,aAA0D;AACxD,WAAO,KAAK;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOd,MAAM,gBAAgB,MAA4B;AAChD,SAAK,OAAO;AACZ,SAAK,YAAY;AAGjB,QAAI,KAAK,SAAS,WAAW,KAAK,oBAAoB;AACpD,WAAK,qBAAqB;AACpB,YAAA,KAAK,gBAAgB,GAAG,IAAI;AAAA,IAAA;AAG7B,WAAA,IAAI,QAAQ,CAAC,YAAY;AACzB,WAAA,aAAa,WAAW,YAAY;AACvC,YAAI,KAAK,cAAc;AACb,kBAAA;AACR;AAAA,QAAA;AAGF,aAAK,qBAAqB;AAEtB,YAAA,KAAK,SAAS,UAAU;AACrB,eAAA,mBAAmB,IAAI,gBAAgB;AACxC,cAAA;AACF,iBAAK,eAAe;AACpB,gBAAI,KAAK,WAAW;AAClB,oBAAM,KAAK,gBAAgB,GAAG,KAAK,SAAS;AAAA,YAAA;AAAA,mBAEvC,OAAO;AACV,gBAAA;AACG,mBAAA,SAAS,QAAQ,KAAK;AAAA,YAAA,QACrB;AACE,sBAAA,MAAM,0BAA0B,KAAK;AAAA,YAAA;AAAA,UAC/C,UACA;AACA,iBAAK,eAAe;AACpB,iBAAK,mBAAmB;AAChB,oBAAA;AAAA,UAAA;AAAA,QACV,OACK;AACG,kBAAA;AAAA,QAAA;AAAA,MACV,GACC,KAAK,SAAS,IAAI;AAAA,IAAA,CACtB;AAAA,EAAA;AAAA,EAGH,MAAc,mBAAmB,MAA4B;AACvD,QAAA,CAAC,KAAK,SAAS,QAAS;AACvB,SAAA;AACC,UAAA,KAAK,GAAG,GAAG,IAAI;AAChB,SAAA,SAAS,UAAU,IAAI;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA,EAM9B,SAAe;AACb,QAAI,KAAK,YAAY;AACnB,mBAAa,KAAK,UAAU;AAC5B,WAAK,aAAa;AAAA,IAAA;AAEpB,QAAI,KAAK,kBAAkB;AACzB,WAAK,iBAAiB,MAAM;AAC5B,WAAK,mBAAmB;AAAA,IAAA;AAE1B,SAAK,YAAY;AACjB,SAAK,qBAAqB;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA,EAM5B,oBAA4B;AAC1B,WAAO,KAAK;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA,EAMd,eAAwB;AACtB,WACE,KAAK,SAAS,YAAY,KAAK,eAAe,QAAQ,KAAK;AAAA,EAAA;AAGjE;AAmBgB,SAAA,cAGd,IAAS,gBAAoE;AAC7E,QAAM,iBAAiB,IAAI,eAAe,IAAI,cAAc;AACrD,SAAA,eAAe,aAAa,KAAK,cAAc;AACxD;"}
|
|
@@ -34,10 +34,18 @@ export interface AsyncQueuerOptions<TValue> {
|
|
|
34
34
|
* Callback fired whenever an item is removed from the queuer
|
|
35
35
|
*/
|
|
36
36
|
onGetNextItem?: (item: () => Promise<TValue>, queuer: AsyncQueuer<TValue>) => void;
|
|
37
|
+
/**
|
|
38
|
+
* Callback fired whenever the queuer's running state changes
|
|
39
|
+
*/
|
|
40
|
+
onIsRunningChange?: (queuer: AsyncQueuer<TValue>) => void;
|
|
37
41
|
/**
|
|
38
42
|
* Callback fired whenever an item is added or removed from the queuer
|
|
39
43
|
*/
|
|
40
|
-
|
|
44
|
+
onItemsChange?: (queuer: AsyncQueuer<TValue>) => void;
|
|
45
|
+
/**
|
|
46
|
+
* Callback fired whenever an item is rejected from being added to the queuer
|
|
47
|
+
*/
|
|
48
|
+
onReject?: (item: () => Promise<TValue>, queuer: AsyncQueuer<TValue>) => void;
|
|
41
49
|
/**
|
|
42
50
|
* Whether the queuer should start processing tasks immediately
|
|
43
51
|
*/
|
|
@@ -48,13 +56,13 @@ export interface AsyncQueuerOptions<TValue> {
|
|
|
48
56
|
wait?: number;
|
|
49
57
|
}
|
|
50
58
|
/**
|
|
51
|
-
* A flexible asynchronous
|
|
59
|
+
* A flexible asynchronous queue that processes tasks with configurable concurrency control.
|
|
52
60
|
*
|
|
53
61
|
* Features:
|
|
54
|
-
* - Priority
|
|
62
|
+
* - Priority queue support via getPriority option
|
|
55
63
|
* - Configurable concurrency limit
|
|
56
64
|
* - Task success/error/completion callbacks
|
|
57
|
-
* - FIFO (First In First Out) or LIFO (Last In First Out)
|
|
65
|
+
* - FIFO (First In First Out) or LIFO (Last In First Out) queue behavior
|
|
58
66
|
* - Pause/resume task processing
|
|
59
67
|
* - Task cancellation
|
|
60
68
|
*
|
|
@@ -63,39 +71,60 @@ export interface AsyncQueuerOptions<TValue> {
|
|
|
63
71
|
*
|
|
64
72
|
* @example
|
|
65
73
|
* ```ts
|
|
66
|
-
* const
|
|
74
|
+
* const asyncQueuer = new AsyncQueuer<string>({ concurrency: 2 });
|
|
67
75
|
*
|
|
68
|
-
*
|
|
76
|
+
* asyncQueuer.addItem(async () => {
|
|
69
77
|
* return 'Hello';
|
|
70
78
|
* });
|
|
71
79
|
*
|
|
72
|
-
*
|
|
80
|
+
* asyncQueuer.start();
|
|
73
81
|
*
|
|
74
|
-
*
|
|
82
|
+
* asyncQueuer.onSuccess((result) => {
|
|
75
83
|
* console.log(result); // 'Hello'
|
|
76
84
|
* });
|
|
77
85
|
* ```
|
|
78
86
|
*/
|
|
79
87
|
export declare class AsyncQueuer<TValue> {
|
|
80
|
-
|
|
81
|
-
private
|
|
82
|
-
private
|
|
83
|
-
private
|
|
84
|
-
private
|
|
85
|
-
private
|
|
86
|
-
private
|
|
87
|
-
private
|
|
88
|
-
private
|
|
88
|
+
private _options;
|
|
89
|
+
private _activeItems;
|
|
90
|
+
private _executionCount;
|
|
91
|
+
private _rejectionCount;
|
|
92
|
+
private _items;
|
|
93
|
+
private _onErrorCallbacks;
|
|
94
|
+
private _onSettledCallbacks;
|
|
95
|
+
private _onSuccessCallbacks;
|
|
96
|
+
private _pendingTick;
|
|
97
|
+
private _running;
|
|
89
98
|
constructor(initialOptions?: AsyncQueuerOptions<TValue>);
|
|
90
99
|
/**
|
|
91
100
|
* Updates the queuer options
|
|
92
101
|
* Returns the new options state
|
|
93
102
|
*/
|
|
94
103
|
setOptions(newOptions: Partial<AsyncQueuerOptions<TValue>>): AsyncQueuerOptions<TValue>;
|
|
104
|
+
/**
|
|
105
|
+
* Returns the current queuer options
|
|
106
|
+
*/
|
|
107
|
+
getOptions(): Required<AsyncQueuerOptions<TValue>>;
|
|
95
108
|
/**
|
|
96
109
|
* Processes items in the queuer
|
|
97
110
|
*/
|
|
98
|
-
|
|
111
|
+
private tick;
|
|
112
|
+
/**
|
|
113
|
+
* Starts the queuer and processes items
|
|
114
|
+
*/
|
|
115
|
+
start(): Promise<void>;
|
|
116
|
+
/**
|
|
117
|
+
* Stops the queuer from processing items
|
|
118
|
+
*/
|
|
119
|
+
stop(): void;
|
|
120
|
+
/**
|
|
121
|
+
* Removes all items from the queuer
|
|
122
|
+
*/
|
|
123
|
+
clear(): void;
|
|
124
|
+
/**
|
|
125
|
+
* Resets the queuer to its initial state
|
|
126
|
+
*/
|
|
127
|
+
reset(withInitialItems?: boolean): void;
|
|
99
128
|
/**
|
|
100
129
|
* Adds a task to the queuer
|
|
101
130
|
*/
|
|
@@ -109,35 +138,23 @@ export declare class AsyncQueuer<TValue> {
|
|
|
109
138
|
/**
|
|
110
139
|
* Returns an item without removing it
|
|
111
140
|
*/
|
|
112
|
-
|
|
141
|
+
getPeek(position?: QueuePosition): (() => Promise<TValue>) | undefined;
|
|
113
142
|
/**
|
|
114
143
|
* Returns true if the queuer is empty
|
|
115
144
|
*/
|
|
116
|
-
|
|
145
|
+
getIsEmpty(): boolean;
|
|
117
146
|
/**
|
|
118
147
|
* Returns true if the queuer is full
|
|
119
148
|
*/
|
|
120
|
-
|
|
149
|
+
getIsFull(): boolean;
|
|
121
150
|
/**
|
|
122
151
|
* Returns the current size of the queuer
|
|
123
152
|
*/
|
|
124
|
-
|
|
125
|
-
/**
|
|
126
|
-
* Removes all items from the queuer
|
|
127
|
-
*/
|
|
128
|
-
clear(): void;
|
|
129
|
-
/**
|
|
130
|
-
* Resets the queuer to its initial state
|
|
131
|
-
*/
|
|
132
|
-
reset(withInitialItems?: boolean): void;
|
|
153
|
+
getSize(): number;
|
|
133
154
|
/**
|
|
134
155
|
* Returns a copy of all items in the queuer
|
|
135
156
|
*/
|
|
136
157
|
getAllItems(): Array<() => Promise<TValue>>;
|
|
137
|
-
/**
|
|
138
|
-
* Returns the number of items that have been removed from the queuer
|
|
139
|
-
*/
|
|
140
|
-
getExecutionCount(): number;
|
|
141
158
|
/**
|
|
142
159
|
* Returns the active items
|
|
143
160
|
*/
|
|
@@ -147,33 +164,33 @@ export declare class AsyncQueuer<TValue> {
|
|
|
147
164
|
*/
|
|
148
165
|
getPendingItems(): Array<() => Promise<TValue>>;
|
|
149
166
|
/**
|
|
150
|
-
*
|
|
167
|
+
* Returns the number of items that have been removed from the queuer
|
|
151
168
|
*/
|
|
152
|
-
|
|
169
|
+
getExecutionCount(): number;
|
|
153
170
|
/**
|
|
154
|
-
*
|
|
171
|
+
* Returns the number of items that have been rejected from the queuer
|
|
155
172
|
*/
|
|
156
|
-
|
|
173
|
+
getRejectionCount(): number;
|
|
157
174
|
/**
|
|
158
|
-
*
|
|
175
|
+
* Returns true if the queuer is running
|
|
159
176
|
*/
|
|
160
|
-
|
|
177
|
+
getIsRunning(): boolean;
|
|
161
178
|
/**
|
|
162
|
-
*
|
|
179
|
+
* Returns true if the queuer is running but has no items to process
|
|
163
180
|
*/
|
|
164
|
-
|
|
181
|
+
getIsIdle(): boolean;
|
|
165
182
|
/**
|
|
166
|
-
*
|
|
183
|
+
* Adds a callback to be called when a task succeeds
|
|
167
184
|
*/
|
|
168
|
-
|
|
185
|
+
onSuccess(cb: (result: TValue) => void): () => void;
|
|
169
186
|
/**
|
|
170
|
-
*
|
|
187
|
+
* Adds a callback to be called when a task errors
|
|
171
188
|
*/
|
|
172
|
-
|
|
189
|
+
onError(cb: (error: Error) => void): () => void;
|
|
173
190
|
/**
|
|
174
|
-
*
|
|
191
|
+
* Adds a callback to be called when a task is settled
|
|
175
192
|
*/
|
|
176
|
-
|
|
193
|
+
onSettled(cb: (result: TValue | Error) => void): () => void;
|
|
177
194
|
}
|
|
178
195
|
/**
|
|
179
196
|
* Creates a new AsyncQueuer instance with the given options and returns a bound addItem function.
|