Skip to content
Source
Sign inSign up
TanStack/pacer
Open History to see the last commit

createAsyncQueuer.md

117 lines4.2 KB

1---
2id: createAsyncQueuer
3title: createAsyncQueuer
4---
5​
6```ts
7function createAsyncQueuer<TValue, TSelected>(
8 scope,
9 fn,
10 options?,
11selector?): AlpineAsyncQueuer<TValue, TSelected>;
12```
13​
14Defined in: [async-queuer/createAsyncQueuer.ts:102](https://github.com/TanStack/pacer/blob/main/packages/alpine-pacer/src/async-queuer/createAsyncQueuer.ts#L102)
15​
16Creates and retains the AsyncQueuer for its Alpine owner.
17​
18Retains items until they are processed. Use addItem to enqueue work and start, stop, execute, clear, or flush to control processing. Selected state exposes pending items, capacity, and completed work.
19​
20The callback may return a Promise. Core result, error, retry, and abort behavior is preserved.
21Use onSuccess, onError, and onSettled for execution outcomes.
22​
23## State and subscriptions
24​
25Pass a selector to track only the state consumed by the owner. The default selection is {},
26so utility state changes do not update the owner unless it opts in. Selection uses shallow
27comparison. The raw store remains available for additional subscriptions.
28Use utility.subscribe(childScope, selector) for a child subscription. It returns a getter
29and cleans up with the child without canceling the parent utility.
30​
31Available state fields:
32​
33- `activeItems`: Items currently being processed by the queuer
34- `addItemCount`: Number of times addItem has been called (for reduction calculations)
35- `errorCount`: Number of task executions that have resulted in errors
36- `executionCount`: Number of times execute has been called
37- `expirationCount`: Number of items that have been removed from the queue due to expiration
38- `isEmpty`: Whether the queuer has no items to process (items array is empty)
39- `isExecuting`: Whether the queuer is currently executing
40- `isFull`: Whether the queuer has reached its maximum capacity
41- `isIdle`: Whether the queuer is not currently processing any items
42- `isRunning`: Whether the queuer is active and will process items automatically
43- `items`: Array of items currently waiting to be processed
44- `itemTimestamps`: Timestamps when items were added to the queue for expiration tracking
45- `lastResult`: The result from the most recent task execution
46- `pendingTick`: Whether the queuer has a pending timeout for processing the next item
47- `rejectionCount`: Number of items that have been rejected from being added to the queue
48- `settleCount`: Number of task executions that have completed (either successfully or with errors)
49- `size`: Number of items currently in the queue
50- `status`: Current processing status - 'idle' when not processing, 'running' when active, 'stopped' when paused
51- `successCount`: Number of task executions that have completed successfully
52​
53## Options and ownership
54​
55Pass an options object with property getters or a factory. Top-level properties are read
56reactively; function-valued core options remain callbacks. Local options override provider
57defaults. Updates retain the utility, its store, counters, and pending work.
58Destroying the owning scope calls stop() and abort().
59onUnmount replaces default cleanup and receives the same adapter instance. A custom callback
60must perform all required cleanup. Use flush() where supported to finish pending work.
61​
62## Type Parameters
63​
64### TValue
65​
66`TValue`
67​
68### TSelected
69​
70`TSelected` = \{
71\}
72​
73## Parameters
74​
75### scope
76​
77[`PacerScope`](../interfaces/PacerScope.md)
78​
79Owner of option updates, subscriptions, and cleanup.
80​
81### fn
82​
83(`item`) => `Promise`\<`any`\>
84​
85Function executed by the utility.
86​
87### options?
88​
89[`AlpinePacerOptions`](../type-aliases/AlpinePacerOptions.md)\<[`AlpineAsyncQueuerOptions`](../interfaces/AlpineAsyncQueuerOptions.md)\<`TValue`, `TSelected`\>\> = `{}`
90​
91Core options or a reactive factory, plus an optional onUnmount callback.
92​
93### selector?
94​
95(`state`) => `TSelected`
96​
97Selects state that updates the owner. Omit to leave selected state empty.
98​
99## Returns
100​
101[`AlpineAsyncQueuer`](../interfaces/AlpineAsyncQueuer.md)\<`TValue`, `TSelected`\>
102​
103The retained utility instance with selected state and child subscriptions.
104​
105## Example
106​
107```ts
108import { createAsyncQueuer } from '@tanstack/alpine-pacer'
109​
110const utility = createAsyncQueuer(
111 scope, async (value: string) => { console.log(value) },
112 { wait: 100 },
113 (state) => ({ size: state.size }),
115utility.addItem('item')
116// Selected state: utility.state.size
117```