@tanstack/solid-pacer 0.6.0 → 0.7.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 +5 -3
- package/dist/cjs/async-queuer/createAsyncQueuer.cjs.map +1 -1
- package/dist/cjs/async-queuer/createAsyncQueuer.d.cts +23 -16
- 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 +2 -2
- package/dist/cjs/queuer/createQueuer.cjs.map +1 -1
- package/dist/cjs/queuer/createQueuer.d.cts +27 -21
- package/dist/esm/async-queuer/createAsyncQueuer.d.ts +23 -16
- package/dist/esm/async-queuer/createAsyncQueuer.js +5 -3
- 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 +27 -21
- package/dist/esm/queuer/createQueuer.js +2 -2
- package/dist/esm/queuer/createQueuer.js.map +1 -1
- package/package.json +12 -2
- package/src/async-queuer/createAsyncQueuer.ts +35 -28
- 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 +28 -21
|
@@ -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
|
+
| 'getAllItems'
|
|
11
|
+
| 'getBatchExecutionCount'
|
|
12
|
+
| 'getIsEmpty'
|
|
13
|
+
| 'getIsRunning'
|
|
14
|
+
| 'getItemExecutionCount'
|
|
15
|
+
| 'getSize'
|
|
16
|
+
> {
|
|
17
|
+
/**
|
|
18
|
+
* Signal version of `getAllItems`
|
|
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.getAllItems(),
|
|
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.getAllItems())
|
|
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'
|
|
@@ -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.getAllItems()),
|
|
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');
|
|
@@ -100,9 +106,10 @@ export interface SolidQueuer<TValue>
|
|
|
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
115
|
queuer.getAllItems(),
|