@tanstack/pacer 0.2.0 → 0.3.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 +78 -45
- package/dist/cjs/async-debouncer.cjs.map +1 -1
- package/dist/cjs/async-debouncer.d.cts +44 -16
- package/dist/cjs/async-queuer.cjs +53 -3
- package/dist/cjs/async-queuer.cjs.map +1 -1
- package/dist/cjs/async-queuer.d.cts +28 -3
- package/dist/cjs/async-rate-limiter.cjs +46 -35
- package/dist/cjs/async-rate-limiter.cjs.map +1 -1
- package/dist/cjs/async-rate-limiter.d.cts +34 -19
- package/dist/cjs/async-throttler.cjs +85 -57
- package/dist/cjs/async-throttler.cjs.map +1 -1
- package/dist/cjs/async-throttler.d.cts +49 -17
- package/dist/cjs/debouncer.cjs +12 -14
- package/dist/cjs/debouncer.cjs.map +1 -1
- package/dist/cjs/debouncer.d.cts +10 -9
- package/dist/cjs/queuer.cjs +51 -1
- package/dist/cjs/queuer.cjs.map +1 -1
- package/dist/cjs/queuer.d.cts +30 -1
- package/dist/cjs/rate-limiter.cjs +1 -5
- package/dist/cjs/rate-limiter.cjs.map +1 -1
- package/dist/cjs/rate-limiter.d.cts +9 -9
- package/dist/cjs/throttler.cjs +29 -36
- package/dist/cjs/throttler.cjs.map +1 -1
- package/dist/cjs/throttler.d.cts +16 -17
- package/dist/cjs/types.d.cts +2 -6
- package/dist/cjs/utils.cjs.map +1 -1
- package/dist/cjs/utils.d.cts +1 -1
- package/dist/esm/async-debouncer.d.ts +44 -16
- package/dist/esm/async-debouncer.js +78 -45
- package/dist/esm/async-debouncer.js.map +1 -1
- package/dist/esm/async-queuer.d.ts +28 -3
- package/dist/esm/async-queuer.js +53 -3
- package/dist/esm/async-queuer.js.map +1 -1
- package/dist/esm/async-rate-limiter.d.ts +34 -19
- package/dist/esm/async-rate-limiter.js +46 -35
- package/dist/esm/async-rate-limiter.js.map +1 -1
- package/dist/esm/async-throttler.d.ts +49 -17
- package/dist/esm/async-throttler.js +85 -57
- package/dist/esm/async-throttler.js.map +1 -1
- package/dist/esm/debouncer.d.ts +10 -9
- package/dist/esm/debouncer.js +12 -14
- package/dist/esm/debouncer.js.map +1 -1
- package/dist/esm/queuer.d.ts +30 -1
- package/dist/esm/queuer.js +51 -1
- package/dist/esm/queuer.js.map +1 -1
- package/dist/esm/rate-limiter.d.ts +9 -9
- package/dist/esm/rate-limiter.js +1 -5
- package/dist/esm/rate-limiter.js.map +1 -1
- package/dist/esm/throttler.d.ts +16 -17
- package/dist/esm/throttler.js +29 -36
- package/dist/esm/throttler.js.map +1 -1
- package/dist/esm/types.d.ts +2 -6
- package/dist/esm/utils.d.ts +1 -1
- package/dist/esm/utils.js.map +1 -1
- package/package.json +1 -1
- package/src/async-debouncer.ts +114 -73
- package/src/async-queuer.ts +93 -8
- package/src/async-rate-limiter.ts +74 -60
- package/src/async-throttler.ts +135 -86
- package/src/debouncer.ts +26 -33
- package/src/queuer.ts +92 -4
- package/src/rate-limiter.ts +14 -26
- package/src/throttler.ts +45 -53
- package/src/types.ts +2 -10
- package/src/utils.ts +1 -1
|
@@ -1 +1 @@
|
|
|
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;"}
|
|
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<TFn extends AnyAsyncFunction> {\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, debouncer: AsyncDebouncer<TFn>) => void\n /**\n * Optional callback to call when the debounced function is executed\n */\n onSettled?: (debouncer: AsyncDebouncer<TFn>) => void\n /**\n * Optional callback to call when the debounced function is executed\n */\n onSuccess?: (result: ReturnType<TFn>, debouncer: AsyncDebouncer<TFn>) => 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>> = {\n enabled: true,\n leading: false,\n onError: () => {},\n onSettled: () => {},\n onSuccess: () => {},\n trailing: true,\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<TFn extends AnyAsyncFunction> {\n private _abortController: AbortController | null = null\n private _canLeadingExecute = true\n private _errorCount = 0\n private _isExecuting = false\n private _isPending = false\n private _lastArgs: Parameters<TFn> | undefined\n private _lastResult: ReturnType<TFn> | undefined\n private _options: Required<AsyncDebouncerOptions<TFn>>\n private _settleCount = 0\n private _successCount = 0\n private _timeoutId: NodeJS.Timeout | null = null\n\n constructor(\n private fn: TFn,\n initialOptions: AsyncDebouncerOptions<TFn>,\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(newOptions: Partial<AsyncDebouncerOptions<TFn>>): void {\n this._options = { ...this._options, ...newOptions }\n\n // End the pending state if the debouncer is disabled\n if (!this._options.enabled) {\n this._isPending = false\n }\n }\n\n /**\n * Returns the current debouncer options\n */\n getOptions(): Required<AsyncDebouncerOptions<TFn>> {\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(\n ...args: Parameters<TFn>\n ): Promise<ReturnType<TFn> | undefined> {\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 return this._lastResult\n }\n\n // Handle trailing execution\n if (this._options.trailing) {\n this._isPending = true\n }\n\n return new Promise((resolve) => {\n this._timeoutId = setTimeout(async () => {\n // Execute trailing if enabled\n if (this._options.trailing && this._lastArgs) {\n await this.executeFunction(...this._lastArgs)\n }\n\n // Reset state and resolve\n this._canLeadingExecute = true\n resolve(this._lastResult)\n }, this._options.wait)\n })\n }\n\n private async executeFunction(\n ...args: Parameters<TFn>\n ): Promise<ReturnType<TFn> | undefined> {\n if (!this._options.enabled) return undefined\n this._abortController = new AbortController()\n try {\n this._isExecuting = true\n this._lastResult = await this.fn(...args) // EXECUTE!\n this._successCount++\n this._options.onSuccess(this._lastResult!, this)\n } catch (error) {\n this._errorCount++\n this._options.onError(error, this)\n } finally {\n this._isExecuting = false\n this._isPending = false\n this._settleCount++\n this._abortController = null\n this._options.onSettled(this)\n }\n return this._lastResult\n }\n\n /**\n * Cancel without resetting _canLeadingExecute\n */\n private _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._isPending = false\n this._isExecuting = false\n }\n\n /**\n * Cancels any pending execution or aborts any execution in progress\n */\n cancel(): void {\n this._canLeadingExecute = true\n this._cancel()\n }\n\n /**\n * Returns the last result of the debounced function\n */\n getLastResult(): ReturnType<TFn> | undefined {\n return this._lastResult\n }\n\n /**\n * Returns the number of times the function has been executed successfully\n */\n getSuccessCount(): number {\n return this._successCount\n }\n\n /**\n * Returns the number of times the function has settled (completed or errored)\n */\n getSettleCount(): number {\n return this._settleCount\n }\n\n /**\n * Returns the number of times the function has errored\n */\n getErrorCount(): number {\n return this._errorCount\n }\n\n /**\n * Returns `true` if there is a pending execution queued up for trailing execution\n */\n getIsPending(): boolean {\n return this._options.enabled && this._isPending\n }\n\n /**\n * Returns `true` if there is currently an execution in progress\n */\n getIsExecuting(): boolean {\n return this._isExecuting\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<TFn extends AnyAsyncFunction>(\n fn: TFn,\n initialOptions: Omit<AsyncDebouncerOptions<TFn>, 'enabled'>,\n) {\n const asyncDebouncer = new AsyncDebouncer(fn, initialOptions)\n return asyncDebouncer.maybeExecute.bind(asyncDebouncer)\n}\n"],"names":[],"mappings":"AAwCA,MAAM,iBAAuD;AAAA,EAC3D,SAAS;AAAA,EACT,SAAS;AAAA,EACT,SAAS,MAAM;AAAA,EAAC;AAAA,EAChB,WAAW,MAAM;AAAA,EAAC;AAAA,EAClB,WAAW,MAAM;AAAA,EAAC;A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,QAAM,iBAAiB,IAAI,eAAe,IAAI,cAAc;AACrD,SAAA,eAAe,aAAa,KAAK,cAAc;AACxD;"}
|
|
@@ -9,6 +9,16 @@ export interface AsyncQueuerOptions<TValue> {
|
|
|
9
9
|
* Maximum number of concurrent tasks to process
|
|
10
10
|
*/
|
|
11
11
|
concurrency?: number;
|
|
12
|
+
/**
|
|
13
|
+
* Maximum time in milliseconds that an item can stay in the queue
|
|
14
|
+
* If not provided, items will never expire
|
|
15
|
+
*/
|
|
16
|
+
expirationDuration?: number;
|
|
17
|
+
/**
|
|
18
|
+
* Function to determine if an item has expired
|
|
19
|
+
* If provided, this overrides the expirationDuration behavior
|
|
20
|
+
*/
|
|
21
|
+
getIsExpired?: (item: () => Promise<TValue>, addedAt: number) => boolean;
|
|
12
22
|
/**
|
|
13
23
|
* Default position to get items from during processing
|
|
14
24
|
* @default 'front'
|
|
@@ -47,7 +57,11 @@ export interface AsyncQueuerOptions<TValue> {
|
|
|
47
57
|
*/
|
|
48
58
|
onReject?: (item: () => Promise<TValue>, queuer: AsyncQueuer<TValue>) => void;
|
|
49
59
|
/**
|
|
50
|
-
*
|
|
60
|
+
* Callback fired whenever an item expires in the queuer
|
|
61
|
+
*/
|
|
62
|
+
onExpire?: (item: () => Promise<TValue>, queuer: AsyncQueuer<TValue>) => void;
|
|
63
|
+
/**
|
|
64
|
+
* Whether the queuer should start processing tasks immediately or not.
|
|
51
65
|
*/
|
|
52
66
|
started?: boolean;
|
|
53
67
|
/**
|
|
@@ -65,6 +79,7 @@ export interface AsyncQueuerOptions<TValue> {
|
|
|
65
79
|
* - FIFO (First In First Out) or LIFO (Last In First Out) queue behavior
|
|
66
80
|
* - Pause/resume task processing
|
|
67
81
|
* - Task cancellation
|
|
82
|
+
* - Item expiration to clear stale items from the queue
|
|
68
83
|
*
|
|
69
84
|
* Tasks are processed concurrently up to the configured concurrency limit. When a task completes,
|
|
70
85
|
* the next pending task is processed if below the concurrency limit.
|
|
@@ -89,7 +104,9 @@ export declare class AsyncQueuer<TValue> {
|
|
|
89
104
|
private _activeItems;
|
|
90
105
|
private _executionCount;
|
|
91
106
|
private _rejectionCount;
|
|
107
|
+
private _expirationCount;
|
|
92
108
|
private _items;
|
|
109
|
+
private _itemTimestamps;
|
|
93
110
|
private _onErrorCallbacks;
|
|
94
111
|
private _onSettledCallbacks;
|
|
95
112
|
private _onSuccessCallbacks;
|
|
@@ -100,7 +117,7 @@ export declare class AsyncQueuer<TValue> {
|
|
|
100
117
|
* Updates the queuer options
|
|
101
118
|
* Returns the new options state
|
|
102
119
|
*/
|
|
103
|
-
setOptions(newOptions: Partial<AsyncQueuerOptions<TValue>>):
|
|
120
|
+
setOptions(newOptions: Partial<AsyncQueuerOptions<TValue>>): void;
|
|
104
121
|
/**
|
|
105
122
|
* Returns the current queuer options
|
|
106
123
|
*/
|
|
@@ -109,6 +126,10 @@ export declare class AsyncQueuer<TValue> {
|
|
|
109
126
|
* Processes items in the queuer
|
|
110
127
|
*/
|
|
111
128
|
private tick;
|
|
129
|
+
/**
|
|
130
|
+
* Checks for and removes expired items from the queuer
|
|
131
|
+
*/
|
|
132
|
+
private checkExpiredItems;
|
|
112
133
|
/**
|
|
113
134
|
* Starts the queuer and processes items
|
|
114
135
|
*/
|
|
@@ -191,6 +212,10 @@ export declare class AsyncQueuer<TValue> {
|
|
|
191
212
|
* Adds a callback to be called when a task is settled
|
|
192
213
|
*/
|
|
193
214
|
onSettled(cb: (result: TValue | Error) => void): () => void;
|
|
215
|
+
/**
|
|
216
|
+
* Returns the number of items that have expired from the queuer
|
|
217
|
+
*/
|
|
218
|
+
getExpirationCount(): number;
|
|
194
219
|
}
|
|
195
220
|
/**
|
|
196
221
|
* Creates a new AsyncQueuer instance with the given options and returns a bound addItem function.
|
|
@@ -209,6 +234,6 @@ export declare class AsyncQueuer<TValue> {
|
|
|
209
234
|
* @param options - Configuration options for the AsyncQueuer
|
|
210
235
|
* @returns A bound addItem function that can be used to add tasks to the queuer
|
|
211
236
|
*/
|
|
212
|
-
export declare function asyncQueue<TValue>(options?: AsyncQueuerOptions<TValue>): (fn: (() => Promise<TValue>) & {
|
|
237
|
+
export declare function asyncQueue<TValue>(options?: Omit<AsyncQueuerOptions<TValue>, 'started'>): (fn: (() => Promise<TValue>) & {
|
|
213
238
|
priority?: number;
|
|
214
239
|
}, position?: QueuePosition, runOnUpdate?: boolean) => Promise<TValue>;
|
package/dist/esm/async-queuer.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
const defaultOptions = {
|
|
2
2
|
addItemsTo: "back",
|
|
3
3
|
concurrency: 1,
|
|
4
|
+
expirationDuration: Infinity,
|
|
5
|
+
getIsExpired: () => false,
|
|
4
6
|
getItemsFrom: "front",
|
|
5
7
|
getPriority: (item) => (item == null ? void 0 : item.priority) ?? 0,
|
|
6
8
|
initialItems: [],
|
|
@@ -13,7 +15,9 @@ const defaultOptions = {
|
|
|
13
15
|
},
|
|
14
16
|
onReject: () => {
|
|
15
17
|
},
|
|
16
|
-
|
|
18
|
+
onExpire: () => {
|
|
19
|
+
},
|
|
20
|
+
started: true,
|
|
17
21
|
wait: 0
|
|
18
22
|
};
|
|
19
23
|
class AsyncQueuer {
|
|
@@ -21,7 +25,9 @@ class AsyncQueuer {
|
|
|
21
25
|
this._activeItems = /* @__PURE__ */ new Set();
|
|
22
26
|
this._executionCount = 0;
|
|
23
27
|
this._rejectionCount = 0;
|
|
28
|
+
this._expirationCount = 0;
|
|
24
29
|
this._items = [];
|
|
30
|
+
this._itemTimestamps = [];
|
|
25
31
|
this._onErrorCallbacks = [];
|
|
26
32
|
this._onSettledCallbacks = [];
|
|
27
33
|
this._onSuccessCallbacks = [];
|
|
@@ -40,7 +46,6 @@ class AsyncQueuer {
|
|
|
40
46
|
*/
|
|
41
47
|
setOptions(newOptions) {
|
|
42
48
|
this._options = { ...this._options, ...newOptions };
|
|
43
|
-
return this._options;
|
|
44
49
|
}
|
|
45
50
|
/**
|
|
46
51
|
* Returns the current queuer options
|
|
@@ -56,6 +61,7 @@ class AsyncQueuer {
|
|
|
56
61
|
this._pendingTick = false;
|
|
57
62
|
return;
|
|
58
63
|
}
|
|
64
|
+
this.checkExpiredItems();
|
|
59
65
|
while (this._activeItems.size < this._options.concurrency && !this.getIsEmpty()) {
|
|
60
66
|
const nextFn = this.getNextItem();
|
|
61
67
|
if (!nextFn) {
|
|
@@ -91,6 +97,38 @@ class AsyncQueuer {
|
|
|
91
97
|
}
|
|
92
98
|
this._pendingTick = false;
|
|
93
99
|
}
|
|
100
|
+
/**
|
|
101
|
+
* Checks for and removes expired items from the queuer
|
|
102
|
+
*/
|
|
103
|
+
checkExpiredItems() {
|
|
104
|
+
if (this._options.expirationDuration === Infinity && this._options.getIsExpired === defaultOptions.getIsExpired)
|
|
105
|
+
return;
|
|
106
|
+
const now = Date.now();
|
|
107
|
+
const expiredIndices = [];
|
|
108
|
+
for (let i = 0; i < this._items.length; i++) {
|
|
109
|
+
const timestamp = this._itemTimestamps[i];
|
|
110
|
+
if (timestamp === void 0) continue;
|
|
111
|
+
const item = this._items[i];
|
|
112
|
+
if (item === void 0) continue;
|
|
113
|
+
const isExpired = this._options.getIsExpired !== defaultOptions.getIsExpired ? this._options.getIsExpired(item, timestamp) : now - timestamp > this._options.expirationDuration;
|
|
114
|
+
if (isExpired) {
|
|
115
|
+
expiredIndices.push(i);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
for (let i = expiredIndices.length - 1; i >= 0; i--) {
|
|
119
|
+
const index = expiredIndices[i];
|
|
120
|
+
if (index === void 0) continue;
|
|
121
|
+
const expiredItem = this._items[index];
|
|
122
|
+
if (expiredItem === void 0) continue;
|
|
123
|
+
this._items.splice(index, 1);
|
|
124
|
+
this._itemTimestamps.splice(index, 1);
|
|
125
|
+
this._expirationCount++;
|
|
126
|
+
this._options.onExpire(expiredItem, this);
|
|
127
|
+
}
|
|
128
|
+
if (expiredIndices.length > 0) {
|
|
129
|
+
this._options.onItemsChange(this);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
94
132
|
/**
|
|
95
133
|
* Starts the queuer and processes items
|
|
96
134
|
*/
|
|
@@ -169,14 +207,18 @@ class AsyncQueuer {
|
|
|
169
207
|
});
|
|
170
208
|
if (insertIndex === -1) {
|
|
171
209
|
this._items.push(task);
|
|
210
|
+
this._itemTimestamps.push(Date.now());
|
|
172
211
|
} else {
|
|
173
212
|
this._items.splice(insertIndex, 0, task);
|
|
213
|
+
this._itemTimestamps.splice(insertIndex, 0, Date.now());
|
|
174
214
|
}
|
|
175
215
|
} else {
|
|
176
216
|
if (position === "front") {
|
|
177
217
|
this._items.unshift(task);
|
|
218
|
+
this._itemTimestamps.unshift(Date.now());
|
|
178
219
|
} else {
|
|
179
220
|
this._items.push(task);
|
|
221
|
+
this._itemTimestamps.push(Date.now());
|
|
180
222
|
}
|
|
181
223
|
}
|
|
182
224
|
if (runOnUpdate) {
|
|
@@ -195,8 +237,10 @@ class AsyncQueuer {
|
|
|
195
237
|
let item;
|
|
196
238
|
if (position === "front") {
|
|
197
239
|
item = this._items.shift();
|
|
240
|
+
this._itemTimestamps.shift();
|
|
198
241
|
} else {
|
|
199
242
|
item = this._items.pop();
|
|
243
|
+
this._itemTimestamps.pop();
|
|
200
244
|
}
|
|
201
245
|
if (item !== void 0) {
|
|
202
246
|
this._executionCount++;
|
|
@@ -305,9 +349,15 @@ class AsyncQueuer {
|
|
|
305
349
|
);
|
|
306
350
|
};
|
|
307
351
|
}
|
|
352
|
+
/**
|
|
353
|
+
* Returns the number of items that have expired from the queuer
|
|
354
|
+
*/
|
|
355
|
+
getExpirationCount() {
|
|
356
|
+
return this._expirationCount;
|
|
357
|
+
}
|
|
308
358
|
}
|
|
309
359
|
function asyncQueue(options = {}) {
|
|
310
|
-
const queuer = new AsyncQueuer(
|
|
360
|
+
const queuer = new AsyncQueuer(options);
|
|
311
361
|
return queuer.addItem.bind(queuer);
|
|
312
362
|
}
|
|
313
363
|
export {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"async-queuer.js","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 the queuer's running state changes\n */\n onIsRunningChange?: (queuer: AsyncQueuer<TValue>) => void\n /**\n * Callback fired whenever an item is added or removed from the queuer\n */\n onItemsChange?: (queuer: AsyncQueuer<TValue>) => void\n /**\n * Callback fired whenever an item is rejected from being added to the queuer\n */\n onReject?: (item: () => Promise<TValue>, 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 onIsRunningChange: () => {},\n onItemsChange: () => {},\n onReject: () => {},\n started: false,\n wait: 0,\n}\n\n/**\n * A flexible asynchronous queue that processes tasks with configurable concurrency control.\n *\n * Features:\n * - Priority queue 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) queue 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 asyncQueuer = new AsyncQueuer<string>({ concurrency: 2 });\n *\n * asyncQueuer.addItem(async () => {\n * return 'Hello';\n * });\n *\n * asyncQueuer.start();\n *\n * asyncQueuer.onSuccess((result) => {\n * console.log(result); // 'Hello'\n * });\n * ```\n */\nexport class AsyncQueuer<TValue> {\n private _options: Required<AsyncQueuerOptions<TValue>>\n private _activeItems: Set<() => Promise<TValue>> = new Set()\n private _executionCount = 0\n private _rejectionCount = 0\n private _items: Array<() => Promise<TValue>> = []\n private _onErrorCallbacks: Array<(error: Error) => void> = []\n private _onSettledCallbacks: Array<(result: TValue | Error) => void> = []\n private _onSuccessCallbacks: Array<(result: TValue) => void> = []\n private _pendingTick = false\n private _running: boolean\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 * Returns the current queuer options\n */\n getOptions(): Required<AsyncQueuerOptions<TValue>> {\n return this._options\n }\n\n /**\n * Processes items in the queuer\n */\n private tick() {\n if (!this._running) {\n this._pendingTick = false\n return\n }\n\n while (\n this._activeItems.size < this._options.concurrency &&\n !this.getIsEmpty()\n ) {\n const nextFn = this.getNextItem()\n if (!nextFn) {\n break\n }\n this._activeItems.add(nextFn)\n this._options.onItemsChange(this)\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._activeItems.delete(nextFn)\n this._options.onItemsChange(this)\n }\n\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 * Starts the queuer and processes items\n */\n start(): Promise<void> {\n this._running = true\n if (!this._pendingTick && !this.getIsEmpty()) {\n this._pendingTick = true\n this.tick()\n }\n this._options.onIsRunningChange(this)\n\n return new Promise<void>((resolve) => {\n const checkIdle = () => {\n if (this.getIsIdle()) {\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.onIsRunningChange(this)\n }\n\n /**\n * Removes all items from the queuer\n */\n clear(): void {\n this._items = []\n this._options.onItemsChange(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 * 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.getIsFull()) {\n this._rejectionCount++\n this._options.onReject(fn, this)\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.onItemsChange(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.onItemsChange(this)\n this._options.onGetNextItem(item, this)\n }\n return item\n }\n\n /**\n * Returns an item without removing it\n */\n getPeek(\n position: QueuePosition = 'front',\n ): (() => 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 getIsEmpty(): boolean {\n return this._items.length === 0\n }\n\n /**\n * Returns true if the queuer is full\n */\n getIsFull(): boolean {\n return this._items.length >= this._options.maxSize\n }\n\n /**\n * Returns the current size of the queuer\n */\n getSize(): number {\n return this._items.length\n }\n\n /**\n * Returns a copy of all items in the queuer\n */\n getAllItems(): Array<() => Promise<TValue>> {\n return [...this.getActiveItems(), ...this.getPendingItems()]\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._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 number of items that have been rejected from the queuer\n */\n getRejectionCount(): number {\n return this._rejectionCount\n }\n\n /**\n * Returns true if the queuer is running\n */\n getIsRunning(): boolean {\n return this._running\n }\n\n /**\n * Returns true if the queuer is running but has no items to process\n */\n getIsIdle(): boolean {\n return this._running && this.getIsEmpty() && this._activeItems.size === 0\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(\n (d) => d !== cb,\n )\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(\n (d) => d !== cb,\n )\n }\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":"AA4DA,MAAM,iBAAoD;AAAA,EACxD,YAAY;AAAA,EACZ,aAAa;AAAA,EACb,cAAc;AAAA,EACd,aAAa,CAAC,UAAU,6BAAc,aAAY;AAAA,EAClD,cAAc,CAAC;AAAA,EACf,SAAS;AAAA,EAC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rC,kBAAgD;AACvC,WAAA,CAAC,GAAG,KAAK,MAAM;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA,EAMxB,oBAA4B;AAC1B,WAAO,KAAK;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA,EAMd,oBAA4B;AAC1B,WAAO,KAAK;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA,EAMd,eAAwB;AACtB,WAAO,KAAK;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA,EAMd,YAAqB;AACnB,WAAO,KAAK,YAAY,KAAK,WAAgB,KAAA,KAAK,aAAa,SAAS;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA,EAM1E,UAAU,IAA8B;AACjC,SAAA,oBAAoB,KAAK,EAAE;AAChC,WAAO,MAAM;AACN,WAAA,sBAAsB,KAAK,oBAAoB;AAAA,QAClD,CAAC,MAAM,MAAM;AAAA,MACf;AAAA,IACF;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA,EAMF,QAAQ,IAA4B;AAC7B,SAAA,kBAAkB,KAAK,EAAE;AAC9B,WAAO,MAAM;AACX,WAAK,oBAAoB,KAAK,kBAAkB,OAAO,CAAC,MAAM,MAAM,EAAE;AAAA,IACxE;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA,EAMF,UAAU,IAAsC;AACzC,SAAA,oBAAoB,KAAK,EAAE;AAChC,WAAO,MAAM;AACN,WAAA,sBAAsB,KAAK,oBAAoB;AAAA,QAClD,CAAC,MAAM,MAAM;AAAA,MACf;AAAA,IACF;AAAA,EAAA;AAEJ;AAmBgB,SAAA,WAAmB,UAAsC,IAAI;AACrE,QAAA,SAAS,IAAI,YAAoB,EAAE,GAAG,SAAS,SAAS,MAAM;AAC7D,SAAA,OAAO,QAAQ,KAAK,MAAM;AACnC;"}
|
|
1
|
+
{"version":3,"file":"async-queuer.js","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 * Maximum time in milliseconds that an item can stay in the queue\n * If not provided, items will never expire\n */\n expirationDuration?: number\n /**\n * Function to determine if an item has expired\n * If provided, this overrides the expirationDuration behavior\n */\n getIsExpired?: (item: () => Promise<TValue>, addedAt: number) => boolean\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 the queuer's running state changes\n */\n onIsRunningChange?: (queuer: AsyncQueuer<TValue>) => void\n /**\n * Callback fired whenever an item is added or removed from the queuer\n */\n onItemsChange?: (queuer: AsyncQueuer<TValue>) => void\n /**\n * Callback fired whenever an item is rejected from being added to the queuer\n */\n onReject?: (item: () => Promise<TValue>, queuer: AsyncQueuer<TValue>) => void\n /**\n * Callback fired whenever an item expires in the queuer\n */\n onExpire?: (item: () => Promise<TValue>, queuer: AsyncQueuer<TValue>) => void\n /**\n * Whether the queuer should start processing tasks immediately or not.\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 expirationDuration: Infinity,\n getIsExpired: () => false,\n getItemsFrom: 'front',\n getPriority: (item) => (item as any)?.priority ?? 0,\n initialItems: [],\n maxSize: Infinity,\n onGetNextItem: () => {},\n onIsRunningChange: () => {},\n onItemsChange: () => {},\n onReject: () => {},\n onExpire: () => {},\n started: true,\n wait: 0,\n}\n\n/**\n * A flexible asynchronous queue that processes tasks with configurable concurrency control.\n *\n * Features:\n * - Priority queue 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) queue behavior\n * - Pause/resume task processing\n * - Task cancellation\n * - Item expiration to clear stale items from the queue\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 asyncQueuer = new AsyncQueuer<string>({ concurrency: 2 });\n *\n * asyncQueuer.addItem(async () => {\n * return 'Hello';\n * });\n *\n * asyncQueuer.start();\n *\n * asyncQueuer.onSuccess((result) => {\n * console.log(result); // 'Hello'\n * });\n * ```\n */\nexport class AsyncQueuer<TValue> {\n private _options: Required<AsyncQueuerOptions<TValue>>\n private _activeItems: Set<() => Promise<TValue>> = new Set()\n private _executionCount = 0\n private _rejectionCount = 0\n private _expirationCount = 0\n private _items: Array<() => Promise<TValue>> = []\n private _itemTimestamps: Array<number> = []\n private _onErrorCallbacks: Array<(error: Error) => void> = []\n private _onSettledCallbacks: Array<(result: TValue | Error) => void> = []\n private _onSuccessCallbacks: Array<(result: TValue) => void> = []\n private _pendingTick = false\n private _running: boolean\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(newOptions: Partial<AsyncQueuerOptions<TValue>>): void {\n this._options = { ...this._options, ...newOptions }\n }\n\n /**\n * Returns the current queuer options\n */\n getOptions(): Required<AsyncQueuerOptions<TValue>> {\n return this._options\n }\n\n /**\n * Processes items in the queuer\n */\n private tick() {\n if (!this._running) {\n this._pendingTick = false\n return\n }\n\n // Check for expired items\n this.checkExpiredItems()\n\n while (\n this._activeItems.size < this._options.concurrency &&\n !this.getIsEmpty()\n ) {\n const nextFn = this.getNextItem()\n if (!nextFn) {\n break\n }\n this._activeItems.add(nextFn)\n this._options.onItemsChange(this)\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._activeItems.delete(nextFn)\n this._options.onItemsChange(this)\n }\n\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 * Checks for and removes expired items from the queuer\n */\n private checkExpiredItems() {\n if (\n this._options.expirationDuration === Infinity &&\n this._options.getIsExpired === defaultOptions.getIsExpired\n )\n return\n\n const now = Date.now()\n const expiredIndices: Array<number> = []\n\n // Find indices of expired items\n for (let i = 0; i < this._items.length; i++) {\n const timestamp = this._itemTimestamps[i]\n if (timestamp === undefined) continue\n\n const item = this._items[i]\n if (item === undefined) continue\n\n const isExpired =\n this._options.getIsExpired !== defaultOptions.getIsExpired\n ? this._options.getIsExpired(item, timestamp)\n : now - timestamp > this._options.expirationDuration\n\n if (isExpired) {\n expiredIndices.push(i)\n }\n }\n\n // Remove expired items from back to front to maintain indices\n for (let i = expiredIndices.length - 1; i >= 0; i--) {\n const index = expiredIndices[i]\n if (index === undefined) continue\n\n const expiredItem = this._items[index]\n if (expiredItem === undefined) continue\n\n this._items.splice(index, 1)\n this._itemTimestamps.splice(index, 1)\n this._expirationCount++\n this._options.onExpire(expiredItem, this)\n }\n\n if (expiredIndices.length > 0) {\n this._options.onItemsChange(this)\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.getIsEmpty()) {\n this._pendingTick = true\n this.tick()\n }\n this._options.onIsRunningChange(this)\n\n return new Promise<void>((resolve) => {\n const checkIdle = () => {\n if (this.getIsIdle()) {\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.onIsRunningChange(this)\n }\n\n /**\n * Removes all items from the queuer\n */\n clear(): void {\n this._items = []\n this._options.onItemsChange(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 * 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.getIsFull()) {\n this._rejectionCount++\n this._options.onReject(fn, this)\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 this._itemTimestamps.push(Date.now())\n } else {\n this._items.splice(insertIndex, 0, task)\n this._itemTimestamps.splice(insertIndex, 0, Date.now())\n }\n } else {\n // Default FIFO/LIFO behavior\n if (position === 'front') {\n this._items.unshift(task)\n this._itemTimestamps.unshift(Date.now())\n } else {\n this._items.push(task)\n this._itemTimestamps.push(Date.now())\n }\n }\n\n if (runOnUpdate) {\n this._options.onItemsChange(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 this._itemTimestamps.shift()\n } else {\n item = this._items.pop()\n this._itemTimestamps.pop()\n }\n\n if (item !== undefined) {\n this._executionCount++\n this._options.onItemsChange(this)\n this._options.onGetNextItem(item, this)\n }\n return item\n }\n\n /**\n * Returns an item without removing it\n */\n getPeek(\n position: QueuePosition = 'front',\n ): (() => 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 getIsEmpty(): boolean {\n return this._items.length === 0\n }\n\n /**\n * Returns true if the queuer is full\n */\n getIsFull(): boolean {\n return this._items.length >= this._options.maxSize\n }\n\n /**\n * Returns the current size of the queuer\n */\n getSize(): number {\n return this._items.length\n }\n\n /**\n * Returns a copy of all items in the queuer\n */\n getAllItems(): Array<() => Promise<TValue>> {\n return [...this.getActiveItems(), ...this.getPendingItems()]\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._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 number of items that have been rejected from the queuer\n */\n getRejectionCount(): number {\n return this._rejectionCount\n }\n\n /**\n * Returns true if the queuer is running\n */\n getIsRunning(): boolean {\n return this._running\n }\n\n /**\n * Returns true if the queuer is running but has no items to process\n */\n getIsIdle(): boolean {\n return this._running && this.getIsEmpty() && this._activeItems.size === 0\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(\n (d) => d !== cb,\n )\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(\n (d) => d !== cb,\n )\n }\n }\n\n /**\n * Returns the number of items that have expired from the queuer\n */\n getExpirationCount(): number {\n return this._expirationCount\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>(\n options: Omit<AsyncQueuerOptions<TValue>, 'started'> = {},\n) {\n const queuer = new AsyncQueuer<TValue>(options)\n return queuer.addItem.bind(queuer)\n}\n"],"names":[],"mappings":"AA0EA,MAAM,iBAAoD;AAAA,EACxD,YAAY;AAAA,EACZ,aAAa;AAAA,EACb,oBAAoB;AAAA,EACpB,cAAc,MAAM;AAAA,EACpB,cAAc;AAAA,EACd,aAAa,CAAC,UAAU,6BAAc,aAAY;AAAA,EAClD,cAAc,CAAC;AAAA,EACf,SAAS;AAAA,EACT,eAAe,MAAM;AAAA,EAAC;AAAA,EACtB,mBAAmB,MAAM;AAAA,EAAC;AAAA,EAC1B,eAAe,MAAM;A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AAAA;AAAA;AAAA,EAMxB,oBAA4B;AAC1B,WAAO,KAAK;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA,EAMd,oBAA4B;AAC1B,WAAO,KAAK;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA,EAMd,eAAwB;AACtB,WAAO,KAAK;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA,EAMd,YAAqB;AACnB,WAAO,KAAK,YAAY,KAAK,WAAgB,KAAA,KAAK,aAAa,SAAS;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA,EAM1E,UAAU,IAA8B;AACjC,SAAA,oBAAoB,KAAK,EAAE;AAChC,WAAO,MAAM;AACN,WAAA,sBAAsB,KAAK,oBAAoB;AAAA,QAClD,CAAC,MAAM,MAAM;AAAA,MACf;AAAA,IACF;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA,EAMF,QAAQ,IAA4B;AAC7B,SAAA,kBAAkB,KAAK,EAAE;AAC9B,WAAO,MAAM;AACX,WAAK,oBAAoB,KAAK,kBAAkB,OAAO,CAAC,MAAM,MAAM,EAAE;AAAA,IACxE;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA,EAMF,UAAU,IAAsC;AACzC,SAAA,oBAAoB,KAAK,EAAE;AAChC,WAAO,MAAM;AACN,WAAA,sBAAsB,KAAK,oBAAoB;AAAA,QAClD,CAAC,MAAM,MAAM;AAAA,MACf;AAAA,IACF;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA,EAMF,qBAA6B;AAC3B,WAAO,KAAK;AAAA,EAAA;AAEhB;AAmBgB,SAAA,WACd,UAAuD,IACvD;AACM,QAAA,SAAS,IAAI,YAAoB,OAAO;AACvC,SAAA,OAAO,QAAQ,KAAK,MAAM;AACnC;"}
|
|
@@ -2,7 +2,7 @@ import { AnyAsyncFunction } from './types.js';
|
|
|
2
2
|
/**
|
|
3
3
|
* Options for configuring an async rate-limited function
|
|
4
4
|
*/
|
|
5
|
-
export interface AsyncRateLimiterOptions<TFn extends AnyAsyncFunction
|
|
5
|
+
export interface AsyncRateLimiterOptions<TFn extends AnyAsyncFunction> {
|
|
6
6
|
/**
|
|
7
7
|
* Whether the rate limiter is enabled. When disabled, maybeExecute will not trigger any executions.
|
|
8
8
|
* Defaults to true.
|
|
@@ -15,15 +15,19 @@ export interface AsyncRateLimiterOptions<TFn extends AnyAsyncFunction, TArgs ext
|
|
|
15
15
|
/**
|
|
16
16
|
* Optional error handler for when the rate-limited function throws
|
|
17
17
|
*/
|
|
18
|
-
onError?: (error: unknown) => void;
|
|
18
|
+
onError?: (error: unknown, rateLimiter: AsyncRateLimiter<TFn>) => void;
|
|
19
19
|
/**
|
|
20
20
|
* Optional function to call when the rate-limited function is executed
|
|
21
21
|
*/
|
|
22
|
-
|
|
22
|
+
onSettled?: (rateLimiter: AsyncRateLimiter<TFn>) => void;
|
|
23
|
+
/**
|
|
24
|
+
* Optional function to call when the rate-limited function is executed
|
|
25
|
+
*/
|
|
26
|
+
onSuccess?: (result: ReturnType<TFn>, rateLimiter: AsyncRateLimiter<TFn>) => void;
|
|
23
27
|
/**
|
|
24
28
|
* Optional callback function that is called when an execution is rejected due to rate limiting
|
|
25
29
|
*/
|
|
26
|
-
onReject?: (rateLimiter: AsyncRateLimiter<TFn
|
|
30
|
+
onReject?: (rateLimiter: AsyncRateLimiter<TFn>) => void;
|
|
27
31
|
/**
|
|
28
32
|
* Time window in milliseconds within which the limit applies
|
|
29
33
|
*/
|
|
@@ -54,22 +58,25 @@ export interface AsyncRateLimiterOptions<TFn extends AnyAsyncFunction, TArgs ext
|
|
|
54
58
|
* await rateLimiter.maybeExecute('123');
|
|
55
59
|
* ```
|
|
56
60
|
*/
|
|
57
|
-
export declare class AsyncRateLimiter<TFn extends AnyAsyncFunction
|
|
61
|
+
export declare class AsyncRateLimiter<TFn extends AnyAsyncFunction> {
|
|
58
62
|
private fn;
|
|
59
|
-
private _executionCount;
|
|
60
|
-
private _executionTimes;
|
|
61
63
|
private _options;
|
|
64
|
+
private _errorCount;
|
|
65
|
+
private _executionTimes;
|
|
66
|
+
private _lastResult;
|
|
62
67
|
private _rejectionCount;
|
|
63
|
-
|
|
68
|
+
private _settleCount;
|
|
69
|
+
private _successCount;
|
|
70
|
+
constructor(fn: TFn, initialOptions: AsyncRateLimiterOptions<TFn>);
|
|
64
71
|
/**
|
|
65
72
|
* Updates the rate limiter options
|
|
66
73
|
* Returns the new options state
|
|
67
74
|
*/
|
|
68
|
-
setOptions(newOptions: Partial<AsyncRateLimiterOptions<TFn
|
|
75
|
+
setOptions(newOptions: Partial<AsyncRateLimiterOptions<TFn>>): void;
|
|
69
76
|
/**
|
|
70
77
|
* Returns the current rate limiter options
|
|
71
78
|
*/
|
|
72
|
-
getOptions(): Required<AsyncRateLimiterOptions<TFn
|
|
79
|
+
getOptions(): Required<AsyncRateLimiterOptions<TFn>>;
|
|
73
80
|
/**
|
|
74
81
|
* Attempts to execute the rate-limited function if within the configured limits.
|
|
75
82
|
* Will reject execution if the number of calls in the current window exceeds the limit.
|
|
@@ -86,26 +93,34 @@ export declare class AsyncRateLimiter<TFn extends AnyAsyncFunction, TArgs extend
|
|
|
86
93
|
* await rateLimiter.maybeExecute('arg1', 'arg2'); // Rejected
|
|
87
94
|
* ```
|
|
88
95
|
*/
|
|
89
|
-
maybeExecute(...args:
|
|
96
|
+
maybeExecute(...args: Parameters<TFn>): Promise<ReturnType<TFn> | undefined>;
|
|
90
97
|
private executeFunction;
|
|
91
98
|
private rejectFunction;
|
|
92
99
|
private cleanupOldExecutions;
|
|
100
|
+
/**
|
|
101
|
+
* Returns the number of remaining executions allowed in the current window
|
|
102
|
+
*/
|
|
103
|
+
getRemainingInWindow(): number;
|
|
104
|
+
/**
|
|
105
|
+
* Returns the number of milliseconds until the next execution will be possible
|
|
106
|
+
*/
|
|
107
|
+
getMsUntilNextWindow(): number;
|
|
93
108
|
/**
|
|
94
109
|
* Returns the number of times the function has been executed
|
|
95
110
|
*/
|
|
96
|
-
|
|
111
|
+
getSuccessCount(): number;
|
|
97
112
|
/**
|
|
98
|
-
* Returns the number of times the function has been
|
|
113
|
+
* Returns the number of times the function has been settled
|
|
99
114
|
*/
|
|
100
|
-
|
|
115
|
+
getSettleCount(): number;
|
|
101
116
|
/**
|
|
102
|
-
* Returns the number of
|
|
117
|
+
* Returns the number of times the function has errored
|
|
103
118
|
*/
|
|
104
|
-
|
|
119
|
+
getErrorCount(): number;
|
|
105
120
|
/**
|
|
106
|
-
* Returns the number of
|
|
121
|
+
* Returns the number of times the function has been rejected
|
|
107
122
|
*/
|
|
108
|
-
|
|
123
|
+
getRejectionCount(): number;
|
|
109
124
|
/**
|
|
110
125
|
* Resets the rate limiter state
|
|
111
126
|
*/
|
|
@@ -141,4 +156,4 @@ export declare class AsyncRateLimiter<TFn extends AnyAsyncFunction, TArgs extend
|
|
|
141
156
|
* const throttled = throttle(makeApiCall, { wait: 12000 }); // One call every 12 seconds
|
|
142
157
|
* ```
|
|
143
158
|
*/
|
|
144
|
-
export declare function asyncRateLimit<TFn extends AnyAsyncFunction
|
|
159
|
+
export declare function asyncRateLimit<TFn extends AnyAsyncFunction>(fn: TFn, initialOptions: Omit<AsyncRateLimiterOptions<TFn>, 'enabled'>): (...args: Parameters<TFn>) => Promise<ReturnType<TFn> | undefined>;
|
|
@@ -1,18 +1,22 @@
|
|
|
1
1
|
const defaultOptions = {
|
|
2
2
|
enabled: true,
|
|
3
|
+
onError: () => {
|
|
4
|
+
},
|
|
3
5
|
onReject: () => {
|
|
4
6
|
},
|
|
5
|
-
|
|
7
|
+
onSettled: () => {
|
|
6
8
|
},
|
|
7
|
-
|
|
9
|
+
onSuccess: () => {
|
|
8
10
|
}
|
|
9
11
|
};
|
|
10
12
|
class AsyncRateLimiter {
|
|
11
13
|
constructor(fn, initialOptions) {
|
|
12
14
|
this.fn = fn;
|
|
13
|
-
this.
|
|
15
|
+
this._errorCount = 0;
|
|
14
16
|
this._executionTimes = [];
|
|
15
17
|
this._rejectionCount = 0;
|
|
18
|
+
this._settleCount = 0;
|
|
19
|
+
this._successCount = 0;
|
|
16
20
|
this._options = {
|
|
17
21
|
...defaultOptions,
|
|
18
22
|
...initialOptions
|
|
@@ -23,11 +27,7 @@ class AsyncRateLimiter {
|
|
|
23
27
|
* Returns the new options state
|
|
24
28
|
*/
|
|
25
29
|
setOptions(newOptions) {
|
|
26
|
-
this._options = {
|
|
27
|
-
...this._options,
|
|
28
|
-
...newOptions
|
|
29
|
-
};
|
|
30
|
-
return this._options;
|
|
30
|
+
this._options = { ...this._options, ...newOptions };
|
|
31
31
|
}
|
|
32
32
|
/**
|
|
33
33
|
* Returns the current rate limiter options
|
|
@@ -55,31 +55,28 @@ class AsyncRateLimiter {
|
|
|
55
55
|
this.cleanupOldExecutions();
|
|
56
56
|
if (this._executionTimes.length < this._options.limit) {
|
|
57
57
|
await this.executeFunction(...args);
|
|
58
|
-
return
|
|
58
|
+
return this._lastResult;
|
|
59
59
|
}
|
|
60
60
|
this.rejectFunction();
|
|
61
|
-
return
|
|
61
|
+
return void 0;
|
|
62
62
|
}
|
|
63
63
|
async executeFunction(...args) {
|
|
64
|
-
var _a, _b;
|
|
64
|
+
var _a, _b, _c, _d, _e, _f;
|
|
65
65
|
if (!this._options.enabled) return;
|
|
66
66
|
const now = Date.now();
|
|
67
|
-
this._executionCount++;
|
|
68
67
|
this._executionTimes.push(now);
|
|
69
68
|
try {
|
|
70
|
-
await this.fn(...args);
|
|
69
|
+
this._lastResult = await this.fn(...args);
|
|
70
|
+
this._successCount++;
|
|
71
|
+
(_b = (_a = this._options).onSuccess) == null ? void 0 : _b.call(_a, this._lastResult, this);
|
|
71
72
|
} catch (error) {
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
this._options.onError(error);
|
|
75
|
-
} catch (error2) {
|
|
76
|
-
console.error("Error in onError handler", error2);
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
throw error;
|
|
73
|
+
this._errorCount++;
|
|
74
|
+
(_d = (_c = this._options).onError) == null ? void 0 : _d.call(_c, error, this);
|
|
80
75
|
} finally {
|
|
81
|
-
|
|
76
|
+
this._settleCount++;
|
|
77
|
+
(_f = (_e = this._options).onSettled) == null ? void 0 : _f.call(_e, this);
|
|
82
78
|
}
|
|
79
|
+
return this._lastResult;
|
|
83
80
|
}
|
|
84
81
|
rejectFunction() {
|
|
85
82
|
this._rejectionCount++;
|
|
@@ -94,18 +91,6 @@ class AsyncRateLimiter {
|
|
|
94
91
|
(time) => time > windowStart
|
|
95
92
|
);
|
|
96
93
|
}
|
|
97
|
-
/**
|
|
98
|
-
* Returns the number of times the function has been executed
|
|
99
|
-
*/
|
|
100
|
-
getExecutionCount() {
|
|
101
|
-
return this._executionCount;
|
|
102
|
-
}
|
|
103
|
-
/**
|
|
104
|
-
* Returns the number of times the function has been rejected
|
|
105
|
-
*/
|
|
106
|
-
getRejectionCount() {
|
|
107
|
-
return this._rejectionCount;
|
|
108
|
-
}
|
|
109
94
|
/**
|
|
110
95
|
* Returns the number of remaining executions allowed in the current window
|
|
111
96
|
*/
|
|
@@ -119,13 +104,39 @@ class AsyncRateLimiter {
|
|
|
119
104
|
getMsUntilNextWindow() {
|
|
120
105
|
return this.getRemainingInWindow() * this._options.window;
|
|
121
106
|
}
|
|
107
|
+
/**
|
|
108
|
+
* Returns the number of times the function has been executed
|
|
109
|
+
*/
|
|
110
|
+
getSuccessCount() {
|
|
111
|
+
return this._successCount;
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Returns the number of times the function has been settled
|
|
115
|
+
*/
|
|
116
|
+
getSettleCount() {
|
|
117
|
+
return this._settleCount;
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Returns the number of times the function has errored
|
|
121
|
+
*/
|
|
122
|
+
getErrorCount() {
|
|
123
|
+
return this._errorCount;
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Returns the number of times the function has been rejected
|
|
127
|
+
*/
|
|
128
|
+
getRejectionCount() {
|
|
129
|
+
return this._rejectionCount;
|
|
130
|
+
}
|
|
122
131
|
/**
|
|
123
132
|
* Resets the rate limiter state
|
|
124
133
|
*/
|
|
125
134
|
reset() {
|
|
126
135
|
this._executionTimes = [];
|
|
127
|
-
this.
|
|
136
|
+
this._successCount = 0;
|
|
137
|
+
this._errorCount = 0;
|
|
128
138
|
this._rejectionCount = 0;
|
|
139
|
+
this._settleCount = 0;
|
|
129
140
|
}
|
|
130
141
|
}
|
|
131
142
|
function asyncRateLimit(fn, initialOptions) {
|