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