@tanstack/solid-pacer 0.5.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-debouncer/createAsyncDebouncer.cjs.map +1 -1
- package/dist/cjs/async-debouncer/createAsyncDebouncer.d.cts +21 -2
- package/dist/cjs/async-queuer/createAsyncQueuer.cjs +26 -13
- package/dist/cjs/async-queuer/createAsyncQueuer.cjs.map +1 -1
- package/dist/cjs/async-queuer/createAsyncQueuer.d.cts +47 -32
- package/dist/cjs/async-rate-limiter/createAsyncRateLimiter.cjs.map +1 -1
- package/dist/cjs/async-rate-limiter/createAsyncRateLimiter.d.cts +22 -7
- package/dist/cjs/async-throttler/createAsyncThrottler.cjs.map +1 -1
- package/dist/cjs/async-throttler/createAsyncThrottler.d.cts +15 -1
- 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/cjs/rate-limiter/createRateLimiter.cjs.map +1 -1
- package/dist/cjs/rate-limiter/createRateLimiter.d.cts +4 -10
- package/dist/esm/async-debouncer/createAsyncDebouncer.d.ts +21 -2
- package/dist/esm/async-debouncer/createAsyncDebouncer.js.map +1 -1
- package/dist/esm/async-queuer/createAsyncQueuer.d.ts +47 -32
- package/dist/esm/async-queuer/createAsyncQueuer.js +26 -13
- package/dist/esm/async-queuer/createAsyncQueuer.js.map +1 -1
- package/dist/esm/async-rate-limiter/createAsyncRateLimiter.d.ts +22 -7
- package/dist/esm/async-rate-limiter/createAsyncRateLimiter.js.map +1 -1
- package/dist/esm/async-throttler/createAsyncThrottler.d.ts +15 -1
- package/dist/esm/async-throttler/createAsyncThrottler.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/dist/esm/rate-limiter/createRateLimiter.d.ts +4 -10
- package/dist/esm/rate-limiter/createRateLimiter.js.map +1 -1
- package/package.json +12 -2
- package/src/async-debouncer/createAsyncDebouncer.ts +21 -3
- package/src/async-queuer/createAsyncQueuer.ts +76 -48
- package/src/async-rate-limiter/createAsyncRateLimiter.ts +22 -7
- package/src/async-throttler/createAsyncThrottler.ts +15 -2
- 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
- package/src/rate-limiter/createRateLimiter.ts +4 -10
|
@@ -9,27 +9,30 @@ export interface SolidAsyncQueuer<TValue>
|
|
|
9
9
|
AsyncQueuer<TValue>,
|
|
10
10
|
| 'getActiveItems'
|
|
11
11
|
| 'getAllItems'
|
|
12
|
-
| '
|
|
12
|
+
| 'getErrorCount'
|
|
13
13
|
| 'getIsEmpty'
|
|
14
14
|
| 'getIsFull'
|
|
15
15
|
| 'getIsIdle'
|
|
16
16
|
| 'getIsRunning'
|
|
17
17
|
| 'getPeek'
|
|
18
18
|
| 'getPendingItems'
|
|
19
|
+
| 'getRejectionCount'
|
|
20
|
+
| 'getSettledCount'
|
|
19
21
|
| 'getSize'
|
|
22
|
+
| 'getSuccessCount'
|
|
20
23
|
> {
|
|
21
24
|
/**
|
|
22
25
|
* Signal version of `getActiveItems`
|
|
23
26
|
*/
|
|
24
|
-
activeItems: Accessor<Array<
|
|
27
|
+
activeItems: Accessor<Array<TValue>>
|
|
25
28
|
/**
|
|
26
29
|
* Signal version of `getAllItems`
|
|
27
30
|
*/
|
|
28
|
-
allItems: Accessor<Array<
|
|
31
|
+
allItems: Accessor<Array<TValue>>
|
|
29
32
|
/**
|
|
30
|
-
* Signal version of `
|
|
33
|
+
* Signal version of `getErrorCount`
|
|
31
34
|
*/
|
|
32
|
-
|
|
35
|
+
errorCount: Accessor<number>
|
|
33
36
|
/**
|
|
34
37
|
* Signal version of `getIsEmpty`
|
|
35
38
|
*/
|
|
@@ -49,48 +52,69 @@ export interface SolidAsyncQueuer<TValue>
|
|
|
49
52
|
/**
|
|
50
53
|
* Signal version of `getPeek`
|
|
51
54
|
*/
|
|
52
|
-
peek: Accessor<
|
|
55
|
+
peek: Accessor<TValue | undefined>
|
|
53
56
|
/**
|
|
54
57
|
* Signal version of `getPendingItems`
|
|
55
58
|
*/
|
|
56
|
-
pendingItems: Accessor<Array<
|
|
59
|
+
pendingItems: Accessor<Array<TValue>>
|
|
57
60
|
/**
|
|
58
61
|
* Signal version of `getRejectionCount`
|
|
59
62
|
*/
|
|
60
63
|
rejectionCount: Accessor<number>
|
|
64
|
+
/**
|
|
65
|
+
* Signal version of `getSettledCount`
|
|
66
|
+
*/
|
|
67
|
+
settledCount: Accessor<number>
|
|
61
68
|
/**
|
|
62
69
|
* Signal version of `getSize`
|
|
63
70
|
*/
|
|
64
71
|
size: Accessor<number>
|
|
72
|
+
/**
|
|
73
|
+
* Signal version of `getSuccessCount`
|
|
74
|
+
*/
|
|
75
|
+
successCount: Accessor<number>
|
|
65
76
|
}
|
|
66
77
|
|
|
67
78
|
/**
|
|
68
|
-
*
|
|
79
|
+
* Creates a Solid-compatible AsyncQueuer instance for managing an asynchronous queue of items, exposing Solid signals for all stateful properties.
|
|
69
80
|
*
|
|
70
|
-
*
|
|
71
|
-
*
|
|
81
|
+
* Features:
|
|
82
|
+
* - Priority queueing via `getPriority` or item `priority` property
|
|
83
|
+
* - Configurable concurrency limit
|
|
84
|
+
* - FIFO (First In First Out) or LIFO (Last In First Out) queue behavior
|
|
85
|
+
* - Pause/resume processing
|
|
86
|
+
* - Task cancellation
|
|
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
|
|
72
90
|
*
|
|
73
|
-
*
|
|
74
|
-
*
|
|
75
|
-
* - Maximum queue size
|
|
76
|
-
* - Processing function for queue items
|
|
77
|
-
* - Various lifecycle callbacks
|
|
91
|
+
* Tasks are processed concurrently up to the configured concurrency limit. When a task completes,
|
|
92
|
+
* the next pending task is processed if the concurrency limit allows.
|
|
78
93
|
*
|
|
79
|
-
*
|
|
80
|
-
* -
|
|
81
|
-
* -
|
|
82
|
-
* -
|
|
83
|
-
* -
|
|
84
|
-
* -
|
|
94
|
+
* Error Handling:
|
|
95
|
+
* - If an `onError` handler is provided, it will be called with the error and queuer instance
|
|
96
|
+
* - If `throwOnError` is true (default when no onError handler is provided), the error will be thrown
|
|
97
|
+
* - If `throwOnError` is false (default when onError handler is provided), the error will be swallowed
|
|
98
|
+
* - Both onError and throwOnError can be used together; the handler will be called before any error is thrown
|
|
99
|
+
* - The error state can be checked using the underlying AsyncQueuer instance
|
|
85
100
|
*
|
|
86
|
-
*
|
|
101
|
+
* Example usage:
|
|
87
102
|
* ```tsx
|
|
88
103
|
* // Basic async queuer for API requests
|
|
89
|
-
* const asyncQueuer = createAsyncQueuer({
|
|
104
|
+
* const asyncQueuer = createAsyncQueuer(async (item) => {
|
|
105
|
+
* // process item
|
|
106
|
+
* return await fetchData(item);
|
|
107
|
+
* }, {
|
|
90
108
|
* initialItems: [],
|
|
91
109
|
* concurrency: 2,
|
|
92
110
|
* maxSize: 100,
|
|
93
111
|
* started: false,
|
|
112
|
+
* onSuccess: (result) => {
|
|
113
|
+
* console.log('Item processed:', result);
|
|
114
|
+
* },
|
|
115
|
+
* onError: (error) => {
|
|
116
|
+
* console.error('Processing failed:', error);
|
|
117
|
+
* }
|
|
94
118
|
* });
|
|
95
119
|
*
|
|
96
120
|
* // Add items to queue
|
|
@@ -99,23 +123,22 @@ export interface SolidAsyncQueuer<TValue>
|
|
|
99
123
|
* // Start processing
|
|
100
124
|
* asyncQueuer.start();
|
|
101
125
|
*
|
|
102
|
-
* //
|
|
103
|
-
* asyncQueuer.
|
|
104
|
-
* console.log('Item processed:', result);
|
|
105
|
-
* });
|
|
106
|
-
*
|
|
107
|
-
* asyncQueuer.onError((error) => {
|
|
108
|
-
* console.error('Processing failed:', error);
|
|
109
|
-
* });
|
|
126
|
+
* // Use Solid signals in your UI
|
|
127
|
+
* const pending = asyncQueuer.pendingItems();
|
|
110
128
|
* ```
|
|
111
129
|
*/
|
|
112
130
|
export function createAsyncQueuer<TValue>(
|
|
131
|
+
fn: (value: TValue) => Promise<any>,
|
|
113
132
|
initialOptions: AsyncQueuerOptions<TValue> = {},
|
|
114
133
|
): SolidAsyncQueuer<TValue> {
|
|
115
|
-
const asyncQueuer = new AsyncQueuer<TValue>(initialOptions)
|
|
134
|
+
const asyncQueuer = new AsyncQueuer<TValue>(fn, initialOptions)
|
|
116
135
|
|
|
117
|
-
const [
|
|
118
|
-
asyncQueuer.
|
|
136
|
+
const [successCount, setSuccessCount] = createSignal(
|
|
137
|
+
asyncQueuer.getSuccessCount(),
|
|
138
|
+
)
|
|
139
|
+
const [errorCount, setErrorCount] = createSignal(asyncQueuer.getErrorCount())
|
|
140
|
+
const [settledCount, setSettledCount] = createSignal(
|
|
141
|
+
asyncQueuer.getSettledCount(),
|
|
119
142
|
)
|
|
120
143
|
const [rejectionCount, setRejectionCount] = createSignal(
|
|
121
144
|
asyncQueuer.getRejectionCount(),
|
|
@@ -124,31 +147,34 @@ export function createAsyncQueuer<TValue>(
|
|
|
124
147
|
const [isFull, setIsFull] = createSignal(asyncQueuer.getIsFull())
|
|
125
148
|
const [isIdle, setIsIdle] = createSignal(asyncQueuer.getIsIdle())
|
|
126
149
|
const [isRunning, setIsRunning] = createSignal(asyncQueuer.getIsRunning())
|
|
127
|
-
const [allItems, setAllItems] = createSignal<Array<
|
|
150
|
+
const [allItems, setAllItems] = createSignal<Array<TValue>>(
|
|
128
151
|
asyncQueuer.getAllItems(),
|
|
129
152
|
)
|
|
130
|
-
const [activeItems, setActiveItems] = createSignal<
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
const [pendingItems, setPendingItems] = createSignal<
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
const [peek, setPeek] = createSignal<
|
|
153
|
+
const [activeItems, setActiveItems] = createSignal<Array<TValue>>(
|
|
154
|
+
asyncQueuer.getActiveItems(),
|
|
155
|
+
)
|
|
156
|
+
const [pendingItems, setPendingItems] = createSignal<Array<TValue>>(
|
|
157
|
+
asyncQueuer.getPendingItems(),
|
|
158
|
+
)
|
|
159
|
+
const [peek, setPeek] = createSignal<TValue | undefined>(
|
|
137
160
|
asyncQueuer.getPeek(),
|
|
138
161
|
)
|
|
139
162
|
const [size, setSize] = createSignal(asyncQueuer.getSize())
|
|
140
163
|
|
|
141
164
|
asyncQueuer.setOptions({
|
|
142
165
|
onItemsChange: (queuer) => {
|
|
143
|
-
|
|
166
|
+
setActiveItems(queuer.getActiveItems())
|
|
167
|
+
setAllItems(queuer.getAllItems())
|
|
168
|
+
setErrorCount(queuer.getErrorCount())
|
|
144
169
|
setIsEmpty(queuer.getIsEmpty())
|
|
145
170
|
setIsFull(queuer.getIsFull())
|
|
146
171
|
setIsIdle(queuer.getIsIdle())
|
|
147
|
-
setAllItems(queuer.getAllItems())
|
|
148
|
-
setActiveItems(queuer.getActiveItems())
|
|
149
|
-
setPendingItems(queuer.getPendingItems())
|
|
150
172
|
setPeek(() => queuer.getPeek())
|
|
173
|
+
setPendingItems(queuer.getPendingItems())
|
|
174
|
+
setRejectionCount(queuer.getRejectionCount())
|
|
175
|
+
setSettledCount(queuer.getSettledCount())
|
|
151
176
|
setSize(queuer.getSize())
|
|
177
|
+
setSuccessCount(queuer.getSuccessCount())
|
|
152
178
|
initialOptions.onItemsChange?.(queuer)
|
|
153
179
|
},
|
|
154
180
|
onIsRunningChange: (queuer) => {
|
|
@@ -165,15 +191,17 @@ export function createAsyncQueuer<TValue>(
|
|
|
165
191
|
return {
|
|
166
192
|
...bindInstanceMethods(asyncQueuer),
|
|
167
193
|
activeItems,
|
|
168
|
-
|
|
194
|
+
allItems,
|
|
195
|
+
errorCount,
|
|
169
196
|
isEmpty,
|
|
170
197
|
isFull,
|
|
171
198
|
isIdle,
|
|
172
199
|
isRunning,
|
|
173
|
-
allItems,
|
|
174
200
|
peek,
|
|
175
201
|
pendingItems,
|
|
176
202
|
rejectionCount,
|
|
203
|
+
settledCount,
|
|
177
204
|
size,
|
|
205
|
+
successCount,
|
|
178
206
|
} as SolidAsyncQueuer<TValue>
|
|
179
207
|
}
|
|
@@ -29,13 +29,9 @@ export interface SolidAsyncRateLimiter<TFn extends AnyAsyncFunction>
|
|
|
29
29
|
* This hook is designed to be flexible and state-management agnostic - it simply returns a rate limiter instance that
|
|
30
30
|
* you can integrate with any state management solution (createSignal, etc).
|
|
31
31
|
*
|
|
32
|
-
* Rate limiting allows
|
|
33
|
-
* then blocks subsequent calls until the window passes. This
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
-
* Unlike the non-async RateLimiter, this async version supports returning values from the rate-limited function,
|
|
37
|
-
* making it ideal for API calls and other async operations where you want the result of the `maybeExecute` call
|
|
38
|
-
* instead of setting the result on a state variable from within the rate-limited function.
|
|
32
|
+
* Rate limiting is a simple approach that allows a function to execute up to a limit within a time window,
|
|
33
|
+
* then blocks all subsequent calls until the window passes. This can lead to "bursty" behavior where
|
|
34
|
+
* all executions happen immediately, followed by a complete block.
|
|
39
35
|
*
|
|
40
36
|
* The rate limiter supports two types of windows:
|
|
41
37
|
* - 'fixed': A strict window that resets after the window period. All executions within the window count
|
|
@@ -43,6 +39,25 @@ export interface SolidAsyncRateLimiter<TFn extends AnyAsyncFunction>
|
|
|
43
39
|
* - 'sliding': A rolling window that allows executions as old ones expire. This provides a more
|
|
44
40
|
* consistent rate of execution over time.
|
|
45
41
|
*
|
|
42
|
+
* Unlike the non-async RateLimiter, this async version supports returning values from the rate-limited function,
|
|
43
|
+
* making it ideal for API calls and other async operations where you want the result of the `maybeExecute` call
|
|
44
|
+
* instead of setting the result on a state variable from within the rate-limited function.
|
|
45
|
+
*
|
|
46
|
+
* For smoother execution patterns, consider using:
|
|
47
|
+
* - Throttling: Ensures consistent spacing between executions (e.g. max once per 200ms)
|
|
48
|
+
* - Debouncing: Waits for a pause in calls before executing (e.g. after 500ms of no calls)
|
|
49
|
+
*
|
|
50
|
+
* Rate limiting is best used for hard API limits or resource constraints. For UI updates or
|
|
51
|
+
* smoothing out frequent events, throttling or debouncing usually provide better user experience.
|
|
52
|
+
*
|
|
53
|
+
* Error Handling:
|
|
54
|
+
* - If an `onError` handler is provided, it will be called with the error and rate limiter instance
|
|
55
|
+
* - If `throwOnError` is true (default when no onError handler is provided), the error will be thrown
|
|
56
|
+
* - If `throwOnError` is false (default when onError handler is provided), the error will be swallowed
|
|
57
|
+
* - Both onError and throwOnError can be used together - the handler will be called before any error is thrown
|
|
58
|
+
* - The error state can be checked using the underlying AsyncRateLimiter instance
|
|
59
|
+
* - Rate limit rejections (when limit is exceeded) are handled separately from execution errors via the `onReject` handler
|
|
60
|
+
*
|
|
46
61
|
* @example
|
|
47
62
|
* ```tsx
|
|
48
63
|
* // Basic API call rate limiting with return value
|
|
@@ -37,6 +37,17 @@ export interface SolidAsyncThrottler<TFn extends AnyAsyncFunction>
|
|
|
37
37
|
* regardless of how many times it is called. This is useful for rate-limiting expensive API calls,
|
|
38
38
|
* database operations, or other async tasks.
|
|
39
39
|
*
|
|
40
|
+
* Unlike the non-async Throttler, this async version supports returning values from the throttled function,
|
|
41
|
+
* making it ideal for API calls and other async operations where you want the result of the `maybeExecute` call
|
|
42
|
+
* instead of setting the result on a state variable from within the throttled function.
|
|
43
|
+
*
|
|
44
|
+
* Error Handling:
|
|
45
|
+
* - If an `onError` handler is provided, it will be called with the error and throttler instance
|
|
46
|
+
* - If `throwOnError` is true (default when no onError handler is provided), the error will be thrown
|
|
47
|
+
* - If `throwOnError` is false (default when onError handler is provided), the error will be swallowed
|
|
48
|
+
* - Both onError and throwOnError can be used together - the handler will be called before any error is thrown
|
|
49
|
+
* - The error state can be checked using the underlying AsyncThrottler instance
|
|
50
|
+
*
|
|
40
51
|
* @example
|
|
41
52
|
* ```tsx
|
|
42
53
|
* // Basic API call throttling
|
|
@@ -58,12 +69,14 @@ export interface SolidAsyncThrottler<TFn extends AnyAsyncFunction>
|
|
|
58
69
|
* {
|
|
59
70
|
* wait: 2000,
|
|
60
71
|
* leading: true, // Execute immediately on first call
|
|
61
|
-
* trailing: false
|
|
72
|
+
* trailing: false, // Skip trailing edge updates
|
|
73
|
+
* onError: (error) => {
|
|
74
|
+
* console.error('API call failed:', error);
|
|
75
|
+
* }
|
|
62
76
|
* }
|
|
63
77
|
* );
|
|
64
78
|
* ```
|
|
65
79
|
*/
|
|
66
|
-
|
|
67
80
|
export function createAsyncThrottler<TFn extends AnyAsyncFunction>(
|
|
68
81
|
fn: TFn,
|
|
69
82
|
initialOptions: AsyncThrottlerOptions<TFn>,
|
|
@@ -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(),
|
|
@@ -46,17 +46,11 @@ export interface SolidRateLimiter<TFn extends AnyFunction>
|
|
|
46
46
|
* const rateLimiter = createRateLimiter(apiCall, {
|
|
47
47
|
* limit: 5,
|
|
48
48
|
* window: 60000,
|
|
49
|
-
* windowType: 'sliding'
|
|
50
|
-
*
|
|
51
|
-
*
|
|
52
|
-
* // Monitor rate limit status
|
|
53
|
-
* const handleClick = () => {
|
|
54
|
-
* if (rateLimiter.remainingInWindow() > 0) {
|
|
55
|
-
* rateLimiter.maybeExecute(data);
|
|
56
|
-
* } else {
|
|
57
|
-
* showRateLimitWarning();
|
|
49
|
+
* windowType: 'sliding',
|
|
50
|
+
* onReject: (rateLimiter) => {
|
|
51
|
+
* console.log(`Rate limit exceeded. Try again in ${rateLimiter.getMsUntilNextWindow()}ms`);
|
|
58
52
|
* }
|
|
59
|
-
* };
|
|
53
|
+
* });
|
|
60
54
|
*
|
|
61
55
|
* // Access rate limiter state via signals
|
|
62
56
|
* console.log('Executions:', rateLimiter.executionCount());
|