@tanstack/pacer 0.1.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/LICENSE +21 -0
- package/dist/cjs/async-debouncer.cjs +109 -0
- package/dist/cjs/async-debouncer.cjs.map +1 -0
- package/dist/cjs/async-debouncer.d.cts +99 -0
- package/dist/cjs/async-queuer.cjs +293 -0
- package/dist/cjs/async-queuer.cjs.map +1 -0
- package/dist/cjs/async-queuer.d.cts +197 -0
- package/dist/cjs/async-rate-limiter.cjs +122 -0
- package/dist/cjs/async-rate-limiter.cjs.map +1 -0
- package/dist/cjs/async-rate-limiter.d.cts +132 -0
- package/dist/cjs/async-throttler.cjs +115 -0
- package/dist/cjs/async-throttler.cjs.map +1 -0
- package/dist/cjs/async-throttler.d.cts +93 -0
- package/dist/cjs/compare.cjs +72 -0
- package/dist/cjs/compare.cjs.map +1 -0
- package/dist/cjs/compare.d.cts +12 -0
- package/dist/cjs/debouncer.cjs +74 -0
- package/dist/cjs/debouncer.cjs.map +1 -0
- package/dist/cjs/debouncer.d.cts +96 -0
- package/dist/cjs/index.cjs +32 -0
- package/dist/cjs/index.cjs.map +1 -0
- package/dist/cjs/index.d.cts +9 -0
- package/dist/cjs/queuer.cjs +231 -0
- package/dist/cjs/queuer.cjs.map +1 -0
- package/dist/cjs/queuer.d.cts +221 -0
- package/dist/cjs/rate-limiter.cjs +115 -0
- package/dist/cjs/rate-limiter.cjs.map +1 -0
- package/dist/cjs/rate-limiter.d.cts +147 -0
- package/dist/cjs/throttler.cjs +120 -0
- package/dist/cjs/throttler.cjs.map +1 -0
- package/dist/cjs/throttler.d.cts +139 -0
- package/dist/esm/async-debouncer.d.ts +99 -0
- package/dist/esm/async-debouncer.js +109 -0
- package/dist/esm/async-debouncer.js.map +1 -0
- package/dist/esm/async-queuer.d.ts +197 -0
- package/dist/esm/async-queuer.js +293 -0
- package/dist/esm/async-queuer.js.map +1 -0
- package/dist/esm/async-rate-limiter.d.ts +132 -0
- package/dist/esm/async-rate-limiter.js +122 -0
- package/dist/esm/async-rate-limiter.js.map +1 -0
- package/dist/esm/async-throttler.d.ts +93 -0
- package/dist/esm/async-throttler.js +115 -0
- package/dist/esm/async-throttler.js.map +1 -0
- package/dist/esm/compare.d.ts +12 -0
- package/dist/esm/compare.js +72 -0
- package/dist/esm/compare.js.map +1 -0
- package/dist/esm/debouncer.d.ts +96 -0
- package/dist/esm/debouncer.js +74 -0
- package/dist/esm/debouncer.js.map +1 -0
- package/dist/esm/index.d.ts +9 -0
- package/dist/esm/index.js +32 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/queuer.d.ts +221 -0
- package/dist/esm/queuer.js +231 -0
- package/dist/esm/queuer.js.map +1 -0
- package/dist/esm/rate-limiter.d.ts +147 -0
- package/dist/esm/rate-limiter.js +115 -0
- package/dist/esm/rate-limiter.js.map +1 -0
- package/dist/esm/throttler.d.ts +139 -0
- package/dist/esm/throttler.js +120 -0
- package/dist/esm/throttler.js.map +1 -0
- package/package.json +150 -0
- package/src/async-debouncer.ts +197 -0
- package/src/async-queuer.ts +447 -0
- package/src/async-rate-limiter.ts +228 -0
- package/src/async-throttler.ts +194 -0
- package/src/compare.ts +105 -0
- package/src/debouncer.ts +164 -0
- package/src/index.ts +9 -0
- package/src/queuer.ts +394 -0
- package/src/rate-limiter.ts +236 -0
- package/src/throttler.ts +222 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Tanner Linsley
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const defaultOptions = {
|
|
4
|
+
enabled: true,
|
|
5
|
+
leading: false,
|
|
6
|
+
trailing: true,
|
|
7
|
+
onError: () => {
|
|
8
|
+
},
|
|
9
|
+
wait: 0
|
|
10
|
+
};
|
|
11
|
+
class AsyncDebouncer {
|
|
12
|
+
constructor(fn, initialOptions) {
|
|
13
|
+
this.fn = fn;
|
|
14
|
+
this.abortController = null;
|
|
15
|
+
this.executionCount = 0;
|
|
16
|
+
this.isExecuting = false;
|
|
17
|
+
this.timeoutId = null;
|
|
18
|
+
this.canLeadingExecute = true;
|
|
19
|
+
this.options = {
|
|
20
|
+
...defaultOptions,
|
|
21
|
+
...initialOptions
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Updates the debouncer options
|
|
26
|
+
* Returns the new options state
|
|
27
|
+
*/
|
|
28
|
+
setOptions(newOptions) {
|
|
29
|
+
this.options = {
|
|
30
|
+
...this.options,
|
|
31
|
+
...newOptions
|
|
32
|
+
};
|
|
33
|
+
return this.options;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Returns the number of times the function has been executed
|
|
37
|
+
*/
|
|
38
|
+
getExecutionCount() {
|
|
39
|
+
return this.executionCount;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Cancels any pending execution
|
|
43
|
+
*/
|
|
44
|
+
cancel() {
|
|
45
|
+
if (this.timeoutId) {
|
|
46
|
+
clearTimeout(this.timeoutId);
|
|
47
|
+
this.timeoutId = null;
|
|
48
|
+
}
|
|
49
|
+
if (this.abortController) {
|
|
50
|
+
this.abortController.abort();
|
|
51
|
+
this.abortController = null;
|
|
52
|
+
}
|
|
53
|
+
this.lastArgs = void 0;
|
|
54
|
+
this.canLeadingExecute = true;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Attempts to execute the debounced function
|
|
58
|
+
* If a call is already in progress, it will be queued
|
|
59
|
+
*/
|
|
60
|
+
async maybeExecute(...args) {
|
|
61
|
+
this.cancel();
|
|
62
|
+
this.lastArgs = args;
|
|
63
|
+
if (this.options.leading && this.canLeadingExecute) {
|
|
64
|
+
this.canLeadingExecute = false;
|
|
65
|
+
await this.executeFunction(...args);
|
|
66
|
+
}
|
|
67
|
+
return new Promise((resolve) => {
|
|
68
|
+
this.timeoutId = setTimeout(async () => {
|
|
69
|
+
if (this.isExecuting) {
|
|
70
|
+
resolve();
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
this.canLeadingExecute = true;
|
|
74
|
+
if (this.options.trailing) {
|
|
75
|
+
this.abortController = new AbortController();
|
|
76
|
+
try {
|
|
77
|
+
this.isExecuting = true;
|
|
78
|
+
if (this.lastArgs) {
|
|
79
|
+
await this.executeFunction(...this.lastArgs);
|
|
80
|
+
}
|
|
81
|
+
} catch (error) {
|
|
82
|
+
try {
|
|
83
|
+
this.options.onError(error);
|
|
84
|
+
} catch {
|
|
85
|
+
}
|
|
86
|
+
} finally {
|
|
87
|
+
this.isExecuting = false;
|
|
88
|
+
this.abortController = null;
|
|
89
|
+
resolve();
|
|
90
|
+
}
|
|
91
|
+
} else {
|
|
92
|
+
resolve();
|
|
93
|
+
}
|
|
94
|
+
}, this.options.wait);
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
async executeFunction(...args) {
|
|
98
|
+
if (!this.options.enabled) return;
|
|
99
|
+
this.executionCount++;
|
|
100
|
+
await this.fn(...args);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
function asyncDebounce(fn, initialOptions) {
|
|
104
|
+
const asyncDebouncer = new AsyncDebouncer(fn, initialOptions);
|
|
105
|
+
return asyncDebouncer.maybeExecute.bind(asyncDebouncer);
|
|
106
|
+
}
|
|
107
|
+
exports.AsyncDebouncer = AsyncDebouncer;
|
|
108
|
+
exports.asyncDebounce = asyncDebounce;
|
|
109
|
+
//# sourceMappingURL=async-debouncer.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"async-debouncer.cjs","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 /**\n * Optional error handler for when the debounced function throws\n */\n onError?: (error: unknown) => void\n}\n\nconst defaultOptions: Required<AsyncDebouncerOptions> = {\n enabled: true,\n leading: false,\n trailing: true,\n onError: () => {},\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 debouncer = 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 * debouncer.maybeExecute(inputElement.value);\n * });\n * ```\n */\nexport class AsyncDebouncer<\n TFn extends (...args: Array<any>) => Promise<any>,\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>\n private timeoutId: ReturnType<typeof setTimeout> | null = null\n private canLeadingExecute = true\n\n constructor(\n private fn: TFn,\n initialOptions: AsyncDebouncerOptions,\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>,\n ): Required<AsyncDebouncerOptions> {\n this.options = {\n ...this.options,\n ...newOptions,\n }\n return this.options\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 * 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 * 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 // Ignore errors from error handler\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 }\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 (...args: Array<any>) => Promise<any>,\n>(fn: TFn, initialOptions: Omit<AsyncDebouncerOptions, 'enabled'>) {\n const asyncDebouncer = new AsyncDebouncer(fn, initialOptions)\n return asyncDebouncer.maybeExecute.bind(asyncDebouncer)\n}\n"],"names":[],"mappings":";;AA8BA,MAAM,iBAAkD;AAAA,EACtD,SAAS;AAAA,EACT,SAAS;AAAA,EACT,UAAU;AAAA,EACV,SAAS,MAAM;AAAA,EAAC;AAAA,EAChB,MAAM;AACR;AAwBO,MAAM,eAGX;AAAA,EASA,YACU,IACR,gBACA;AAFQ,SAAA,KAAA;AATV,SAAQ,kBAA0C;AAClD,SAAQ,iBAAiB;AACzB,SAAQ,cAAc;AAGtB,SAAQ,YAAkD;AAC1D,SAAQ,oBAAoB;AAM1B,SAAK,UAAU;AAAA,MACb,GAAG;AAAA,MACH,GAAG;AAAA,IACL;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOF,WACE,YACiC;A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AAA;AAAA,EAGH,MAAc,mBAAmB,MAA4B;AACvD,QAAA,CAAC,KAAK,QAAQ,QAAS;AACtB,SAAA;AACC,UAAA,KAAK,GAAG,GAAG,IAAI;AAAA,EAAA;AAEzB;AAmBgB,SAAA,cAEd,IAAS,gBAAwD;AACjE,QAAM,iBAAiB,IAAI,eAAe,IAAI,cAAc;AACrD,SAAA,eAAe,aAAa,KAAK,cAAc;AACxD;;;"}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Options for configuring an async debounced function
|
|
3
|
+
*/
|
|
4
|
+
export interface AsyncDebouncerOptions {
|
|
5
|
+
/**
|
|
6
|
+
* Whether the debouncer is enabled. When disabled, maybeExecute will not trigger any executions.
|
|
7
|
+
* Defaults to true.
|
|
8
|
+
*/
|
|
9
|
+
enabled?: boolean;
|
|
10
|
+
/**
|
|
11
|
+
* Whether to execute on the leading edge of the timeout.
|
|
12
|
+
* Defaults to false.
|
|
13
|
+
*/
|
|
14
|
+
leading?: boolean;
|
|
15
|
+
/**
|
|
16
|
+
* Whether to execute on the trailing edge of the timeout.
|
|
17
|
+
* Defaults to true.
|
|
18
|
+
*/
|
|
19
|
+
trailing?: boolean;
|
|
20
|
+
/**
|
|
21
|
+
* Delay in milliseconds to wait after the last call before executing
|
|
22
|
+
* Defaults to 0ms
|
|
23
|
+
*/
|
|
24
|
+
wait: number;
|
|
25
|
+
/**
|
|
26
|
+
* Optional error handler for when the debounced function throws
|
|
27
|
+
*/
|
|
28
|
+
onError?: (error: unknown) => void;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* A class that creates an async debounced function.
|
|
32
|
+
*
|
|
33
|
+
* Debouncing ensures that a function is only executed after a specified delay has passed since its last invocation.
|
|
34
|
+
* Each new invocation resets the delay timer. This is useful for handling frequent events like window resizing
|
|
35
|
+
* or input changes where you only want to execute the handler after the events have stopped occurring.
|
|
36
|
+
*
|
|
37
|
+
* Unlike throttling which allows execution at regular intervals, debouncing prevents any execution until
|
|
38
|
+
* the function stops being called for the specified delay period.
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* ```ts
|
|
42
|
+
* const debouncer = new AsyncDebouncer(async (value: string) => {
|
|
43
|
+
* await searchAPI(value);
|
|
44
|
+
* }, { wait: 500 });
|
|
45
|
+
*
|
|
46
|
+
* // Called on each keystroke but only executes after 500ms of no typing
|
|
47
|
+
* inputElement.addEventListener('input', () => {
|
|
48
|
+
* debouncer.maybeExecute(inputElement.value);
|
|
49
|
+
* });
|
|
50
|
+
* ```
|
|
51
|
+
*/
|
|
52
|
+
export declare class AsyncDebouncer<TFn extends (...args: Array<any>) => Promise<any>, TArgs extends Parameters<TFn>> {
|
|
53
|
+
private fn;
|
|
54
|
+
private abortController;
|
|
55
|
+
private executionCount;
|
|
56
|
+
private isExecuting;
|
|
57
|
+
private lastArgs;
|
|
58
|
+
private options;
|
|
59
|
+
private timeoutId;
|
|
60
|
+
private canLeadingExecute;
|
|
61
|
+
constructor(fn: TFn, initialOptions: AsyncDebouncerOptions);
|
|
62
|
+
/**
|
|
63
|
+
* Updates the debouncer options
|
|
64
|
+
* Returns the new options state
|
|
65
|
+
*/
|
|
66
|
+
setOptions(newOptions: Partial<AsyncDebouncerOptions>): Required<AsyncDebouncerOptions>;
|
|
67
|
+
/**
|
|
68
|
+
* Returns the number of times the function has been executed
|
|
69
|
+
*/
|
|
70
|
+
getExecutionCount(): number;
|
|
71
|
+
/**
|
|
72
|
+
* Cancels any pending execution
|
|
73
|
+
*/
|
|
74
|
+
cancel(): void;
|
|
75
|
+
/**
|
|
76
|
+
* Attempts to execute the debounced function
|
|
77
|
+
* If a call is already in progress, it will be queued
|
|
78
|
+
*/
|
|
79
|
+
maybeExecute(...args: TArgs): Promise<void>;
|
|
80
|
+
private executeFunction;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Creates an async debounced function that delays execution until after a specified wait time.
|
|
84
|
+
* The debounced function will only execute once the wait period has elapsed without any new calls.
|
|
85
|
+
* If called again during the wait period, the timer resets and a new wait period begins.
|
|
86
|
+
*
|
|
87
|
+
* @example
|
|
88
|
+
* ```ts
|
|
89
|
+
* const debounced = asyncDebounce(async (value: string) => {
|
|
90
|
+
* await saveToAPI(value);
|
|
91
|
+
* }, { wait: 1000 });
|
|
92
|
+
*
|
|
93
|
+
* // Will only execute once, 1 second after the last call
|
|
94
|
+
* await debounced("first"); // Cancelled
|
|
95
|
+
* await debounced("second"); // Cancelled
|
|
96
|
+
* await debounced("third"); // Executes after 1s
|
|
97
|
+
* ```
|
|
98
|
+
*/
|
|
99
|
+
export declare function asyncDebounce<TFn extends (...args: Array<any>) => Promise<any>>(fn: TFn, initialOptions: Omit<AsyncDebouncerOptions, 'enabled'>): (...args: Parameters<TFn>) => Promise<void>;
|
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const defaultOptions = {
|
|
4
|
+
addItemsTo: "back",
|
|
5
|
+
concurrency: 1,
|
|
6
|
+
getItemsFrom: "front",
|
|
7
|
+
getPriority: (item) => item.priority ?? 0,
|
|
8
|
+
initialItems: [],
|
|
9
|
+
maxSize: Infinity,
|
|
10
|
+
onGetNextItem: () => {
|
|
11
|
+
},
|
|
12
|
+
onUpdate: () => {
|
|
13
|
+
},
|
|
14
|
+
started: false,
|
|
15
|
+
wait: 0
|
|
16
|
+
};
|
|
17
|
+
class AsyncQueuer {
|
|
18
|
+
constructor(initialOptions = defaultOptions) {
|
|
19
|
+
this.items = [];
|
|
20
|
+
this.activeItems = /* @__PURE__ */ new Set();
|
|
21
|
+
this.onSuccessCallbacks = [];
|
|
22
|
+
this.onErrorCallbacks = [];
|
|
23
|
+
this.onSettledCallbacks = [];
|
|
24
|
+
this.pendingTick = false;
|
|
25
|
+
this.executionCount = 0;
|
|
26
|
+
this.options = { ...defaultOptions, ...initialOptions };
|
|
27
|
+
this.running = this.options.started;
|
|
28
|
+
for (let i = 0; i < this.options.initialItems.length; i++) {
|
|
29
|
+
const item = this.options.initialItems[i];
|
|
30
|
+
const isLast = i === this.options.initialItems.length - 1;
|
|
31
|
+
this.addItem(item, this.options.addItemsTo, isLast);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Updates the queuer options
|
|
36
|
+
* Returns the new options state
|
|
37
|
+
*/
|
|
38
|
+
setOptions(newOptions) {
|
|
39
|
+
this.options = { ...this.options, ...newOptions };
|
|
40
|
+
return this.options;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Processes items in the queuer
|
|
44
|
+
*/
|
|
45
|
+
tick() {
|
|
46
|
+
if (!this.running) {
|
|
47
|
+
this.pendingTick = false;
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
while (this.activeItems.size < this.options.concurrency && !this.isEmpty()) {
|
|
51
|
+
const nextFn = this.getNextItem();
|
|
52
|
+
if (!nextFn) {
|
|
53
|
+
break;
|
|
54
|
+
}
|
|
55
|
+
this.activeItems.add(nextFn);
|
|
56
|
+
(async () => {
|
|
57
|
+
let success = false;
|
|
58
|
+
let res;
|
|
59
|
+
let error;
|
|
60
|
+
try {
|
|
61
|
+
res = await nextFn();
|
|
62
|
+
success = true;
|
|
63
|
+
} catch (e) {
|
|
64
|
+
error = e;
|
|
65
|
+
} finally {
|
|
66
|
+
this.options.onUpdate(this);
|
|
67
|
+
}
|
|
68
|
+
this.activeItems.delete(nextFn);
|
|
69
|
+
if (success) {
|
|
70
|
+
this.onSuccessCallbacks.forEach((cb) => cb(res));
|
|
71
|
+
} else {
|
|
72
|
+
this.onErrorCallbacks.forEach((cb) => cb(error));
|
|
73
|
+
}
|
|
74
|
+
this.onSettledCallbacks.forEach((cb) => cb(success ? res : error));
|
|
75
|
+
if (this.options.wait > 0) {
|
|
76
|
+
setTimeout(() => this.tick(), this.options.wait);
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
this.tick();
|
|
80
|
+
})();
|
|
81
|
+
}
|
|
82
|
+
this.pendingTick = false;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Adds a task to the queuer
|
|
86
|
+
*/
|
|
87
|
+
addItem(fn, position = this.options.addItemsTo, runOnUpdate = true) {
|
|
88
|
+
if (this.isFull()) {
|
|
89
|
+
return Promise.reject(new Error("Queuer is full"));
|
|
90
|
+
}
|
|
91
|
+
return new Promise((resolve, reject) => {
|
|
92
|
+
const task = Object.assign(
|
|
93
|
+
async () => {
|
|
94
|
+
try {
|
|
95
|
+
const result = await fn();
|
|
96
|
+
resolve(result);
|
|
97
|
+
return result;
|
|
98
|
+
} catch (error) {
|
|
99
|
+
reject(error);
|
|
100
|
+
throw error;
|
|
101
|
+
}
|
|
102
|
+
},
|
|
103
|
+
{ priority: fn.priority ?? void 0 }
|
|
104
|
+
);
|
|
105
|
+
const priority = this.options.getPriority !== defaultOptions.getPriority ? this.options.getPriority(task) : task.priority;
|
|
106
|
+
if (priority !== void 0) {
|
|
107
|
+
const insertIndex = this.items.findIndex((existing) => {
|
|
108
|
+
const existingPriority = this.options.getPriority !== defaultOptions.getPriority ? this.options.getPriority(existing) : existing.priority;
|
|
109
|
+
return existingPriority > priority;
|
|
110
|
+
});
|
|
111
|
+
if (insertIndex === -1) {
|
|
112
|
+
this.items.push(task);
|
|
113
|
+
} else {
|
|
114
|
+
this.items.splice(insertIndex, 0, task);
|
|
115
|
+
}
|
|
116
|
+
} else {
|
|
117
|
+
if (position === "front") {
|
|
118
|
+
this.items.unshift(task);
|
|
119
|
+
} else {
|
|
120
|
+
this.items.push(task);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
if (runOnUpdate) {
|
|
124
|
+
this.options.onUpdate(this);
|
|
125
|
+
}
|
|
126
|
+
if (this.running && !this.pendingTick) {
|
|
127
|
+
this.pendingTick = true;
|
|
128
|
+
this.tick();
|
|
129
|
+
}
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Removes and returns an item from the queuer
|
|
134
|
+
*/
|
|
135
|
+
getNextItem(position = this.options.getItemsFrom) {
|
|
136
|
+
let item;
|
|
137
|
+
if (position === "front") {
|
|
138
|
+
item = this.items.shift();
|
|
139
|
+
} else {
|
|
140
|
+
item = this.items.pop();
|
|
141
|
+
}
|
|
142
|
+
if (item !== void 0) {
|
|
143
|
+
this.executionCount++;
|
|
144
|
+
this.options.onUpdate(this);
|
|
145
|
+
this.options.onGetNextItem(item, this);
|
|
146
|
+
}
|
|
147
|
+
return item;
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Returns an item without removing it
|
|
151
|
+
*/
|
|
152
|
+
peek(position = "front") {
|
|
153
|
+
if (position === "front") {
|
|
154
|
+
return this.items[0];
|
|
155
|
+
}
|
|
156
|
+
return this.items[this.items.length - 1];
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Returns true if the queuer is empty
|
|
160
|
+
*/
|
|
161
|
+
isEmpty() {
|
|
162
|
+
return this.items.length === 0;
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Returns true if the queuer is full
|
|
166
|
+
*/
|
|
167
|
+
isFull() {
|
|
168
|
+
return this.items.length >= this.options.maxSize;
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Returns the current size of the queuer
|
|
172
|
+
*/
|
|
173
|
+
size() {
|
|
174
|
+
return this.items.length;
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* Removes all items from the queuer
|
|
178
|
+
*/
|
|
179
|
+
clear() {
|
|
180
|
+
this.items = [];
|
|
181
|
+
this.options.onUpdate(this);
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Resets the queuer to its initial state
|
|
185
|
+
*/
|
|
186
|
+
reset(withInitialItems) {
|
|
187
|
+
this.clear();
|
|
188
|
+
this.executionCount = 0;
|
|
189
|
+
if (withInitialItems) {
|
|
190
|
+
this.items = [...this.options.initialItems];
|
|
191
|
+
}
|
|
192
|
+
this.running = this.options.started;
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* Returns a copy of all items in the queuer
|
|
196
|
+
*/
|
|
197
|
+
getAllItems() {
|
|
198
|
+
return [...this.items];
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* Returns the number of items that have been removed from the queuer
|
|
202
|
+
*/
|
|
203
|
+
getExecutionCount() {
|
|
204
|
+
return this.executionCount;
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* Returns the active items
|
|
208
|
+
*/
|
|
209
|
+
getActiveItems() {
|
|
210
|
+
return Array.from(this.activeItems);
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* Returns the pending items
|
|
214
|
+
*/
|
|
215
|
+
getPendingItems() {
|
|
216
|
+
return this.getAllItems();
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* Adds a callback to be called when a task succeeds
|
|
220
|
+
*/
|
|
221
|
+
onSuccess(cb) {
|
|
222
|
+
this.onSuccessCallbacks.push(cb);
|
|
223
|
+
return () => {
|
|
224
|
+
this.onSuccessCallbacks = this.onSuccessCallbacks.filter((d) => d !== cb);
|
|
225
|
+
};
|
|
226
|
+
}
|
|
227
|
+
/**
|
|
228
|
+
* Adds a callback to be called when a task errors
|
|
229
|
+
*/
|
|
230
|
+
onError(cb) {
|
|
231
|
+
this.onErrorCallbacks.push(cb);
|
|
232
|
+
return () => {
|
|
233
|
+
this.onErrorCallbacks = this.onErrorCallbacks.filter((d) => d !== cb);
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
/**
|
|
237
|
+
* Adds a callback to be called when a task is settled
|
|
238
|
+
*/
|
|
239
|
+
onSettled(cb) {
|
|
240
|
+
this.onSettledCallbacks.push(cb);
|
|
241
|
+
return () => {
|
|
242
|
+
this.onSettledCallbacks = this.onSettledCallbacks.filter((d) => d !== cb);
|
|
243
|
+
};
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* Starts the queuer and processes items
|
|
247
|
+
*/
|
|
248
|
+
start() {
|
|
249
|
+
this.running = true;
|
|
250
|
+
if (!this.pendingTick && !this.isEmpty()) {
|
|
251
|
+
this.pendingTick = true;
|
|
252
|
+
this.tick();
|
|
253
|
+
}
|
|
254
|
+
this.options.onUpdate(this);
|
|
255
|
+
return new Promise((resolve) => {
|
|
256
|
+
const checkIdle = () => {
|
|
257
|
+
if (this.isIdle()) {
|
|
258
|
+
resolve();
|
|
259
|
+
} else {
|
|
260
|
+
setTimeout(checkIdle, 100);
|
|
261
|
+
}
|
|
262
|
+
};
|
|
263
|
+
checkIdle();
|
|
264
|
+
});
|
|
265
|
+
}
|
|
266
|
+
/**
|
|
267
|
+
* Stops the queuer from processing items
|
|
268
|
+
*/
|
|
269
|
+
stop() {
|
|
270
|
+
this.running = false;
|
|
271
|
+
this.pendingTick = false;
|
|
272
|
+
this.options.onUpdate(this);
|
|
273
|
+
}
|
|
274
|
+
/**
|
|
275
|
+
* Returns true if the queuer is running
|
|
276
|
+
*/
|
|
277
|
+
isRunning() {
|
|
278
|
+
return this.running;
|
|
279
|
+
}
|
|
280
|
+
/**
|
|
281
|
+
* Returns true if the queuer is running but has no items to process
|
|
282
|
+
*/
|
|
283
|
+
isIdle() {
|
|
284
|
+
return this.running && this.isEmpty() && this.activeItems.size === 0;
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
function asyncQueue(options = {}) {
|
|
288
|
+
const queuer = new AsyncQueuer({ ...options, started: true });
|
|
289
|
+
return queuer.addItem.bind(queuer);
|
|
290
|
+
}
|
|
291
|
+
exports.AsyncQueuer = AsyncQueuer;
|
|
292
|
+
exports.asyncQueue = asyncQueue;
|
|
293
|
+
//# sourceMappingURL=async-queuer.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"async-queuer.cjs","sources":["../../src/async-queuer.ts"],"sourcesContent":["import type { QueuePosition } from './queuer'\n\nexport interface AsyncQueuerOptions<TValue> {\n /**\n * Default position to add items to the queuer\n * @default 'back'\n */\n addItemsTo?: QueuePosition\n /**\n * Maximum number of concurrent tasks to process\n */\n concurrency?: number\n /**\n * Default position to get items from during processing\n * @default 'front'\n */\n getItemsFrom?: QueuePosition\n /**\n * Function to determine priority of items in the queuer\n * Higher priority items will be processed first\n * If not provided, will use static priority values attached to tasks\n */\n getPriority?: (item: () => Promise<TValue>) => number\n /**\n * Initial items to populate the queuer with\n */\n initialItems?: Array<(() => Promise<TValue>) & { priority?: number }>\n /**\n * Maximum number of items allowed in the queuer\n */\n maxSize?: number\n /**\n * Callback fired whenever an item is removed from the queuer\n */\n onGetNextItem?: (\n item: () => Promise<TValue>,\n queuer: AsyncQueuer<TValue>,\n ) => void\n /**\n * Callback fired whenever an item is added or removed from the queuer\n */\n onUpdate?: (queuer: AsyncQueuer<TValue>) => void\n /**\n * Whether the queuer should start processing tasks immediately\n */\n started?: boolean\n /**\n * Time in milliseconds to wait between processing items\n */\n wait?: number\n}\n\nconst defaultOptions: Required<AsyncQueuerOptions<any>> = {\n addItemsTo: 'back',\n concurrency: 1,\n getItemsFrom: 'front',\n getPriority: (item) => (item as any).priority ?? 0,\n initialItems: [],\n maxSize: Infinity,\n onGetNextItem: () => {},\n onUpdate: () => {},\n started: false,\n wait: 0,\n}\n\n/**\n * A flexible asynchronous queuer that processes tasks with configurable concurrency control.\n *\n * Features:\n * - Priority queuer support via getPriority option\n * - Configurable concurrency limit\n * - Task success/error/completion callbacks\n * - FIFO (First In First Out) or LIFO (Last In First Out) queuer behavior\n * - Pause/resume task processing\n * - Task cancellation\n *\n * Tasks are processed concurrently up to the configured concurrency limit. When a task completes,\n * the next pending task is processed if below the concurrency limit.\n *\n * @example\n * ```ts\n * const queuer = new AsyncQueuer<string>({ concurrency: 2 });\n *\n * queuer.addItem(async () => {\n * return 'Hello';\n * });\n *\n * queuer.start();\n *\n * queuer.onSuccess((result) => {\n * console.log(result); // 'Hello'\n * });\n * ```\n */\nexport class AsyncQueuer<TValue> {\n protected options: Required<AsyncQueuerOptions<TValue>>\n private items: Array<() => Promise<TValue>> = []\n private activeItems: Set<() => Promise<TValue>> = new Set()\n private onSuccessCallbacks: Array<(result: TValue) => void> = []\n private onErrorCallbacks: Array<(error: Error) => void> = []\n private onSettledCallbacks: Array<(result: TValue | Error) => void> = []\n private running: boolean\n private pendingTick = false\n private executionCount = 0\n\n constructor(initialOptions: AsyncQueuerOptions<TValue> = defaultOptions) {\n this.options = { ...defaultOptions, ...initialOptions }\n this.running = this.options.started\n\n for (let i = 0; i < this.options.initialItems.length; i++) {\n const item = this.options.initialItems[i]!\n const isLast = i === this.options.initialItems.length - 1\n this.addItem(item, this.options.addItemsTo, isLast)\n }\n }\n\n /**\n * Updates the queuer options\n * Returns the new options state\n */\n setOptions(\n newOptions: Partial<AsyncQueuerOptions<TValue>>,\n ): AsyncQueuerOptions<TValue> {\n this.options = { ...this.options, ...newOptions }\n return this.options\n }\n\n /**\n * Processes items in the queuer\n */\n protected tick() {\n if (!this.running) {\n this.pendingTick = false\n return\n }\n\n while (\n this.activeItems.size < this.options.concurrency &&\n !this.isEmpty()\n ) {\n const nextFn = this.getNextItem()\n if (!nextFn) {\n break\n }\n\n this.activeItems.add(nextFn)\n ;(async () => {\n let success = false\n let res!: TValue\n let error: Error | undefined\n\n try {\n res = await nextFn()\n success = true\n } catch (e) {\n error = e as Error\n } finally {\n this.options.onUpdate(this)\n }\n\n this.activeItems.delete(nextFn)\n if (success) {\n this.onSuccessCallbacks.forEach((cb) => cb(res))\n } else {\n this.onErrorCallbacks.forEach((cb) => cb(error!))\n }\n this.onSettledCallbacks.forEach((cb) => cb(success ? res : error!))\n\n if (this.options.wait > 0) {\n setTimeout(() => this.tick(), this.options.wait)\n return\n }\n\n this.tick()\n })()\n }\n\n this.pendingTick = false\n }\n\n /**\n * Adds a task to the queuer\n */\n addItem(\n fn: (() => Promise<TValue>) & { priority?: number },\n position: QueuePosition = this.options.addItemsTo,\n runOnUpdate: boolean = true,\n ): Promise<TValue> {\n if (this.isFull()) {\n return Promise.reject(new Error('Queuer is full'))\n }\n\n return new Promise<TValue>((resolve, reject) => {\n const task = Object.assign(\n async () => {\n try {\n const result = await fn()\n resolve(result)\n return result\n } catch (error) {\n reject(error)\n throw error\n }\n },\n { priority: fn.priority ?? undefined },\n )\n\n // Get priority either from the function or from getPriority option\n const priority =\n this.options.getPriority !== defaultOptions.getPriority\n ? this.options.getPriority(task)\n : task.priority\n\n if (priority !== undefined) {\n // Insert based on priority\n const insertIndex = this.items.findIndex((existing) => {\n const existingPriority =\n this.options.getPriority !== defaultOptions.getPriority\n ? this.options.getPriority(existing)\n : (existing as any).priority\n return existingPriority > priority\n })\n\n if (insertIndex === -1) {\n this.items.push(task)\n } else {\n this.items.splice(insertIndex, 0, task)\n }\n } else {\n // Default FIFO/LIFO behavior\n if (position === 'front') {\n this.items.unshift(task)\n } else {\n this.items.push(task)\n }\n }\n\n if (runOnUpdate) {\n this.options.onUpdate(this)\n }\n\n if (this.running && !this.pendingTick) {\n this.pendingTick = true\n this.tick()\n }\n })\n }\n\n /**\n * Removes and returns an item from the queuer\n */\n getNextItem(\n position: QueuePosition = this.options.getItemsFrom,\n ): (() => Promise<TValue>) | undefined {\n let item: (() => Promise<TValue>) | undefined\n\n if (position === 'front') {\n item = this.items.shift()\n } else {\n item = this.items.pop()\n }\n\n if (item !== undefined) {\n this.executionCount++\n this.options.onUpdate(this)\n this.options.onGetNextItem(item, this)\n }\n return item\n }\n\n /**\n * Returns an item without removing it\n */\n peek(position: QueuePosition = 'front'): (() => Promise<TValue>) | undefined {\n if (position === 'front') {\n return this.items[0]\n }\n return this.items[this.items.length - 1]\n }\n\n /**\n * Returns true if the queuer is empty\n */\n isEmpty(): boolean {\n return this.items.length === 0\n }\n\n /**\n * Returns true if the queuer is full\n */\n isFull(): boolean {\n return this.items.length >= this.options.maxSize\n }\n\n /**\n * Returns the current size of the queuer\n */\n size(): number {\n return this.items.length\n }\n\n /**\n * Removes all items from the queuer\n */\n clear(): void {\n this.items = []\n this.options.onUpdate(this)\n }\n\n /**\n * Resets the queuer to its initial state\n */\n reset(withInitialItems?: boolean): void {\n this.clear()\n this.executionCount = 0\n if (withInitialItems) {\n this.items = [...this.options.initialItems]\n }\n this.running = this.options.started\n }\n\n /**\n * Returns a copy of all items in the queuer\n */\n getAllItems(): Array<() => Promise<TValue>> {\n return [...this.items]\n }\n\n /**\n * Returns the number of items that have been removed from the queuer\n */\n getExecutionCount(): number {\n return this.executionCount\n }\n\n /**\n * Returns the active items\n */\n getActiveItems(): Array<() => Promise<TValue>> {\n return Array.from(this.activeItems)\n }\n\n /**\n * Returns the pending items\n */\n getPendingItems(): Array<() => Promise<TValue>> {\n return this.getAllItems()\n }\n\n /**\n * Adds a callback to be called when a task succeeds\n */\n onSuccess(cb: (result: TValue) => void) {\n this.onSuccessCallbacks.push(cb)\n return () => {\n this.onSuccessCallbacks = this.onSuccessCallbacks.filter((d) => d !== cb)\n }\n }\n\n /**\n * Adds a callback to be called when a task errors\n */\n onError(cb: (error: Error) => void) {\n this.onErrorCallbacks.push(cb)\n return () => {\n this.onErrorCallbacks = this.onErrorCallbacks.filter((d) => d !== cb)\n }\n }\n\n /**\n * Adds a callback to be called when a task is settled\n */\n onSettled(cb: (result: TValue | Error) => void) {\n this.onSettledCallbacks.push(cb)\n return () => {\n this.onSettledCallbacks = this.onSettledCallbacks.filter((d) => d !== cb)\n }\n }\n\n /**\n * Starts the queuer and processes items\n */\n start(): Promise<void> {\n this.running = true\n if (!this.pendingTick && !this.isEmpty()) {\n this.pendingTick = true\n this.tick()\n }\n this.options.onUpdate(this)\n\n return new Promise<void>((resolve) => {\n const checkIdle = () => {\n if (this.isIdle()) {\n resolve()\n } else {\n setTimeout(checkIdle, 100)\n }\n }\n checkIdle()\n })\n }\n\n /**\n * Stops the queuer from processing items\n */\n stop(): void {\n this.running = false\n this.pendingTick = false\n this.options.onUpdate(this)\n }\n\n /**\n * Returns true if the queuer is running\n */\n isRunning(): boolean {\n return this.running\n }\n\n /**\n * Returns true if the queuer is running but has no items to process\n */\n isIdle(): boolean {\n return this.running && this.isEmpty() && this.activeItems.size === 0\n }\n}\n\n/**\n * Creates a new AsyncQueuer instance with the given options and returns a bound addItem function.\n * The queuer is automatically started and ready to process items.\n *\n * @example\n * ```ts\n * const enqueue = asyncQueue<string>();\n *\n * // Add items to be processed\n * enqueue(async () => {\n * return 'Hello';\n * });\n * ```\n *\n * @param options - Configuration options for the AsyncQueuer\n * @returns A bound addItem function that can be used to add tasks to the queuer\n */\nexport function asyncQueue<TValue>(options: AsyncQueuerOptions<TValue> = {}) {\n const queuer = new AsyncQueuer<TValue>({ ...options, started: true })\n return queuer.addItem.bind(queuer)\n}\n"],"names":[],"mappings":";;AAoDA,MAAM,iBAAoD;AAAA,EACxD,YAAY;AAAA,EACZ,aAAa;AAAA,EACb,cAAc;AAAA,EACd,aAAa,CAAC,SAAU,KAAa,YAAY;AAAA,EACjD,cAAc,CAAC;AAAA,EACf,SAAS;AAAA,EACT,eAAe,MAAM;AAAA,EAAC;AAAA,EACtB,UAAU,MAAM;AAAA,EAAC;AAAA,EACjB,SAAS;AAAA,EACT,MAAM;AACR;AA+BO,MAAM,YAAoB;AAAA,EAW/B,YAAY,iBAA6C,gBAAgB;AATzE,SAAQ,QAAsC,CAAC;AACvC,SAAA,kCAA8C,IAAI;AAC1D,SAAQ,qBAAsD,CAAC;AAC/D,SAAQ,mBAAkD,CAAC;AAC3D,SAAQ,qBAA8D,CAAC;AAEvE,SAAQ,cAAc;AACtB,SAAQ,iBAAiB;AAGvB,SAAK,UAAU,EAAE,GAAG,gBAAgB,GAAG,eAAe;AACjD,SAAA,UAAU,KAAK,QAAQ;AAE5B,aAAS,IAAI,GAAG,IAAI,KAAK,QAAQ,aAAa,QAAQ,KAAK;AACzD,YAAM,OAAO,KAAK,QAAQ,aAAa,CAAC;AACxC,YAAM,SAAS,MAAM,KAAK,QAAQ,aAAa,SAAS;AACxD,WAAK,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