@tanstack/solid-pacer 0.22.0 → 0.23.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/README.md +4 -4
- package/dist/async-batcher/createAsyncBatcher.d.ts +5 -9
- package/dist/async-batcher/createAsyncBatcher.js +2 -5
- package/dist/async-debouncer/createAsyncDebouncer.d.ts +8 -11
- package/dist/async-debouncer/createAsyncDebouncer.js +5 -7
- package/dist/async-queuer/createAsyncQueuer.d.ts +9 -11
- package/dist/async-queuer/createAsyncQueuer.js +4 -5
- package/dist/async-rate-limiter/createAsyncRateLimiter.d.ts +10 -12
- package/dist/async-rate-limiter/createAsyncRateLimiter.js +7 -8
- package/dist/async-throttler/createAsyncThrottler.d.ts +8 -13
- package/dist/async-throttler/createAsyncThrottler.js +5 -9
- package/dist/batcher/createBatcher.d.ts +11 -14
- package/dist/batcher/createBatcher.js +6 -8
- package/dist/debouncer/createDebouncedSignal.d.ts +2 -4
- package/dist/debouncer/createDebouncedSignal.js +1 -2
- package/dist/debouncer/createDebouncedValue.d.ts +2 -4
- package/dist/debouncer/createDebouncedValue.js +1 -2
- package/dist/debouncer/createDebouncer.d.ts +4 -6
- package/dist/debouncer/createDebouncer.js +1 -2
- package/dist/provider/PacerProvider.d.ts +6 -8
- package/dist/provider/PacerProvider.js +1 -2
- package/dist/queuer/createQueuedSignal.d.ts +2 -4
- package/dist/queuer/createQueuedSignal.js +1 -2
- package/dist/queuer/createQueuer.d.ts +4 -6
- package/dist/queuer/createQueuer.js +1 -2
- package/dist/rate-limiter/createRateLimitedSignal.d.ts +12 -13
- package/dist/rate-limiter/createRateLimitedSignal.js +11 -11
- package/dist/rate-limiter/createRateLimitedValue.d.ts +2 -4
- package/dist/rate-limiter/createRateLimitedValue.js +1 -2
- package/dist/rate-limiter/createRateLimiter.d.ts +8 -9
- package/dist/rate-limiter/createRateLimiter.js +5 -5
- package/dist/throttler/createThrottledSignal.d.ts +2 -6
- package/dist/throttler/createThrottledSignal.js +1 -4
- package/dist/throttler/createThrottledValue.d.ts +2 -6
- package/dist/throttler/createThrottledValue.js +1 -4
- package/dist/throttler/createThrottler.d.ts +4 -8
- package/dist/throttler/createThrottler.js +1 -4
- package/package.json +23 -67
- package/dist/async-batcher/createAsyncBatcher.cjs +0 -166
- package/dist/async-batcher/createAsyncBatcher.cjs.map +0 -1
- package/dist/async-batcher/createAsyncBatcher.d.cts +0 -177
- package/dist/async-batcher/createAsyncBatcher.js.map +0 -1
- package/dist/async-batcher/index.cjs +0 -11
- package/dist/async-batcher/index.d.cts +0 -3
- package/dist/async-debouncer/createAsyncDebouncer.cjs +0 -151
- package/dist/async-debouncer/createAsyncDebouncer.cjs.map +0 -1
- package/dist/async-debouncer/createAsyncDebouncer.d.cts +0 -163
- package/dist/async-debouncer/createAsyncDebouncer.js.map +0 -1
- package/dist/async-debouncer/index.cjs +0 -11
- package/dist/async-debouncer/index.d.cts +0 -3
- package/dist/async-queuer/createAsyncQueuer.cjs +0 -158
- package/dist/async-queuer/createAsyncQueuer.cjs.map +0 -1
- package/dist/async-queuer/createAsyncQueuer.d.cts +0 -169
- package/dist/async-queuer/createAsyncQueuer.js.map +0 -1
- package/dist/async-queuer/index.cjs +0 -11
- package/dist/async-queuer/index.d.cts +0 -3
- package/dist/async-rate-limiter/createAsyncRateLimiter.cjs +0 -193
- package/dist/async-rate-limiter/createAsyncRateLimiter.cjs.map +0 -1
- package/dist/async-rate-limiter/createAsyncRateLimiter.d.cts +0 -208
- package/dist/async-rate-limiter/createAsyncRateLimiter.js.map +0 -1
- package/dist/async-rate-limiter/index.cjs +0 -11
- package/dist/async-rate-limiter/index.d.cts +0 -3
- package/dist/async-throttler/createAsyncThrottler.cjs +0 -151
- package/dist/async-throttler/createAsyncThrottler.cjs.map +0 -1
- package/dist/async-throttler/createAsyncThrottler.d.cts +0 -163
- package/dist/async-throttler/createAsyncThrottler.js.map +0 -1
- package/dist/async-throttler/index.cjs +0 -11
- package/dist/async-throttler/index.d.cts +0 -3
- package/dist/batcher/createBatcher.cjs +0 -132
- package/dist/batcher/createBatcher.cjs.map +0 -1
- package/dist/batcher/createBatcher.d.cts +0 -146
- package/dist/batcher/createBatcher.js.map +0 -1
- package/dist/batcher/index.cjs +0 -11
- package/dist/batcher/index.d.cts +0 -3
- package/dist/debouncer/createDebouncedSignal.cjs +0 -88
- package/dist/debouncer/createDebouncedSignal.cjs.map +0 -1
- package/dist/debouncer/createDebouncedSignal.d.cts +0 -79
- package/dist/debouncer/createDebouncedSignal.js.map +0 -1
- package/dist/debouncer/createDebouncedValue.cjs +0 -77
- package/dist/debouncer/createDebouncedValue.cjs.map +0 -1
- package/dist/debouncer/createDebouncedValue.d.cts +0 -70
- package/dist/debouncer/createDebouncedValue.js.map +0 -1
- package/dist/debouncer/createDebouncer.cjs +0 -129
- package/dist/debouncer/createDebouncer.cjs.map +0 -1
- package/dist/debouncer/createDebouncer.d.cts +0 -144
- package/dist/debouncer/createDebouncer.js.map +0 -1
- package/dist/debouncer/index.cjs +0 -15
- package/dist/debouncer/index.d.cts +0 -5
- package/dist/index.cjs +0 -47
- package/dist/index.d.cts +0 -20
- package/dist/provider/PacerProvider.cjs +0 -27
- package/dist/provider/PacerProvider.cjs.map +0 -1
- package/dist/provider/PacerProvider.d.cts +0 -28
- package/dist/provider/PacerProvider.js.map +0 -1
- package/dist/provider/index.cjs +0 -6
- package/dist/provider/index.d.cts +0 -2
- package/dist/queuer/createQueuedSignal.cjs +0 -129
- package/dist/queuer/createQueuedSignal.cjs.map +0 -1
- package/dist/queuer/createQueuedSignal.d.cts +0 -121
- package/dist/queuer/createQueuedSignal.js.map +0 -1
- package/dist/queuer/createQueuer.cjs +0 -133
- package/dist/queuer/createQueuer.cjs.map +0 -1
- package/dist/queuer/createQueuer.d.cts +0 -147
- package/dist/queuer/createQueuer.js.map +0 -1
- package/dist/queuer/index.cjs +0 -13
- package/dist/queuer/index.d.cts +0 -4
- package/dist/rate-limiter/createRateLimitedSignal.cjs +0 -102
- package/dist/rate-limiter/createRateLimitedSignal.cjs.map +0 -1
- package/dist/rate-limiter/createRateLimitedSignal.d.cts +0 -93
- package/dist/rate-limiter/createRateLimitedSignal.js.map +0 -1
- package/dist/rate-limiter/createRateLimitedValue.cjs +0 -102
- package/dist/rate-limiter/createRateLimitedValue.cjs.map +0 -1
- package/dist/rate-limiter/createRateLimitedValue.d.cts +0 -95
- package/dist/rate-limiter/createRateLimitedValue.js.map +0 -1
- package/dist/rate-limiter/createRateLimiter.cjs +0 -157
- package/dist/rate-limiter/createRateLimiter.cjs.map +0 -1
- package/dist/rate-limiter/createRateLimiter.d.cts +0 -173
- package/dist/rate-limiter/createRateLimiter.js.map +0 -1
- package/dist/rate-limiter/index.cjs +0 -15
- package/dist/rate-limiter/index.d.cts +0 -5
- package/dist/throttler/createThrottledSignal.cjs +0 -79
- package/dist/throttler/createThrottledSignal.cjs.map +0 -1
- package/dist/throttler/createThrottledSignal.d.cts +0 -70
- package/dist/throttler/createThrottledSignal.js.map +0 -1
- package/dist/throttler/createThrottledValue.cjs +0 -74
- package/dist/throttler/createThrottledValue.cjs.map +0 -1
- package/dist/throttler/createThrottledValue.d.cts +0 -67
- package/dist/throttler/createThrottledValue.js.map +0 -1
- package/dist/throttler/createThrottler.cjs +0 -132
- package/dist/throttler/createThrottler.cjs.map +0 -1
- package/dist/throttler/createThrottler.d.cts +0 -147
- package/dist/throttler/createThrottler.js.map +0 -1
- package/dist/throttler/index.cjs +0 -15
- package/dist/throttler/index.d.cts +0 -5
- package/dist/types/index.cjs +0 -8
- package/dist/types/index.d.cts +0 -1
- package/dist/utils/index.cjs +0 -8
- package/dist/utils/index.d.cts +0 -1
- package/src/async-batcher/createAsyncBatcher.ts +0 -234
- package/src/async-batcher/index.ts +0 -3
- package/src/async-debouncer/createAsyncDebouncer.ts +0 -225
- package/src/async-debouncer/index.ts +0 -4
- package/src/async-queuer/createAsyncQueuer.ts +0 -226
- package/src/async-queuer/index.ts +0 -3
- package/src/async-rate-limiter/createAsyncRateLimiter.ts +0 -269
- package/src/async-rate-limiter/index.ts +0 -4
- package/src/async-throttler/createAsyncThrottler.ts +0 -225
- package/src/async-throttler/index.ts +0 -4
- package/src/batcher/createBatcher.ts +0 -199
- package/src/batcher/index.ts +0 -3
- package/src/debouncer/createDebouncedSignal.ts +0 -92
- package/src/debouncer/createDebouncedValue.ts +0 -85
- package/src/debouncer/createDebouncer.ts +0 -201
- package/src/debouncer/index.ts +0 -6
- package/src/index.ts +0 -46
- package/src/provider/PacerProvider.tsx +0 -63
- package/src/provider/index.ts +0 -1
- package/src/queuer/createQueuedSignal.ts +0 -138
- package/src/queuer/createQueuer.ts +0 -199
- package/src/queuer/index.ts +0 -4
- package/src/rate-limiter/createRateLimitedSignal.ts +0 -117
- package/src/rate-limiter/createRateLimitedValue.ts +0 -110
- package/src/rate-limiter/createRateLimiter.ts +0 -226
- package/src/rate-limiter/index.ts +0 -5
- package/src/throttler/createThrottledSignal.ts +0 -81
- package/src/throttler/createThrottledValue.ts +0 -82
- package/src/throttler/createThrottler.ts +0 -204
- package/src/throttler/index.ts +0 -6
- package/src/types/index.ts +0 -1
- package/src/utils/index.ts +0 -1
|
@@ -1,234 +0,0 @@
|
|
|
1
|
-
import { AsyncBatcher } from '@tanstack/pacer/async-batcher'
|
|
2
|
-
import { shallow, useSelector } from '@tanstack/solid-store'
|
|
3
|
-
import { createEffect, onCleanup } from 'solid-js'
|
|
4
|
-
import { useDefaultPacerOptions } from '../provider/PacerProvider'
|
|
5
|
-
import type { Store } from '@tanstack/solid-store'
|
|
6
|
-
import type { Accessor, JSX } from 'solid-js'
|
|
7
|
-
import type {
|
|
8
|
-
AsyncBatcherOptions,
|
|
9
|
-
AsyncBatcherState,
|
|
10
|
-
} from '@tanstack/pacer/async-batcher'
|
|
11
|
-
|
|
12
|
-
export interface SolidAsyncBatcherOptions<
|
|
13
|
-
TValue,
|
|
14
|
-
TSelected = {},
|
|
15
|
-
> extends AsyncBatcherOptions<TValue> {
|
|
16
|
-
/**
|
|
17
|
-
* Optional callback invoked when the owning component unmounts. Receives the batcher instance.
|
|
18
|
-
* When provided, replaces the default cleanup (cancel + abort); use it to call flush(), reset(), cancel(), add logging, etc.
|
|
19
|
-
*/
|
|
20
|
-
onUnmount?: (batcher: SolidAsyncBatcher<TValue, TSelected>) => void
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export interface SolidAsyncBatcher<TValue, TSelected = {}> extends Omit<
|
|
24
|
-
AsyncBatcher<TValue>,
|
|
25
|
-
'store'
|
|
26
|
-
> {
|
|
27
|
-
/**
|
|
28
|
-
* A Solid component that allows you to subscribe to the batcher state.
|
|
29
|
-
*
|
|
30
|
-
* This is useful for tracking specific parts of the batcher state
|
|
31
|
-
* deep in your component tree without needing to pass a selector to the hook.
|
|
32
|
-
*
|
|
33
|
-
* @example
|
|
34
|
-
* <batcher.Subscribe selector={(state) => ({ size: state.size, isExecuting: state.isExecuting })}>
|
|
35
|
-
* {(state) => (
|
|
36
|
-
* <div>Batch: {state().size} items, {state().isExecuting ? 'Executing...' : 'Idle'}</div>
|
|
37
|
-
* )}
|
|
38
|
-
* </batcher.Subscribe>
|
|
39
|
-
*/
|
|
40
|
-
Subscribe: <TSelected>(props: {
|
|
41
|
-
selector: (state: AsyncBatcherState<TValue>) => TSelected
|
|
42
|
-
children: ((state: Accessor<TSelected>) => JSX.Element) | JSX.Element
|
|
43
|
-
}) => JSX.Element
|
|
44
|
-
/**
|
|
45
|
-
* Reactive state that will be updated when the batcher state changes
|
|
46
|
-
*
|
|
47
|
-
* Use this instead of `batcher.store.state`
|
|
48
|
-
*/
|
|
49
|
-
readonly state: Accessor<Readonly<TSelected>>
|
|
50
|
-
/**
|
|
51
|
-
* @deprecated Use `batcher.state` instead of `batcher.store.state` if you want to read reactive state.
|
|
52
|
-
* The state on the store object is not reactive, as it has not been wrapped in a `useSelector` hook internally.
|
|
53
|
-
* Although, you can make the state reactive by using the `useSelector` in your own usage.
|
|
54
|
-
*/
|
|
55
|
-
readonly store: Store<Readonly<AsyncBatcherState<TValue>>>
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* Creates a Solid-compatible AsyncBatcher instance for managing asynchronous batches of items, exposing Solid signals for all stateful properties.
|
|
60
|
-
*
|
|
61
|
-
* This is the async version of the createBatcher hook. Unlike the sync version, this async batcher:
|
|
62
|
-
* - Handles promises and returns results from batch executions
|
|
63
|
-
* - Provides error handling with configurable error behavior
|
|
64
|
-
* - Tracks success, error, and settle counts separately
|
|
65
|
-
* - Has state tracking for when batches are executing
|
|
66
|
-
* - Returns the result of the batch function execution
|
|
67
|
-
*
|
|
68
|
-
* Features:
|
|
69
|
-
* - Configurable batch size and wait time
|
|
70
|
-
* - Custom batch processing logic via getShouldExecute
|
|
71
|
-
* - Event callbacks for monitoring batch operations
|
|
72
|
-
* - Error handling for failed batch operations
|
|
73
|
-
* - Automatic or manual batch processing
|
|
74
|
-
* - All stateful properties (items, counts, etc.) are exposed as Solid signals for reactivity
|
|
75
|
-
*
|
|
76
|
-
* The batcher collects items and processes them in batches based on:
|
|
77
|
-
* - Maximum batch size (number of items per batch)
|
|
78
|
-
* - Time-based batching (process after X milliseconds)
|
|
79
|
-
* - Custom batch processing logic via getShouldExecute
|
|
80
|
-
*
|
|
81
|
-
* Error Handling:
|
|
82
|
-
* - If an `onError` handler is provided, it will be called with the error and batcher instance
|
|
83
|
-
* - If `throwOnError` is true (default when no onError handler is provided), the error will be thrown
|
|
84
|
-
* - If `throwOnError` is false (default when onError handler is provided), the error will be swallowed
|
|
85
|
-
* - Both onError and throwOnError can be used together; the handler will be called before any error is thrown
|
|
86
|
-
* - The error state can be checked using the underlying AsyncBatcher instance
|
|
87
|
-
*
|
|
88
|
-
* ## State Management and Selector
|
|
89
|
-
*
|
|
90
|
-
* The hook uses TanStack Store for reactive state management. You can subscribe to state changes
|
|
91
|
-
* in two ways:
|
|
92
|
-
*
|
|
93
|
-
* **1. Using `batcher.Subscribe` component (Recommended for component tree subscriptions)**
|
|
94
|
-
*
|
|
95
|
-
* Use the `Subscribe` component to subscribe to state changes deep in your component tree without
|
|
96
|
-
* needing to pass a selector to the hook. This is ideal when you want to subscribe to state
|
|
97
|
-
* in child components.
|
|
98
|
-
*
|
|
99
|
-
* **2. Using the `selector` parameter (For hook-level subscriptions)**
|
|
100
|
-
*
|
|
101
|
-
* The `selector` parameter allows you to specify which state changes will trigger reactive updates
|
|
102
|
-
* at the hook level, optimizing performance by preventing unnecessary updates when irrelevant
|
|
103
|
-
* state changes occur.
|
|
104
|
-
*
|
|
105
|
-
* **By default, there will be no reactive state subscriptions** and you must opt-in to state
|
|
106
|
-
* tracking by providing a selector function or using the `Subscribe` component. This prevents unnecessary
|
|
107
|
-
* updates and gives you full control over when your component tracks state changes.
|
|
108
|
-
*
|
|
109
|
-
* Available state properties:
|
|
110
|
-
* - `errorCount`: Number of failed batch executions
|
|
111
|
-
* - `executionCount`: Total number of batch execution attempts (successful + failed)
|
|
112
|
-
* - `hasError`: Whether the last batch execution resulted in an error
|
|
113
|
-
* - `isExecuting`: Whether a batch execution is currently in progress
|
|
114
|
-
* - `items`: Array of items currently queued for batching
|
|
115
|
-
* - `lastError`: The error from the most recent failed batch execution (if any)
|
|
116
|
-
* - `lastResult`: The result from the most recent successful batch execution
|
|
117
|
-
* - `settleCount`: Number of batch executions that have completed (successful or failed)
|
|
118
|
-
* - `successCount`: Number of successful batch executions
|
|
119
|
-
*
|
|
120
|
-
* ## Unmount behavior
|
|
121
|
-
*
|
|
122
|
-
* By default, the primitive cancels any pending batch and aborts any in-flight execution when the owning component unmounts.
|
|
123
|
-
* Abort only cancels underlying operations (e.g. fetch) when the abort signal from `getAbortSignal()` is passed to them.
|
|
124
|
-
* Use the `onUnmount` option to customize this. For example, to flush pending work instead:
|
|
125
|
-
*
|
|
126
|
-
* ```tsx
|
|
127
|
-
* const batcher = createAsyncBatcher(fn, {
|
|
128
|
-
* maxSize: 10,
|
|
129
|
-
* wait: 2000,
|
|
130
|
-
* onUnmount: (b) => b.flush()
|
|
131
|
-
* });
|
|
132
|
-
* ```
|
|
133
|
-
*
|
|
134
|
-
* Note: For async utils, `flush()` returns a Promise and runs fire-and-forget in the cleanup.
|
|
135
|
-
* If your batch function updates Solid signals, those updates may run after the component has
|
|
136
|
-
* unmounted, which can cause unexpected reactive updates. Guard your callbacks accordingly when
|
|
137
|
-
* using onUnmount with flush.
|
|
138
|
-
*
|
|
139
|
-
* Example usage:
|
|
140
|
-
* ```tsx
|
|
141
|
-
* // Default behavior - no reactive state subscriptions
|
|
142
|
-
* const asyncBatcher = createAsyncBatcher(
|
|
143
|
-
* async (items) => {
|
|
144
|
-
* const results = await Promise.all(items.map(item => processItem(item)));
|
|
145
|
-
* return results;
|
|
146
|
-
* },
|
|
147
|
-
* {
|
|
148
|
-
* maxSize: 10,
|
|
149
|
-
* wait: 2000,
|
|
150
|
-
* onSuccess: (result) => {
|
|
151
|
-
* console.log('Batch processed successfully:', result);
|
|
152
|
-
* },
|
|
153
|
-
* onError: (error) => {
|
|
154
|
-
* console.error('Batch processing failed:', error);
|
|
155
|
-
* }
|
|
156
|
-
* }
|
|
157
|
-
* );
|
|
158
|
-
*
|
|
159
|
-
* // Opt-in to track items or isExecuting changes (optimized for UI updates)
|
|
160
|
-
* const asyncBatcher = createAsyncBatcher(
|
|
161
|
-
* async (items) => {
|
|
162
|
-
* const results = await Promise.all(items.map(item => processItem(item)));
|
|
163
|
-
* return results;
|
|
164
|
-
* },
|
|
165
|
-
* { maxSize: 10, wait: 2000 },
|
|
166
|
-
* (state) => ({ items: state.items, isExecuting: state.isExecuting })
|
|
167
|
-
* );
|
|
168
|
-
*
|
|
169
|
-
* // Opt-in to track error state changes (optimized for error handling)
|
|
170
|
-
* const asyncBatcher = createAsyncBatcher(
|
|
171
|
-
* async (items) => {
|
|
172
|
-
* const results = await Promise.all(items.map(item => processItem(item)));
|
|
173
|
-
* return results;
|
|
174
|
-
* },
|
|
175
|
-
* { maxSize: 10, wait: 2000 },
|
|
176
|
-
* (state) => ({ hasError: state.hasError, lastError: state.lastError })
|
|
177
|
-
* );
|
|
178
|
-
*
|
|
179
|
-
* // Add items to batch
|
|
180
|
-
* asyncBatcher.addItem(newItem);
|
|
181
|
-
*
|
|
182
|
-
* // Manually execute batch
|
|
183
|
-
* const result = await asyncBatcher.execute();
|
|
184
|
-
*
|
|
185
|
-
* // Access the selected state (will be empty object {} unless selector provided)
|
|
186
|
-
* const { items, isExecuting } = asyncBatcher.state();
|
|
187
|
-
* ```
|
|
188
|
-
*/
|
|
189
|
-
export function createAsyncBatcher<TValue, TSelected = {}>(
|
|
190
|
-
fn: (items: Array<TValue>) => Promise<any>,
|
|
191
|
-
options: SolidAsyncBatcherOptions<TValue, TSelected> = {},
|
|
192
|
-
selector: (state: AsyncBatcherState<TValue>) => TSelected = () =>
|
|
193
|
-
({}) as TSelected,
|
|
194
|
-
): SolidAsyncBatcher<TValue, TSelected> {
|
|
195
|
-
const mergedOptions = {
|
|
196
|
-
...useDefaultPacerOptions().asyncBatcher,
|
|
197
|
-
...options,
|
|
198
|
-
} as SolidAsyncBatcherOptions<TValue, TSelected>
|
|
199
|
-
const asyncBatcher = new AsyncBatcher<TValue>(
|
|
200
|
-
fn,
|
|
201
|
-
mergedOptions,
|
|
202
|
-
) as unknown as SolidAsyncBatcher<TValue, TSelected>
|
|
203
|
-
|
|
204
|
-
asyncBatcher.Subscribe = function Subscribe<TSelected>(props: {
|
|
205
|
-
selector: (state: AsyncBatcherState<TValue>) => TSelected
|
|
206
|
-
children: ((state: Accessor<TSelected>) => JSX.Element) | JSX.Element
|
|
207
|
-
}) {
|
|
208
|
-
const selected = useSelector(asyncBatcher.store, props.selector, {
|
|
209
|
-
compare: shallow,
|
|
210
|
-
})
|
|
211
|
-
|
|
212
|
-
return typeof props.children === 'function'
|
|
213
|
-
? props.children(selected)
|
|
214
|
-
: props.children
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
const state = useSelector(asyncBatcher.store, selector, { compare: shallow })
|
|
218
|
-
|
|
219
|
-
createEffect(() => {
|
|
220
|
-
onCleanup(() => {
|
|
221
|
-
if (mergedOptions.onUnmount) {
|
|
222
|
-
mergedOptions.onUnmount(asyncBatcher)
|
|
223
|
-
} else {
|
|
224
|
-
asyncBatcher.cancel()
|
|
225
|
-
asyncBatcher.abort()
|
|
226
|
-
}
|
|
227
|
-
})
|
|
228
|
-
})
|
|
229
|
-
|
|
230
|
-
return {
|
|
231
|
-
...asyncBatcher,
|
|
232
|
-
state,
|
|
233
|
-
} as SolidAsyncBatcher<TValue, TSelected> // omit `store` in favor of `state`
|
|
234
|
-
}
|
|
@@ -1,225 +0,0 @@
|
|
|
1
|
-
import { AsyncDebouncer } from '@tanstack/pacer/async-debouncer'
|
|
2
|
-
import { shallow, useSelector } from '@tanstack/solid-store'
|
|
3
|
-
import { createEffect, onCleanup } from 'solid-js'
|
|
4
|
-
import { useDefaultPacerOptions } from '../provider/PacerProvider'
|
|
5
|
-
import type { Store } from '@tanstack/solid-store'
|
|
6
|
-
import type { Accessor, JSX } from 'solid-js'
|
|
7
|
-
import type {
|
|
8
|
-
AsyncDebouncerOptions,
|
|
9
|
-
AsyncDebouncerState,
|
|
10
|
-
} from '@tanstack/pacer/async-debouncer'
|
|
11
|
-
import type { AnyAsyncFunction } from '@tanstack/pacer/types'
|
|
12
|
-
|
|
13
|
-
export interface SolidAsyncDebouncerOptions<
|
|
14
|
-
TFn extends AnyAsyncFunction,
|
|
15
|
-
TSelected = {},
|
|
16
|
-
> extends AsyncDebouncerOptions<TFn> {
|
|
17
|
-
/**
|
|
18
|
-
* Optional callback invoked when the owning component unmounts. Receives the debouncer instance.
|
|
19
|
-
* When provided, replaces the default cleanup (cancel + abort); use it to call flush(), reset(), cancel(), add logging, etc.
|
|
20
|
-
*/
|
|
21
|
-
onUnmount?: (debouncer: SolidAsyncDebouncer<TFn, TSelected>) => void
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export interface SolidAsyncDebouncer<
|
|
25
|
-
TFn extends AnyAsyncFunction,
|
|
26
|
-
TSelected = {},
|
|
27
|
-
> extends Omit<AsyncDebouncer<TFn>, 'store'> {
|
|
28
|
-
/**
|
|
29
|
-
* A Solid component that allows you to subscribe to the debouncer state.
|
|
30
|
-
*
|
|
31
|
-
* This is useful for tracking specific parts of the debouncer state
|
|
32
|
-
* deep in your component tree without needing to pass a selector to the hook.
|
|
33
|
-
*
|
|
34
|
-
* @example
|
|
35
|
-
* <debouncer.Subscribe selector={(state) => ({ isPending: state.isPending, isExecuting: state.isExecuting })}>
|
|
36
|
-
* {(state) => (
|
|
37
|
-
* <div>{state().isPending ? 'Waiting...' : state().isExecuting ? 'Executing...' : 'Ready'}</div>
|
|
38
|
-
* )}
|
|
39
|
-
* </debouncer.Subscribe>
|
|
40
|
-
*/
|
|
41
|
-
Subscribe: <TSelected>(props: {
|
|
42
|
-
selector: (state: AsyncDebouncerState<TFn>) => TSelected
|
|
43
|
-
children: ((state: Accessor<TSelected>) => JSX.Element) | JSX.Element
|
|
44
|
-
}) => JSX.Element
|
|
45
|
-
/**
|
|
46
|
-
* Reactive state that will be updated when the debouncer state changes
|
|
47
|
-
*
|
|
48
|
-
* Use this instead of `debouncer.store.state`
|
|
49
|
-
*/
|
|
50
|
-
readonly state: Accessor<Readonly<TSelected>>
|
|
51
|
-
/**
|
|
52
|
-
* @deprecated Use `debouncer.state` instead of `debouncer.store.state` if you want to read reactive state.
|
|
53
|
-
* The state on the store object is not reactive, as it has not been wrapped in a `useSelector` hook internally.
|
|
54
|
-
* Although, you can make the state reactive by using the `useSelector` in your own usage.
|
|
55
|
-
*/
|
|
56
|
-
readonly store: Store<Readonly<AsyncDebouncerState<TFn>>>
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* A low-level Solid hook that creates an `AsyncDebouncer` instance to delay execution of an async function.
|
|
61
|
-
*
|
|
62
|
-
* This hook is designed to be flexible and state-management agnostic - it simply returns a debouncer instance that
|
|
63
|
-
* you can integrate with any state management solution (createSignal, etc).
|
|
64
|
-
*
|
|
65
|
-
* Async debouncing ensures that an async function only executes after a specified delay has passed since its last invocation.
|
|
66
|
-
* Each new invocation resets the delay timer. This is useful for handling frequent events like window resizing
|
|
67
|
-
* or input changes where you only want to execute the handler after the events have stopped occurring.
|
|
68
|
-
*
|
|
69
|
-
* Unlike throttling which allows execution at regular intervals, debouncing prevents any execution until
|
|
70
|
-
* the function stops being called for the specified delay period.
|
|
71
|
-
*
|
|
72
|
-
* Unlike the non-async Debouncer, this async version supports returning values from the debounced function,
|
|
73
|
-
* making it ideal for API calls and other async operations where you want the result of the `maybeExecute` call
|
|
74
|
-
* instead of setting the result on a state variable from within the debounced function.
|
|
75
|
-
*
|
|
76
|
-
* Error Handling:
|
|
77
|
-
* - If an `onError` handler is provided, it will be called with the error and debouncer instance
|
|
78
|
-
* - If `throwOnError` is true (default when no onError handler is provided), the error will be thrown
|
|
79
|
-
* - If `throwOnError` is false (default when onError handler is provided), the error will be swallowed
|
|
80
|
-
* - Both onError and throwOnError can be used together - the handler will be called before any error is thrown
|
|
81
|
-
* - The error state can be checked using the underlying AsyncDebouncer instance
|
|
82
|
-
*
|
|
83
|
-
* ## State Management and Selector
|
|
84
|
-
*
|
|
85
|
-
* The hook uses TanStack Store for reactive state management. You can subscribe to state changes
|
|
86
|
-
* in two ways:
|
|
87
|
-
*
|
|
88
|
-
* **1. Using `debouncer.Subscribe` component (Recommended for component tree subscriptions)**
|
|
89
|
-
*
|
|
90
|
-
* Use the `Subscribe` component to subscribe to state changes deep in your component tree without
|
|
91
|
-
* needing to pass a selector to the hook. This is ideal when you want to subscribe to state
|
|
92
|
-
* in child components.
|
|
93
|
-
*
|
|
94
|
-
* **2. Using the `selector` parameter (For hook-level subscriptions)**
|
|
95
|
-
*
|
|
96
|
-
* The `selector` parameter allows you to specify which state changes will trigger reactive updates
|
|
97
|
-
* at the hook level, optimizing performance by preventing unnecessary updates when irrelevant
|
|
98
|
-
* state changes occur.
|
|
99
|
-
*
|
|
100
|
-
* **By default, there will be no reactive state subscriptions** and you must opt-in to state
|
|
101
|
-
* tracking by providing a selector function or using the `Subscribe` component. This prevents unnecessary
|
|
102
|
-
* updates and gives you full control over when your component tracks state changes.
|
|
103
|
-
*
|
|
104
|
-
* Available state properties:
|
|
105
|
-
* - `canLeadingExecute`: Whether the debouncer can execute on the leading edge
|
|
106
|
-
* - `executionCount`: Number of function executions that have been completed
|
|
107
|
-
* - `hasError`: Whether the last execution resulted in an error
|
|
108
|
-
* - `isPending`: Whether the debouncer is waiting for the timeout to trigger execution
|
|
109
|
-
* - `isExecuting`: Whether an async function execution is currently in progress
|
|
110
|
-
* - `lastArgs`: The arguments from the most recent call to maybeExecute
|
|
111
|
-
* - `lastError`: The error from the most recent failed execution (if any)
|
|
112
|
-
* - `lastResult`: The result from the most recent successful execution
|
|
113
|
-
* - `status`: Current execution status ('disabled' | 'idle' | 'pending' | 'executing')
|
|
114
|
-
*
|
|
115
|
-
* ## Unmount behavior
|
|
116
|
-
*
|
|
117
|
-
* By default, the primitive cancels any pending execution and aborts any in-flight execution when the owning component unmounts.
|
|
118
|
-
* Abort only cancels underlying operations (e.g. fetch) when the abort signal from `getAbortSignal()` is passed to them.
|
|
119
|
-
* Use the `onUnmount` option to customize this. For example, to flush pending work instead:
|
|
120
|
-
*
|
|
121
|
-
* ```tsx
|
|
122
|
-
* const debouncer = createAsyncDebouncer(fn, {
|
|
123
|
-
* wait: 500,
|
|
124
|
-
* onUnmount: (d) => d.flush()
|
|
125
|
-
* });
|
|
126
|
-
* ```
|
|
127
|
-
*
|
|
128
|
-
* Note: For async utils, `flush()` returns a Promise and runs fire-and-forget in the cleanup.
|
|
129
|
-
* If your debounced function updates Solid signals, those updates may run after the component has
|
|
130
|
-
* unmounted, which can cause unexpected reactive updates. Guard your callbacks accordingly when
|
|
131
|
-
* using onUnmount with flush.
|
|
132
|
-
*
|
|
133
|
-
* @example
|
|
134
|
-
* ```tsx
|
|
135
|
-
* // Default behavior - no reactive state subscriptions
|
|
136
|
-
* const { maybeExecute } = createAsyncDebouncer(
|
|
137
|
-
* async (query: string) => {
|
|
138
|
-
* const results = await api.search(query);
|
|
139
|
-
* return results;
|
|
140
|
-
* },
|
|
141
|
-
* { wait: 500 }
|
|
142
|
-
* );
|
|
143
|
-
*
|
|
144
|
-
* // Opt-in to track isPending or isExecuting changes (optimized for loading states)
|
|
145
|
-
* const debouncer = createAsyncDebouncer(
|
|
146
|
-
* async (query: string) => {
|
|
147
|
-
* const results = await api.search(query);
|
|
148
|
-
* return results;
|
|
149
|
-
* },
|
|
150
|
-
* { wait: 500 },
|
|
151
|
-
* (state) => ({ isPending: state.isPending, isExecuting: state.isExecuting })
|
|
152
|
-
* );
|
|
153
|
-
*
|
|
154
|
-
* // Opt-in to track error state changes (optimized for error handling)
|
|
155
|
-
* const debouncer = createAsyncDebouncer(
|
|
156
|
-
* async (searchTerm) => {
|
|
157
|
-
* const data = await searchAPI(searchTerm);
|
|
158
|
-
* return data;
|
|
159
|
-
* },
|
|
160
|
-
* {
|
|
161
|
-
* wait: 300,
|
|
162
|
-
* leading: true, // Execute immediately on first call
|
|
163
|
-
* trailing: false, // Skip trailing edge updates
|
|
164
|
-
* onError: (error) => {
|
|
165
|
-
* console.error('API call failed:', error);
|
|
166
|
-
* }
|
|
167
|
-
* },
|
|
168
|
-
* (state) => ({ hasError: state.hasError, lastError: state.lastError })
|
|
169
|
-
* );
|
|
170
|
-
*
|
|
171
|
-
* // Access the selected state (will be empty object {} unless selector provided)
|
|
172
|
-
* const { isPending, isExecuting } = debouncer.state();
|
|
173
|
-
* ```
|
|
174
|
-
*/
|
|
175
|
-
export function createAsyncDebouncer<
|
|
176
|
-
TFn extends AnyAsyncFunction,
|
|
177
|
-
TSelected = {},
|
|
178
|
-
>(
|
|
179
|
-
fn: TFn,
|
|
180
|
-
options: SolidAsyncDebouncerOptions<TFn, TSelected>,
|
|
181
|
-
selector: (state: AsyncDebouncerState<TFn>) => TSelected = () =>
|
|
182
|
-
({}) as TSelected,
|
|
183
|
-
): SolidAsyncDebouncer<TFn, TSelected> {
|
|
184
|
-
const mergedOptions = {
|
|
185
|
-
...useDefaultPacerOptions().asyncDebouncer,
|
|
186
|
-
...options,
|
|
187
|
-
} as SolidAsyncDebouncerOptions<TFn, TSelected>
|
|
188
|
-
const asyncDebouncer = new AsyncDebouncer<TFn>(
|
|
189
|
-
fn,
|
|
190
|
-
mergedOptions,
|
|
191
|
-
) as unknown as SolidAsyncDebouncer<TFn, TSelected>
|
|
192
|
-
|
|
193
|
-
asyncDebouncer.Subscribe = function Subscribe<TSelected>(props: {
|
|
194
|
-
selector: (state: AsyncDebouncerState<TFn>) => TSelected
|
|
195
|
-
children: ((state: Accessor<TSelected>) => JSX.Element) | JSX.Element
|
|
196
|
-
}) {
|
|
197
|
-
const selected = useSelector(asyncDebouncer.store, props.selector, {
|
|
198
|
-
compare: shallow,
|
|
199
|
-
})
|
|
200
|
-
|
|
201
|
-
return typeof props.children === 'function'
|
|
202
|
-
? props.children(selected)
|
|
203
|
-
: props.children
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
const state = useSelector(asyncDebouncer.store, selector, {
|
|
207
|
-
compare: shallow,
|
|
208
|
-
})
|
|
209
|
-
|
|
210
|
-
createEffect(() => {
|
|
211
|
-
onCleanup(() => {
|
|
212
|
-
if (mergedOptions.onUnmount) {
|
|
213
|
-
mergedOptions.onUnmount(asyncDebouncer)
|
|
214
|
-
} else {
|
|
215
|
-
asyncDebouncer.cancel()
|
|
216
|
-
asyncDebouncer.abort()
|
|
217
|
-
}
|
|
218
|
-
})
|
|
219
|
-
})
|
|
220
|
-
|
|
221
|
-
return {
|
|
222
|
-
...asyncDebouncer,
|
|
223
|
-
state,
|
|
224
|
-
} as SolidAsyncDebouncer<TFn, TSelected> // omit `store` in favor of `state`
|
|
225
|
-
}
|
|
@@ -1,226 +0,0 @@
|
|
|
1
|
-
import { AsyncQueuer } from '@tanstack/pacer/async-queuer'
|
|
2
|
-
import { shallow, useSelector } from '@tanstack/solid-store'
|
|
3
|
-
import { createEffect, onCleanup } from 'solid-js'
|
|
4
|
-
import { useDefaultPacerOptions } from '../provider/PacerProvider'
|
|
5
|
-
import type { Store } from '@tanstack/solid-store'
|
|
6
|
-
import type { Accessor, JSX } from 'solid-js'
|
|
7
|
-
import type {
|
|
8
|
-
AsyncQueuerOptions,
|
|
9
|
-
AsyncQueuerState,
|
|
10
|
-
} from '@tanstack/pacer/async-queuer'
|
|
11
|
-
|
|
12
|
-
export interface SolidAsyncQueuerOptions<
|
|
13
|
-
TValue,
|
|
14
|
-
TSelected = {},
|
|
15
|
-
> extends AsyncQueuerOptions<TValue> {
|
|
16
|
-
/**
|
|
17
|
-
* Optional callback invoked when the owning component unmounts. Receives the queuer instance.
|
|
18
|
-
* When provided, replaces the default cleanup (stop + abort); use it to call flush(), flushAsBatch(), stop(), add logging, etc.
|
|
19
|
-
*/
|
|
20
|
-
onUnmount?: (queuer: SolidAsyncQueuer<TValue, TSelected>) => void
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export interface SolidAsyncQueuer<TValue, TSelected = {}> extends Omit<
|
|
24
|
-
AsyncQueuer<TValue>,
|
|
25
|
-
'store'
|
|
26
|
-
> {
|
|
27
|
-
/**
|
|
28
|
-
* A Solid component that allows you to subscribe to the queuer state.
|
|
29
|
-
*
|
|
30
|
-
* This is useful for tracking specific parts of the queuer state
|
|
31
|
-
* deep in your component tree without needing to pass a selector to the hook.
|
|
32
|
-
*
|
|
33
|
-
* @example
|
|
34
|
-
* <queuer.Subscribe selector={(state) => ({ pendingItems: state.pendingItems, activeItems: state.activeItems })}>
|
|
35
|
-
* {(state) => (
|
|
36
|
-
* <div>Pending: {state().pendingItems.length}, Active: {state().activeItems.length}</div>
|
|
37
|
-
* )}
|
|
38
|
-
* </queuer.Subscribe>
|
|
39
|
-
*/
|
|
40
|
-
Subscribe: <TSelected>(props: {
|
|
41
|
-
selector: (state: AsyncQueuerState<TValue>) => TSelected
|
|
42
|
-
children: ((state: Accessor<TSelected>) => JSX.Element) | JSX.Element
|
|
43
|
-
}) => JSX.Element
|
|
44
|
-
/**
|
|
45
|
-
* Reactive state that will be updated when the queuer state changes
|
|
46
|
-
*
|
|
47
|
-
* Use this instead of `queuer.store.state`
|
|
48
|
-
*/
|
|
49
|
-
readonly state: Accessor<Readonly<TSelected>>
|
|
50
|
-
/**
|
|
51
|
-
* @deprecated Use `queuer.state` instead of `queuer.store.state` if you want to read reactive state.
|
|
52
|
-
* The state on the store object is not reactive, as it has not been wrapped in a `useSelector` hook internally.
|
|
53
|
-
* Although, you can make the state reactive by using the `useSelector` in your own usage.
|
|
54
|
-
*/
|
|
55
|
-
readonly store: Store<Readonly<AsyncQueuerState<TValue>>>
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* Creates a Solid-compatible AsyncQueuer instance for managing an asynchronous queue of items, exposing Solid signals for all stateful properties.
|
|
60
|
-
*
|
|
61
|
-
* Features:
|
|
62
|
-
* - Priority queueing via `getPriority` or item `priority` property
|
|
63
|
-
* - Configurable concurrency limit
|
|
64
|
-
* - FIFO (First In First Out) or LIFO (Last In First Out) queue behavior
|
|
65
|
-
* - Pause/resume processing
|
|
66
|
-
* - Task cancellation
|
|
67
|
-
* - Item expiration
|
|
68
|
-
* - Lifecycle callbacks for success, error, settled, items change, etc.
|
|
69
|
-
* - All stateful properties (active items, pending items, counts, etc.) are exposed as Solid signals for reactivity
|
|
70
|
-
*
|
|
71
|
-
* Tasks are processed concurrently up to the configured concurrency limit. When a task completes,
|
|
72
|
-
* the next pending task is processed if the concurrency limit allows.
|
|
73
|
-
*
|
|
74
|
-
* Error Handling:
|
|
75
|
-
* - If an `onError` handler is provided, it will be called with the error and queuer instance
|
|
76
|
-
* - If `throwOnError` is true (default when no onError handler is provided), the error will be thrown
|
|
77
|
-
* - If `throwOnError` is false (default when onError handler is provided), the error will be swallowed
|
|
78
|
-
* - Both onError and throwOnError can be used together; the handler will be called before any error is thrown
|
|
79
|
-
* - The error state can be checked using the underlying AsyncQueuer instance
|
|
80
|
-
*
|
|
81
|
-
* ## State Management and Selector
|
|
82
|
-
*
|
|
83
|
-
* The hook uses TanStack Store for reactive state management. You can subscribe to state changes
|
|
84
|
-
* in two ways:
|
|
85
|
-
*
|
|
86
|
-
* **1. Using `queuer.Subscribe` component (Recommended for component tree subscriptions)**
|
|
87
|
-
*
|
|
88
|
-
* Use the `Subscribe` component to subscribe to state changes deep in your component tree without
|
|
89
|
-
* needing to pass a selector to the hook. This is ideal when you want to subscribe to state
|
|
90
|
-
* in child components.
|
|
91
|
-
*
|
|
92
|
-
* **2. Using the `selector` parameter (For hook-level subscriptions)**
|
|
93
|
-
*
|
|
94
|
-
* The `selector` parameter allows you to specify which state changes will trigger reactive updates
|
|
95
|
-
* at the hook level, optimizing performance by preventing unnecessary updates when irrelevant
|
|
96
|
-
* state changes occur.
|
|
97
|
-
*
|
|
98
|
-
* **By default, there will be no reactive state subscriptions** and you must opt-in to state
|
|
99
|
-
* tracking by providing a selector function or using the `Subscribe` component. This prevents unnecessary
|
|
100
|
-
* updates and gives you full control over when your component tracks state changes.
|
|
101
|
-
*
|
|
102
|
-
* Available state properties:
|
|
103
|
-
* - `activeItems`: Array of items currently being processed
|
|
104
|
-
* - `errorCount`: Number of items that failed processing
|
|
105
|
-
* - `isRunning`: Whether the queuer is currently running (not stopped)
|
|
106
|
-
* - `pendingItems`: Array of items waiting to be processed
|
|
107
|
-
* - `rejectionCount`: Number of items that were rejected (expired or failed validation)
|
|
108
|
-
* - `settleCount`: Number of items that have completed processing (successful or failed)
|
|
109
|
-
* - `successCount`: Number of items that were processed successfully
|
|
110
|
-
*
|
|
111
|
-
* ## Unmount behavior
|
|
112
|
-
*
|
|
113
|
-
* By default, the primitive stops the queuer and aborts any in-flight task executions when the owning component unmounts.
|
|
114
|
-
* Abort only cancels underlying operations (e.g. fetch) when the abort signal from `getAbortSignal()` is passed to them.
|
|
115
|
-
* Use the `onUnmount` option to customize this. For example, to flush pending items instead:
|
|
116
|
-
*
|
|
117
|
-
* ```tsx
|
|
118
|
-
* const queuer = createAsyncQueuer(fn, {
|
|
119
|
-
* concurrency: 2,
|
|
120
|
-
* started: false,
|
|
121
|
-
* onUnmount: (q) => q.flush()
|
|
122
|
-
* });
|
|
123
|
-
* ```
|
|
124
|
-
*
|
|
125
|
-
* Note: For async utils, `flush()` returns a Promise and runs fire-and-forget in the cleanup.
|
|
126
|
-
* If your task function updates Solid signals, those updates may run after the component has
|
|
127
|
-
* unmounted, which can cause unexpected reactive updates. Guard your callbacks accordingly when
|
|
128
|
-
* using onUnmount with flush.
|
|
129
|
-
*
|
|
130
|
-
* Example usage:
|
|
131
|
-
* ```tsx
|
|
132
|
-
* // Default behavior - no reactive state subscriptions
|
|
133
|
-
* const asyncQueuer = createAsyncQueuer(async (item) => {
|
|
134
|
-
* // process item
|
|
135
|
-
* return await fetchData(item);
|
|
136
|
-
* }, {
|
|
137
|
-
* initialItems: [],
|
|
138
|
-
* concurrency: 2,
|
|
139
|
-
* maxSize: 100,
|
|
140
|
-
* started: false,
|
|
141
|
-
* onSuccess: (result) => {
|
|
142
|
-
* console.log('Item processed:', result);
|
|
143
|
-
* },
|
|
144
|
-
* onError: (error) => {
|
|
145
|
-
* console.error('Processing failed:', error);
|
|
146
|
-
* }
|
|
147
|
-
* });
|
|
148
|
-
*
|
|
149
|
-
* // Opt-in to track queue state changes (optimized for UI updates)
|
|
150
|
-
* const asyncQueuer = createAsyncQueuer(
|
|
151
|
-
* async (item) => await fetchData(item),
|
|
152
|
-
* { concurrency: 2, started: true },
|
|
153
|
-
* (state) => ({
|
|
154
|
-
* pendingItems: state.pendingItems,
|
|
155
|
-
* activeItems: state.activeItems,
|
|
156
|
-
* isRunning: state.isRunning
|
|
157
|
-
* })
|
|
158
|
-
* );
|
|
159
|
-
*
|
|
160
|
-
* // Opt-in to track processing metrics changes (optimized for tracking progress)
|
|
161
|
-
* const asyncQueuer = createAsyncQueuer(
|
|
162
|
-
* async (item) => await fetchData(item),
|
|
163
|
-
* { concurrency: 2, started: true },
|
|
164
|
-
* (state) => ({
|
|
165
|
-
* successCount: state.successCount,
|
|
166
|
-
* errorCount: state.errorCount,
|
|
167
|
-
* settleCount: state.settleCount
|
|
168
|
-
* })
|
|
169
|
-
* );
|
|
170
|
-
*
|
|
171
|
-
* // Add items to queue
|
|
172
|
-
* asyncQueuer.addItem(newItem);
|
|
173
|
-
*
|
|
174
|
-
* // Start processing
|
|
175
|
-
* asyncQueuer.start();
|
|
176
|
-
*
|
|
177
|
-
* // Access the selected state (will be empty object {} unless selector provided)
|
|
178
|
-
* const { pendingItems, activeItems } = asyncQueuer.state();
|
|
179
|
-
* ```
|
|
180
|
-
*/
|
|
181
|
-
export function createAsyncQueuer<TValue, TSelected = {}>(
|
|
182
|
-
fn: (value: TValue) => Promise<any>,
|
|
183
|
-
options: SolidAsyncQueuerOptions<TValue, TSelected> = {},
|
|
184
|
-
selector: (state: AsyncQueuerState<TValue>) => TSelected = () =>
|
|
185
|
-
({}) as TSelected,
|
|
186
|
-
): SolidAsyncQueuer<TValue, TSelected> {
|
|
187
|
-
const mergedOptions = {
|
|
188
|
-
...useDefaultPacerOptions().asyncQueuer,
|
|
189
|
-
...options,
|
|
190
|
-
} as SolidAsyncQueuerOptions<TValue, TSelected>
|
|
191
|
-
const asyncQueuer = new AsyncQueuer<TValue>(
|
|
192
|
-
fn,
|
|
193
|
-
mergedOptions,
|
|
194
|
-
) as unknown as SolidAsyncQueuer<TValue, TSelected>
|
|
195
|
-
|
|
196
|
-
asyncQueuer.Subscribe = function Subscribe<TSelected>(props: {
|
|
197
|
-
selector: (state: AsyncQueuerState<TValue>) => TSelected
|
|
198
|
-
children: ((state: Accessor<TSelected>) => JSX.Element) | JSX.Element
|
|
199
|
-
}) {
|
|
200
|
-
const selected = useSelector(asyncQueuer.store, props.selector, {
|
|
201
|
-
compare: shallow,
|
|
202
|
-
})
|
|
203
|
-
|
|
204
|
-
return typeof props.children === 'function'
|
|
205
|
-
? props.children(selected)
|
|
206
|
-
: props.children
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
const state = useSelector(asyncQueuer.store, selector, { compare: shallow })
|
|
210
|
-
|
|
211
|
-
createEffect(() => {
|
|
212
|
-
onCleanup(() => {
|
|
213
|
-
if (mergedOptions.onUnmount) {
|
|
214
|
-
mergedOptions.onUnmount(asyncQueuer)
|
|
215
|
-
} else {
|
|
216
|
-
asyncQueuer.stop()
|
|
217
|
-
asyncQueuer.abort()
|
|
218
|
-
}
|
|
219
|
-
})
|
|
220
|
-
})
|
|
221
|
-
|
|
222
|
-
return {
|
|
223
|
-
...asyncQueuer,
|
|
224
|
-
state,
|
|
225
|
-
} as SolidAsyncQueuer<TValue, TSelected> // omit `store` in favor of `state`
|
|
226
|
-
}
|