@tanstack/pacer 0.5.0 → 0.7.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 (74) hide show
  1. package/dist/cjs/async-debouncer.cjs +32 -17
  2. package/dist/cjs/async-debouncer.cjs.map +1 -1
  3. package/dist/cjs/async-debouncer.d.cts +51 -9
  4. package/dist/cjs/async-queuer.cjs +189 -191
  5. package/dist/cjs/async-queuer.cjs.map +1 -1
  6. package/dist/cjs/async-queuer.d.cts +151 -94
  7. package/dist/cjs/async-rate-limiter.cjs +23 -13
  8. package/dist/cjs/async-rate-limiter.cjs.map +1 -1
  9. package/dist/cjs/async-rate-limiter.d.cts +54 -5
  10. package/dist/cjs/async-throttler.cjs +38 -17
  11. package/dist/cjs/async-throttler.cjs.map +1 -1
  12. package/dist/cjs/async-throttler.d.cts +51 -8
  13. package/dist/cjs/batcher.cjs +138 -0
  14. package/dist/cjs/batcher.cjs.map +1 -0
  15. package/dist/cjs/batcher.d.cts +149 -0
  16. package/dist/cjs/debouncer.cjs +3 -4
  17. package/dist/cjs/debouncer.cjs.map +1 -1
  18. package/dist/cjs/debouncer.d.cts +1 -2
  19. package/dist/cjs/index.cjs +3 -0
  20. package/dist/cjs/index.cjs.map +1 -1
  21. package/dist/cjs/index.d.cts +1 -0
  22. package/dist/cjs/queuer.cjs +68 -41
  23. package/dist/cjs/queuer.cjs.map +1 -1
  24. package/dist/cjs/queuer.d.cts +123 -92
  25. package/dist/cjs/rate-limiter.cjs +3 -4
  26. package/dist/cjs/rate-limiter.cjs.map +1 -1
  27. package/dist/cjs/rate-limiter.d.cts +1 -2
  28. package/dist/cjs/throttler.cjs +3 -4
  29. package/dist/cjs/throttler.cjs.map +1 -1
  30. package/dist/cjs/throttler.d.cts +1 -2
  31. package/dist/cjs/types.d.cts +1 -0
  32. package/dist/esm/async-debouncer.d.ts +51 -9
  33. package/dist/esm/async-debouncer.js +32 -17
  34. package/dist/esm/async-debouncer.js.map +1 -1
  35. package/dist/esm/async-queuer.d.ts +151 -94
  36. package/dist/esm/async-queuer.js +189 -191
  37. package/dist/esm/async-queuer.js.map +1 -1
  38. package/dist/esm/async-rate-limiter.d.ts +54 -5
  39. package/dist/esm/async-rate-limiter.js +23 -13
  40. package/dist/esm/async-rate-limiter.js.map +1 -1
  41. package/dist/esm/async-throttler.d.ts +51 -8
  42. package/dist/esm/async-throttler.js +38 -17
  43. package/dist/esm/async-throttler.js.map +1 -1
  44. package/dist/esm/batcher.d.ts +149 -0
  45. package/dist/esm/batcher.js +138 -0
  46. package/dist/esm/batcher.js.map +1 -0
  47. package/dist/esm/debouncer.d.ts +1 -2
  48. package/dist/esm/debouncer.js +3 -4
  49. package/dist/esm/debouncer.js.map +1 -1
  50. package/dist/esm/index.d.ts +1 -0
  51. package/dist/esm/index.js +3 -0
  52. package/dist/esm/index.js.map +1 -1
  53. package/dist/esm/queuer.d.ts +123 -92
  54. package/dist/esm/queuer.js +68 -41
  55. package/dist/esm/queuer.js.map +1 -1
  56. package/dist/esm/rate-limiter.d.ts +1 -2
  57. package/dist/esm/rate-limiter.js +3 -4
  58. package/dist/esm/rate-limiter.js.map +1 -1
  59. package/dist/esm/throttler.d.ts +1 -2
  60. package/dist/esm/throttler.js +3 -4
  61. package/dist/esm/throttler.js.map +1 -1
  62. package/dist/esm/types.d.ts +1 -0
  63. package/package.json +11 -1
  64. package/src/async-debouncer.ts +76 -20
  65. package/src/async-queuer.ts +309 -275
  66. package/src/async-rate-limiter.ts +73 -16
  67. package/src/async-throttler.ts +84 -20
  68. package/src/batcher.ts +253 -0
  69. package/src/debouncer.ts +3 -4
  70. package/src/index.ts +1 -0
  71. package/src/queuer.ts +142 -98
  72. package/src/rate-limiter.ts +3 -4
  73. package/src/throttler.ts +3 -4
  74. package/src/types.ts +3 -0
