@tanstack/pacer 0.16.3 → 0.17.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.
Files changed (169) hide show
  1. package/README.md +165 -0
  2. package/dist/async-batcher.cjs +355 -0
  3. package/dist/async-batcher.cjs.map +1 -0
  4. package/dist/async-batcher.d.cts +344 -0
  5. package/dist/async-batcher.d.ts +344 -0
  6. package/dist/async-batcher.js +353 -0
  7. package/dist/async-batcher.js.map +1 -0
  8. package/dist/async-debouncer.cjs +340 -0
  9. package/dist/async-debouncer.cjs.map +1 -0
  10. package/dist/async-debouncer.d.cts +300 -0
  11. package/dist/async-debouncer.d.ts +300 -0
  12. package/dist/async-debouncer.js +338 -0
  13. package/dist/async-debouncer.js.map +1 -0
  14. package/dist/async-queuer.cjs +496 -0
  15. package/dist/async-queuer.cjs.map +1 -0
  16. package/dist/async-queuer.d.cts +440 -0
  17. package/dist/async-queuer.d.ts +440 -0
  18. package/dist/async-queuer.js +494 -0
  19. package/dist/async-queuer.js.map +1 -0
  20. package/dist/async-rate-limiter.cjs +381 -0
  21. package/dist/async-rate-limiter.cjs.map +1 -0
  22. package/dist/{cjs/async-rate-limiter.d.cts → async-rate-limiter.d.cts} +190 -185
  23. package/dist/{esm/async-rate-limiter.d.ts → async-rate-limiter.d.ts} +190 -185
  24. package/dist/async-rate-limiter.js +379 -0
  25. package/dist/async-rate-limiter.js.map +1 -0
  26. package/dist/async-retryer.cjs +378 -0
  27. package/dist/async-retryer.cjs.map +1 -0
  28. package/dist/async-retryer.d.cts +312 -0
  29. package/dist/async-retryer.d.ts +312 -0
  30. package/dist/async-retryer.js +376 -0
  31. package/dist/async-retryer.js.map +1 -0
  32. package/dist/async-throttler.cjs +362 -0
  33. package/dist/async-throttler.cjs.map +1 -0
  34. package/dist/async-throttler.d.cts +320 -0
  35. package/dist/async-throttler.d.ts +320 -0
  36. package/dist/async-throttler.js +360 -0
  37. package/dist/async-throttler.js.map +1 -0
  38. package/dist/batcher.cjs +194 -0
  39. package/dist/batcher.cjs.map +1 -0
  40. package/dist/batcher.d.cts +180 -0
  41. package/dist/batcher.d.ts +180 -0
  42. package/dist/batcher.js +193 -0
  43. package/dist/batcher.js.map +1 -0
  44. package/dist/debouncer.cjs +197 -0
  45. package/dist/debouncer.cjs.map +1 -0
  46. package/dist/debouncer.d.cts +167 -0
  47. package/dist/debouncer.d.ts +167 -0
  48. package/dist/debouncer.js +195 -0
  49. package/dist/debouncer.js.map +1 -0
  50. package/dist/event-client.cjs +20 -0
  51. package/dist/event-client.cjs.map +1 -0
  52. package/dist/event-client.d.cts +49 -0
  53. package/dist/event-client.d.ts +49 -0
  54. package/dist/event-client.js +19 -0
  55. package/dist/event-client.js.map +1 -0
  56. package/dist/index.cjs +49 -0
  57. package/dist/index.d.cts +15 -0
  58. package/dist/index.d.ts +15 -0
  59. package/dist/{esm/index.js → index.js} +5 -41
  60. package/dist/queuer.cjs +393 -0
  61. package/dist/queuer.cjs.map +1 -0
  62. package/dist/queuer.d.cts +345 -0
  63. package/dist/queuer.d.ts +345 -0
  64. package/dist/queuer.js +391 -0
  65. package/dist/queuer.js.map +1 -0
  66. package/dist/rate-limiter.cjs +251 -0
  67. package/dist/rate-limiter.cjs.map +1 -0
  68. package/dist/{cjs/rate-limiter.d.cts → rate-limiter.d.cts} +113 -108
  69. package/dist/{esm/rate-limiter.d.ts → rate-limiter.d.ts} +113 -108
  70. package/dist/rate-limiter.js +249 -0
  71. package/dist/rate-limiter.js.map +1 -0
  72. package/dist/throttler.cjs +209 -0
  73. package/dist/throttler.cjs.map +1 -0
  74. package/dist/throttler.d.cts +207 -0
  75. package/dist/throttler.d.ts +207 -0
  76. package/dist/throttler.js +207 -0
  77. package/dist/throttler.js.map +1 -0
  78. package/dist/types.cjs +0 -0
  79. package/dist/types.d.cts +13 -0
  80. package/dist/types.d.ts +13 -0
  81. package/dist/types.js +1 -0
  82. package/dist/utils.cjs +13 -0
  83. package/dist/utils.cjs.map +1 -0
  84. package/dist/utils.d.cts +8 -0
  85. package/dist/utils.d.ts +8 -0
  86. package/dist/utils.js +11 -0
  87. package/dist/utils.js.map +1 -0
  88. package/package.json +39 -123
  89. package/dist/cjs/async-batcher.cjs +0 -222
  90. package/dist/cjs/async-batcher.cjs.map +0 -1
  91. package/dist/cjs/async-batcher.d.cts +0 -340
  92. package/dist/cjs/async-debouncer.cjs +0 -233
  93. package/dist/cjs/async-debouncer.cjs.map +0 -1
  94. package/dist/cjs/async-debouncer.d.cts +0 -295
  95. package/dist/cjs/async-queuer.cjs +0 -399
  96. package/dist/cjs/async-queuer.cjs.map +0 -1
  97. package/dist/cjs/async-queuer.d.cts +0 -435
  98. package/dist/cjs/async-rate-limiter.cjs +0 -254
  99. package/dist/cjs/async-rate-limiter.cjs.map +0 -1
  100. package/dist/cjs/async-retryer.cjs +0 -287
  101. package/dist/cjs/async-retryer.cjs.map +0 -1
  102. package/dist/cjs/async-retryer.d.cts +0 -308
  103. package/dist/cjs/async-throttler.cjs +0 -263
  104. package/dist/cjs/async-throttler.cjs.map +0 -1
  105. package/dist/cjs/async-throttler.d.cts +0 -315
  106. package/dist/cjs/batcher.cjs +0 -132
  107. package/dist/cjs/batcher.cjs.map +0 -1
  108. package/dist/cjs/batcher.d.cts +0 -176
  109. package/dist/cjs/debouncer.cjs +0 -139
  110. package/dist/cjs/debouncer.cjs.map +0 -1
  111. package/dist/cjs/debouncer.d.cts +0 -162
  112. package/dist/cjs/event-client.cjs +0 -20
  113. package/dist/cjs/event-client.cjs.map +0 -1
  114. package/dist/cjs/event-client.d.cts +0 -45
  115. package/dist/cjs/index.cjs +0 -51
  116. package/dist/cjs/index.cjs.map +0 -1
  117. package/dist/cjs/index.d.cts +0 -15
  118. package/dist/cjs/queuer.cjs +0 -308
  119. package/dist/cjs/queuer.cjs.map +0 -1
  120. package/dist/cjs/queuer.d.cts +0 -340
  121. package/dist/cjs/rate-limiter.cjs +0 -181
  122. package/dist/cjs/rate-limiter.cjs.map +0 -1
  123. package/dist/cjs/throttler.cjs +0 -153
  124. package/dist/cjs/throttler.cjs.map +0 -1
  125. package/dist/cjs/throttler.d.cts +0 -202
  126. package/dist/cjs/types.d.cts +0 -9
  127. package/dist/cjs/utils.cjs +0 -11
  128. package/dist/cjs/utils.cjs.map +0 -1
  129. package/dist/cjs/utils.d.cts +0 -3
  130. package/dist/esm/async-batcher.d.ts +0 -340
  131. package/dist/esm/async-batcher.js +0 -222
  132. package/dist/esm/async-batcher.js.map +0 -1
  133. package/dist/esm/async-debouncer.d.ts +0 -295
  134. package/dist/esm/async-debouncer.js +0 -233
  135. package/dist/esm/async-debouncer.js.map +0 -1
  136. package/dist/esm/async-queuer.d.ts +0 -435
  137. package/dist/esm/async-queuer.js +0 -399
  138. package/dist/esm/async-queuer.js.map +0 -1
  139. package/dist/esm/async-rate-limiter.js +0 -254
  140. package/dist/esm/async-rate-limiter.js.map +0 -1
  141. package/dist/esm/async-retryer.d.ts +0 -308
  142. package/dist/esm/async-retryer.js +0 -287
  143. package/dist/esm/async-retryer.js.map +0 -1
  144. package/dist/esm/async-throttler.d.ts +0 -315
  145. package/dist/esm/async-throttler.js +0 -263
  146. package/dist/esm/async-throttler.js.map +0 -1
  147. package/dist/esm/batcher.d.ts +0 -176
  148. package/dist/esm/batcher.js +0 -132
  149. package/dist/esm/batcher.js.map +0 -1
  150. package/dist/esm/debouncer.d.ts +0 -162
  151. package/dist/esm/debouncer.js +0 -139
  152. package/dist/esm/debouncer.js.map +0 -1
  153. package/dist/esm/event-client.d.ts +0 -45
  154. package/dist/esm/event-client.js +0 -20
  155. package/dist/esm/event-client.js.map +0 -1
  156. package/dist/esm/index.d.ts +0 -15
  157. package/dist/esm/index.js.map +0 -1
  158. package/dist/esm/queuer.d.ts +0 -340
  159. package/dist/esm/queuer.js +0 -308
  160. package/dist/esm/queuer.js.map +0 -1
  161. package/dist/esm/rate-limiter.js +0 -181
  162. package/dist/esm/rate-limiter.js.map +0 -1
  163. package/dist/esm/throttler.d.ts +0 -202
  164. package/dist/esm/throttler.js +0 -153
  165. package/dist/esm/throttler.js.map +0 -1
  166. package/dist/esm/types.d.ts +0 -9
  167. package/dist/esm/utils.d.ts +0 -3
  168. package/dist/esm/utils.js +0 -11
  169. package/dist/esm/utils.js.map +0 -1
