@tanstack/pacer 0.6.0 → 0.8.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 +10 -6
- package/dist/cjs/async-debouncer.cjs.map +1 -1
- package/dist/cjs/async-debouncer.d.cts +2 -2
- package/dist/cjs/async-queuer.cjs +140 -111
- package/dist/cjs/async-queuer.cjs.map +1 -1
- package/dist/cjs/async-queuer.d.cts +121 -93
- package/dist/cjs/async-rate-limiter.cjs +3 -4
- package/dist/cjs/async-rate-limiter.cjs.map +1 -1
- package/dist/cjs/async-rate-limiter.d.cts +1 -2
- package/dist/cjs/async-throttler.cjs +14 -4
- package/dist/cjs/async-throttler.cjs.map +1 -1
- package/dist/cjs/async-throttler.d.cts +3 -2
- package/dist/cjs/batcher.cjs +138 -0
- package/dist/cjs/batcher.cjs.map +1 -0
- package/dist/cjs/batcher.d.cts +149 -0
- package/dist/cjs/debouncer.cjs +3 -4
- package/dist/cjs/debouncer.cjs.map +1 -1
- package/dist/cjs/debouncer.d.cts +1 -2
- package/dist/cjs/index.cjs +3 -0
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.cts +1 -0
- package/dist/cjs/queuer.cjs +70 -43
- package/dist/cjs/queuer.cjs.map +1 -1
- package/dist/cjs/queuer.d.cts +126 -95
- package/dist/cjs/rate-limiter.cjs +3 -4
- package/dist/cjs/rate-limiter.cjs.map +1 -1
- package/dist/cjs/rate-limiter.d.cts +1 -2
- package/dist/cjs/throttler.cjs +3 -4
- package/dist/cjs/throttler.cjs.map +1 -1
- package/dist/cjs/throttler.d.cts +1 -2
- package/dist/esm/async-debouncer.d.ts +2 -2
- package/dist/esm/async-debouncer.js +10 -6
- package/dist/esm/async-debouncer.js.map +1 -1
- package/dist/esm/async-queuer.d.ts +121 -93
- package/dist/esm/async-queuer.js +140 -111
- package/dist/esm/async-queuer.js.map +1 -1
- package/dist/esm/async-rate-limiter.d.ts +1 -2
- package/dist/esm/async-rate-limiter.js +3 -4
- package/dist/esm/async-rate-limiter.js.map +1 -1
- package/dist/esm/async-throttler.d.ts +3 -2
- package/dist/esm/async-throttler.js +14 -4
- package/dist/esm/async-throttler.js.map +1 -1
- package/dist/esm/batcher.d.ts +149 -0
- package/dist/esm/batcher.js +138 -0
- package/dist/esm/batcher.js.map +1 -0
- package/dist/esm/debouncer.d.ts +1 -2
- package/dist/esm/debouncer.js +3 -4
- package/dist/esm/debouncer.js.map +1 -1
- package/dist/esm/index.d.ts +1 -0
- package/dist/esm/index.js +3 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/queuer.d.ts +126 -95
- package/dist/esm/queuer.js +70 -43
- package/dist/esm/queuer.js.map +1 -1
- package/dist/esm/rate-limiter.d.ts +1 -2
- package/dist/esm/rate-limiter.js +3 -4
- package/dist/esm/rate-limiter.js.map +1 -1
- package/dist/esm/throttler.d.ts +1 -2
- package/dist/esm/throttler.js +3 -4
- package/dist/esm/throttler.js.map +1 -1
- package/package.json +11 -1
- package/src/async-debouncer.ts +12 -6
- package/src/async-queuer.ts +217 -194
- package/src/async-rate-limiter.ts +3 -4
- package/src/async-throttler.ts +18 -4
- package/src/batcher.ts +253 -0
- package/src/debouncer.ts +3 -4
- package/src/index.ts +1 -0
- package/src/queuer.ts +145 -101
- package/src/rate-limiter.ts +3 -4
- package/src/throttler.ts +3 -4
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"debouncer.js","sources":["../../src/debouncer.ts"],"sourcesContent":["import { parseFunctionOrValue } from './utils'\nimport type { AnyFunction } from './types'\n\n/**\n * Options for configuring a debounced function\n */\nexport interface DebouncerOptions<TFn extends AnyFunction> {\n /**\n * Whether the debouncer is enabled. When disabled, maybeExecute will not trigger any executions.\n * Can be a boolean or a function that returns a boolean.\n * Defaults to true.\n */\n enabled?: boolean | ((debouncer: Debouncer<TFn>) => boolean)\n /**\n * Whether to execute on the leading edge of the timeout.\n * The first call will execute immediately and the rest will wait the delay.\n * Defaults to false.\n */\n leading?: boolean\n /**\n * Callback function that is called after the function is executed\n */\n onExecute?: (debouncer: Debouncer<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 before executing the function.\n * Can be a number or a function that returns a number.\n * Defaults to 0ms\n */\n wait: number | ((debouncer: Debouncer<TFn>) => number)\n}\n\nconst defaultOptions: Required<DebouncerOptions<any>> = {\n enabled: true,\n leading: false,\n onExecute: () => {},\n trailing: true,\n wait: 0,\n}\n\n/**\n * A class that creates a debounced function.\n *\n * Debouncing ensures that a function is only executed after a certain amount of time has passed\n * since its last invocation. This is useful for handling frequent events like window resizing,\n * scroll events, or input changes where you want to limit the rate of execution.\n *\n * The debounced function can be configured to execute either at the start of the delay period\n * (leading edge) or at the end (trailing edge, default). Each new call during the wait period\n * will reset the timer.\n *\n * @example\n * ```ts\n * const debouncer = new Debouncer((value: string) => {\n * saveToDatabase(value);\n * }, { wait: 500 });\n *\n * // Will only save after 500ms of no new input\n * inputElement.addEventListener('input', () => {\n * debouncer.maybeExecute(inputElement.value);\n * });\n * ```\n */\nexport class Debouncer<TFn extends AnyFunction> {\n private _canLeadingExecute = true\n private _executionCount = 0\n private _isPending = false\n private _options: Required<DebouncerOptions<TFn>>\n private _timeoutId: NodeJS.Timeout | undefined\n\n constructor(\n private fn: TFn,\n initialOptions: DebouncerOptions<TFn>,\n ) {\n this._options = {\n ...defaultOptions,\n ...initialOptions,\n }\n }\n\n /**\n * Updates the debouncer options\n
|
|
1
|
+
{"version":3,"file":"debouncer.js","sources":["../../src/debouncer.ts"],"sourcesContent":["import { parseFunctionOrValue } from './utils'\nimport type { AnyFunction } from './types'\n\n/**\n * Options for configuring a debounced function\n */\nexport interface DebouncerOptions<TFn extends AnyFunction> {\n /**\n * Whether the debouncer is enabled. When disabled, maybeExecute will not trigger any executions.\n * Can be a boolean or a function that returns a boolean.\n * Defaults to true.\n */\n enabled?: boolean | ((debouncer: Debouncer<TFn>) => boolean)\n /**\n * Whether to execute on the leading edge of the timeout.\n * The first call will execute immediately and the rest will wait the delay.\n * Defaults to false.\n */\n leading?: boolean\n /**\n * Callback function that is called after the function is executed\n */\n onExecute?: (debouncer: Debouncer<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 before executing the function.\n * Can be a number or a function that returns a number.\n * Defaults to 0ms\n */\n wait: number | ((debouncer: Debouncer<TFn>) => number)\n}\n\nconst defaultOptions: Required<DebouncerOptions<any>> = {\n enabled: true,\n leading: false,\n onExecute: () => {},\n trailing: true,\n wait: 0,\n}\n\n/**\n * A class that creates a debounced function.\n *\n * Debouncing ensures that a function is only executed after a certain amount of time has passed\n * since its last invocation. This is useful for handling frequent events like window resizing,\n * scroll events, or input changes where you want to limit the rate of execution.\n *\n * The debounced function can be configured to execute either at the start of the delay period\n * (leading edge) or at the end (trailing edge, default). Each new call during the wait period\n * will reset the timer.\n *\n * @example\n * ```ts\n * const debouncer = new Debouncer((value: string) => {\n * saveToDatabase(value);\n * }, { wait: 500 });\n *\n * // Will only save after 500ms of no new input\n * inputElement.addEventListener('input', () => {\n * debouncer.maybeExecute(inputElement.value);\n * });\n * ```\n */\nexport class Debouncer<TFn extends AnyFunction> {\n private _canLeadingExecute = true\n private _executionCount = 0\n private _isPending = false\n private _options: Required<DebouncerOptions<TFn>>\n private _timeoutId: NodeJS.Timeout | undefined\n\n constructor(\n private fn: TFn,\n initialOptions: DebouncerOptions<TFn>,\n ) {\n this._options = {\n ...defaultOptions,\n ...initialOptions,\n }\n }\n\n /**\n * Updates the debouncer options\n */\n setOptions(newOptions: Partial<DebouncerOptions<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<DebouncerOptions<TFn>> {\n return this._options\n }\n\n /**\n * Returns the current enabled state of the debouncer\n */\n getEnabled(): boolean {\n return parseFunctionOrValue(this._options.enabled, this)\n }\n\n /**\n * Returns the current wait time in milliseconds\n */\n getWait(): number {\n return parseFunctionOrValue(this._options.wait, this)\n }\n\n /**\n * Attempts to execute the debounced function\n * If a call is already in progress, it will be queued\n */\n maybeExecute(...args: Parameters<TFn>): void {\n let _didLeadingExecute = false\n\n // Handle leading execution\n if (this._options.leading && this._canLeadingExecute) {\n this._canLeadingExecute = false\n _didLeadingExecute = true\n this.execute(...args)\n }\n\n // Start pending state to indicate that the debouncer is waiting for the trailing edge\n if (this._options.trailing) {\n this._isPending = true\n }\n\n // Clear any existing timeout\n if (this._timeoutId) clearTimeout(this._timeoutId)\n\n // Set new timeout that will reset canLeadingExecute and execute trailing only if enabled and did not execute leading\n this._timeoutId = setTimeout(() => {\n this._canLeadingExecute = true\n if (this._options.trailing && !_didLeadingExecute) {\n this.execute(...args)\n }\n }, this.getWait())\n }\n\n private execute(...args: Parameters<TFn>): void {\n if (!this.getEnabled()) return undefined\n this.fn(...args) // EXECUTE!\n this._isPending = false\n this._executionCount++\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._canLeadingExecute = true\n this._isPending = false\n }\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 debouncing\n */\n getIsPending(): boolean {\n return this.getEnabled() && this._isPending\n }\n}\n\n/**\n * Creates a debounced function that delays invoking the provided function until after a specified wait time.\n * Multiple calls during the wait period will cancel previous pending invocations and reset the timer.\n *\n * This the the simple function wrapper implementation pulled from the Debouncer class. If you need\n * more control over the debouncing behavior, use the Debouncer class directly.\n *\n * If leading option is true, the function will execute immediately on the first call, then wait the delay\n * before allowing another execution.\n *\n * @example\n * ```ts\n * const debounced = debounce(() => {\n * saveChanges();\n * }, { wait: 1000 });\n *\n * // Called repeatedly but executes at most once per second\n * inputElement.addEventListener('input', debounced);\n * ```\n */\nexport function debounce<TFn extends AnyFunction>(\n fn: TFn,\n initialOptions: DebouncerOptions<TFn>,\n): (...args: Parameters<TFn>) => void {\n const debouncer = new Debouncer(fn, initialOptions)\n return debouncer.maybeExecute.bind(debouncer)\n}\n"],"names":[],"mappings":";AAoCA,MAAM,iBAAkD;AAAA,EACtD,SAAS;AAAA,EACT,SAAS;AAAA,EACT,WAAW,MAAM;AAAA,EAAC;AAAA,EAClB,UAAU;AAAA,EACV,MAAM;AACR;AAyBO,MAAM,UAAmC;AAAA,EAO9C,YACU,IACR,gBACA;AAFQ,SAAA,KAAA;AAPV,SAAQ,qBAAqB;AAC7B,SAAQ,kBAAkB;AAC1B,SAAQ,aAAa;AAQnB,SAAK,WAAW;AAAA,MACd,GAAG;AAAA,MACH,GAAG;AAAA,IACL;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA,EAMF,WAAW,YAAkD;AAC3D,SAAK,WAAW,EAAE,GAAG,KAAK,UAAU,GAAG,WAAW;AAG9C,QAAA,CAAC,KAAK,SAAS,SAAS;AAC1B,WAAK,aAAa;AAAA,IAAA;AAAA,EACpB;AAAA;AAAA;AAAA;AAAA,EAMF,aAA8C;AAC5C,WAAO,KAAK;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA,EAMd,aAAsB;AACpB,WAAO,qBAAqB,KAAK,SAAS,SAAS,IAAI;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA,EAMzD,UAAkB;AAChB,WAAO,qBAAqB,KAAK,SAAS,MAAM,IAAI;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOtD,gBAAgB,MAA6B;AAC3C,QAAI,qBAAqB;AAGzB,QAAI,KAAK,SAAS,WAAW,KAAK,oBAAoB;AACpD,WAAK,qBAAqB;AACL,2BAAA;AAChB,WAAA,QAAQ,GAAG,IAAI;AAAA,IAAA;AAIlB,QAAA,KAAK,SAAS,UAAU;AAC1B,WAAK,aAAa;AAAA,IAAA;AAIpB,QAAI,KAAK,WAAyB,cAAA,KAAK,UAAU;AAG5C,SAAA,aAAa,WAAW,MAAM;AACjC,WAAK,qBAAqB;AAC1B,UAAI,KAAK,SAAS,YAAY,CAAC,oBAAoB;AAC5C,aAAA,QAAQ,GAAG,IAAI;AAAA,MAAA;AAAA,IACtB,GACC,KAAK,SAAS;AAAA,EAAA;AAAA,EAGX,WAAW,MAA6B;AAC9C,QAAI,CAAC,KAAK,WAAW,EAAU,QAAA;AAC1B,SAAA,GAAG,GAAG,IAAI;AACf,SAAK,aAAa;AACb,SAAA;AACA,SAAA,SAAS,UAAU,IAAI;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA,EAM9B,SAAe;AACb,QAAI,KAAK,YAAY;AACnB,mBAAa,KAAK,UAAU;AAC5B,WAAK,qBAAqB;AAC1B,WAAK,aAAa;AAAA,IAAA;AAAA,EACpB;AAAA;AAAA;AAAA;AAAA,EAMF,oBAA4B;AAC1B,WAAO,KAAK;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA,EAMd,eAAwB;AACf,WAAA,KAAK,gBAAgB,KAAK;AAAA,EAAA;AAErC;AAsBgB,SAAA,SACd,IACA,gBACoC;AACpC,QAAM,YAAY,IAAI,UAAU,IAAI,cAAc;AAC3C,SAAA,UAAU,aAAa,KAAK,SAAS;AAC9C;"}
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -2,6 +2,7 @@ export * from './async-debouncer.js';
|
|
|
2
2
|
export * from './async-queuer.js';
|
|
3
3
|
export * from './async-rate-limiter.js';
|
|
4
4
|
export * from './async-throttler.js';
|
|
5
|
+
export * from './batcher.js';
|
|
5
6
|
export * from './compare.js';
|
|
6
7
|
export * from './debouncer.js';
|
|
7
8
|
export * from './queuer.js';
|
package/dist/esm/index.js
CHANGED
|
@@ -2,6 +2,7 @@ import { AsyncDebouncer, asyncDebounce } from "./async-debouncer.js";
|
|
|
2
2
|
import { AsyncQueuer, asyncQueue } from "./async-queuer.js";
|
|
3
3
|
import { AsyncRateLimiter, asyncRateLimit } from "./async-rate-limiter.js";
|
|
4
4
|
import { AsyncThrottler, asyncThrottle } from "./async-throttler.js";
|
|
5
|
+
import { Batcher, batch } from "./batcher.js";
|
|
5
6
|
import { isPlainArray, isPlainObject, replaceEqualDeep, shallowEqualObjects } from "./compare.js";
|
|
6
7
|
import { Debouncer, debounce } from "./debouncer.js";
|
|
7
8
|
import { Queuer, queue } from "./queuer.js";
|
|
@@ -13,6 +14,7 @@ export {
|
|
|
13
14
|
AsyncQueuer,
|
|
14
15
|
AsyncRateLimiter,
|
|
15
16
|
AsyncThrottler,
|
|
17
|
+
Batcher,
|
|
16
18
|
Debouncer,
|
|
17
19
|
Queuer,
|
|
18
20
|
RateLimiter,
|
|
@@ -21,6 +23,7 @@ export {
|
|
|
21
23
|
asyncQueue,
|
|
22
24
|
asyncRateLimit,
|
|
23
25
|
asyncThrottle,
|
|
26
|
+
batch,
|
|
24
27
|
bindInstanceMethods,
|
|
25
28
|
debounce,
|
|
26
29
|
isFunction,
|
package/dist/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;"}
|
package/dist/esm/queuer.d.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Options for configuring a Queuer instance
|
|
2
|
+
* Options for configuring a Queuer instance.
|
|
3
|
+
*
|
|
4
|
+
* These options control queue behavior, item expiration, callbacks, and more.
|
|
3
5
|
*/
|
|
4
6
|
export interface QueuerOptions<TValue> {
|
|
5
7
|
/**
|
|
@@ -42,7 +44,7 @@ export interface QueuerOptions<TValue> {
|
|
|
42
44
|
/**
|
|
43
45
|
* Callback fired whenever an item is removed from the queuer
|
|
44
46
|
*/
|
|
45
|
-
|
|
47
|
+
onExecute?: (item: TValue, queuer: Queuer<TValue>) => void;
|
|
46
48
|
/**
|
|
47
49
|
* Callback fired whenever the queuer's running state changes
|
|
48
50
|
*/
|
|
@@ -67,64 +69,75 @@ export interface QueuerOptions<TValue> {
|
|
|
67
69
|
wait?: number | ((queuer: Queuer<TValue>) => number);
|
|
68
70
|
}
|
|
69
71
|
/**
|
|
70
|
-
* Position type for addItem and getNextItem operations
|
|
72
|
+
* Position type for addItem and getNextItem operations.
|
|
73
|
+
*
|
|
74
|
+
* - 'front': Operate on the front of the queue (FIFO)
|
|
75
|
+
* - 'back': Operate on the back of the queue (LIFO)
|
|
71
76
|
*/
|
|
72
77
|
export type QueuePosition = 'front' | 'back';
|
|
73
78
|
/**
|
|
74
|
-
* A flexible queue
|
|
75
|
-
* with optional position overrides for stack-like or double-ended operations.
|
|
79
|
+
* A flexible queue that processes items with configurable wait times, expiration, and priority.
|
|
76
80
|
*
|
|
77
|
-
*
|
|
78
|
-
*
|
|
79
|
-
*
|
|
81
|
+
* Features:
|
|
82
|
+
* - Automatic or manual processing of items
|
|
83
|
+
* - FIFO (First In First Out), LIFO (Last In First Out), or double-ended queue behavior
|
|
84
|
+
* - Priority-based ordering when getPriority is provided
|
|
85
|
+
* - Item expiration and removal of stale items
|
|
86
|
+
* - Callbacks for queue state changes, execution, rejection, and expiration
|
|
80
87
|
*
|
|
81
|
-
*
|
|
82
|
-
*
|
|
88
|
+
* Running behavior:
|
|
89
|
+
* - `start()`: Begins automatically processing items in the queue (defaults to running)
|
|
90
|
+
* - `stop()`: Pauses processing but maintains queue state
|
|
91
|
+
* - `wait`: Configurable delay between processing items
|
|
92
|
+
* - `onItemsChange`/`onExecute`: Callbacks for monitoring queue state
|
|
83
93
|
*
|
|
84
|
-
*
|
|
85
|
-
* -
|
|
86
|
-
* - getNextItem()
|
|
94
|
+
* Manual processing is also supported when automatic processing is disabled:
|
|
95
|
+
* - `execute()`: Processes the next item using the provided function
|
|
96
|
+
* - `getNextItem()`: Removes and returns the next item without processing
|
|
87
97
|
*
|
|
88
|
-
*
|
|
89
|
-
* - addItem(item
|
|
90
|
-
* -
|
|
98
|
+
* Queue behavior defaults to FIFO:
|
|
99
|
+
* - `addItem(item)`: Adds to the back of the queue
|
|
100
|
+
* - Items processed from the front of the queue
|
|
91
101
|
*
|
|
92
|
-
*
|
|
93
|
-
* -
|
|
94
|
-
* - getNextItem(position): removes and returns from specified position
|
|
102
|
+
* Priority queue:
|
|
103
|
+
* - Provide a `getPriority` function; higher values are processed first
|
|
95
104
|
*
|
|
96
|
-
*
|
|
97
|
-
* -
|
|
98
|
-
* -
|
|
99
|
-
* - wait: configurable delay between processing items
|
|
100
|
-
* - onItemsChange/onGetNextItem: callbacks for monitoring queuer state
|
|
105
|
+
* Stack (LIFO):
|
|
106
|
+
* - `addItem(item, 'back')`: Adds to the back
|
|
107
|
+
* - `getNextItem('back')`: Removes from the back
|
|
101
108
|
*
|
|
102
|
-
*
|
|
103
|
-
* -
|
|
104
|
-
* -
|
|
105
|
-
* - onExpire: callback for when an item expires
|
|
109
|
+
* Double-ended queue:
|
|
110
|
+
* - `addItem(item, position)`: Adds to specified position ('front'/'back')
|
|
111
|
+
* - `getNextItem(position)`: Removes from specified position
|
|
106
112
|
*
|
|
107
|
-
*
|
|
108
|
-
*
|
|
109
|
-
*
|
|
110
|
-
*
|
|
111
|
-
* queuer.addItem(1); // [1]
|
|
112
|
-
* queuer.addItem(2); // [1, 2]
|
|
113
|
-
* queuer.getNextItem(); // returns 1, queuer is [2]
|
|
113
|
+
* Item expiration:
|
|
114
|
+
* - `expirationDuration`: Maximum time items can stay in the queue
|
|
115
|
+
* - `getIsExpired`: Function to override default expiration
|
|
116
|
+
* - `onExpire`: Callback for expired items
|
|
114
117
|
*
|
|
115
|
-
*
|
|
116
|
-
*
|
|
117
|
-
*
|
|
118
|
+
* Example usage:
|
|
119
|
+
* ```ts
|
|
120
|
+
* // Auto-processing queue with wait time
|
|
121
|
+
* const autoQueue = new Queuer<number>((n) => console.log(n), {
|
|
118
122
|
* started: true, // Begin processing immediately
|
|
119
123
|
* wait: 1000, // Wait 1s between items
|
|
120
|
-
*
|
|
124
|
+
* onExecute: (item) => console.log(`Processed ${item}`)
|
|
121
125
|
* });
|
|
122
|
-
*
|
|
123
|
-
*
|
|
124
|
-
*
|
|
126
|
+
* autoQueue.addItem(1); // Will process after 1s
|
|
127
|
+
* autoQueue.addItem(2); // Will process 1s after first item
|
|
128
|
+
*
|
|
129
|
+
* // Manual processing queue
|
|
130
|
+
* const manualQueue = new Queuer<number>((n) => console.log(n), {
|
|
131
|
+
* started: false
|
|
132
|
+
* });
|
|
133
|
+
* manualQueue.addItem(1); // [1]
|
|
134
|
+
* manualQueue.addItem(2); // [1, 2]
|
|
135
|
+
* manualQueue.execute(); // logs 1, queue is [2]
|
|
136
|
+
* manualQueue.getNextItem(); // returns 2, queue is empty
|
|
125
137
|
* ```
|
|
126
138
|
*/
|
|
127
139
|
export declare class Queuer<TValue> {
|
|
140
|
+
private fn;
|
|
128
141
|
private _options;
|
|
129
142
|
private _items;
|
|
130
143
|
private _itemTimestamps;
|
|
@@ -134,136 +147,154 @@ export declare class Queuer<TValue> {
|
|
|
134
147
|
private _onItemsChanges;
|
|
135
148
|
private _running;
|
|
136
149
|
private _pendingTick;
|
|
137
|
-
constructor(initialOptions?: QueuerOptions<TValue>);
|
|
150
|
+
constructor(fn: (item: TValue) => void, initialOptions?: QueuerOptions<TValue>);
|
|
138
151
|
/**
|
|
139
|
-
* Updates the queuer options
|
|
140
|
-
* Returns the new options state
|
|
152
|
+
* Updates the queuer options. New options are merged with existing options.
|
|
141
153
|
*/
|
|
142
154
|
setOptions(newOptions: Partial<QueuerOptions<TValue>>): void;
|
|
143
155
|
/**
|
|
144
|
-
* Returns the current queuer options
|
|
156
|
+
* Returns the current queuer options, including defaults and any overrides.
|
|
145
157
|
*/
|
|
146
158
|
getOptions(): Required<QueuerOptions<TValue>>;
|
|
147
159
|
/**
|
|
148
|
-
* Returns the current wait time in milliseconds
|
|
160
|
+
* Returns the current wait time (in milliseconds) between processing items.
|
|
161
|
+
* If a function is provided, it is called with the queuer instance.
|
|
149
162
|
*/
|
|
150
163
|
getWait(): number;
|
|
151
164
|
/**
|
|
152
|
-
* Processes items in the
|
|
165
|
+
* Processes items in the queue up to the wait interval. Internal use only.
|
|
153
166
|
*/
|
|
154
167
|
private tick;
|
|
155
168
|
/**
|
|
156
|
-
* Checks for and removes
|
|
169
|
+
* Checks for expired items in the queue and removes them. Calls onExpire for each expired item.
|
|
170
|
+
* Internal use only.
|
|
157
171
|
*/
|
|
158
172
|
private checkExpiredItems;
|
|
159
173
|
/**
|
|
160
|
-
* Stops the
|
|
174
|
+
* Stops processing items in the queue. Does not clear the queue.
|
|
161
175
|
*/
|
|
162
176
|
stop(): void;
|
|
163
177
|
/**
|
|
164
|
-
* Starts the
|
|
178
|
+
* Starts processing items in the queue. If already running, does nothing.
|
|
165
179
|
*/
|
|
166
180
|
start(): void;
|
|
167
181
|
/**
|
|
168
|
-
* Removes all items from the
|
|
182
|
+
* Removes all pending items from the queue. Does not affect items being processed.
|
|
169
183
|
*/
|
|
170
184
|
clear(): void;
|
|
171
185
|
/**
|
|
172
|
-
* Resets the queuer to its initial state
|
|
186
|
+
* Resets the queuer to its initial state. Optionally repopulates with initial items.
|
|
187
|
+
* Does not affect callbacks or options.
|
|
173
188
|
*/
|
|
174
189
|
reset(withInitialItems?: boolean): void;
|
|
175
190
|
/**
|
|
176
|
-
* Adds an item to the
|
|
177
|
-
*
|
|
191
|
+
* Adds an item to the queue. If the queue is full, the item is rejected and onReject is called.
|
|
192
|
+
* Items can be inserted based on priority or at the front/back depending on configuration.
|
|
193
|
+
*
|
|
194
|
+
* Returns true if the item was added, false if the queue is full.
|
|
195
|
+
*
|
|
196
|
+
* Example usage:
|
|
197
|
+
* ```ts
|
|
198
|
+
* queuer.addItem('task');
|
|
199
|
+
* queuer.addItem('task2', 'front');
|
|
200
|
+
* ```
|
|
178
201
|
*/
|
|
179
202
|
addItem(item: TValue, position?: QueuePosition, runOnUpdate?: boolean): boolean;
|
|
180
203
|
/**
|
|
181
|
-
* Removes and returns
|
|
204
|
+
* Removes and returns the next item from the queue without executing the function.
|
|
205
|
+
* Use for manual queue management. Normally, use execute() to process items.
|
|
182
206
|
*
|
|
183
|
-
*
|
|
207
|
+
* Example usage:
|
|
184
208
|
* ```ts
|
|
185
|
-
* //
|
|
186
|
-
* queuer.getNextItem()
|
|
187
|
-
* //
|
|
188
|
-
* queuer.getNextItem('back')
|
|
209
|
+
* // FIFO
|
|
210
|
+
* queuer.getNextItem();
|
|
211
|
+
* // LIFO
|
|
212
|
+
* queuer.getNextItem('back');
|
|
189
213
|
* ```
|
|
190
214
|
*/
|
|
191
215
|
getNextItem(position?: QueuePosition): TValue | undefined;
|
|
192
216
|
/**
|
|
193
|
-
*
|
|
217
|
+
* Removes and returns the next item from the queue and processes it using the provided function.
|
|
218
|
+
*
|
|
219
|
+
* Example usage:
|
|
220
|
+
* ```ts
|
|
221
|
+
* queuer.execute();
|
|
222
|
+
* // LIFO
|
|
223
|
+
* queuer.execute('back');
|
|
224
|
+
* ```
|
|
225
|
+
*/
|
|
226
|
+
execute(position?: QueuePosition): TValue | undefined;
|
|
227
|
+
/**
|
|
228
|
+
* Returns the next item in the queue without removing it.
|
|
194
229
|
*
|
|
195
|
-
*
|
|
230
|
+
* Example usage:
|
|
196
231
|
* ```ts
|
|
197
|
-
* //
|
|
198
|
-
* queuer.
|
|
199
|
-
* // Look at last item (like stack top)
|
|
200
|
-
* queuer.getPeek('back')
|
|
232
|
+
* queuer.peekNextItem(); // front
|
|
233
|
+
* queuer.peekNextItem('back'); // back
|
|
201
234
|
* ```
|
|
202
235
|
*/
|
|
203
|
-
|
|
236
|
+
peekNextItem(position?: QueuePosition): TValue | undefined;
|
|
204
237
|
/**
|
|
205
|
-
* Returns true if the
|
|
238
|
+
* Returns true if the queue is empty (no pending items).
|
|
206
239
|
*/
|
|
207
240
|
getIsEmpty(): boolean;
|
|
208
241
|
/**
|
|
209
|
-
* Returns true if the
|
|
242
|
+
* Returns true if the queue is full (reached maxSize).
|
|
210
243
|
*/
|
|
211
244
|
getIsFull(): boolean;
|
|
212
245
|
/**
|
|
213
|
-
* Returns the
|
|
246
|
+
* Returns the number of pending items in the queue.
|
|
214
247
|
*/
|
|
215
248
|
getSize(): number;
|
|
216
249
|
/**
|
|
217
|
-
* Returns a copy of all items in the
|
|
250
|
+
* Returns a copy of all items in the queue.
|
|
218
251
|
*/
|
|
219
|
-
|
|
252
|
+
peekAllItems(): Array<TValue>;
|
|
220
253
|
/**
|
|
221
|
-
* Returns the number of items that have been removed from the
|
|
254
|
+
* Returns the number of items that have been processed and removed from the queue.
|
|
222
255
|
*/
|
|
223
256
|
getExecutionCount(): number;
|
|
224
257
|
/**
|
|
225
|
-
* Returns the number of items that have been rejected from the
|
|
258
|
+
* Returns the number of items that have been rejected from being added to the queue.
|
|
226
259
|
*/
|
|
227
260
|
getRejectionCount(): number;
|
|
228
261
|
/**
|
|
229
|
-
* Returns the number of items that have expired from the
|
|
262
|
+
* Returns the number of items that have expired and been removed from the queue.
|
|
230
263
|
*/
|
|
231
264
|
getExpirationCount(): number;
|
|
232
265
|
/**
|
|
233
|
-
* Returns true if the queuer is running
|
|
266
|
+
* Returns true if the queuer is currently running (processing items).
|
|
234
267
|
*/
|
|
235
268
|
getIsRunning(): boolean;
|
|
236
269
|
/**
|
|
237
|
-
* Returns true if the queuer is running but has no items to process
|
|
270
|
+
* Returns true if the queuer is running but has no items to process.
|
|
238
271
|
*/
|
|
239
272
|
getIsIdle(): boolean;
|
|
240
273
|
}
|
|
241
274
|
/**
|
|
242
|
-
* Creates a queue that processes items
|
|
275
|
+
* Creates a queue that processes items immediately upon addition.
|
|
243
276
|
* Items are processed sequentially in FIFO order by default.
|
|
244
277
|
*
|
|
245
278
|
* This is a simplified wrapper around the Queuer class that only exposes the
|
|
246
|
-
* `addItem` method.
|
|
247
|
-
* For more control over
|
|
248
|
-
* directly which provides methods like `start`, `stop`, `reset`, and more.
|
|
279
|
+
* `addItem` method. The queue is always running and will process items as they are added.
|
|
280
|
+
* For more control over queue processing, use the Queuer class directly.
|
|
249
281
|
*
|
|
250
|
-
*
|
|
282
|
+
* Example usage:
|
|
251
283
|
* ```ts
|
|
252
284
|
* // Basic sequential processing
|
|
253
|
-
* const processItems =
|
|
285
|
+
* const processItems = queue<number>((n) => console.log(n), {
|
|
254
286
|
* wait: 1000,
|
|
255
|
-
* onItemsChange: (queuer) => console.log(queuer.
|
|
256
|
-
* })
|
|
257
|
-
* processItems(1) // Logs: 1
|
|
258
|
-
* processItems(2) // Logs: 2 after 1 completes
|
|
287
|
+
* onItemsChange: (queuer) => console.log(queuer.peekAllItems())
|
|
288
|
+
* });
|
|
289
|
+
* processItems(1); // Logs: 1
|
|
290
|
+
* processItems(2); // Logs: 2 after 1 completes
|
|
259
291
|
*
|
|
260
|
-
* // Priority
|
|
261
|
-
* const processPriority =
|
|
262
|
-
* process: async (n) => console.log(n),
|
|
292
|
+
* // Priority queue
|
|
293
|
+
* const processPriority = queue<number>((n) => console.log(n), {
|
|
263
294
|
* getPriority: n => n // Higher numbers processed first
|
|
264
|
-
* })
|
|
265
|
-
* processPriority(1)
|
|
266
|
-
* processPriority(3) // Processed before 1
|
|
295
|
+
* });
|
|
296
|
+
* processPriority(1);
|
|
297
|
+
* processPriority(3); // Processed before 1
|
|
267
298
|
* ```
|
|
268
299
|
*/
|
|
269
|
-
export declare function queue<TValue>(options: QueuerOptions<TValue>): (item: TValue, position?: QueuePosition, runOnUpdate?: boolean) => boolean;
|
|
300
|
+
export declare function queue<TValue>(fn: (item: TValue) => void, options: QueuerOptions<TValue>): (item: TValue, position?: QueuePosition, runOnUpdate?: boolean) => boolean;
|