Skip to content

Commit d229d42

Browse files
authored
feat: Enhance async utilities with breaking changes and new features (#40)
- Removed `isRunning` state from `Batcher` and `AsyncBatcher` utilities. - Updated `flush()` method in `AsyncDebouncer`, `AsyncThrottler`, and `AsyncQueuer` to return Promises. - Introduced `flushAsBatch` methods in `Queuer` and `AsyncQueuer` for batch processing. - Added `isExceeded` and `status` properties to `RateLimiter` and `AsyncRateLimiter` for improved tracking. - Enhanced error handling in `AsyncDebouncer` and `AsyncThrottler` with promise rejection support. - Improved timeout management in rate limiters with automatic cleanup of expired execution times.
1 parent bcc760f commit d229d42

56 files changed

Lines changed: 3060 additions & 655 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.changeset/tame-spies-wish.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
'@tanstack/pacer': minor
3+
'@tanstack/react-pacer': minor
4+
'@tanstack/solid-pacer': minor
5+
---
6+
7+
breaking: Removed `isRunning` state from `Batcher` and `AsyncBatcher` utils, and also removed `start` and `stop` methods
8+
breaking: Changed `flush()` method in `AsyncDebouncer`, `AsyncThrottler`, and `AsyncQueuer` to return Promises instead of void
9+
feat: Added `flushAsBatch` methods to the `Queuer` and `AsyncQueuer` utils
10+
feat: Added `isExceeded` and `status` state properties to `RateLimiter` and `AsyncRateLimiter` for better rate limit tracking
11+
feat: Enhanced error handling in `AsyncDebouncer` and `AsyncThrottler` with proper promise rejection support
12+
feat: Improved timeout management in rate limiters with automatic cleanup of expired execution times

docs/reference/classes/asyncbatcher.md

Lines changed: 11 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ title: AsyncBatcher
77

88
# Class: AsyncBatcher\<TValue\>
99

10-
Defined in: [async-batcher.ts:229](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-batcher.ts#L229)
10+
Defined in: [async-batcher.ts:224](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-batcher.ts#L224)
1111

1212
A class that collects items and processes them in batches asynchronously.
1313

@@ -82,7 +82,7 @@ batcher.addItem(2);
8282
new AsyncBatcher<TValue>(fn, initialOptions): AsyncBatcher<TValue>
8383
```
8484

85-
Defined in: [async-batcher.ts:236](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-batcher.ts#L236)
85+
Defined in: [async-batcher.ts:231](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-batcher.ts#L231)
8686

8787
#### Parameters
8888

@@ -106,7 +106,7 @@ Defined in: [async-batcher.ts:236](https://github.com/TanStack/pacer/blob/main/p
106106
options: AsyncBatcherOptionsWithOptionalCallbacks<TValue>;
107107
```
108108

109-
Defined in: [async-batcher.ts:233](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-batcher.ts#L233)
109+
Defined in: [async-batcher.ts:228](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-batcher.ts#L228)
110110

111111
***
112112

@@ -116,7 +116,7 @@ Defined in: [async-batcher.ts:233](https://github.com/TanStack/pacer/blob/main/p
116116
readonly store: Store<Readonly<AsyncBatcherState<TValue>>>;
117117
```
118118

119-
Defined in: [async-batcher.ts:230](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-batcher.ts#L230)
119+
Defined in: [async-batcher.ts:225](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-batcher.ts#L225)
120120

121121
## Methods
122122

@@ -126,7 +126,7 @@ Defined in: [async-batcher.ts:230](https://github.com/TanStack/pacer/blob/main/p
126126
addItem(item): void
127127
```
128128

129-
Defined in: [async-batcher.ts:287](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-batcher.ts#L287)
129+
Defined in: [async-batcher.ts:282](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-batcher.ts#L282)
130130

131131
Adds an item to the async batcher
132132
If the batch size is reached, timeout occurs, or shouldProcess returns true, the batch will be processed
@@ -149,7 +149,7 @@ If the batch size is reached, timeout occurs, or shouldProcess returns true, the
149149
clear(): void
150150
```
151151

152-
Defined in: [async-batcher.ts:407](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-batcher.ts#L407)
152+
Defined in: [async-batcher.ts:384](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-batcher.ts#L384)
153153

154154
Removes all items from the async batcher
155155

@@ -165,7 +165,7 @@ Removes all items from the async batcher
165165
flush(): Promise<any>
166166
```
167167

168-
Defined in: [async-batcher.ts:363](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-batcher.ts#L363)
168+
Defined in: [async-batcher.ts:358](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-batcher.ts#L358)
169169

170170
Processes the current batch of items immediately
171171

@@ -181,7 +181,7 @@ Processes the current batch of items immediately
181181
peekAllItems(): TValue[]
182182
```
183183

184-
Defined in: [async-batcher.ts:389](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-batcher.ts#L389)
184+
Defined in: [async-batcher.ts:366](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-batcher.ts#L366)
185185

186186
Returns a copy of all items in the async batcher
187187

@@ -197,7 +197,7 @@ Returns a copy of all items in the async batcher
197197
peekFailedItems(): TValue[]
198198
```
199199

200-
Defined in: [async-batcher.ts:393](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-batcher.ts#L393)
200+
Defined in: [async-batcher.ts:370](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-batcher.ts#L370)
201201

202202
#### Returns
203203

@@ -211,7 +211,7 @@ Defined in: [async-batcher.ts:393](https://github.com/TanStack/pacer/blob/main/p
211211
reset(): void
212212
```
213213

214-
Defined in: [async-batcher.ts:414](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-batcher.ts#L414)
214+
Defined in: [async-batcher.ts:391](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-batcher.ts#L391)
215215

216216
Resets the async batcher state to its default values
217217

@@ -227,7 +227,7 @@ Resets the async batcher state to its default values
227227
setOptions(newOptions): void
228228
```
229229

230-
Defined in: [async-batcher.ts:251](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-batcher.ts#L251)
230+
Defined in: [async-batcher.ts:246](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-batcher.ts#L246)
231231

232232
Updates the async batcher options
233233

@@ -240,35 +240,3 @@ Updates the async batcher options
240240
#### Returns
241241

242242
`void`
243-
244-
***
245-
246-
### start()
247-
248-
```ts
249-
start(): void
250-
```
251-
252-
Defined in: [async-batcher.ts:379](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-batcher.ts#L379)
253-
254-
Starts the async batcher and processes any pending items
255-
256-
#### Returns
257-
258-
`void`
259-
260-
***
261-
262-
### stop()
263-
264-
```ts
265-
stop(): void
266-
```
267-
268-
Defined in: [async-batcher.ts:371](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-batcher.ts#L371)
269-
270-
Stops the async batcher from processing batches
271-
272-
#### Returns
273-
274-
`void`

docs/reference/classes/asyncdebouncer.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ const results = await asyncDebouncer.maybeExecute(inputElement.value);
6666
new AsyncDebouncer<TFn>(fn, initialOptions): AsyncDebouncer<TFn>
6767
```
6868

69-
Defined in: [async-debouncer.ts:180](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-debouncer.ts#L180)
69+
Defined in: [async-debouncer.ts:181](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-debouncer.ts#L181)
7070

7171
#### Parameters
7272

@@ -110,7 +110,7 @@ Defined in: [async-debouncer.ts:170](https://github.com/TanStack/pacer/blob/main
110110
cancel(): void
111111
```
112112

113-
Defined in: [async-debouncer.ts:363](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-debouncer.ts#L363)
113+
Defined in: [async-debouncer.ts:382](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-debouncer.ts#L382)
114114

115115
Cancels any pending execution or aborts any execution in progress
116116

@@ -123,16 +123,16 @@ Cancels any pending execution or aborts any execution in progress
123123
### flush()
124124

125125
```ts
126-
flush(): void
126+
flush(): Promise<undefined | ReturnType<TFn>>
127127
```
128128

129-
Defined in: [async-debouncer.ts:325](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-debouncer.ts#L325)
129+
Defined in: [async-debouncer.ts:327](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-debouncer.ts#L327)
130130

131131
Processes the current pending execution immediately
132132

133133
#### Returns
134134

135-
`void`
135+
`Promise`\<`undefined` \| `ReturnType`\<`TFn`\>\>
136136

137137
***
138138

@@ -142,7 +142,7 @@ Processes the current pending execution immediately
142142
maybeExecute(...args): Promise<undefined | ReturnType<TFn>>
143143
```
144144

145-
Defined in: [async-debouncer.ts:254](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-debouncer.ts#L254)
145+
Defined in: [async-debouncer.ts:255](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-debouncer.ts#L255)
146146

147147
Attempts to execute the debounced function.
148148
If a call is already in progress, it will be queued.
@@ -178,7 +178,7 @@ The error from the debounced function if no onError handler is configured
178178
reset(): void
179179
```
180180

181-
Defined in: [async-debouncer.ts:372](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-debouncer.ts#L372)
181+
Defined in: [async-debouncer.ts:391](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-debouncer.ts#L391)
182182

183183
Resets the debouncer state to its default values
184184

@@ -194,7 +194,7 @@ Resets the debouncer state to its default values
194194
setOptions(newOptions): void
195195
```
196196

197-
Defined in: [async-debouncer.ts:195](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-debouncer.ts#L195)
197+
Defined in: [async-debouncer.ts:196](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-debouncer.ts#L196)
198198

199199
Updates the async debouncer options
200200

docs/reference/classes/asyncqueuer.md

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ queuer.addItem('task2', 'front');
156156
clear(): void
157157
```
158158

159-
Defined in: [async-queuer.ts:686](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-queuer.ts#L686)
159+
Defined in: [async-queuer.ts:704](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-queuer.ts#L704)
160160

161161
Removes all pending items from the queue. Does not affect active tasks.
162162

@@ -172,7 +172,7 @@ Removes all pending items from the queue. Does not affect active tasks.
172172
execute(position?): Promise<any>
173173
```
174174
175-
Defined in: [async-queuer.ts:520](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-queuer.ts#L520)
175+
Defined in: [async-queuer.ts:526](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-queuer.ts#L526)
176176
177177
Removes and returns the next item from the queue and executes the task function with it.
178178
@@ -199,10 +199,10 @@ queuer.execute('back');
199199
### flush()
200200
201201
```ts
202-
flush(numberOfItems, position?): void
202+
flush(numberOfItems, position?): Promise<void>
203203
```
204204
205-
Defined in: [async-queuer.ts:555](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-queuer.ts#L555)
205+
Defined in: [async-queuer.ts:561](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-queuer.ts#L561)
206206
207207
Processes a specified number of items to execute immediately with no wait time
208208
If no numberOfItems is provided, all items will be processed
@@ -219,7 +219,30 @@ If no numberOfItems is provided, all items will be processed
219219
220220
#### Returns
221221
222-
`void`
222+
`Promise`\<`void`\>
223+
224+
***
225+
226+
### flushAsBatch()
227+
228+
```ts
229+
flushAsBatch(batchFunction): Promise<void>
230+
```
231+
232+
Defined in: [async-queuer.ts:575](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-queuer.ts#L575)
233+
234+
Processes all items in the queue as a batch using the provided function
235+
The queue is cleared after processing
236+
237+
#### Parameters
238+
239+
##### batchFunction
240+
241+
(`items`) => `Promise`\<`any`\>
242+
243+
#### Returns
244+
245+
`Promise`\<`void`\>
223246
224247
***
225248
@@ -261,7 +284,7 @@ queuer.getNextItem('back');
261284
peekActiveItems(): TValue[]
262285
```
263286
264-
Defined in: [async-queuer.ts:649](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-queuer.ts#L649)
287+
Defined in: [async-queuer.ts:667](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-queuer.ts#L667)
265288
266289
Returns the items currently being processed (active tasks).
267290
@@ -277,7 +300,7 @@ Returns the items currently being processed (active tasks).
277300
peekAllItems(): TValue[]
278301
```
279302
280-
Defined in: [async-queuer.ts:642](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-queuer.ts#L642)
303+
Defined in: [async-queuer.ts:660](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-queuer.ts#L660)
281304
282305
Returns a copy of all items in the queue, including active and pending items.
283306
@@ -293,7 +316,7 @@ Returns a copy of all items in the queue, including active and pending items.
293316
peekNextItem(position): undefined | TValue
294317
```
295318
296-
Defined in: [async-queuer.ts:632](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-queuer.ts#L632)
319+
Defined in: [async-queuer.ts:650](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-queuer.ts#L650)
297320
298321
Returns the next item in the queue without removing it.
299322
@@ -322,7 +345,7 @@ queuer.peekNextItem('back'); // back
322345
peekPendingItems(): TValue[]
323346
```
324347
325-
Defined in: [async-queuer.ts:656](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-queuer.ts#L656)
348+
Defined in: [async-queuer.ts:674](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-queuer.ts#L674)
326349
327350
Returns the items waiting to be processed (pending tasks).
328351
@@ -338,7 +361,7 @@ Returns the items waiting to be processed (pending tasks).
338361
reset(): void
339362
```
340363
341-
Defined in: [async-queuer.ts:694](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-queuer.ts#L694)
364+
Defined in: [async-queuer.ts:712](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-queuer.ts#L712)
342365
343366
Resets the queuer state to its default values
344367
@@ -376,7 +399,7 @@ Updates the queuer options. New options are merged with existing options.
376399
start(): void
377400
```
378401
379-
Defined in: [async-queuer.ts:663](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-queuer.ts#L663)
402+
Defined in: [async-queuer.ts:681](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-queuer.ts#L681)
380403
381404
Starts processing items in the queue. If already running, does nothing.
382405
@@ -392,7 +415,7 @@ Starts processing items in the queue. If already running, does nothing.
392415
stop(): void
393416
```
394417
395-
Defined in: [async-queuer.ts:673](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-queuer.ts#L673)
418+
Defined in: [async-queuer.ts:691](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-queuer.ts#L691)
396419
397420
Stops processing items in the queue. Does not clear the queue.
398421

0 commit comments

Comments
 (0)