@@ -0,0 +1,345 @@
1
+ import { Store } from "@tanstack/store";
2
+
3
+ //#region src/queuer.d.ts
4
+ interface QueuerState<TValue> {
5
+ /**
6
+ * Number of times addItem has been called (for reduction calculations)
7
+ */
8
+ addItemCount: number;
9
+ /**
10
+ * Number of items that have been processed by the queuer
11
+ */
12
+ executionCount: number;
13
+ /**
14
+ * Number of items that have been removed from the queue due to expiration
15
+ */
16
+ expirationCount: number;
17
+ /**
18
+ * Whether the queuer has no items to process (items array is empty)
19
+ */
20
+ isEmpty: boolean;
21
+ /**
22
+ * Whether the queuer has reached its maximum capacity
23
+ */
24
+ isFull: boolean;
25
+ /**
26
+ * Whether the queuer is not currently processing any items
27
+ */
28
+ isIdle: boolean;
29
+ /**
30
+ * Whether the queuer is active and will process items automatically
31
+ */
32
+ isRunning: boolean;
33
+ /**
34
+ * Array of items currently waiting to be processed
35
+ */
36
+ items: Array<TValue>;
37
+ /**
38
+ * Timestamps when items were added to the queue for expiration tracking
39
+ */
40
+ itemTimestamps: Array<number>;
41
+ /**
42
+ * Whether the queuer has a pending timeout for processing the next item
43
+ */
44
+ pendingTick: boolean;
45
+ /**
46
+ * Number of items that have been rejected from being added to the queue
47
+ */
48
+ rejectionCount: number;
49
+ /**
50
+ * Number of items currently in the queue
51
+ */
52
+ size: number;
53
+ /**
54
+ * Current processing status - 'idle' when not processing, 'running' when active, 'stopped' when paused
55
+ */
56
+ status: 'idle' | 'running' | 'stopped';
57
+ }
58
+ /**
59
+ * Options for configuring a Queuer instance.
60
+ *
61
+ * These options control queue behavior, item expiration, callbacks, and more.
62
+ */
63
+ interface QueuerOptions<TValue> {
64
+ /**
65
+ * Default position to add items to the queuer
66
+ * @default 'back'
67
+ */
68
+ addItemsTo?: QueuePosition;
69
+ /**
70
+ * Maximum time in milliseconds that an item can stay in the queue
71
+ * If not provided, items will never expire
72
+ */
73
+ expirationDuration?: number;
74
+ /**
75
+ * Function to determine if an item has expired
76
+ * If provided, this overrides the expirationDuration behavior
77
+ */
78
+ getIsExpired?: (item: TValue, addedAt: number) => boolean;
79
+ /**
80
+ * Default position to get items from during processing
81
+ * @default 'front'
82
+ */
83
+ getItemsFrom?: QueuePosition;
84
+ /**
85
+ * Function to determine priority of items in the queuer
86
+ * Higher priority items will be processed first
87
+ */
88
+ getPriority?: (item: TValue) => number;
89
+ /**
90
+ * Initial items to populate the queuer with
91
+ */
92
+ initialItems?: Array<TValue>;
93
+ /**
94
+ * Initial state for the queuer
95
+ */
96
+ initialState?: Partial<QueuerState<TValue>>;
97
+ /**
98
+ * Optional key to identify this queuer instance.
99
+ * If provided, the queuer will be identified by this key in the devtools and PacerProvider if applicable.
100
+ */
101
+ key?: string;
102
+ /**
103
+ * Maximum number of items allowed in the queuer
104
+ */
105
+ maxSize?: number;
106
+ /**
107
+ * Callback fired whenever an item is removed from the queuer
108
+ */
109
+ onExecute?: (item: TValue, queuer: Queuer<TValue>) => void;
110
+ /**
111
+ * Callback fired whenever an item expires in the queuer
112
+ */
113
+ onExpire?: (item: TValue, queuer: Queuer<TValue>) => void;
114
+ /**
115
+ * Callback fired whenever an item is added or removed from the queuer
116
+ */
117
+ onItemsChange?: (queuer: Queuer<TValue>) => void;
118
+ /**
119
+ * Callback fired whenever an item is rejected from being added to the queuer
120
+ */
121
+ onReject?: (item: TValue, queuer: Queuer<TValue>) => void;
122
+ /**
123
+ * Whether the queuer should start processing tasks immediately
124
+ */
125
+ started?: boolean;
126
+ /**
127
+ * Time in milliseconds to wait between processing items.
128
+ * Can be a number or a function that returns a number.
129
+ * @default 0
130
+ */
131
+ wait?: number | ((queuer: Queuer<TValue>) => number);
132
+ }
133
+ /**
134
+ * Utility function for sharing common `QueuerOptions` options between different `Queuer` instances.
135
+ */
136
+ declare function queuerOptions<TValue = any, TOptions extends Partial<QueuerOptions<TValue>> = Partial<QueuerOptions<TValue>>>(options: TOptions): TOptions;
137
+ /**
138
+ * Position type for addItem and getNextItem operations.
139
+ *
140
+ * - 'front': Operate on the front of the queue (FIFO)
141
+ * - 'back': Operate on the back of the queue (LIFO)
142
+ */
143
+ type QueuePosition = 'front' | 'back';
144
+ /**
145
+ * A flexible queue that processes items with configurable wait times, expiration, and priority.
146
+ *
147
+ * This synchronous version is lighter weight and often all you need - upgrade to AsyncQueuer when you need promises, retry support, abort capabilities, concurrent execution, or advanced error handling.
148
+ *
149
+ * Features:
150
+ * - Automatic or manual processing of items
151
+ * - FIFO (First In First Out), LIFO (Last In First Out), or double-ended queue behavior
152
+ * - Priority-based ordering when getPriority is provided
153
+ * - Item expiration and removal of stale items
154
+ * - Callbacks for queue state changes, execution, rejection, and expiration
155
+ *
156
+ * Running behavior:
157
+ * - `start()`: Begins automatically processing items in the queue (defaults to isRunning)
158
+ * - `stop()`: Pauses processing but maintains queue state
159
+ * - `wait`: Configurable delay between processing items
160
+ * - `onItemsChange`/`onExecute`: Callbacks for monitoring queue state
161
+ *
162
+ * Manual processing is also supported when automatic processing is disabled:
163
+ * - `execute()`: Processes the next item using the provided function
164
+ * - `getNextItem()`: Removes and returns the next item without processing
165
+ *
166
+ * Queue behavior defaults to FIFO:
167
+ * - `addItem(item)`: Adds to the back of the queue
168
+ * - Items processed from the front of the queue
169
+ *
170
+ * Priority queue:
171
+ * - Provide a `getPriority` function; higher values are processed first
172
+ *
173
+ * Stack (LIFO):
174
+ * - `addItem(item, 'back')`: Adds to the back
175
+ * - `getNextItem('back')`: Removes from the back
176
+ *
177
+ * Double-ended queue:
178
+ * - `addItem(item, position)`: Adds to specified position ('front'/'back')
179
+ * - `getNextItem(position)`: Removes from specified position
180
+ *
181
+ * Item expiration:
182
+ * - `expirationDuration`: Maximum time items can stay in the queue
183
+ * - `getIsExpired`: Function to override default expiration
184
+ * - `onExpire`: Callback for expired items
185
+ *
186
+ * State Management:
187
+ * - Uses TanStack Store for reactive state management
188
+ * - Use `initialState` to provide initial state values when creating the queuer
189
+ * - Use `onExecute` callback to react to item execution and implement custom logic
190
+ * - Use `onItemsChange` callback to react to items being added or removed from the queue
191
+ * - Use `onExpire` callback to react to items expiring and implement custom logic
192
+ * - Use `onReject` callback to react to items being rejected when the queue is full
193
+ * - The state includes execution count, expiration count, rejection count, and isRunning status
194
+ * - State can be accessed via `queuer.store.state` when using the class directly
195
+ * - When using framework adapters (React/Solid), state is accessed from `queuer.state`
196
+ *
197
+ * Example usage:
198
+ * ```ts
199
+ * // Auto-processing queue with wait time
200
+ * const autoQueue = new Queuer<number>((n) => console.log(n), {
201
+ * started: true, // Begin processing immediately
202
+ * wait: 1000, // Wait 1s between items
203
+ * onExecute: (item, queuer) => console.log(`Processed ${item}`)
204
+ * });
205
+ * autoQueue.addItem(1); // Will process after 1s
206
+ * autoQueue.addItem(2); // Will process 1s after first item
207
+ *
208
+ * // Manual processing queue
209
+ * const manualQueue = new Queuer<number>((n) => console.log(n), {
210
+ * started: false
211
+ * });
212
+ * manualQueue.addItem(1); // [1]
213
+ * manualQueue.addItem(2); // [1, 2]
214
+ * manualQueue.execute(); // logs 1, queue is [2]
215
+ * manualQueue.getNextItem(); // returns 2, queue is empty
216
+ * ```
217
+ */
218
+ declare class Queuer<TValue> {
219
+ #private;
220
+ fn: (item: TValue) => void;
221
+ readonly store: Store<Readonly<QueuerState<TValue>>>;
222
+ key: string | undefined;
223
+ options: QueuerOptions<TValue>;
224
+ constructor(fn: (item: TValue) => void, initialOptions?: QueuerOptions<TValue>);
225
+ /**
226
+ * Updates the queuer options. New options are merged with existing options.
227
+ */
228
+ setOptions: (newOptions: Partial<QueuerOptions<TValue>>) => void;
229
+ /**
230
+ * Adds an item to the queue. If the queue is full, the item is rejected and onReject is called.
231
+ * Items can be inserted based on priority or at the front/back depending on configuration.
232
+ *
233
+ * Returns true if the item was added, false if the queue is full.
234
+ *
235
+ * Example usage:
236
+ * ```ts
237
+ * queuer.addItem('task');
238
+ * queuer.addItem('task2', 'front');
239
+ * ```
240
+ */
241
+ addItem: (item: TValue, position?: QueuePosition, runOnItemsChange?: boolean) => boolean;
242
+ /**
243
+ * Removes and returns the next item from the queue without executing the function.
244
+ * Use for manual queue management. Normally, use execute() to process items.
245
+ *
246
+ * Example usage:
247
+ * ```ts
248
+ * // FIFO
249
+ * queuer.getNextItem();
250
+ * // LIFO
251
+ * queuer.getNextItem('back');
252
+ * ```
253
+ */
254
+ getNextItem: (position?: QueuePosition) => TValue | undefined;
255
+ /**
256
+ * Removes and returns the next item from the queue and processes it using the provided function.
257
+ *
258
+ * Example usage:
259
+ * ```ts
260
+ * queuer.execute();
261
+ * // LIFO
262
+ * queuer.execute('back');
263
+ * ```
264
+ */
265
+ execute: (position?: QueuePosition) => TValue | undefined;
266
+ /**
267
+ * Processes a specified number of items to execute immediately with no wait time
268
+ * If no numberOfItems is provided, all items will be processed
269
+ */
270
+ flush: (numberOfItems?: number, position?: QueuePosition) => void;
271
+ /**
272
+ * Processes all items in the queue as a batch using the provided function as an argument
273
+ * The queue is cleared after processing
274
+ */
275
+ flushAsBatch: (batchFunction: (items: Array<TValue>) => void) => void;
276
+ /**
277
+ * Returns the next item in the queue without removing it.
278
+ *
279
+ * Example usage:
280
+ * ```ts
281
+ * queuer.peekNextItem(); // front
282
+ * queuer.peekNextItem('back'); // back
283
+ * ```
284
+ */
285
+ peekNextItem: (position?: QueuePosition) => TValue | undefined;
286
+ /**
287
+ * Returns a copy of all items in the queue.
288
+ */
289
+ peekAllItems: () => Array<TValue>;
290
+ /**
291
+ * Starts processing items in the queue. If already isRunning, does nothing.
292
+ */
293
+ start: () => void;
294
+ /**
295
+ * Stops processing items in the queue. Does not clear the queue.
296
+ */
297
+ stop: () => void;
298
+ /**
299
+ * Removes all pending items from the queue. Does not affect items being processed.
300
+ */
301
+ clear: () => void;
302
+ /**
303
+ * Resets the queuer state to its default values
304
+ */
305
+ reset: () => void;
306
+ }
307
+ /**
308
+ * Creates a queue that processes items immediately upon addition.
309
+ * Items are processed sequentially in FIFO order by default.
310
+ *
311
+ * This synchronous version is lighter weight and often all you need - upgrade to asyncQueue when you need promises, retry support, abort capabilities, concurrent execution, or advanced error handling.
312
+ *
313
+ * State Management:
314
+ * - Uses TanStack Store for reactive state management
315
+ * - Use `initialState` to provide initial state values when creating the queuer
316
+ * - Use `onExecute` callback to react to item execution and implement custom logic
317
+ * - Use `onItemsChange` callback to react to items being added or removed from the queue
318
+ * - Use `onExpire` callback to react to items expiring and implement custom logic
319
+ * - Use `onReject` callback to react to items being rejected when the queue is full
320
+ * - The state includes execution count, expiration count, rejection count, and isRunning status
321
+ * - State can be accessed via the underlying Queuer instance's `store.state` property
322
+ * - When using framework adapters (React/Solid), state is accessed from the hook's state property
323
+ *
324
+ * Example usage:
325
+ * ```ts
326
+ * // Basic sequential processing
327
+ * const processItems = queue<number>((n) => console.log(n), {
328
+ * wait: 1000,
329
+ * onItemsChange: (queuer) => console.log(queuer.peekAllItems())
330
+ * });
331
+ * processItems(1); // Logs: 1
332
+ * processItems(2); // Logs: 2 after 1 completes
333
+ *
334
+ * // Priority queue
335
+ * const processPriority = queue<number>((n) => console.log(n), {
336
+ * getPriority: n => n // Higher numbers processed first
337
+ * });
338
+ * processPriority(1);
339
+ * processPriority(3); // Processed before 1
340
+ * ```
341
+ */
342
+ declare function queue<TValue>(fn: (item: TValue) => void, initialOptions: QueuerOptions<TValue>): (item: TValue, position?: QueuePosition, runOnItemsChange?: boolean) => boolean;
343
+ //#endregion
344
+ export { QueuePosition, Queuer, QueuerOptions, QueuerState, queue, queuerOptions };
345
+ //# sourceMappingURL=queuer.d.cts.map
@@ -0,0 +1,345 @@
1
+ import { Store } from "@tanstack/store";
2
+
3
+ //#region src/queuer.d.ts
4
+ interface QueuerState<TValue> {
5
+ /**
6
+ * Number of times addItem has been called (for reduction calculations)
7
+ */
8
+ addItemCount: number;
9
+ /**
10
+ * Number of items that have been processed by the queuer
11
+ */
12
+ executionCount: number;
13
+ /**
14
+ * Number of items that have been removed from the queue due to expiration
15
+ */
16
+ expirationCount: number;
17
+ /**
18
+ * Whether the queuer has no items to process (items array is empty)
19
+ */
20
+ isEmpty: boolean;
21
+ /**
22
+ * Whether the queuer has reached its maximum capacity
23
+ */
24
+ isFull: boolean;
25
+ /**
26
+ * Whether the queuer is not currently processing any items
27
+ */
28
+ isIdle: boolean;
29
+ /**
30
+ * Whether the queuer is active and will process items automatically
31
+ */
32
+ isRunning: boolean;
33
+ /**
34
+ * Array of items currently waiting to be processed
35
+ */
36
+ items: Array<TValue>;
37
+ /**
38
+ * Timestamps when items were added to the queue for expiration tracking
39
+ */
40
+ itemTimestamps: Array<number>;
41
+ /**
42
+ * Whether the queuer has a pending timeout for processing the next item
43
+ */
44
+ pendingTick: boolean;
45
+ /**
46
+ * Number of items that have been rejected from being added to the queue
47
+ */
48
+ rejectionCount: number;
49
+ /**
50
+ * Number of items currently in the queue
51
+ */
52
+ size: number;
53
+ /**
54
+ * Current processing status - 'idle' when not processing, 'running' when active, 'stopped' when paused
55
+ */
56
+ status: 'idle' | 'running' | 'stopped';
57
+ }
58
+ /**
59
+ * Options for configuring a Queuer instance.
60
+ *
61
+ * These options control queue behavior, item expiration, callbacks, and more.
62
+ */
63
+ interface QueuerOptions<TValue> {
64
+ /**
65
+ * Default position to add items to the queuer
66
+ * @default 'back'
67
+ */
68
+ addItemsTo?: QueuePosition;
69
+ /**
70
+ * Maximum time in milliseconds that an item can stay in the queue
71
+ * If not provided, items will never expire
72
+ */
73
+ expirationDuration?: number;
74
+ /**
75
+ * Function to determine if an item has expired
76
+ * If provided, this overrides the expirationDuration behavior
77
+ */
78
+ getIsExpired?: (item: TValue, addedAt: number) => boolean;
79
+ /**
80
+ * Default position to get items from during processing
81
+ * @default 'front'
82
+ */
83
+ getItemsFrom?: QueuePosition;
84
+ /**
85
+ * Function to determine priority of items in the queuer
86
+ * Higher priority items will be processed first
87
+ */
88
+ getPriority?: (item: TValue) => number;
89
+ /**
90
+ * Initial items to populate the queuer with
91
+ */
92
+ initialItems?: Array<TValue>;
93
+ /**
94
+ * Initial state for the queuer
95
+ */
96
+ initialState?: Partial<QueuerState<TValue>>;
97
+ /**
98
+ * Optional key to identify this queuer instance.
99
+ * If provided, the queuer will be identified by this key in the devtools and PacerProvider if applicable.
100
+ */
101
+ key?: string;
102
+ /**
103
+ * Maximum number of items allowed in the queuer
104
+ */
105
+ maxSize?: number;
106
+ /**
107
+ * Callback fired whenever an item is removed from the queuer
108
+ */
109
+ onExecute?: (item: TValue, queuer: Queuer<TValue>) => void;
110
+ /**
111
+ * Callback fired whenever an item expires in the queuer
112
+ */
113
+ onExpire?: (item: TValue, queuer: Queuer<TValue>) => void;
114
+ /**
115
+ * Callback fired whenever an item is added or removed from the queuer
116
+ */
117
+ onItemsChange?: (queuer: Queuer<TValue>) => void;
118
+ /**
119
+ * Callback fired whenever an item is rejected from being added to the queuer
120
+ */
121
+ onReject?: (item: TValue, queuer: Queuer<TValue>) => void;
122
+ /**
123
+ * Whether the queuer should start processing tasks immediately
124
+ */
125
+ started?: boolean;
126
+ /**
127
+ * Time in milliseconds to wait between processing items.
128
+ * Can be a number or a function that returns a number.
129
+ * @default 0
130
+ */
131
+ wait?: number | ((queuer: Queuer<TValue>) => number);
132
+ }
133
+ /**
134
+ * Utility function for sharing common `QueuerOptions` options between different `Queuer` instances.
135
+ */
136
+ declare function queuerOptions<TValue = any, TOptions extends Partial<QueuerOptions<TValue>> = Partial<QueuerOptions<TValue>>>(options: TOptions): TOptions;
137
+ /**
138
+ * Position type for addItem and getNextItem operations.
139
+ *
140
+ * - 'front': Operate on the front of the queue (FIFO)
141
+ * - 'back': Operate on the back of the queue (LIFO)
142
+ */
143
+ type QueuePosition = 'front' | 'back';
144
+ /**
145
+ * A flexible queue that processes items with configurable wait times, expiration, and priority.
146
+ *
147
+ * This synchronous version is lighter weight and often all you need - upgrade to AsyncQueuer when you need promises, retry support, abort capabilities, concurrent execution, or advanced error handling.
148
+ *
149
+ * Features:
150
+ * - Automatic or manual processing of items
151
+ * - FIFO (First In First Out), LIFO (Last In First Out), or double-ended queue behavior
152
+ * - Priority-based ordering when getPriority is provided
153
+ * - Item expiration and removal of stale items
154
+ * - Callbacks for queue state changes, execution, rejection, and expiration
155
+ *
156
+ * Running behavior:
157
+ * - `start()`: Begins automatically processing items in the queue (defaults to isRunning)
158
+ * - `stop()`: Pauses processing but maintains queue state
159
+ * - `wait`: Configurable delay between processing items
160
+ * - `onItemsChange`/`onExecute`: Callbacks for monitoring queue state
161
+ *
162
+ * Manual processing is also supported when automatic processing is disabled:
163
+ * - `execute()`: Processes the next item using the provided function
164
+ * - `getNextItem()`: Removes and returns the next item without processing
165
+ *
166
+ * Queue behavior defaults to FIFO:
167
+ * - `addItem(item)`: Adds to the back of the queue
168
+ * - Items processed from the front of the queue
169
+ *
170
+ * Priority queue:
171
+ * - Provide a `getPriority` function; higher values are processed first
172
+ *
173
+ * Stack (LIFO):
174
+ * - `addItem(item, 'back')`: Adds to the back
175
+ * - `getNextItem('back')`: Removes from the back
176
+ *
177
+ * Double-ended queue:
178
+ * - `addItem(item, position)`: Adds to specified position ('front'/'back')
179
+ * - `getNextItem(position)`: Removes from specified position
180
+ *
181
+ * Item expiration:
182
+ * - `expirationDuration`: Maximum time items can stay in the queue
183
+ * - `getIsExpired`: Function to override default expiration
184
+ * - `onExpire`: Callback for expired items
185
+ *
186
+ * State Management:
187
+ * - Uses TanStack Store for reactive state management
188
+ * - Use `initialState` to provide initial state values when creating the queuer
189
+ * - Use `onExecute` callback to react to item execution and implement custom logic
190
+ * - Use `onItemsChange` callback to react to items being added or removed from the queue
191
+ * - Use `onExpire` callback to react to items expiring and implement custom logic
192
+ * - Use `onReject` callback to react to items being rejected when the queue is full
193
+ * - The state includes execution count, expiration count, rejection count, and isRunning status
194
+ * - State can be accessed via `queuer.store.state` when using the class directly
195
+ * - When using framework adapters (React/Solid), state is accessed from `queuer.state`
196
+ *
197
+ * Example usage:
198
+ * ```ts
199
+ * // Auto-processing queue with wait time
200
+ * const autoQueue = new Queuer<number>((n) => console.log(n), {
201
+ * started: true, // Begin processing immediately
202
+ * wait: 1000, // Wait 1s between items
203
+ * onExecute: (item, queuer) => console.log(`Processed ${item}`)
204
+ * });
205
+ * autoQueue.addItem(1); // Will process after 1s
206
+ * autoQueue.addItem(2); // Will process 1s after first item
207
+ *
208
+ * // Manual processing queue
209
+ * const manualQueue = new Queuer<number>((n) => console.log(n), {
210
+ * started: false
211
+ * });
212
+ * manualQueue.addItem(1); // [1]
213
+ * manualQueue.addItem(2); // [1, 2]
214
+ * manualQueue.execute(); // logs 1, queue is [2]
215
+ * manualQueue.getNextItem(); // returns 2, queue is empty
216
+ * ```
217
+ */
218
+ declare class Queuer<TValue> {
219
+ #private;
220
+ fn: (item: TValue) => void;
221
+ readonly store: Store<Readonly<QueuerState<TValue>>>;
222
+ key: string | undefined;
223
+ options: QueuerOptions<TValue>;
224
+ constructor(fn: (item: TValue) => void, initialOptions?: QueuerOptions<TValue>);
225
+ /**
226
+ * Updates the queuer options. New options are merged with existing options.
227
+ */
228
+ setOptions: (newOptions: Partial<QueuerOptions<TValue>>) => void;
229
+ /**
230
+ * Adds an item to the queue. If the queue is full, the item is rejected and onReject is called.
231
+ * Items can be inserted based on priority or at the front/back depending on configuration.
232
+ *
233
+ * Returns true if the item was added, false if the queue is full.
234
+ *
235
+ * Example usage:
236
+ * ```ts
237
+ * queuer.addItem('task');
238
+ * queuer.addItem('task2', 'front');
239
+ * ```
240
+ */
241
+ addItem: (item: TValue, position?: QueuePosition, runOnItemsChange?: boolean) => boolean;
242
+ /**
243
+ * Removes and returns the next item from the queue without executing the function.
244
+ * Use for manual queue management. Normally, use execute() to process items.
245
+ *
246
+ * Example usage:
247
+ * ```ts
248
+ * // FIFO
249
+ * queuer.getNextItem();
250
+ * // LIFO
251
+ * queuer.getNextItem('back');
252
+ * ```
253
+ */
254
+ getNextItem: (position?: QueuePosition) => TValue | undefined;
255
+ /**
256
+ * Removes and returns the next item from the queue and processes it using the provided function.
257
+ *
258
+ * Example usage:
259
+ * ```ts
260
+ * queuer.execute();
261
+ * // LIFO
262
+ * queuer.execute('back');
263
+ * ```
264
+ */
265
+ execute: (position?: QueuePosition) => TValue | undefined;
266
+ /**
267
+ * Processes a specified number of items to execute immediately with no wait time
268
+ * If no numberOfItems is provided, all items will be processed
269
+ */
270
+ flush: (numberOfItems?: number, position?: QueuePosition) => void;
271
+ /**
272
+ * Processes all items in the queue as a batch using the provided function as an argument
273
+ * The queue is cleared after processing
274
+ */
275
+ flushAsBatch: (batchFunction: (items: Array<TValue>) => void) => void;
276
+ /**
277
+ * Returns the next item in the queue without removing it.
278
+ *
279
+ * Example usage:
280
+ * ```ts
281
+ * queuer.peekNextItem(); // front
282
+ * queuer.peekNextItem('back'); // back
283
+ * ```
284
+ */
285
+ peekNextItem: (position?: QueuePosition) => TValue | undefined;
286
+ /**
287
+ * Returns a copy of all items in the queue.
288
+ */
289
+ peekAllItems: () => Array<TValue>;
290
+ /**
291
+ * Starts processing items in the queue. If already isRunning, does nothing.
292
+ */
293
+ start: () => void;
294
+ /**
295
+ * Stops processing items in the queue. Does not clear the queue.
296
+ */
297
+ stop: () => void;
298
+ /**
299
+ * Removes all pending items from the queue. Does not affect items being processed.
300
+ */
301
+ clear: () => void;
302
+ /**
303
+ * Resets the queuer state to its default values
304
+ */
305
+ reset: () => void;
306
+ }
307
+ /**
308
+ * Creates a queue that processes items immediately upon addition.
309
+ * Items are processed sequentially in FIFO order by default.
310
+ *
311
+ * This synchronous version is lighter weight and often all you need - upgrade to asyncQueue when you need promises, retry support, abort capabilities, concurrent execution, or advanced error handling.
312
+ *
313
+ * State Management:
314
+ * - Uses TanStack Store for reactive state management
315
+ * - Use `initialState` to provide initial state values when creating the queuer
316
+ * - Use `onExecute` callback to react to item execution and implement custom logic
317
+ * - Use `onItemsChange` callback to react to items being added or removed from the queue
318
+ * - Use `onExpire` callback to react to items expiring and implement custom logic
319
+ * - Use `onReject` callback to react to items being rejected when the queue is full
320
+ * - The state includes execution count, expiration count, rejection count, and isRunning status
321
+ * - State can be accessed via the underlying Queuer instance's `store.state` property
322
+ * - When using framework adapters (React/Solid), state is accessed from the hook's state property
323
+ *
324
+ * Example usage:
325
+ * ```ts
326
+ * // Basic sequential processing
327
+ * const processItems = queue<number>((n) => console.log(n), {
328
+ * wait: 1000,
329
+ * onItemsChange: (queuer) => console.log(queuer.peekAllItems())
330
+ * });
331
+ * processItems(1); // Logs: 1
332
+ * processItems(2); // Logs: 2 after 1 completes
333
+ *
334
+ * // Priority queue
335
+ * const processPriority = queue<number>((n) => console.log(n), {
336
+ * getPriority: n => n // Higher numbers processed first
337
+ * });
338
+ * processPriority(1);
339
+ * processPriority(3); // Processed before 1
340
+ * ```
341
+ */
342
+ declare function queue<TValue>(fn: (item: TValue) => void, initialOptions: QueuerOptions<TValue>): (item: TValue, position?: QueuePosition, runOnItemsChange?: boolean) => boolean;
343
+ //#endregion
344
+ export { QueuePosition, Queuer, QueuerOptions, QueuerState, queue, queuerOptions };
345
+ //# sourceMappingURL=queuer.d.ts.map