@tanstack/solid-pacer 0.17.0 → 0.18.1
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/async-batcher/createAsyncBatcher.cjs +26 -12
- package/dist/async-batcher/createAsyncBatcher.cjs.map +1 -1
- package/dist/async-batcher/createAsyncBatcher.d.cts +36 -9
- package/dist/async-batcher/createAsyncBatcher.d.ts +36 -9
- package/dist/async-batcher/createAsyncBatcher.js +22 -8
- package/dist/async-batcher/createAsyncBatcher.js.map +1 -1
- package/dist/async-batcher/index.cjs +3 -3
- package/dist/async-debouncer/createAsyncDebouncer.cjs +26 -12
- package/dist/async-debouncer/createAsyncDebouncer.cjs.map +1 -1
- package/dist/async-debouncer/createAsyncDebouncer.d.cts +36 -9
- package/dist/async-debouncer/createAsyncDebouncer.d.ts +36 -9
- package/dist/async-debouncer/createAsyncDebouncer.js +22 -8
- package/dist/async-debouncer/createAsyncDebouncer.js.map +1 -1
- package/dist/async-debouncer/index.cjs +3 -3
- package/dist/async-queuer/createAsyncQueuer.cjs +26 -12
- package/dist/async-queuer/createAsyncQueuer.cjs.map +1 -1
- package/dist/async-queuer/createAsyncQueuer.d.cts +36 -9
- package/dist/async-queuer/createAsyncQueuer.d.ts +36 -9
- package/dist/async-queuer/createAsyncQueuer.js +22 -8
- package/dist/async-queuer/createAsyncQueuer.js.map +1 -1
- package/dist/async-queuer/index.cjs +3 -3
- package/dist/async-rate-limiter/createAsyncRateLimiter.cjs +89 -30
- package/dist/async-rate-limiter/createAsyncRateLimiter.cjs.map +1 -1
- package/dist/async-rate-limiter/createAsyncRateLimiter.d.cts +99 -27
- package/dist/async-rate-limiter/createAsyncRateLimiter.d.ts +99 -27
- package/dist/async-rate-limiter/createAsyncRateLimiter.js +85 -26
- package/dist/async-rate-limiter/createAsyncRateLimiter.js.map +1 -1
- package/dist/async-rate-limiter/index.cjs +3 -3
- package/dist/async-throttler/createAsyncThrottler.cjs +26 -12
- package/dist/async-throttler/createAsyncThrottler.cjs.map +1 -1
- package/dist/async-throttler/createAsyncThrottler.d.cts +36 -9
- package/dist/async-throttler/createAsyncThrottler.d.ts +36 -9
- package/dist/async-throttler/createAsyncThrottler.js +22 -8
- package/dist/async-throttler/createAsyncThrottler.js.map +1 -1
- package/dist/async-throttler/index.cjs +3 -3
- package/dist/batcher/createBatcher.cjs +26 -12
- package/dist/batcher/createBatcher.cjs.map +1 -1
- package/dist/batcher/createBatcher.d.cts +36 -9
- package/dist/batcher/createBatcher.d.ts +36 -9
- package/dist/batcher/createBatcher.js +22 -8
- package/dist/batcher/createBatcher.js.map +1 -1
- package/dist/batcher/index.cjs +3 -3
- package/dist/debouncer/createDebouncer.cjs +27 -13
- package/dist/debouncer/createDebouncer.cjs.map +1 -1
- package/dist/debouncer/createDebouncer.d.cts +37 -10
- package/dist/debouncer/createDebouncer.d.ts +37 -10
- package/dist/debouncer/createDebouncer.js +23 -9
- package/dist/debouncer/createDebouncer.js.map +1 -1
- package/dist/debouncer/index.cjs +3 -3
- package/dist/index.cjs +3 -3
- package/dist/queuer/createQueuedSignal.cjs +8 -8
- package/dist/queuer/createQueuedSignal.cjs.map +1 -1
- package/dist/queuer/createQueuedSignal.d.cts +8 -8
- package/dist/queuer/createQueuedSignal.d.ts +8 -8
- package/dist/queuer/createQueuedSignal.js +8 -8
- package/dist/queuer/createQueuedSignal.js.map +1 -1
- package/dist/queuer/createQueuer.cjs +26 -12
- package/dist/queuer/createQueuer.cjs.map +1 -1
- package/dist/queuer/createQueuer.d.cts +36 -9
- package/dist/queuer/createQueuer.d.ts +36 -9
- package/dist/queuer/createQueuer.js +22 -8
- package/dist/queuer/createQueuer.js.map +1 -1
- package/dist/queuer/index.cjs +3 -3
- package/dist/rate-limiter/createRateLimiter.cjs +78 -23
- package/dist/rate-limiter/createRateLimiter.cjs.map +1 -1
- package/dist/rate-limiter/createRateLimiter.d.cts +88 -20
- package/dist/rate-limiter/createRateLimiter.d.ts +88 -20
- package/dist/rate-limiter/createRateLimiter.js +74 -19
- package/dist/rate-limiter/createRateLimiter.js.map +1 -1
- package/dist/rate-limiter/index.cjs +3 -3
- package/dist/throttler/createThrottledSignal.cjs +1 -1
- package/dist/throttler/createThrottledSignal.cjs.map +1 -1
- package/dist/throttler/createThrottledSignal.d.cts +1 -1
- package/dist/throttler/createThrottledSignal.d.ts +1 -1
- package/dist/throttler/createThrottledSignal.js +1 -1
- package/dist/throttler/createThrottledSignal.js.map +1 -1
- package/dist/throttler/createThrottledValue.cjs +1 -1
- package/dist/throttler/createThrottledValue.cjs.map +1 -1
- package/dist/throttler/createThrottledValue.d.cts +1 -1
- package/dist/throttler/createThrottledValue.d.ts +1 -1
- package/dist/throttler/createThrottledValue.js +1 -1
- package/dist/throttler/createThrottledValue.js.map +1 -1
- package/dist/throttler/createThrottler.cjs +34 -13
- package/dist/throttler/createThrottler.cjs.map +1 -1
- package/dist/throttler/createThrottler.d.cts +44 -10
- package/dist/throttler/createThrottler.d.ts +44 -10
- package/dist/throttler/createThrottler.js +30 -9
- package/dist/throttler/createThrottler.js.map +1 -1
- package/dist/throttler/index.cjs +3 -3
- package/dist/types/index.cjs +3 -3
- package/dist/utils/index.cjs +3 -3
- package/package.json +2 -2
- package/src/async-batcher/createAsyncBatcher.ts +51 -10
- package/src/async-debouncer/createAsyncDebouncer.ts +51 -10
- package/src/async-queuer/createAsyncQueuer.ts +51 -10
- package/src/async-rate-limiter/createAsyncRateLimiter.ts +114 -28
- package/src/async-throttler/createAsyncThrottler.ts +51 -10
- package/src/batcher/createBatcher.ts +51 -10
- package/src/debouncer/createDebouncer.ts +52 -11
- package/src/queuer/createQueuedSignal.ts +8 -8
- package/src/queuer/createQueuer.ts +51 -10
- package/src/rate-limiter/createRateLimiter.ts +103 -21
- package/src/throttler/createThrottledSignal.ts +1 -1
- package/src/throttler/createThrottledValue.ts +1 -1
- package/src/throttler/createThrottler.ts +59 -11
|
@@ -2,13 +2,30 @@ import { Queuer } from '@tanstack/pacer/queuer'
|
|
|
2
2
|
import { useStore } from '@tanstack/solid-store'
|
|
3
3
|
import { useDefaultPacerOptions } from '../provider/PacerProvider'
|
|
4
4
|
import type { Store } from '@tanstack/solid-store'
|
|
5
|
-
import type { Accessor } from 'solid-js'
|
|
5
|
+
import type { Accessor, JSX } from 'solid-js'
|
|
6
6
|
import type { QueuerOptions, QueuerState } from '@tanstack/pacer/queuer'
|
|
7
7
|
|
|
8
8
|
export interface SolidQueuer<TValue, TSelected = {}> extends Omit<
|
|
9
9
|
Queuer<TValue>,
|
|
10
10
|
'store'
|
|
11
11
|
> {
|
|
12
|
+
/**
|
|
13
|
+
* A Solid component that allows you to subscribe to the queuer state.
|
|
14
|
+
*
|
|
15
|
+
* This is useful for tracking specific parts of the queuer state
|
|
16
|
+
* deep in your component tree without needing to pass a selector to the hook.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* <queuer.Subscribe selector={(state) => ({ size: state.size, isRunning: state.isRunning })}>
|
|
20
|
+
* {(state) => (
|
|
21
|
+
* <div>Queue: {state().size} items, {state().isRunning ? 'Processing' : 'Idle'}</div>
|
|
22
|
+
* )}
|
|
23
|
+
* </queuer.Subscribe>
|
|
24
|
+
*/
|
|
25
|
+
Subscribe: <TSelected>(props: {
|
|
26
|
+
selector: (state: QueuerState<TValue>) => TSelected
|
|
27
|
+
children: ((state: Accessor<TSelected>) => JSX.Element) | JSX.Element
|
|
28
|
+
}) => JSX.Element
|
|
12
29
|
/**
|
|
13
30
|
* Reactive state that will be updated when the queuer state changes
|
|
14
31
|
*
|
|
@@ -42,14 +59,24 @@ export interface SolidQueuer<TValue, TSelected = {}> extends Omit<
|
|
|
42
59
|
*
|
|
43
60
|
* ## State Management and Selector
|
|
44
61
|
*
|
|
45
|
-
* The hook uses TanStack Store for reactive state management.
|
|
46
|
-
*
|
|
47
|
-
*
|
|
62
|
+
* The hook uses TanStack Store for reactive state management. You can subscribe to state changes
|
|
63
|
+
* in two ways:
|
|
64
|
+
*
|
|
65
|
+
* **1. Using `queuer.Subscribe` component (Recommended for component tree subscriptions)**
|
|
66
|
+
*
|
|
67
|
+
* Use the `Subscribe` component to subscribe to state changes deep in your component tree without
|
|
68
|
+
* needing to pass a selector to the hook. This is ideal when you want to subscribe to state
|
|
69
|
+
* in child components.
|
|
70
|
+
*
|
|
71
|
+
* **2. Using the `selector` parameter (For hook-level subscriptions)**
|
|
72
|
+
*
|
|
73
|
+
* The `selector` parameter allows you to specify which state changes will trigger reactive updates
|
|
74
|
+
* at the hook level, optimizing performance by preventing unnecessary updates when irrelevant
|
|
75
|
+
* state changes occur.
|
|
48
76
|
*
|
|
49
77
|
* **By default, there will be no reactive state subscriptions** and you must opt-in to state
|
|
50
|
-
* tracking by providing a selector function. This prevents unnecessary
|
|
51
|
-
* full control over when your component
|
|
52
|
-
* component re-render when the selected state values change.
|
|
78
|
+
* tracking by providing a selector function or using the `Subscribe` component. This prevents unnecessary
|
|
79
|
+
* updates and gives you full control over when your component tracks state changes.
|
|
53
80
|
*
|
|
54
81
|
* Available state properties:
|
|
55
82
|
* - `executionCount`: Number of items that have been processed
|
|
@@ -72,14 +99,14 @@ export interface SolidQueuer<TValue, TSelected = {}> extends Omit<
|
|
|
72
99
|
* }
|
|
73
100
|
* );
|
|
74
101
|
*
|
|
75
|
-
* // Opt-in to
|
|
102
|
+
* // Opt-in to track items or isRunning changes (optimized for UI updates)
|
|
76
103
|
* const queue = createQueuer(
|
|
77
104
|
* (item) => console.log('Processing', item),
|
|
78
105
|
* { started: true, wait: 1000 },
|
|
79
106
|
* (state) => ({ items: state.items, isRunning: state.isRunning })
|
|
80
107
|
* );
|
|
81
108
|
*
|
|
82
|
-
* // Opt-in to
|
|
109
|
+
* // Opt-in to track execution metrics changes (optimized for tracking progress)
|
|
83
110
|
* const queue = createQueuer(
|
|
84
111
|
* (item) => console.log('Processing', item),
|
|
85
112
|
* { started: true, wait: 1000 },
|
|
@@ -111,7 +138,21 @@ export function createQueuer<TValue, TSelected = {}>(
|
|
|
111
138
|
...options,
|
|
112
139
|
} as QueuerOptions<TValue>
|
|
113
140
|
|
|
114
|
-
const queuer = new Queuer(fn, mergedOptions)
|
|
141
|
+
const queuer = new Queuer(fn, mergedOptions) as unknown as SolidQueuer<
|
|
142
|
+
TValue,
|
|
143
|
+
TSelected
|
|
144
|
+
>
|
|
145
|
+
|
|
146
|
+
queuer.Subscribe = function Subscribe<TSelected>(props: {
|
|
147
|
+
selector: (state: QueuerState<TValue>) => TSelected
|
|
148
|
+
children: ((state: Accessor<TSelected>) => JSX.Element) | JSX.Element
|
|
149
|
+
}) {
|
|
150
|
+
const selected = useStore(queuer.store, props.selector)
|
|
151
|
+
|
|
152
|
+
return typeof props.children === 'function'
|
|
153
|
+
? props.children(selected)
|
|
154
|
+
: props.children
|
|
155
|
+
}
|
|
115
156
|
|
|
116
157
|
const state = useStore(queuer.store, selector)
|
|
117
158
|
|
|
@@ -2,7 +2,7 @@ import { RateLimiter } from '@tanstack/pacer/rate-limiter'
|
|
|
2
2
|
import { useStore } from '@tanstack/solid-store'
|
|
3
3
|
import { useDefaultPacerOptions } from '../provider/PacerProvider'
|
|
4
4
|
import type { Store } from '@tanstack/solid-store'
|
|
5
|
-
import type { Accessor } from 'solid-js'
|
|
5
|
+
import type { Accessor, JSX } from 'solid-js'
|
|
6
6
|
import type { AnyFunction } from '@tanstack/pacer/types'
|
|
7
7
|
import type {
|
|
8
8
|
RateLimiterOptions,
|
|
@@ -13,6 +13,23 @@ export interface SolidRateLimiter<
|
|
|
13
13
|
TFn extends AnyFunction,
|
|
14
14
|
TSelected = {},
|
|
15
15
|
> extends Omit<RateLimiter<TFn>, 'store'> {
|
|
16
|
+
/**
|
|
17
|
+
* A Solid component that allows you to subscribe to the rate limiter state.
|
|
18
|
+
*
|
|
19
|
+
* This is useful for tracking specific parts of the rate limiter state
|
|
20
|
+
* deep in your component tree without needing to pass a selector to the hook.
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* <rateLimiter.Subscribe selector={(state) => ({ rejectionCount: state.rejectionCount })}>
|
|
24
|
+
* {(state) => (
|
|
25
|
+
* <div>Rejections: {state().rejectionCount}</div>
|
|
26
|
+
* )}
|
|
27
|
+
* </rateLimiter.Subscribe>
|
|
28
|
+
*/
|
|
29
|
+
Subscribe: <TSelected>(props: {
|
|
30
|
+
selector: (state: RateLimiterState) => TSelected
|
|
31
|
+
children: ((state: Accessor<TSelected>) => JSX.Element) | JSX.Element
|
|
32
|
+
}) => JSX.Element
|
|
16
33
|
/**
|
|
17
34
|
* Reactive state that will be updated when the rate limiter state changes
|
|
18
35
|
*
|
|
@@ -50,14 +67,24 @@ export interface SolidRateLimiter<
|
|
|
50
67
|
*
|
|
51
68
|
* ## State Management and Selector
|
|
52
69
|
*
|
|
53
|
-
* The hook uses TanStack Store for reactive state management.
|
|
54
|
-
*
|
|
55
|
-
*
|
|
70
|
+
* The hook uses TanStack Store for reactive state management. You can subscribe to state changes
|
|
71
|
+
* in two ways:
|
|
72
|
+
*
|
|
73
|
+
* **1. Using `rateLimiter.Subscribe` component (Recommended for component tree subscriptions)**
|
|
74
|
+
*
|
|
75
|
+
* Use the `Subscribe` component to subscribe to state changes deep in your component tree without
|
|
76
|
+
* needing to pass a selector to the hook. This is ideal when you want to subscribe to state
|
|
77
|
+
* in child components.
|
|
78
|
+
*
|
|
79
|
+
* **2. Using the `selector` parameter (For hook-level subscriptions)**
|
|
80
|
+
*
|
|
81
|
+
* The `selector` parameter allows you to specify which state changes will trigger reactive updates
|
|
82
|
+
* at the hook level, optimizing performance by preventing unnecessary updates when irrelevant
|
|
83
|
+
* state changes occur.
|
|
56
84
|
*
|
|
57
85
|
* **By default, there will be no reactive state subscriptions** and you must opt-in to state
|
|
58
|
-
* tracking by providing a selector function. This prevents unnecessary
|
|
59
|
-
* full control over when your component
|
|
60
|
-
* component re-render when the selected state values change.
|
|
86
|
+
* tracking by providing a selector function or using the `Subscribe` component. This prevents unnecessary
|
|
87
|
+
* updates and gives you full control over when your component tracks state changes.
|
|
61
88
|
*
|
|
62
89
|
* Available state properties:
|
|
63
90
|
* - `executionCount`: Number of function executions that have been completed
|
|
@@ -73,33 +100,74 @@ export interface SolidRateLimiter<
|
|
|
73
100
|
* limit: 5,
|
|
74
101
|
* window: 60000,
|
|
75
102
|
* windowType: 'sliding',
|
|
76
|
-
* onReject: (rateLimiter) => {
|
|
77
|
-
* console.log(`Rate limit exceeded. Try again in ${rateLimiter.getMsUntilNextWindow()}ms`);
|
|
78
|
-
* }
|
|
79
103
|
* });
|
|
80
104
|
*
|
|
81
|
-
* //
|
|
105
|
+
* // Subscribe to state changes deep in component tree using Subscribe component
|
|
106
|
+
* <rateLimiter.Subscribe selector={(state) => ({ rejectionCount: state.rejectionCount })}>
|
|
107
|
+
* {(state) => (
|
|
108
|
+
* <div>Rejections: {state().rejectionCount}</div>
|
|
109
|
+
* )}
|
|
110
|
+
* </rateLimiter.Subscribe>
|
|
111
|
+
*
|
|
112
|
+
* // Opt-in to track execution count changes at hook level (optimized for tracking successful executions)
|
|
82
113
|
* const rateLimiter = createRateLimiter(
|
|
83
114
|
* apiCall,
|
|
84
|
-
* {
|
|
85
|
-
*
|
|
86
|
-
*
|
|
87
|
-
*
|
|
88
|
-
* }
|
|
115
|
+
* {
|
|
116
|
+
* limit: 5,
|
|
117
|
+
* window: 60000,
|
|
118
|
+
* windowType: 'sliding',
|
|
119
|
+
* },
|
|
120
|
+
* (state) => ({ executionCount: state.executionCount })
|
|
89
121
|
* );
|
|
90
122
|
*
|
|
91
|
-
* // Opt-in to
|
|
123
|
+
* // Opt-in to track rejection count changes (optimized for tracking rate limit violations)
|
|
92
124
|
* const rateLimiter = createRateLimiter(
|
|
93
125
|
* apiCall,
|
|
94
|
-
* {
|
|
126
|
+
* {
|
|
127
|
+
* limit: 5,
|
|
128
|
+
* window: 60000,
|
|
129
|
+
* windowType: 'sliding',
|
|
130
|
+
* },
|
|
131
|
+
* (state) => ({ rejectionCount: state.rejectionCount })
|
|
132
|
+
* );
|
|
133
|
+
*
|
|
134
|
+
* // Opt-in to track execution times changes (optimized for window calculations)
|
|
135
|
+
* const rateLimiter = createRateLimiter(
|
|
136
|
+
* apiCall,
|
|
137
|
+
* {
|
|
138
|
+
* limit: 5,
|
|
139
|
+
* window: 60000,
|
|
140
|
+
* windowType: 'sliding',
|
|
141
|
+
* },
|
|
142
|
+
* (state) => ({ executionTimes: state.executionTimes })
|
|
143
|
+
* );
|
|
144
|
+
*
|
|
145
|
+
* // Multiple state properties - track when any of these change
|
|
146
|
+
* const rateLimiter = createRateLimiter(
|
|
147
|
+
* apiCall,
|
|
148
|
+
* {
|
|
149
|
+
* limit: 5,
|
|
150
|
+
* window: 60000,
|
|
151
|
+
* windowType: 'sliding',
|
|
152
|
+
* },
|
|
95
153
|
* (state) => ({
|
|
96
154
|
* executionCount: state.executionCount,
|
|
97
|
-
*
|
|
155
|
+
* rejectionCount: state.rejectionCount
|
|
98
156
|
* })
|
|
99
157
|
* );
|
|
100
158
|
*
|
|
159
|
+
* // Monitor rate limit status
|
|
160
|
+
* const handleClick = () => {
|
|
161
|
+
* const remaining = rateLimiter.getRemainingInWindow();
|
|
162
|
+
* if (remaining > 0) {
|
|
163
|
+
* rateLimiter.maybeExecute(data);
|
|
164
|
+
* } else {
|
|
165
|
+
* showRateLimitWarning();
|
|
166
|
+
* }
|
|
167
|
+
* };
|
|
168
|
+
*
|
|
101
169
|
* // Access the selected state (will be empty object {} unless selector provided)
|
|
102
|
-
* const {
|
|
170
|
+
* const { executionCount, rejectionCount } = rateLimiter.state();
|
|
103
171
|
* ```
|
|
104
172
|
*/
|
|
105
173
|
export function createRateLimiter<TFn extends AnyFunction, TSelected = {}>(
|
|
@@ -112,7 +180,21 @@ export function createRateLimiter<TFn extends AnyFunction, TSelected = {}>(
|
|
|
112
180
|
...options,
|
|
113
181
|
} as RateLimiterOptions<TFn>
|
|
114
182
|
|
|
115
|
-
const rateLimiter = new RateLimiter<TFn>(
|
|
183
|
+
const rateLimiter = new RateLimiter<TFn>(
|
|
184
|
+
fn,
|
|
185
|
+
mergedOptions,
|
|
186
|
+
) as unknown as SolidRateLimiter<TFn, TSelected>
|
|
187
|
+
|
|
188
|
+
rateLimiter.Subscribe = function Subscribe<TSelected>(props: {
|
|
189
|
+
selector: (state: RateLimiterState) => TSelected
|
|
190
|
+
children: ((state: Accessor<TSelected>) => JSX.Element) | JSX.Element
|
|
191
|
+
}) {
|
|
192
|
+
const selected = useStore(rateLimiter.store, props.selector)
|
|
193
|
+
|
|
194
|
+
return typeof props.children === 'function'
|
|
195
|
+
? props.children(selected)
|
|
196
|
+
: props.children
|
|
197
|
+
}
|
|
116
198
|
|
|
117
199
|
const state = useStore(rateLimiter.store, selector)
|
|
118
200
|
|
|
@@ -12,7 +12,7 @@ import type {
|
|
|
12
12
|
* This hook combines Solid's createSignal with throttling functionality to provide controlled state updates.
|
|
13
13
|
*
|
|
14
14
|
* Throttling ensures state updates occur at a controlled rate regardless of how frequently the setter is called.
|
|
15
|
-
* This is useful for rate-limiting expensive
|
|
15
|
+
* This is useful for rate-limiting expensive updates or operations that depend on rapidly changing state.
|
|
16
16
|
*
|
|
17
17
|
* The hook returns a tuple containing:
|
|
18
18
|
* - The throttled state value accessor
|
|
@@ -12,7 +12,7 @@ import type {
|
|
|
12
12
|
* This hook uses Solid's createSignal internally to manage the throttled state.
|
|
13
13
|
*
|
|
14
14
|
* Throttling ensures the value updates occur at a controlled rate regardless of how frequently the input value changes.
|
|
15
|
-
* This is useful for rate-limiting expensive
|
|
15
|
+
* This is useful for rate-limiting expensive updates or API calls that depend on rapidly changing values.
|
|
16
16
|
*
|
|
17
17
|
* The hook returns a tuple containing:
|
|
18
18
|
* - An accessor function that provides the throttled value
|
|
@@ -3,7 +3,7 @@ import { createEffect, onCleanup } from 'solid-js'
|
|
|
3
3
|
import { useStore } from '@tanstack/solid-store'
|
|
4
4
|
import { useDefaultPacerOptions } from '../provider/PacerProvider'
|
|
5
5
|
import type { Store } from '@tanstack/solid-store'
|
|
6
|
-
import type { Accessor } from 'solid-js'
|
|
6
|
+
import type { Accessor, JSX } from 'solid-js'
|
|
7
7
|
import type { AnyFunction } from '@tanstack/pacer/types'
|
|
8
8
|
import type {
|
|
9
9
|
ThrottlerOptions,
|
|
@@ -14,6 +14,23 @@ export interface SolidThrottler<
|
|
|
14
14
|
TFn extends AnyFunction,
|
|
15
15
|
TSelected = {},
|
|
16
16
|
> extends Omit<Throttler<TFn>, 'store'> {
|
|
17
|
+
/**
|
|
18
|
+
* A Solid component that allows you to subscribe to the throttler state.
|
|
19
|
+
*
|
|
20
|
+
* This is useful for tracking specific parts of the throttler state
|
|
21
|
+
* deep in your component tree without needing to pass a selector to the hook.
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* <throttler.Subscribe selector={(state) => ({ isPending: state.isPending })}>
|
|
25
|
+
* {(state) => (
|
|
26
|
+
* <div>{state().isPending ? 'Loading...' : 'Ready'}</div>
|
|
27
|
+
* )}
|
|
28
|
+
* </throttler.Subscribe>
|
|
29
|
+
*/
|
|
30
|
+
Subscribe: <TSelected>(props: {
|
|
31
|
+
selector: (state: ThrottlerState<TFn>) => TSelected
|
|
32
|
+
children: ((state: Accessor<TSelected>) => JSX.Element) | JSX.Element
|
|
33
|
+
}) => JSX.Element
|
|
17
34
|
/**
|
|
18
35
|
* Reactive state that will be updated when the throttler state changes
|
|
19
36
|
*
|
|
@@ -41,14 +58,24 @@ export interface SolidThrottler<
|
|
|
41
58
|
*
|
|
42
59
|
* ## State Management and Selector
|
|
43
60
|
*
|
|
44
|
-
* The hook uses TanStack Store for reactive state management.
|
|
45
|
-
*
|
|
46
|
-
*
|
|
61
|
+
* The hook uses TanStack Store for reactive state management. You can subscribe to state changes
|
|
62
|
+
* in two ways:
|
|
63
|
+
*
|
|
64
|
+
* **1. Using `throttler.Subscribe` component (Recommended for component tree subscriptions)**
|
|
65
|
+
*
|
|
66
|
+
* Use the `Subscribe` component to subscribe to state changes deep in your component tree without
|
|
67
|
+
* needing to pass a selector to the hook. This is ideal when you want to subscribe to state
|
|
68
|
+
* in child components.
|
|
69
|
+
*
|
|
70
|
+
* **2. Using the `selector` parameter (For hook-level subscriptions)**
|
|
71
|
+
*
|
|
72
|
+
* The `selector` parameter allows you to specify which state changes will trigger reactive updates
|
|
73
|
+
* at the hook level, optimizing performance by preventing unnecessary updates when irrelevant
|
|
74
|
+
* state changes occur.
|
|
47
75
|
*
|
|
48
76
|
* **By default, there will be no reactive state subscriptions** and you must opt-in to state
|
|
49
|
-
* tracking by providing a selector function. This prevents unnecessary
|
|
50
|
-
* full control over when your component
|
|
51
|
-
* component re-render when the selected state values change.
|
|
77
|
+
* tracking by providing a selector function or using the `Subscribe` component. This prevents unnecessary
|
|
78
|
+
* updates and gives you full control over when your component tracks state changes.
|
|
52
79
|
*
|
|
53
80
|
* Available state properties:
|
|
54
81
|
* - `canLeadingExecute`: Whether the throttler can execute on the leading edge
|
|
@@ -65,21 +92,28 @@ export interface SolidThrottler<
|
|
|
65
92
|
* // Default behavior - no reactive state subscriptions
|
|
66
93
|
* const throttler = createThrottler(setValue, { wait: 1000 });
|
|
67
94
|
*
|
|
68
|
-
* //
|
|
95
|
+
* // Subscribe to state changes deep in component tree using Subscribe component
|
|
96
|
+
* <throttler.Subscribe selector={(state) => ({ isPending: state.isPending })}>
|
|
97
|
+
* {(state) => (
|
|
98
|
+
* <div>{state().isPending ? 'Loading...' : 'Ready'}</div>
|
|
99
|
+
* )}
|
|
100
|
+
* </throttler.Subscribe>
|
|
101
|
+
*
|
|
102
|
+
* // Opt-in to track isPending changes at hook level (optimized for loading states)
|
|
69
103
|
* const throttler = createThrottler(
|
|
70
104
|
* setValue,
|
|
71
105
|
* { wait: 1000 },
|
|
72
106
|
* (state) => ({ isPending: state.isPending })
|
|
73
107
|
* );
|
|
74
108
|
*
|
|
75
|
-
* // Opt-in to
|
|
109
|
+
* // Opt-in to track executionCount changes (optimized for tracking execution)
|
|
76
110
|
* const throttler = createThrottler(
|
|
77
111
|
* setValue,
|
|
78
112
|
* { wait: 1000 },
|
|
79
113
|
* (state) => ({ executionCount: state.executionCount })
|
|
80
114
|
* );
|
|
81
115
|
*
|
|
82
|
-
* // Multiple state properties -
|
|
116
|
+
* // Multiple state properties - track when any of these change
|
|
83
117
|
* const throttler = createThrottler(
|
|
84
118
|
* setValue,
|
|
85
119
|
* {
|
|
@@ -109,7 +143,21 @@ export function createThrottler<TFn extends AnyFunction, TSelected = {}>(
|
|
|
109
143
|
...options,
|
|
110
144
|
} as ThrottlerOptions<TFn>
|
|
111
145
|
|
|
112
|
-
const asyncThrottler = new Throttler<TFn>(
|
|
146
|
+
const asyncThrottler = new Throttler<TFn>(
|
|
147
|
+
fn,
|
|
148
|
+
mergedOptions,
|
|
149
|
+
) as unknown as SolidThrottler<TFn, TSelected>
|
|
150
|
+
|
|
151
|
+
asyncThrottler.Subscribe = function Subscribe<TSelected>(props: {
|
|
152
|
+
selector: (state: ThrottlerState<TFn>) => TSelected
|
|
153
|
+
children: ((state: Accessor<TSelected>) => JSX.Element) | JSX.Element
|
|
154
|
+
}) {
|
|
155
|
+
const selected = useStore(asyncThrottler.store, props.selector)
|
|
156
|
+
|
|
157
|
+
return typeof props.children === 'function'
|
|
158
|
+
? props.children(selected)
|
|
159
|
+
: props.children
|
|
160
|
+
}
|
|
113
161
|
|
|
114
162
|
const state = useStore(asyncThrottler.store, selector)
|
|
115
163
|
|