@@ -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
- onGetNextItem?: (item: TValue, queuer: Queuer<TValue>) => void;
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 data structure that defaults to FIFO (First In First Out) behavior
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
- * The queuer can automatically process items as they are added, with configurable
78
- * wait times between processing each item. Processing can be started/stopped
79
- * and the queuer will maintain its state.
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
- * Supports priority-based ordering when a getPriority function is provided.
82
- * Items with higher priority values will be processed first.
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
- * Default queue behavior:
85
- * - addItem(item): adds to back of queuer
86
- * - getNextItem(): removes and returns from front of queuer
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
- * Stack (LIFO) behavior:
89
- * - addItem(item, 'back'): adds to back
90
- * - getNextItem('back'): removes and returns from back
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
- * Double-ended queuer behavior:
93
- * - addItem(item, position): adds to specified position ('front' or 'back')
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
- * Processing behavior:
97
- * - start(): begins processing items in the queuer
98
- * - stop(): pauses processing
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
- * Supports item expiration to clear stale items from the queuer
103
- * - expirationDuration: maximum time in milliseconds that an item can stay in the queue
104
- * - getIsExpired: function to override default expiration behavior
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
- * @example
108
- * ```ts
109
- * // FIFO queuer
110
- * const queuer = new Queuer<number>();
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
- * // Priority queuer with processing
116
- * const priorityQueue = new Queuer<number>({
117
- * getPriority: (n) => n, // Higher numbers have priority
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
- * onGetNextItem: (item, queuer) => console.log(item)
124
+ * onExecute: (item) => console.log(`Processed ${item}`)
121
125
  * });
122
- * priorityQueue.addItem(1); // [1]
123
- * priorityQueue.addItem(3); // [3, 1] - 3 processed first
124
- * priorityQueue.addItem(2); // [3, 2, 1]
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 queuer
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 expired items from the queuer
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 queuer from processing items
174
+ * Stops processing items in the queue. Does not clear the queue.
161
175
  */
162
176
  stop(): void;
163
177
  /**
164
- * Starts the queuer and processes items
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 queuer
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 queuer and starts processing if not already running
177
- * @returns true if item was added, false if queuer is full
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 an item from the queuer using shift (default) or pop
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
- * @example
207
+ * Example usage:
184
208
  * ```ts
185
- * // Standard FIFO queuer
186
- * queuer.getNextItem()
187
- * // Stack-like behavior (LIFO)
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
- * Returns an item without removing it
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
- * @example
230
+ * Example usage:
196
231
  * ```ts
197
- * // Look at next item to getNextItem
198
- * queuer.getPeek()
199
- * // Look at last item (like stack top)
200
- * queuer.getPeek('back')
232
+ * queuer.getPeek(); // front
233
+ * queuer.getPeek('back'); // back
201
234
  * ```
202
235
  */
203
236
  getPeek(position?: QueuePosition): TValue | undefined;
204
237
  /**
205
- * Returns true if the queuer is empty
238
+ * Returns true if the queue is empty (no pending items).
206
239
  */
207
240
  getIsEmpty(): boolean;
208
241
  /**
209
- * Returns true if the queuer is full
242
+ * Returns true if the queue is full (reached maxSize).
210
243
  */
211
244
  getIsFull(): boolean;
212
245
  /**
213
- * Returns the current size of the queuer
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 queuer
250
+ * Returns a copy of all items in the queue.
218
251
  */
219
252
  getAllItems(): Array<TValue>;
220
253
  /**
221
- * Returns the number of items that have been removed from the queuer
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 queuer
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 queuer
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 in a queuer immediately upon addition.
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. This queue is always running and will process items as they are added.
247
- * For more control over queuer processing, use the Queuer class
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
- * @example
282
+ * Example usage:
251
283
  * ```ts
252
284
  * // Basic sequential processing
253
- * const processItems = queuer<number>({
285
+ * const processItems = queue<number>((n) => console.log(n), {
254
286
  * wait: 1000,
255
287
  * onItemsChange: (queuer) => console.log(queuer.getAllItems())
256
- * })
257
- * processItems(1) // Logs: 1
258
- * processItems(2) // Logs: 2 after 1 completes
288
+ * });
289
+ * processItems(1); // Logs: 1
290
+ * processItems(2); // Logs: 2 after 1 completes
259
291
  *
260
- * // Priority queuer
261
- * const processPriority = queuer<number>({
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;
@@ -7,7 +7,7 @@ const defaultOptions = {
7
7
  expirationDuration: Infinity,
8
8
  initialItems: [],
9
9
  maxSize: Infinity,
10
- onGetNextItem: () => {
10
+ onExecute: () => {
11
11
  },
12
12
  onIsRunningChange: () => {
13
13
  },
@@ -21,7 +21,8 @@ const defaultOptions = {
21
21
  wait: 0
22
22
  };
23
23
  class Queuer {
24
- constructor(initialOptions = defaultOptions) {
24
+ constructor(fn, initialOptions = {}) {
25
+ this.fn = fn;
25
26
  this._items = [];
26
27
  this._itemTimestamps = [];
27
28
  this._executionCount = 0;
@@ -38,26 +39,26 @@ class Queuer {
38
39
  }
39
40
  }
40
41
  /**
41
- * Updates the queuer options
42
- * Returns the new options state
42
+ * Updates the queuer options. New options are merged with existing options.
43
43
  */
