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

createQueuer.md

108 lines3.6 KB

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