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

createAsyncQueuer.md

118 lines4.3 KB

1---
2id: createAsyncQueuer
3title: createAsyncQueuer
4---
5​
6```ts
7function createAsyncQueuer<TValue, TSelected>(
8 host,
9 fn,
10 options?,
11selector?): LitAsyncQueuer<TValue, TSelected>;
12```
13​
14Defined in: [async-queuer/createAsyncQueuer.ts:104](https://github.com/TanStack/pacer/blob/main/packages/lit-pacer/src/async-queuer/createAsyncQueuer.ts#L104)
15​
16Creates and retains the AsyncQueuer for its Lit 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(childHost, 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.
58Disconnecting the host 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.
61Reconnecting the host refreshes options and restores subscriptions to the same instance.
62​
63## Type Parameters
64​
65### TValue
66​
67`TValue`
68​
69### TSelected
70​
71`TSelected` = \{
72\}
73​
74## Parameters
75​
76### host
77​
78`ReactiveControllerHost`
79​
80Owner of option updates, subscriptions, and cleanup.
81​
82### fn
83​
84(`item`) => `Promise`\<`any`\>
85​
86Function executed by the utility.
87​
88### options?
89​
90[`LitPacerOptions`](../type-aliases/LitPacerOptions.md)\<[`LitAsyncQueuerOptions`](../interfaces/LitAsyncQueuerOptions.md)\<`TValue`, `TSelected`\>\> = `{}`
91​
92Core options or a reactive factory, plus an optional onUnmount callback.
93​
94### selector?
95​
96(`state`) => `TSelected`
97​
98Selects state that updates the owner. Omit to leave selected state empty.
99​
100## Returns
101​
102[`LitAsyncQueuer`](../interfaces/LitAsyncQueuer.md)\<`TValue`, `TSelected`\>
103​
104The retained utility instance with selected state and child subscriptions.
105​
106## Example
107​
108```ts
109import { createAsyncQueuer } from '@tanstack/lit-pacer'
110​
111const utility = createAsyncQueuer(
112 this, async (value: string) => { console.log(value) },
113 { wait: 100 },
114 (state) => ({ size: state.size }),
116utility.addItem('item')
117// Selected state: utility.state.size
118```