@tanstack/solid-pacer 0.6.0 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/async-queuer/createAsyncQueuer.cjs +13 -11
- package/dist/cjs/async-queuer/createAsyncQueuer.cjs.map +1 -1
- package/dist/cjs/async-queuer/createAsyncQueuer.d.cts +27 -20
- package/dist/cjs/batcher/createBatcher.cjs +58 -0
- package/dist/cjs/batcher/createBatcher.cjs.map +1 -0
- package/dist/cjs/batcher/createBatcher.d.cts +76 -0
- package/dist/cjs/batcher/index.cjs +12 -0
- package/dist/cjs/batcher/index.cjs.map +1 -0
- package/dist/cjs/batcher/index.d.cts +2 -0
- package/dist/cjs/index.cjs +2 -0
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.cts +1 -0
- package/dist/cjs/queuer/createQueuer.cjs +9 -7
- package/dist/cjs/queuer/createQueuer.cjs.map +1 -1
- package/dist/cjs/queuer/createQueuer.d.cts +32 -26
- package/dist/esm/async-queuer/createAsyncQueuer.d.ts +27 -20
- package/dist/esm/async-queuer/createAsyncQueuer.js +13 -11
- package/dist/esm/async-queuer/createAsyncQueuer.js.map +1 -1
- package/dist/esm/batcher/createBatcher.d.ts +76 -0
- package/dist/esm/batcher/createBatcher.js +58 -0
- package/dist/esm/batcher/createBatcher.js.map +1 -0
- package/dist/esm/batcher/index.d.ts +2 -0
- package/dist/esm/batcher/index.js +6 -0
- package/dist/esm/batcher/index.js.map +1 -0
- package/dist/esm/index.d.ts +1 -0
- package/dist/esm/index.js +2 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/queuer/createQueuer.d.ts +32 -26
- package/dist/esm/queuer/createQueuer.js +9 -7
- package/dist/esm/queuer/createQueuer.js.map +1 -1
- package/package.json +13 -3
- package/src/async-queuer/createAsyncQueuer.ts +51 -44
- package/src/batcher/createBatcher.ts +152 -0
- package/src/batcher/index.ts +3 -0
- package/src/index.ts +3 -0
- package/src/queuer/createQueuer.ts +41 -32
|
@@ -2,36 +2,33 @@ import { AsyncQueuer } from '@tanstack/pacer/async-queuer'
|
|
|
2
2
|
import { createSignal } from 'solid-js'
|
|
3
3
|
import { bindInstanceMethods } from '@tanstack/pacer/utils'
|
|
4
4
|
import type { Accessor } from 'solid-js'
|
|
5
|
-
import type {
|
|
6
|
-
AsyncQueuerFn,
|
|
7
|
-
AsyncQueuerOptions,
|
|
8
|
-
} from '@tanstack/pacer/async-queuer'
|
|
5
|
+
import type { AsyncQueuerOptions } from '@tanstack/pacer/async-queuer'
|
|
9
6
|
|
|
10
|
-
export interface SolidAsyncQueuer<
|
|
7
|
+
export interface SolidAsyncQueuer<TValue>
|
|
11
8
|
extends Omit<
|
|
12
|
-
AsyncQueuer<
|
|
13
|
-
| 'getActiveItems'
|
|
14
|
-
| 'getAllItems'
|
|
9
|
+
AsyncQueuer<TValue>,
|
|
15
10
|
| 'getErrorCount'
|
|
16
11
|
| 'getIsEmpty'
|
|
17
12
|
| 'getIsFull'
|
|
18
13
|
| 'getIsIdle'
|
|
19
14
|
| 'getIsRunning'
|
|
20
|
-
| 'getPeek'
|
|
21
|
-
| 'getPendingItems'
|
|
22
15
|
| 'getRejectionCount'
|
|
23
16
|
| 'getSettledCount'
|
|
24
17
|
| 'getSize'
|
|
25
18
|
| 'getSuccessCount'
|
|
19
|
+
| 'peekActiveItems'
|
|
20
|
+
| 'peekAllItems'
|
|
21
|
+
| 'peekNextItem'
|
|
22
|
+
| 'peekPendingItems'
|
|
26
23
|
> {
|
|
27
24
|
/**
|
|
28
|
-
* Signal version of `
|
|
25
|
+
* Signal version of `peekActiveItems`
|
|
29
26
|
*/
|
|
30
|
-
activeItems: Accessor<Array<
|
|
27
|
+
activeItems: Accessor<Array<TValue>>
|
|
31
28
|
/**
|
|
32
|
-
* Signal version of `
|
|
29
|
+
* Signal version of `peekAllItems`
|
|
33
30
|
*/
|
|
34
|
-
allItems: Accessor<Array<
|
|
31
|
+
allItems: Accessor<Array<TValue>>
|
|
35
32
|
/**
|
|
36
33
|
* Signal version of `getErrorCount`
|
|
37
34
|
*/
|
|
@@ -53,13 +50,13 @@ export interface SolidAsyncQueuer<TFn extends AsyncQueuerFn>
|
|
|
53
50
|
*/
|
|
54
51
|
isRunning: Accessor<boolean>
|
|
55
52
|
/**
|
|
56
|
-
* Signal version of `
|
|
53
|
+
* Signal version of `peekNextItem`
|
|
57
54
|
*/
|
|
58
|
-
|
|
55
|
+
nextItem: Accessor<TValue | undefined>
|
|
59
56
|
/**
|
|
60
|
-
* Signal version of `
|
|
57
|
+
* Signal version of `peekPendingItems`
|
|
61
58
|
*/
|
|
62
|
-
pendingItems: Accessor<Array<
|
|
59
|
+
pendingItems: Accessor<Array<TValue>>
|
|
63
60
|
/**
|
|
64
61
|
* Signal version of `getRejectionCount`
|
|
65
62
|
*/
|
|
@@ -79,31 +76,35 @@ export interface SolidAsyncQueuer<TFn extends AsyncQueuerFn>
|
|
|
79
76
|
}
|
|
80
77
|
|
|
81
78
|
/**
|
|
82
|
-
*
|
|
79
|
+
* Creates a Solid-compatible AsyncQueuer instance for managing an asynchronous queue of items, exposing Solid signals for all stateful properties.
|
|
83
80
|
*
|
|
84
81
|
* Features:
|
|
85
|
-
* - Priority
|
|
82
|
+
* - Priority queueing via `getPriority` or item `priority` property
|
|
86
83
|
* - Configurable concurrency limit
|
|
87
|
-
* - Task success/error/completion callbacks
|
|
88
84
|
* - FIFO (First In First Out) or LIFO (Last In First Out) queue behavior
|
|
89
|
-
* - Pause/resume
|
|
85
|
+
* - Pause/resume processing
|
|
90
86
|
* - Task cancellation
|
|
91
|
-
* - Item expiration
|
|
87
|
+
* - Item expiration
|
|
88
|
+
* - Lifecycle callbacks for success, error, settled, items change, etc.
|
|
89
|
+
* - All stateful properties (active items, pending items, counts, etc.) are exposed as Solid signals for reactivity
|
|
92
90
|
*
|
|
93
91
|
* Tasks are processed concurrently up to the configured concurrency limit. When a task completes,
|
|
94
|
-
* the next pending task is processed if
|
|
92
|
+
* the next pending task is processed if the concurrency limit allows.
|
|
95
93
|
*
|
|
96
94
|
* Error Handling:
|
|
97
95
|
* - If an `onError` handler is provided, it will be called with the error and queuer instance
|
|
98
96
|
* - If `throwOnError` is true (default when no onError handler is provided), the error will be thrown
|
|
99
97
|
* - If `throwOnError` is false (default when onError handler is provided), the error will be swallowed
|
|
100
|
-
* - Both onError and throwOnError can be used together
|
|
98
|
+
* - Both onError and throwOnError can be used together; the handler will be called before any error is thrown
|
|
101
99
|
* - The error state can be checked using the underlying AsyncQueuer instance
|
|
102
100
|
*
|
|
103
|
-
*
|
|
101
|
+
* Example usage:
|
|
104
102
|
* ```tsx
|
|
105
103
|
* // Basic async queuer for API requests
|
|
106
|
-
* const asyncQueuer = createAsyncQueuer({
|
|
104
|
+
* const asyncQueuer = createAsyncQueuer(async (item) => {
|
|
105
|
+
* // process item
|
|
106
|
+
* return await fetchData(item);
|
|
107
|
+
* }, {
|
|
107
108
|
* initialItems: [],
|
|
108
109
|
* concurrency: 2,
|
|
109
110
|
* maxSize: 100,
|
|
@@ -121,12 +122,16 @@ export interface SolidAsyncQueuer<TFn extends AsyncQueuerFn>
|
|
|
121
122
|
*
|
|
122
123
|
* // Start processing
|
|
123
124
|
* asyncQueuer.start();
|
|
125
|
+
*
|
|
126
|
+
* // Use Solid signals in your UI
|
|
127
|
+
* const pending = asyncQueuer.pendingItems();
|
|
124
128
|
* ```
|
|
125
129
|
*/
|
|
126
|
-
export function createAsyncQueuer<
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
+
export function createAsyncQueuer<TValue>(
|
|
131
|
+
fn: (value: TValue) => Promise<any>,
|
|
132
|
+
initialOptions: AsyncQueuerOptions<TValue> = {},
|
|
133
|
+
): SolidAsyncQueuer<TValue> {
|
|
134
|
+
const asyncQueuer = new AsyncQueuer<TValue>(fn, initialOptions)
|
|
130
135
|
|
|
131
136
|
const [successCount, setSuccessCount] = createSignal(
|
|
132
137
|
asyncQueuer.getSuccessCount(),
|
|
@@ -142,28 +147,30 @@ export function createAsyncQueuer<TFn extends AsyncQueuerFn>(
|
|
|
142
147
|
const [isFull, setIsFull] = createSignal(asyncQueuer.getIsFull())
|
|
143
148
|
const [isIdle, setIsIdle] = createSignal(asyncQueuer.getIsIdle())
|
|
144
149
|
const [isRunning, setIsRunning] = createSignal(asyncQueuer.getIsRunning())
|
|
145
|
-
const [allItems, setAllItems] = createSignal<Array<
|
|
146
|
-
asyncQueuer.
|
|
150
|
+
const [allItems, setAllItems] = createSignal<Array<TValue>>(
|
|
151
|
+
asyncQueuer.peekAllItems(),
|
|
152
|
+
)
|
|
153
|
+
const [activeItems, setActiveItems] = createSignal<Array<TValue>>(
|
|
154
|
+
asyncQueuer.peekActiveItems(),
|
|
147
155
|
)
|
|
148
|
-
const [
|
|
149
|
-
asyncQueuer.
|
|
156
|
+
const [pendingItems, setPendingItems] = createSignal<Array<TValue>>(
|
|
157
|
+
asyncQueuer.peekPendingItems(),
|
|
150
158
|
)
|
|
151
|
-
const [
|
|
152
|
-
asyncQueuer.
|
|
159
|
+
const [nextItem, setNextItem] = createSignal<TValue | undefined>(
|
|
160
|
+
asyncQueuer.peekNextItem(),
|
|
153
161
|
)
|
|
154
|
-
const [peek, setPeek] = createSignal<TFn | undefined>(asyncQueuer.getPeek())
|
|
155
162
|
const [size, setSize] = createSignal(asyncQueuer.getSize())
|
|
156
163
|
|
|
157
164
|
asyncQueuer.setOptions({
|
|
158
165
|
onItemsChange: (queuer) => {
|
|
159
|
-
setActiveItems(queuer.
|
|
160
|
-
setAllItems(queuer.
|
|
166
|
+
setActiveItems(queuer.peekActiveItems())
|
|
167
|
+
setAllItems(queuer.peekAllItems())
|
|
161
168
|
setErrorCount(queuer.getErrorCount())
|
|
162
169
|
setIsEmpty(queuer.getIsEmpty())
|
|
163
170
|
setIsFull(queuer.getIsFull())
|
|
164
171
|
setIsIdle(queuer.getIsIdle())
|
|
165
|
-
|
|
166
|
-
setPendingItems(queuer.
|
|
172
|
+
setNextItem(() => queuer.peekNextItem())
|
|
173
|
+
setPendingItems(queuer.peekPendingItems())
|
|
167
174
|
setRejectionCount(queuer.getRejectionCount())
|
|
168
175
|
setSettledCount(queuer.getSettledCount())
|
|
169
176
|
setSize(queuer.getSize())
|
|
@@ -190,11 +197,11 @@ export function createAsyncQueuer<TFn extends AsyncQueuerFn>(
|
|
|
190
197
|
isFull,
|
|
191
198
|
isIdle,
|
|
192
199
|
isRunning,
|
|
193
|
-
|
|
200
|
+
nextItem,
|
|
194
201
|
pendingItems,
|
|
195
202
|
rejectionCount,
|
|
196
203
|
settledCount,
|
|
197
204
|
size,
|
|
198
205
|
successCount,
|
|
199
|
-
} as SolidAsyncQueuer<
|
|
206
|
+
} as SolidAsyncQueuer<TValue>
|
|
200
207
|
}
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import { Batcher } from '@tanstack/pacer/batcher'
|
|
2
|
+
import { createSignal } from 'solid-js'
|
|
3
|
+
import { bindInstanceMethods } from '@tanstack/pacer/utils'
|
|
4
|
+
import type { Accessor } from 'solid-js'
|
|
5
|
+
import type { BatcherOptions } from '@tanstack/pacer/batcher'
|
|
6
|
+
|
|
7
|
+
export interface SolidBatcher<TValue>
|
|
8
|
+
extends Omit<
|
|
9
|
+
Batcher<TValue>,
|
|
10
|
+
| 'peekAllItems'
|
|
11
|
+
| 'getBatchExecutionCount'
|
|
12
|
+
| 'getIsEmpty'
|
|
13
|
+
| 'getIsRunning'
|
|
14
|
+
| 'getItemExecutionCount'
|
|
15
|
+
| 'getSize'
|
|
16
|
+
> {
|
|
17
|
+
/**
|
|
18
|
+
* Signal version of `peekAllItems`
|
|
19
|
+
*/
|
|
20
|
+
allItems: Accessor<Array<TValue>>
|
|
21
|
+
/**
|
|
22
|
+
* Signal version of `getBatchExecutionCount`
|
|
23
|
+
*/
|
|
24
|
+
batchExecutionCount: Accessor<number>
|
|
25
|
+
/**
|
|
26
|
+
* Signal version of `getIsEmpty`
|
|
27
|
+
*/
|
|
28
|
+
isEmpty: Accessor<boolean>
|
|
29
|
+
/**
|
|
30
|
+
* Signal version of `getIsRunning`
|
|
31
|
+
*/
|
|
32
|
+
isRunning: Accessor<boolean>
|
|
33
|
+
/**
|
|
34
|
+
* Signal version of `getItemExecutionCount`
|
|
35
|
+
*/
|
|
36
|
+
itemExecutionCount: Accessor<number>
|
|
37
|
+
/**
|
|
38
|
+
* Signal version of `getSize`
|
|
39
|
+
*/
|
|
40
|
+
size: Accessor<number>
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Creates a Solid-compatible Batcher instance for managing batches of items, exposing Solid signals for all stateful properties.
|
|
45
|
+
*
|
|
46
|
+
* Features:
|
|
47
|
+
* - Batch processing of items using the provided `fn` function
|
|
48
|
+
* - Configurable batch size and wait time
|
|
49
|
+
* - Custom batch processing logic via getShouldExecute
|
|
50
|
+
* - Event callbacks for monitoring batch operations
|
|
51
|
+
* - All stateful properties (items, counts, etc.) are exposed as Solid signals for reactivity
|
|
52
|
+
*
|
|
53
|
+
* The batcher collects items and processes them in batches based on:
|
|
54
|
+
* - Maximum batch size
|
|
55
|
+
* - Time-based batching (process after X milliseconds)
|
|
56
|
+
* - Custom batch processing logic via getShouldExecute
|
|
57
|
+
*
|
|
58
|
+
* Example usage:
|
|
59
|
+
* ```tsx
|
|
60
|
+
* const batcher = createBatcher(
|
|
61
|
+
* (items) => {
|
|
62
|
+
* // Process batch of items
|
|
63
|
+
* console.log('Processing batch:', items);
|
|
64
|
+
* },
|
|
65
|
+
* {
|
|
66
|
+
* maxSize: 5,
|
|
67
|
+
* wait: 2000,
|
|
68
|
+
* onExecute: (batcher) => console.log('Batch executed'),
|
|
69
|
+
* getShouldExecute: (items) => items.length >= 3
|
|
70
|
+
* }
|
|
71
|
+
* );
|
|
72
|
+
*
|
|
73
|
+
* // Add items to batch
|
|
74
|
+
* batcher.addItem('task1');
|
|
75
|
+
* batcher.addItem('task2');
|
|
76
|
+
*
|
|
77
|
+
* // Control the batcher
|
|
78
|
+
* batcher.stop(); // Pause processing
|
|
79
|
+
* batcher.start(); // Resume processing
|
|
80
|
+
*
|
|
81
|
+
* // Access batcher state via signals
|
|
82
|
+
* console.log('Items:', batcher.allItems());
|
|
83
|
+
* console.log('Size:', batcher.size());
|
|
84
|
+
* console.log('Is empty:', batcher.isEmpty());
|
|
85
|
+
* console.log('Is running:', batcher.isRunning());
|
|
86
|
+
* console.log('Batch count:', batcher.batchExecutionCount());
|
|
87
|
+
* console.log('Item count:', batcher.itemExecutionCount());
|
|
88
|
+
* ```
|
|
89
|
+
*/
|
|
90
|
+
export function createBatcher<TValue>(
|
|
91
|
+
fn: (items: Array<TValue>) => void,
|
|
92
|
+
initialOptions: BatcherOptions<TValue> = {},
|
|
93
|
+
): SolidBatcher<TValue> {
|
|
94
|
+
const batcher = bindInstanceMethods(new Batcher<TValue>(fn, initialOptions))
|
|
95
|
+
|
|
96
|
+
const [allItems, setAllItems] = createSignal<Array<TValue>>(
|
|
97
|
+
batcher.peekAllItems(),
|
|
98
|
+
)
|
|
99
|
+
const [batchExecutionCount, setBatchExecutionCount] = createSignal(
|
|
100
|
+
batcher.getBatchExecutionCount(),
|
|
101
|
+
)
|
|
102
|
+
const [itemExecutionCount, setItemExecutionCount] = createSignal(
|
|
103
|
+
batcher.getItemExecutionCount(),
|
|
104
|
+
)
|
|
105
|
+
const [isEmpty, setIsEmpty] = createSignal(batcher.getIsEmpty())
|
|
106
|
+
const [isRunning, setIsRunning] = createSignal(batcher.getIsRunning())
|
|
107
|
+
const [size, setSize] = createSignal(batcher.getSize())
|
|
108
|
+
|
|
109
|
+
function setOptions(newOptions: Partial<BatcherOptions<TValue>>) {
|
|
110
|
+
batcher.setOptions({
|
|
111
|
+
...newOptions,
|
|
112
|
+
onItemsChange: (batcher) => {
|
|
113
|
+
setAllItems(batcher.peekAllItems())
|
|
114
|
+
setBatchExecutionCount(batcher.getBatchExecutionCount())
|
|
115
|
+
setItemExecutionCount(batcher.getItemExecutionCount())
|
|
116
|
+
setIsEmpty(batcher.getIsEmpty())
|
|
117
|
+
setSize(batcher.getSize())
|
|
118
|
+
|
|
119
|
+
const onItemsChange =
|
|
120
|
+
newOptions.onItemsChange ?? initialOptions.onItemsChange
|
|
121
|
+
onItemsChange?.(batcher)
|
|
122
|
+
},
|
|
123
|
+
onExecute: (batcher) => {
|
|
124
|
+
setBatchExecutionCount(batcher.getBatchExecutionCount())
|
|
125
|
+
setItemExecutionCount(batcher.getItemExecutionCount())
|
|
126
|
+
|
|
127
|
+
const onExecute = newOptions.onExecute ?? initialOptions.onExecute
|
|
128
|
+
onExecute?.(batcher)
|
|
129
|
+
},
|
|
130
|
+
onIsRunningChange: (batcher) => {
|
|
131
|
+
setIsRunning(batcher.getIsRunning())
|
|
132
|
+
|
|
133
|
+
const onIsRunningChange =
|
|
134
|
+
newOptions.onIsRunningChange ?? initialOptions.onIsRunningChange
|
|
135
|
+
onIsRunningChange?.(batcher)
|
|
136
|
+
},
|
|
137
|
+
})
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
setOptions(initialOptions)
|
|
141
|
+
|
|
142
|
+
return {
|
|
143
|
+
...batcher,
|
|
144
|
+
allItems,
|
|
145
|
+
batchExecutionCount,
|
|
146
|
+
isEmpty,
|
|
147
|
+
isRunning,
|
|
148
|
+
itemExecutionCount,
|
|
149
|
+
size,
|
|
150
|
+
setOptions,
|
|
151
|
+
} as SolidBatcher<TValue>
|
|
152
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -17,6 +17,9 @@ export * from './async-rate-limiter/createAsyncRateLimiter'
|
|
|
17
17
|
// async-throttler
|
|
18
18
|
export * from './async-throttler/createAsyncThrottler'
|
|
19
19
|
|
|
20
|
+
// batcher
|
|
21
|
+
export * from './batcher/createBatcher'
|
|
22
|
+
|
|
20
23
|
// debouncer
|
|
21
24
|
export * from './debouncer/createDebouncedSignal'
|
|
22
25
|
export * from './debouncer/createDebouncedValue'
|
|
@@ -7,17 +7,17 @@ import type { QueuerOptions } from '@tanstack/pacer/queuer'
|
|
|
7
7
|
export interface SolidQueuer<TValue>
|
|
8
8
|
extends Omit<
|
|
9
9
|
Queuer<TValue>,
|
|
10
|
-
| 'getAllItems'
|
|
11
10
|
| 'getExecutionCount'
|
|
12
11
|
| 'getIsEmpty'
|
|
13
12
|
| 'getIsFull'
|
|
14
13
|
| 'getIsIdle'
|
|
15
14
|
| 'getIsRunning'
|
|
16
|
-
| 'getPeek'
|
|
17
15
|
| 'getSize'
|
|
16
|
+
| 'peekAllItems'
|
|
17
|
+
| 'peekNextItem'
|
|
18
18
|
> {
|
|
19
19
|
/**
|
|
20
|
-
* Signal version of `
|
|
20
|
+
* Signal version of `peekAllItems`
|
|
21
21
|
*/
|
|
22
22
|
allItems: Accessor<Array<TValue>>
|
|
23
23
|
/**
|
|
@@ -41,9 +41,9 @@ export interface SolidQueuer<TValue>
|
|
|
41
41
|
*/
|
|
42
42
|
isRunning: Accessor<boolean>
|
|
43
43
|
/**
|
|
44
|
-
* Signal version of `
|
|
44
|
+
* Signal version of `peekNextItem`
|
|
45
45
|
*/
|
|
46
|
-
|
|
46
|
+
nextItem: Accessor<TValue | undefined>
|
|
47
47
|
/**
|
|
48
48
|
* Signal version of `getRejectionCount`
|
|
49
49
|
*/
|
|
@@ -55,33 +55,39 @@ export interface SolidQueuer<TValue>
|
|
|
55
55
|
}
|
|
56
56
|
|
|
57
57
|
/**
|
|
58
|
-
*
|
|
58
|
+
* Creates a Solid-compatible Queuer instance for managing a synchronous queue of items, exposing Solid signals for all stateful properties.
|
|
59
59
|
*
|
|
60
|
-
*
|
|
61
|
-
*
|
|
62
|
-
*
|
|
60
|
+
* Features:
|
|
61
|
+
* - Synchronous processing of items using the provided `fn` function
|
|
62
|
+
* - FIFO (First In First Out) or LIFO (Last In First Out) queue behavior
|
|
63
|
+
* - Priority queueing via `getPriority` or item `priority` property
|
|
64
|
+
* - Item expiration and removal of stale items
|
|
65
|
+
* - Configurable wait time between processing items
|
|
66
|
+
* - Pause/resume processing
|
|
67
|
+
* - Callbacks for queue state changes, execution, rejection, and expiration
|
|
68
|
+
* - All stateful properties (items, counts, etc.) are exposed as Solid signals for reactivity
|
|
63
69
|
*
|
|
64
|
-
*
|
|
70
|
+
* The queue processes items synchronously in order, with optional delays between each item. When started, it will process one item per tick, with an optional wait time between ticks. You can pause and resume processing with `stop()` and `start()`.
|
|
65
71
|
*
|
|
66
|
-
*
|
|
67
|
-
* synchronously in order, with optional delays between processing each item. The queuer includes
|
|
68
|
-
* an internal tick mechanism that can be started and stopped, making it useful as a scheduler.
|
|
69
|
-
* When started, it will process one item per tick, with an optional wait time between ticks.
|
|
72
|
+
* By default, the queue uses FIFO behavior, but you can configure LIFO or double-ended queueing by specifying the position when adding or removing items.
|
|
70
73
|
*
|
|
71
|
-
*
|
|
72
|
-
* (Last In First Out) by specifying 'front' position when adding items.
|
|
73
|
-
*
|
|
74
|
-
* @example
|
|
74
|
+
* Example usage:
|
|
75
75
|
* ```tsx
|
|
76
|
-
* // Example with
|
|
76
|
+
* // Example with Solid signals and scheduling
|
|
77
77
|
* const [items, setItems] = createSignal([]);
|
|
78
78
|
*
|
|
79
|
-
* const queue = createQueuer(
|
|
80
|
-
*
|
|
81
|
-
*
|
|
82
|
-
*
|
|
83
|
-
*
|
|
84
|
-
*
|
|
79
|
+
* const queue = createQueuer(
|
|
80
|
+
* (item) => {
|
|
81
|
+
* // process item synchronously
|
|
82
|
+
* console.log('Processing', item);
|
|
83
|
+
* },
|
|
84
|
+
* {
|
|
85
|
+
* started: true, // Start processing immediately
|
|
86
|
+
* wait: 1000, // Process one item every second
|
|
87
|
+
* onItemsChange: (queue) => setItems(queue.peekAllItems()),
|
|
88
|
+
* getPriority: (item) => item.priority // Process higher priority items first
|
|
89
|
+
* }
|
|
90
|
+
* );
|
|
85
91
|
*
|
|
86
92
|
* // Add items to process - they'll be handled automatically
|
|
87
93
|
* queue.addItem('task1');
|
|
@@ -96,16 +102,17 @@ export interface SolidQueuer<TValue>
|
|
|
96
102
|
* console.log('Size:', queue.size());
|
|
97
103
|
* console.log('Is empty:', queue.isEmpty());
|
|
98
104
|
* console.log('Is running:', queue.isRunning());
|
|
99
|
-
* console.log('Next item:', queue.
|
|
105
|
+
* console.log('Next item:', queue.nextItem());
|
|
100
106
|
* ```
|
|
101
107
|
*/
|
|
102
108
|
export function createQueuer<TValue>(
|
|
109
|
+
fn: (item: TValue) => void,
|
|
103
110
|
initialOptions: QueuerOptions<TValue> = {},
|
|
104
111
|
): SolidQueuer<TValue> {
|
|
105
|
-
const queuer = bindInstanceMethods(new Queuer<TValue>(initialOptions))
|
|
112
|
+
const queuer = bindInstanceMethods(new Queuer<TValue>(fn, initialOptions))
|
|
106
113
|
|
|
107
114
|
const [allItems, setAllItems] = createSignal<Array<TValue>>(
|
|
108
|
-
queuer.
|
|
115
|
+
queuer.peekAllItems(),
|
|
109
116
|
)
|
|
110
117
|
const [executionCount, setExecutionCount] = createSignal(
|
|
111
118
|
queuer.getExecutionCount(),
|
|
@@ -117,19 +124,21 @@ export function createQueuer<TValue>(
|
|
|
117
124
|
const [isFull, setIsFull] = createSignal(queuer.getIsFull())
|
|
118
125
|
const [isIdle, setIsIdle] = createSignal(queuer.getIsIdle())
|
|
119
126
|
const [isRunning, setIsRunning] = createSignal(queuer.getIsRunning())
|
|
120
|
-
const [
|
|
127
|
+
const [nextItem, setNextItem] = createSignal<TValue | undefined>(
|
|
128
|
+
queuer.peekNextItem(),
|
|
129
|
+
)
|
|
121
130
|
const [size, setSize] = createSignal(queuer.getSize())
|
|
122
131
|
|
|
123
132
|
function setOptions(newOptions: Partial<QueuerOptions<TValue>>) {
|
|
124
133
|
queuer.setOptions({
|
|
125
134
|
...newOptions,
|
|
126
135
|
onItemsChange: (queuer) => {
|
|
127
|
-
setAllItems(queuer.
|
|
136
|
+
setAllItems(queuer.peekAllItems())
|
|
128
137
|
setExecutionCount(queuer.getExecutionCount())
|
|
129
138
|
setIsEmpty(queuer.getIsEmpty())
|
|
130
139
|
setIsFull(queuer.getIsFull())
|
|
131
140
|
setIsIdle(queuer.getIsIdle())
|
|
132
|
-
|
|
141
|
+
setNextItem(() => queuer.peekNextItem())
|
|
133
142
|
setSize(queuer.getSize())
|
|
134
143
|
|
|
135
144
|
const onItemsChange =
|
|
@@ -163,7 +172,7 @@ export function createQueuer<TValue>(
|
|
|
163
172
|
isFull,
|
|
164
173
|
isIdle,
|
|
165
174
|
isRunning,
|
|
166
|
-
|
|
175
|
+
nextItem,
|
|
167
176
|
rejectionCount,
|
|
168
177
|
size,
|
|
169
178
|
setOptions,
|