44
44
  setOptions(newOptions) {
45
45
  this._options = { ...this._options, ...newOptions };
46
46
  }
47
47
  /**
48
- * Returns the current queuer options
48
+ * Returns the current queuer options, including defaults and any overrides.
49
49
  */
50
50
  getOptions() {
51
51
  return this._options;
52
52
  }
53
53
  /**
54
- * Returns the current wait time in milliseconds
54
+ * Returns the current wait time (in milliseconds) between processing items.
55
+ * If a function is provided, it is called with the queuer instance.
55
56
  */
56
57
  getWait() {
57
58
  return parseFunctionOrValue(this._options.wait, this);
58
59
  }
59
60
  /**
60
- * Processes items in the queuer
61
+ * Processes items in the queue up to the wait interval. Internal use only.
61
62
  */
62
63
  tick() {
63
64
  if (!this._running) {
@@ -66,7 +67,7 @@ class Queuer {
66
67
  }
67
68
  this.checkExpiredItems();
68
69
  while (!this.getIsEmpty()) {
69
- const nextItem = this.getNextItem(this._options.getItemsFrom);
70
+ const nextItem = this.execute(this._options.getItemsFrom);
70
71
  if (nextItem === void 0) {
71
72
  break;
72
73
  }
@@ -81,7 +82,8 @@ class Queuer {
81
82
  this._pendingTick = false;
82
83
  }
83
84
  /**
84
- * Checks for and removes expired items from the queuer
85
+ * Checks for expired items in the queue and removes them. Calls onExpire for each expired item.
86
+ * Internal use only.
85
87
  */
86
88
  checkExpiredItems() {
87
89
  if (this._options.expirationDuration === Infinity && this._options.getIsExpired === defaultOptions.getIsExpired)
@@ -113,7 +115,7 @@ class Queuer {
113
115
  }
114
116
  }
115
117
  /**
116
- * Stops the queuer from processing items
118
+ * Stops processing items in the queue. Does not clear the queue.
117
119
  */
118
120
  stop() {
119
121
  this._running = false;
@@ -121,7 +123,7 @@ class Queuer {
121
123
  this._options.onIsRunningChange(this);
122
124
  }
123
125
  /**
124
- * Starts the queuer and processes items
126
+ * Starts processing items in the queue. If already running, does nothing.
125
127
  */
126
128
  start() {
127
129
  this._running = true;
@@ -132,14 +134,15 @@ class Queuer {
132
134
  this._options.onIsRunningChange(this);
133
135
  }
134
136
  /**
135
- * Removes all items from the queuer
137
+ * Removes all pending items from the queue. Does not affect items being processed.
136
138
  */
137
139
  clear() {
138
140
  this._items = [];
139
141
  this._options.onItemsChange(this);
140
142
  }
141
143
  /**
142
- * Resets the queuer to its initial state
144
+ * Resets the queuer to its initial state. Optionally repopulates with initial items.
145
+ * Does not affect callbacks or options.
143
146
  */
144
147
  reset(withInitialItems) {
145
148
  this.clear();
@@ -150,8 +153,16 @@ class Queuer {
150
153
  this._running = this._options.started;
151
154
  }
152
155
  /**
153
- * Adds an item to the queuer and starts processing if not already running
154
- * @returns true if item was added, false if queuer is full
156
+ * Adds an item to the queue. If the queue is full, the item is rejected and onReject is called.
157
+ * Items can be inserted based on priority or at the front/back depending on configuration.
158
+ *
159
+ * Returns true if the item was added, false if the queue is full.
160
+ *
161
+ * Example usage:
162
+ * ```ts
163
+ * queuer.addItem('task');
164
+ * queuer.addItem('task2', 'front');
165
+ * ```
155
166
  */
156
167
  addItem(item, position = this._options.addItemsTo, runOnUpdate = true) {
157
168
  if (this.getIsFull()) {
@@ -162,7 +173,7 @@ class Queuer {
162
173
  if (this._options.getPriority !== defaultOptions.getPriority) {
163
174
  const priority = this._options.getPriority(item);
164
175
  const insertIndex = this._items.findIndex(
165
- (existing) => this._options.getPriority(existing) > priority
176
+ (existing) => this._options.getPriority(existing) < priority
166
177
  );
167
178
  if (insertIndex === -1) {
168
179
  this._items.push(item);
@@ -190,14 +201,15 @@ class Queuer {
190
201
  return true;
191
202
  }
192
203
  /**
193
- * Removes and returns an item from the queuer using shift (default) or pop
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.
194
206
  *
195
- * @example
207
+ * Example usage:
196
208
  * ```ts
197
- * // Standard FIFO queuer
198
- * queuer.getNextItem()
199
- * // Stack-like behavior (LIFO)
200
- * queuer.getNextItem('back')
209
+ * // FIFO
210
+ * queuer.getNextItem();
211
+ * // LIFO
212
+ * queuer.getNextItem('back');
201
213
  * ```
202
214
  */
203
215
  getNextItem(position = this._options.getItemsFrom) {
@@ -210,21 +222,36 @@ class Queuer {
210
222
  this._itemTimestamps.pop();
211
223
  }
212
224
  if (item !== void 0) {
213
- this._executionCount++;
214
225
  this._options.onItemsChange(this);
215
- this._options.onGetNextItem(item, this);
216
226
  }
217
227
  return item;
218
228
  }
219
229
  /**
220
- * Returns an item without removing it
230
+ * Removes and returns the next item from the queue and processes it using the provided function.
231
+ *
232
+ * Example usage:
233
+ * ```ts
234
+ * queuer.execute();
235
+ * // LIFO
236
+ * queuer.execute('back');
237
+ * ```
238
+ */
239
+ execute(position) {
240
+ const item = this.getNextItem(position);
241
+ if (item !== void 0) {
242
+ this.fn(item);
243
+ this._executionCount++;
244
+ this._options.onExecute(item, this);
245
+ }
246
+ return item;
247
+ }
248
+ /**
249
+ * Returns the next item in the queue without removing it.
221
250
  *
222
- * @example
251
+ * Example usage:
223
252
  * ```ts
224
- * // Look at next item to getNextItem
225
- * queuer.getPeek()
226
- * // Look at last item (like stack top)
227
- * queuer.getPeek('back')
253
+ * queuer.getPeek(); // front
254
+ * queuer.getPeek('back'); // back
228
255
  * ```
229
256
  */
230
257
  getPeek(position = this._options.getItemsFrom) {
@@ -234,62 +261,62 @@ class Queuer {
234
261
  return this._items[this._items.length - 1];
235
262
  }
236
263
  /**
237
- * Returns true if the queuer is empty
264
+ * Returns true if the queue is empty (no pending items).
238
265
  */
239
266
  getIsEmpty() {
240
267
  return this._items.length === 0;
241
268
  }
242
269
  /**
243
- * Returns true if the queuer is full
270
+ * Returns true if the queue is full (reached maxSize).
244
271
  */
245
272
  getIsFull() {
246
273
  return this._items.length >= this._options.maxSize;
247
274
  }
248
275
  /**
249
- * Returns the current size of the queuer
276
+ * Returns the number of pending items in the queue.
250
277
  */
251
278
  getSize() {
252
279
  return this._items.length;
253
280
  }
254
281
  /**
255
- * Returns a copy of all items in the queuer
282
+ * Returns a copy of all items in the queue.
256
283
  */
257
284
  getAllItems() {
258
285
  return [...this._items];
259
286
  }
260
287
  /**
261
- * Returns the number of items that have been removed from the queuer
288
+ * Returns the number of items that have been processed and removed from the queue.
262
289
  */
263
290
  getExecutionCount() {
264
291
  return this._executionCount;
265
292
  }
266
293
  /**
267
- * Returns the number of items that have been rejected from the queuer
294
+ * Returns the number of items that have been rejected from being added to the queue.
268
295
  */
269
296
  getRejectionCount() {
270
297
  return this._rejectionCount;
271
298
  }
272
299
  /**
273
- * Returns the number of items that have expired from the queuer
300
+ * Returns the number of items that have expired and been removed from the queue.
274
301
  */
275
302
  getExpirationCount() {
276
303
  return this._expirationCount;
277
304
  }
278
305
  /**
279
- * Returns true if the queuer is running
306
+ * Returns true if the queuer is currently running (processing items).
280
307
  */
281
308
  getIsRunning() {
282
309
  return this._running;
283
310
  }
284
311
  /**
285
- * Returns true if the queuer is running but has no items to process
312
+ * Returns true if the queuer is running but has no items to process.
286
313
  */
287
314
  getIsIdle() {
288
315
  return this._running && this.getIsEmpty();
289
316
  }
290
317
  }
291
- function queue(options) {
292
- const queuer = new Queuer(options);
318
+ function queue(fn, options) {
319
+ const queuer = new Queuer(fn, options);
293
320
  return queuer.addItem.bind(queuer);
294
321
  }
295
322
  export {