Skip to content

Commit 7feefc7

Browse files
committed
add getPriority function back to async queuer
1 parent d597699 commit 7feefc7

File tree

7 files changed

+95
-50
lines changed

7 files changed

+95
-50
lines changed

docs/framework/react/reference/functions/useasyncqueuer.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ Adds a task to the queuer
5757
5858
##### fn
5959
60-
() => `Promise`\<`TValue`\>
60+
() => `Promise`\<`TValue`\> & `object`
6161
6262
##### position?
6363
@@ -275,7 +275,7 @@ Returns an item without removing it
275275
276276
##### position?
277277
278-
`"front"` | `"back"`
278+
`QueuePosition`
279279
280280
#### Returns
281281

docs/guides/async-queueing.md

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,9 @@ stack.addItem(async () => 'second') // [first, second]
152152

153153
#### Priority Queue
154154

155-
Priority queues process tasks based on their assigned priority values, ensuring important tasks are handled first:
155+
Priority queues process tasks based on their assigned priority values, ensuring important tasks are handled first. There are two ways to specify priorities:
156156

157+
1. Static priority values attached to tasks:
157158
```ts
158159
const priorityQueue = new AsyncQueuer<string>({
159160
concurrency: 2
@@ -182,6 +183,36 @@ priorityQueue.addItem(mediumPriorityTask)
182183
// Processes: high and medium concurrently, then low
183184
```
184185

186+
2. Dynamic priority calculation using `getPriority` option:
187+
```ts
188+
const dynamicPriorityQueue = new AsyncQueuer<string>({
189+
concurrency: 2,
190+
getPriority: (task) => {
191+
// Calculate priority based on task properties or other factors
192+
// Higher numbers have priority
193+
return calculateTaskPriority(task)
194+
}
195+
})
196+
197+
// Add tasks - priority will be calculated dynamically
198+
dynamicPriorityQueue.addItem(async () => {
199+
const result = await processTask('low')
200+
return result
201+
})
202+
203+
dynamicPriorityQueue.addItem(async () => {
204+
const result = await processTask('high')
205+
return result
206+
})
207+
```
208+
209+
Priority queues are essential when:
210+
- Tasks have different importance levels
211+
- Critical operations need to run first
212+
- You need flexible task ordering based on priority
213+
- Resource allocation should favor important tasks
214+
- Priority needs to be determined dynamically based on task properties or external factors
215+
185216
### Error Handling
186217

187218
The AsyncQueuer provides comprehensive error handling capabilities to ensure robust task processing. You can handle errors at both the queue level and individual task level:

docs/reference/classes/asyncqueuer.md

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ title: AsyncQueuer
77

88
# Class: AsyncQueuer\<TValue\>
99

10-
Defined in: [async-queuer.ts:94](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-queuer.ts#L94)
10+
Defined in: [async-queuer.ts:95](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-queuer.ts#L95)
1111

1212
A flexible asynchronous queuer that processes tasks with configurable concurrency control.
1313

@@ -50,7 +50,7 @@ queuer.onSuccess((result) => {
5050
new AsyncQueuer<TValue>(initialOptions): AsyncQueuer<TValue>
5151
```
5252

53-
Defined in: [async-queuer.ts:105](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-queuer.ts#L105)
53+
Defined in: [async-queuer.ts:106](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-queuer.ts#L106)
5454

5555
#### Parameters
5656

@@ -70,7 +70,7 @@ Defined in: [async-queuer.ts:105](https://github.com/TanStack/pacer/blob/main/pa
7070
protected options: Required<AsyncQueuerOptions<TValue>>;
7171
```
7272

73-
Defined in: [async-queuer.ts:95](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-queuer.ts#L95)
73+
Defined in: [async-queuer.ts:96](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-queuer.ts#L96)
7474

7575
## Methods
7676

@@ -83,15 +83,15 @@ addItem(
8383
runOnUpdate): Promise<TValue>
8484
```
8585

86-
Defined in: [async-queuer.ts:183](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-queuer.ts#L183)
86+
Defined in: [async-queuer.ts:184](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-queuer.ts#L184)
8787

8888
Adds a task to the queuer
8989

9090
#### Parameters
9191

9292
##### fn
9393

94-
() => `Promise`\<`TValue`\>
94+
() => `Promise`\<`TValue`\> & `object`
9595

9696
##### position
9797

@@ -113,7 +113,7 @@ Adds a task to the queuer
113113
clear(): void
114114
```
115115

116-
Defined in: [async-queuer.ts:294](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-queuer.ts#L294)
116+
Defined in: [async-queuer.ts:305](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-queuer.ts#L305)
117117

118118
Removes all items from the queuer
119119

@@ -129,7 +129,7 @@ Removes all items from the queuer
129129
getActiveItems(): () => Promise<TValue>[]
130130
```
131131

132-
Defined in: [async-queuer.ts:328](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-queuer.ts#L328)
132+
Defined in: [async-queuer.ts:339](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-queuer.ts#L339)
133133

134134
Returns the active items
135135

@@ -145,7 +145,7 @@ Returns the active items
145145
getAllItems(): () => Promise<TValue>[]
146146
```
147147

148-
Defined in: [async-queuer.ts:314](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-queuer.ts#L314)
148+
Defined in: [async-queuer.ts:325](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-queuer.ts#L325)
149149

150150
Returns a copy of all items in the queuer
151151

@@ -161,7 +161,7 @@ Returns a copy of all items in the queuer
161161
getExecutionCount(): number
162162
```
163163

164-
Defined in: [async-queuer.ts:321](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-queuer.ts#L321)
164+
Defined in: [async-queuer.ts:332](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-queuer.ts#L332)
165165

166166
Returns the number of items that have been removed from the queuer
167167

@@ -177,7 +177,7 @@ Returns the number of items that have been removed from the queuer
177177
getNextItem(position): undefined | () => Promise<TValue>
178178
```
179179

180-
Defined in: [async-queuer.ts:239](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-queuer.ts#L239)
180+
Defined in: [async-queuer.ts:252](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-queuer.ts#L252)
181181

182182
Removes and returns an item from the queuer
183183

@@ -199,7 +199,7 @@ Removes and returns an item from the queuer
199199
getPendingItems(): () => Promise<TValue>[]
200200
```
201201

202-
Defined in: [async-queuer.ts:335](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-queuer.ts#L335)
202+
Defined in: [async-queuer.ts:346](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-queuer.ts#L346)
203203

204204
Returns the pending items
205205

@@ -215,7 +215,7 @@ Returns the pending items
215215
isEmpty(): boolean
216216
```
217217

218-
Defined in: [async-queuer.ts:273](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-queuer.ts#L273)
218+
Defined in: [async-queuer.ts:284](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-queuer.ts#L284)
219219

220220
Returns true if the queuer is empty
221221

@@ -231,7 +231,7 @@ Returns true if the queuer is empty
231231
isFull(): boolean
232232
```
233233

234-
Defined in: [async-queuer.ts:280](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-queuer.ts#L280)
234+
Defined in: [async-queuer.ts:291](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-queuer.ts#L291)
235235

236236
Returns true if the queuer is full
237237

@@ -247,7 +247,7 @@ Returns true if the queuer is full
247247
isIdle(): boolean
248248
```
249249

250-
Defined in: [async-queuer.ts:411](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-queuer.ts#L411)
250+
Defined in: [async-queuer.ts:422](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-queuer.ts#L422)
251251

252252
Returns true if the queuer is running but has no items to process
253253

@@ -263,7 +263,7 @@ Returns true if the queuer is running but has no items to process
263263
isRunning(): boolean
264264
```
265265

266-
Defined in: [async-queuer.ts:404](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-queuer.ts#L404)
266+
Defined in: [async-queuer.ts:415](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-queuer.ts#L415)
267267

268268
Returns true if the queuer is running
269269

@@ -279,7 +279,7 @@ Returns true if the queuer is running
279279
onError(cb): () => void
280280
```
281281

282-
Defined in: [async-queuer.ts:352](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-queuer.ts#L352)
282+
Defined in: [async-queuer.ts:363](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-queuer.ts#L363)
283283

284284
Adds a callback to be called when a task errors
285285

@@ -305,7 +305,7 @@ Adds a callback to be called when a task errors
305305
onSettled(cb): () => void
306306
```
307307

308-
Defined in: [async-queuer.ts:362](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-queuer.ts#L362)
308+
Defined in: [async-queuer.ts:373](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-queuer.ts#L373)
309309

310310
Adds a callback to be called when a task is settled
311311

@@ -331,7 +331,7 @@ Adds a callback to be called when a task is settled
331331
onSuccess(cb): () => void
332332
```
333333

334-
Defined in: [async-queuer.ts:342](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-queuer.ts#L342)
334+
Defined in: [async-queuer.ts:353](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-queuer.ts#L353)
335335

336336
Adds a callback to be called when a task succeeds
337337

@@ -357,15 +357,15 @@ Adds a callback to be called when a task succeeds
357357
peek(position): undefined | () => Promise<TValue>
358358
```
359359

360-
Defined in: [async-queuer.ts:261](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-queuer.ts#L261)
360+
Defined in: [async-queuer.ts:274](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-queuer.ts#L274)
361361

362362
Returns an item without removing it
363363

364364
#### Parameters
365365

366366
##### position
367367

368-
`"front"` | `"back"`
368+
[`QueuePosition`](../type-aliases/queueposition.md) = `'front'`
369369

370370
#### Returns
371371

@@ -379,7 +379,7 @@ Returns an item without removing it
379379
reset(withInitialItems?): void
380380
```
381381
382-
Defined in: [async-queuer.ts:302](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-queuer.ts#L302)
382+
Defined in: [async-queuer.ts:313](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-queuer.ts#L313)
383383
384384
Resets the queuer to its initial state
385385
@@ -401,7 +401,7 @@ Resets the queuer to its initial state
401401
setOptions(newOptions): AsyncQueuerOptions<TValue>
402402
```
403403
404-
Defined in: [async-queuer.ts:120](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-queuer.ts#L120)
404+
Defined in: [async-queuer.ts:121](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-queuer.ts#L121)
405405
406406
Updates the queuer options
407407
Returns the new options state
@@ -424,7 +424,7 @@ Returns the new options state
424424
size(): number
425425
```
426426
427-
Defined in: [async-queuer.ts:287](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-queuer.ts#L287)
427+
Defined in: [async-queuer.ts:298](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-queuer.ts#L298)
428428
429429
Returns the current size of the queuer
430430
@@ -440,7 +440,7 @@ Returns the current size of the queuer
440440
start(): Promise<void>
441441
```
442442
443-
Defined in: [async-queuer.ts:372](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-queuer.ts#L372)
443+
Defined in: [async-queuer.ts:383](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-queuer.ts#L383)
444444
445445
Starts the queuer and processes items
446446
@@ -456,7 +456,7 @@ Starts the queuer and processes items
456456
stop(): void
457457
```
458458
459-
Defined in: [async-queuer.ts:395](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-queuer.ts#L395)
459+
Defined in: [async-queuer.ts:406](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-queuer.ts#L406)
460460
461461
Stops the queuer from processing items
462462
@@ -472,7 +472,7 @@ Stops the queuer from processing items
472472
protected tick(): void
473473
```
474474
475-
Defined in: [async-queuer.ts:130](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-queuer.ts#L130)
475+
Defined in: [async-queuer.ts:131](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-queuer.ts#L131)
476476
477477
Processes items in the queuer
478478

docs/reference/functions/asyncqueue.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ title: asyncQueue
1111
function asyncQueue<TValue>(options): (fn, position, runOnUpdate) => Promise<TValue>
1212
```
1313

14-
Defined in: [async-queuer.ts:433](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-queuer.ts#L433)
14+
Defined in: [async-queuer.ts:444](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-queuer.ts#L444)
1515

1616
Creates a new AsyncQueuer instance with the given options and returns a bound addItem function.
1717
The queuer is automatically started and ready to process items.
@@ -40,7 +40,7 @@ Adds a task to the queuer
4040
4141
#### fn
4242
43-
() => `Promise`\<`TValue`\>
43+
() => `Promise`\<`TValue`\> & `object`
4444
4545
#### position
4646

docs/reference/interfaces/asyncqueueroptions.md

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,11 @@ Default position to get items from during processing
6969
optional getPriority: (item) => number;
7070
```
7171

72-
Defined in: [async-queuer.ts:22](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-queuer.ts#L22)
72+
Defined in: [async-queuer.ts:23](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-queuer.ts#L23)
7373

7474
Function to determine priority of items in the queuer
7575
Higher priority items will be processed first
76+
If not provided, will use static priority values attached to tasks
7677

7778
#### Parameters
7879

@@ -89,17 +90,13 @@ Higher priority items will be processed first
8990
### initialItems?
9091

9192
```ts
92-
optional initialItems: () => Promise<TValue>[];
93+
optional initialItems: () => Promise<TValue> & object[];
9394
```
9495

95-
Defined in: [async-queuer.ts:26](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-queuer.ts#L26)
96+
Defined in: [async-queuer.ts:27](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-queuer.ts#L27)
9697

9798
Initial items to populate the queuer with
9899

99-
#### Returns
100-
101-
`Promise`\<`TValue`\>
102-
103100
***
104101

105102
### maxSize?
@@ -108,7 +105,7 @@ Initial items to populate the queuer with
108105
optional maxSize: number;
109106
```
110107

111-
Defined in: [async-queuer.ts:30](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-queuer.ts#L30)
108+
Defined in: [async-queuer.ts:31](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-queuer.ts#L31)
112109

113110
Maximum number of items allowed in the queuer
114111

@@ -120,7 +117,7 @@ Maximum number of items allowed in the queuer
120117
optional onGetNextItem: (item, queuer) => void;
121118
```
122119

123-
Defined in: [async-queuer.ts:34](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-queuer.ts#L34)
120+
Defined in: [async-queuer.ts:35](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-queuer.ts#L35)
124121

125122
Callback fired whenever an item is removed from the queuer
126123

@@ -146,7 +143,7 @@ Callback fired whenever an item is removed from the queuer
146143
optional onUpdate: (queuer) => void;
147144
```
148145

149-
Defined in: [async-queuer.ts:41](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-queuer.ts#L41)
146+
Defined in: [async-queuer.ts:42](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-queuer.ts#L42)
150147

151148
Callback fired whenever an item is added or removed from the queuer
152149

@@ -168,7 +165,7 @@ Callback fired whenever an item is added or removed from the queuer
168165
optional started: boolean;
169166
```
170167

171-
Defined in: [async-queuer.ts:45](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-queuer.ts#L45)
168+
Defined in: [async-queuer.ts:46](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-queuer.ts#L46)
172169

173170
Whether the queuer should start processing tasks immediately
174171

@@ -180,6 +177,6 @@ Whether the queuer should start processing tasks immediately
180177
optional wait: number;
181178
```
182179

183-
Defined in: [async-queuer.ts:49](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-queuer.ts#L49)
180+
Defined in: [async-queuer.ts:50](https://github.com/TanStack/pacer/blob/main/packages/pacer/src/async-queuer.ts#L50)
184181

185182
Time in milliseconds to wait between processing items

0 commit comments

Comments
 (0)