@tanstack/solid-pacer 0.21.1 → 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.
Files changed (170) hide show
  1. package/README.md +21 -6
  2. package/dist/async-batcher/createAsyncBatcher.d.ts +5 -10
  3. package/dist/async-batcher/createAsyncBatcher.js +2 -5
  4. package/dist/async-debouncer/createAsyncDebouncer.d.ts +8 -12
  5. package/dist/async-debouncer/createAsyncDebouncer.js +5 -7
  6. package/dist/async-queuer/createAsyncQueuer.d.ts +9 -12
  7. package/dist/async-queuer/createAsyncQueuer.js +4 -5
  8. package/dist/async-rate-limiter/createAsyncRateLimiter.d.ts +10 -13
  9. package/dist/async-rate-limiter/createAsyncRateLimiter.js +7 -8
  10. package/dist/async-throttler/createAsyncThrottler.d.ts +8 -14
  11. package/dist/async-throttler/createAsyncThrottler.js +5 -9
  12. package/dist/batcher/createBatcher.d.ts +11 -15
  13. package/dist/batcher/createBatcher.js +6 -8
  14. package/dist/debouncer/createDebouncedSignal.d.ts +2 -5
  15. package/dist/debouncer/createDebouncedSignal.js +1 -2
  16. package/dist/debouncer/createDebouncedValue.d.ts +2 -5
  17. package/dist/debouncer/createDebouncedValue.js +1 -2
  18. package/dist/debouncer/createDebouncer.d.ts +4 -7
  19. package/dist/debouncer/createDebouncer.js +1 -2
  20. package/dist/provider/PacerProvider.d.ts +6 -9
  21. package/dist/provider/PacerProvider.js +1 -2
  22. package/dist/queuer/createQueuedSignal.d.ts +2 -5
  23. package/dist/queuer/createQueuedSignal.js +1 -2
  24. package/dist/queuer/createQueuer.d.ts +4 -7
  25. package/dist/queuer/createQueuer.js +1 -2
  26. package/dist/rate-limiter/createRateLimitedSignal.d.ts +12 -14
  27. package/dist/rate-limiter/createRateLimitedSignal.js +11 -11
  28. package/dist/rate-limiter/createRateLimitedValue.d.ts +2 -5
  29. package/dist/rate-limiter/createRateLimitedValue.js +1 -2
  30. package/dist/rate-limiter/createRateLimiter.d.ts +8 -10
  31. package/dist/rate-limiter/createRateLimiter.js +5 -5
  32. package/dist/throttler/createThrottledSignal.d.ts +2 -7
  33. package/dist/throttler/createThrottledSignal.js +1 -4
  34. package/dist/throttler/createThrottledValue.d.ts +2 -7
  35. package/dist/throttler/createThrottledValue.js +1 -4
  36. package/dist/throttler/createThrottler.d.ts +4 -9
  37. package/dist/throttler/createThrottler.js +1 -4
  38. package/package.json +24 -68
  39. package/dist/async-batcher/createAsyncBatcher.cjs +0 -166
  40. package/dist/async-batcher/createAsyncBatcher.cjs.map +0 -1
  41. package/dist/async-batcher/createAsyncBatcher.d.cts +0 -178
  42. package/dist/async-batcher/createAsyncBatcher.js.map +0 -1
  43. package/dist/async-batcher/index.cjs +0 -11
  44. package/dist/async-batcher/index.d.cts +0 -3
  45. package/dist/async-debouncer/createAsyncDebouncer.cjs +0 -151
  46. package/dist/async-debouncer/createAsyncDebouncer.cjs.map +0 -1
  47. package/dist/async-debouncer/createAsyncDebouncer.d.cts +0 -164
  48. package/dist/async-debouncer/createAsyncDebouncer.js.map +0 -1
  49. package/dist/async-debouncer/index.cjs +0 -11
  50. package/dist/async-debouncer/index.d.cts +0 -3
  51. package/dist/async-queuer/createAsyncQueuer.cjs +0 -158
  52. package/dist/async-queuer/createAsyncQueuer.cjs.map +0 -1
  53. package/dist/async-queuer/createAsyncQueuer.d.cts +0 -170
  54. package/dist/async-queuer/createAsyncQueuer.js.map +0 -1
  55. package/dist/async-queuer/index.cjs +0 -11
  56. package/dist/async-queuer/index.d.cts +0 -3
  57. package/dist/async-rate-limiter/createAsyncRateLimiter.cjs +0 -193
  58. package/dist/async-rate-limiter/createAsyncRateLimiter.cjs.map +0 -1
  59. package/dist/async-rate-limiter/createAsyncRateLimiter.d.cts +0 -209
  60. package/dist/async-rate-limiter/createAsyncRateLimiter.js.map +0 -1
  61. package/dist/async-rate-limiter/index.cjs +0 -11
  62. package/dist/async-rate-limiter/index.d.cts +0 -3
  63. package/dist/async-throttler/createAsyncThrottler.cjs +0 -151
  64. package/dist/async-throttler/createAsyncThrottler.cjs.map +0 -1
  65. package/dist/async-throttler/createAsyncThrottler.d.cts +0 -164
  66. package/dist/async-throttler/createAsyncThrottler.js.map +0 -1
  67. package/dist/async-throttler/index.cjs +0 -11
  68. package/dist/async-throttler/index.d.cts +0 -3
  69. package/dist/batcher/createBatcher.cjs +0 -132
  70. package/dist/batcher/createBatcher.cjs.map +0 -1
  71. package/dist/batcher/createBatcher.d.cts +0 -147
  72. package/dist/batcher/createBatcher.js.map +0 -1
  73. package/dist/batcher/index.cjs +0 -11
  74. package/dist/batcher/index.d.cts +0 -3
  75. package/dist/debouncer/createDebouncedSignal.cjs +0 -88
  76. package/dist/debouncer/createDebouncedSignal.cjs.map +0 -1
  77. package/dist/debouncer/createDebouncedSignal.d.cts +0 -80
  78. package/dist/debouncer/createDebouncedSignal.js.map +0 -1
  79. package/dist/debouncer/createDebouncedValue.cjs +0 -77
  80. package/dist/debouncer/createDebouncedValue.cjs.map +0 -1
  81. package/dist/debouncer/createDebouncedValue.d.cts +0 -71
  82. package/dist/debouncer/createDebouncedValue.js.map +0 -1
  83. package/dist/debouncer/createDebouncer.cjs +0 -129
  84. package/dist/debouncer/createDebouncer.cjs.map +0 -1
  85. package/dist/debouncer/createDebouncer.d.cts +0 -145
  86. package/dist/debouncer/createDebouncer.js.map +0 -1
  87. package/dist/debouncer/index.cjs +0 -15
  88. package/dist/debouncer/index.d.cts +0 -5
  89. package/dist/index.cjs +0 -47
  90. package/dist/index.d.cts +0 -20
  91. package/dist/provider/PacerProvider.cjs +0 -27
  92. package/dist/provider/PacerProvider.cjs.map +0 -1
  93. package/dist/provider/PacerProvider.d.cts +0 -29
  94. package/dist/provider/PacerProvider.js.map +0 -1
  95. package/dist/provider/index.cjs +0 -6
  96. package/dist/provider/index.d.cts +0 -2
  97. package/dist/queuer/createQueuedSignal.cjs +0 -129
  98. package/dist/queuer/createQueuedSignal.cjs.map +0 -1
  99. package/dist/queuer/createQueuedSignal.d.cts +0 -122
  100. package/dist/queuer/createQueuedSignal.js.map +0 -1
  101. package/dist/queuer/createQueuer.cjs +0 -133
  102. package/dist/queuer/createQueuer.cjs.map +0 -1
  103. package/dist/queuer/createQueuer.d.cts +0 -148
  104. package/dist/queuer/createQueuer.js.map +0 -1
  105. package/dist/queuer/index.cjs +0 -13
  106. package/dist/queuer/index.d.cts +0 -4
  107. package/dist/rate-limiter/createRateLimitedSignal.cjs +0 -102
  108. package/dist/rate-limiter/createRateLimitedSignal.cjs.map +0 -1
  109. package/dist/rate-limiter/createRateLimitedSignal.d.cts +0 -94
  110. package/dist/rate-limiter/createRateLimitedSignal.js.map +0 -1
  111. package/dist/rate-limiter/createRateLimitedValue.cjs +0 -102
  112. package/dist/rate-limiter/createRateLimitedValue.cjs.map +0 -1
  113. package/dist/rate-limiter/createRateLimitedValue.d.cts +0 -96
  114. package/dist/rate-limiter/createRateLimitedValue.js.map +0 -1
  115. package/dist/rate-limiter/createRateLimiter.cjs +0 -157
  116. package/dist/rate-limiter/createRateLimiter.cjs.map +0 -1
  117. package/dist/rate-limiter/createRateLimiter.d.cts +0 -174
  118. package/dist/rate-limiter/createRateLimiter.js.map +0 -1
  119. package/dist/rate-limiter/index.cjs +0 -15
  120. package/dist/rate-limiter/index.d.cts +0 -5
  121. package/dist/throttler/createThrottledSignal.cjs +0 -79
  122. package/dist/throttler/createThrottledSignal.cjs.map +0 -1
  123. package/dist/throttler/createThrottledSignal.d.cts +0 -71
  124. package/dist/throttler/createThrottledSignal.js.map +0 -1
  125. package/dist/throttler/createThrottledValue.cjs +0 -74
  126. package/dist/throttler/createThrottledValue.cjs.map +0 -1
  127. package/dist/throttler/createThrottledValue.d.cts +0 -68
  128. package/dist/throttler/createThrottledValue.js.map +0 -1
  129. package/dist/throttler/createThrottler.cjs +0 -132
  130. package/dist/throttler/createThrottler.cjs.map +0 -1
  131. package/dist/throttler/createThrottler.d.cts +0 -148
  132. package/dist/throttler/createThrottler.js.map +0 -1
  133. package/dist/throttler/index.cjs +0 -15
  134. package/dist/throttler/index.d.cts +0 -5
  135. package/dist/types/index.cjs +0 -9
  136. package/dist/types/index.d.cts +0 -1
  137. package/dist/utils/index.cjs +0 -9
  138. package/dist/utils/index.d.cts +0 -1
  139. package/src/async-batcher/createAsyncBatcher.ts +0 -234
  140. package/src/async-batcher/index.ts +0 -3
  141. package/src/async-debouncer/createAsyncDebouncer.ts +0 -225
  142. package/src/async-debouncer/index.ts +0 -4
  143. package/src/async-queuer/createAsyncQueuer.ts +0 -226
  144. package/src/async-queuer/index.ts +0 -3
  145. package/src/async-rate-limiter/createAsyncRateLimiter.ts +0 -269
  146. package/src/async-rate-limiter/index.ts +0 -4
  147. package/src/async-throttler/createAsyncThrottler.ts +0 -225
  148. package/src/async-throttler/index.ts +0 -4
  149. package/src/batcher/createBatcher.ts +0 -199
  150. package/src/batcher/index.ts +0 -3
  151. package/src/debouncer/createDebouncedSignal.ts +0 -92
  152. package/src/debouncer/createDebouncedValue.ts +0 -85
  153. package/src/debouncer/createDebouncer.ts +0 -201
  154. package/src/debouncer/index.ts +0 -6
  155. package/src/index.ts +0 -46
  156. package/src/provider/PacerProvider.tsx +0 -63
  157. package/src/provider/index.ts +0 -1
  158. package/src/queuer/createQueuedSignal.ts +0 -138
  159. package/src/queuer/createQueuer.ts +0 -199
  160. package/src/queuer/index.ts +0 -4
  161. package/src/rate-limiter/createRateLimitedSignal.ts +0 -117
  162. package/src/rate-limiter/createRateLimitedValue.ts +0 -110
  163. package/src/rate-limiter/createRateLimiter.ts +0 -226
  164. package/src/rate-limiter/index.ts +0 -5
  165. package/src/throttler/createThrottledSignal.ts +0 -81
  166. package/src/throttler/createThrottledValue.ts +0 -82
  167. package/src/throttler/createThrottler.ts +0 -204
  168. package/src/throttler/index.ts +0 -6
  169. package/src/types/index.ts +0 -1
  170. package/src/utils/index.ts +0 -1
@@ -1,199 +0,0 @@
1
- import { Batcher } from '@tanstack/pacer/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 { BatcherOptions, BatcherState } from '@tanstack/pacer/batcher'
8
-
9
- export interface SolidBatcherOptions<
10
- TValue,
11
- TSelected = {},
12
- > extends BatcherOptions<TValue> {
13
- /**
14
- * Optional callback invoked when the owning component unmounts. Receives the batcher instance.
15
- * When provided, replaces the default cleanup (cancel); use it to call flush(), reset(), cancel(), add logging, etc.
16
- */
17
- onUnmount?: (batcher: SolidBatcher<TValue, TSelected>) => void
18
- }
19
-
20
- export interface SolidBatcher<TValue, TSelected = {}> extends Omit<
21
- Batcher<TValue>,
22
- 'store'
23
- > {
24
- /**
25
- * A Solid component that allows you to subscribe to the batcher state.
26
- *
27
- * This is useful for tracking specific parts of the batcher state
28
- * deep in your component tree without needing to pass a selector to the hook.
29
- *
30
- * @example
31
- * <batcher.Subscribe selector={(state) => ({ size: state.size, isRunning: state.isRunning })}>
32
- * {(state) => (
33
- * <div>Batch: {state().size} items, {state().isRunning ? 'Processing' : 'Idle'}</div>
34
- * )}
35
- * </batcher.Subscribe>
36
- */
37
- Subscribe: <TSelected>(props: {
38
- selector: (state: BatcherState<TValue>) => TSelected
39
- children: ((state: Accessor<TSelected>) => JSX.Element) | JSX.Element
40
- }) => JSX.Element
41
- /**
42
- * Reactive state that will be updated when the batcher state changes
43
- *
44
- * Use this instead of `batcher.store.state`
45
- */
46
- readonly state: Accessor<Readonly<TSelected>>
47
- /**
48
- * @deprecated Use `batcher.state` instead of `batcher.store.state` if you want to read reactive state.
49
- * The state on the store object is not reactive, as it has not been wrapped in a `useSelector` hook internally.
50
- * Although, you can make the state reactive by using the `useSelector` in your own usage.
51
- */
52
- readonly store: Store<Readonly<BatcherState<TValue>>>
53
- }
54
-
55
- /**
56
- * Creates a Solid-compatible Batcher instance for managing batches of items, exposing Solid signals for all stateful properties.
57
- *
58
- * Features:
59
- * - Batch processing of items using the provided `fn` function
60
- * - Configurable batch size and wait time
61
- * - Custom batch processing logic via getShouldExecute
62
- * - Event callbacks for monitoring batch operations
63
- * - All stateful properties (items, counts, etc.) are exposed as Solid signals for reactivity
64
- *
65
- * The batcher collects items and processes them in batches based on:
66
- * - Maximum batch size
67
- * - Time-based batching (process after X milliseconds)
68
- * - Custom batch processing logic via getShouldExecute
69
- *
70
- * ## State Management and Selector
71
- *
72
- * The hook uses TanStack Store for reactive state management. You can subscribe to state changes
73
- * in two ways:
74
- *
75
- * **1. Using `batcher.Subscribe` component (Recommended for component tree subscriptions)**
76
- *
77
- * Use the `Subscribe` component to subscribe to state changes deep in your component tree without
78
- * needing to pass a selector to the hook. This is ideal when you want to subscribe to state
79
- * in child components.
80
- *
81
- * **2. Using the `selector` parameter (For hook-level subscriptions)**
82
- *
83
- * The `selector` parameter allows you to specify which state changes will trigger reactive updates
84
- * at the hook level, optimizing performance by preventing unnecessary updates when irrelevant
85
- * state changes occur.
86
- *
87
- * **By default, there will be no reactive state subscriptions** and you must opt-in to state
88
- * tracking by providing a selector function or using the `Subscribe` component. This prevents unnecessary
89
- * updates and gives you full control over when your component tracks state changes.
90
- *
91
- * Available state properties:
92
- * - `executionCount`: Number of batch executions that have been completed
93
- * - `isRunning`: Whether the batcher is currently running (not stopped)
94
- * - `items`: Array of items currently queued for batching
95
- * - `totalItemsProcessed`: Total number of individual items that have been processed across all batches
96
- *
97
- * ## Unmount behavior
98
- *
99
- * By default, the primitive cancels any pending batch when the owning component unmounts.
100
- * Use the `onUnmount` option to customize this. For example, to flush pending work instead:
101
- *
102
- * ```tsx
103
- * const batcher = createBatcher(fn, {
104
- * maxSize: 10,
105
- * wait: 2000,
106
- * onUnmount: (b) => b.flush()
107
- * });
108
- * ```
109
- *
110
- * Example usage:
111
- * ```tsx
112
- * // Default behavior - no reactive state subscriptions
113
- * const batcher = createBatcher(
114
- * (items) => {
115
- * // Process batch of items
116
- * console.log('Processing batch:', items);
117
- * },
118
- * {
119
- * maxSize: 5,
120
- * wait: 2000,
121
- * onExecute: (batcher) => console.log('Batch executed'),
122
- * getShouldExecute: (items) => items.length >= 3
123
- * }
124
- * );
125
- *
126
- * // Opt-in to track items or isRunning changes (optimized for UI updates)
127
- * const batcher = createBatcher(
128
- * (items) => console.log('Processing batch:', items),
129
- * { maxSize: 5, wait: 2000 },
130
- * (state) => ({ items: state.items, isRunning: state.isRunning })
131
- * );
132
- *
133
- * // Opt-in to track execution metrics changes (optimized for tracking progress)
134
- * const batcher = createBatcher(
135
- * (items) => console.log('Processing batch:', items),
136
- * { maxSize: 5, wait: 2000 },
137
- * (state) => ({
138
- * executionCount: state.executionCount,
139
- * totalItemsProcessed: state.totalItemsProcessed
140
- * })
141
- * );
142
- *
143
- * // Add items to batch
144
- * batcher.addItem('task1');
145
- * batcher.addItem('task2');
146
- *
147
- * // Control the batcher
148
- * batcher.stop(); // Pause processing
149
- * batcher.start(); // Resume processing
150
- *
151
- * // Access the selected state (will be empty object {} unless selector provided)
152
- * const { items, isRunning } = batcher.state();
153
- * ```
154
- */
155
- export function createBatcher<TValue, TSelected = {}>(
156
- fn: (items: Array<TValue>) => void,
157
- options: SolidBatcherOptions<TValue, TSelected> = {},
158
- selector: (state: BatcherState<TValue>) => TSelected = () =>
159
- ({}) as TSelected,
160
- ): SolidBatcher<TValue, TSelected> {
161
- const mergedOptions = {
162
- ...useDefaultPacerOptions().batcher,
163
- ...options,
164
- } as SolidBatcherOptions<TValue, TSelected>
165
- const batcher = new Batcher(fn, mergedOptions) as unknown as SolidBatcher<
166
- TValue,
167
- TSelected
168
- >
169
-
170
- batcher.Subscribe = function Subscribe<TSelected>(props: {
171
- selector: (state: BatcherState<TValue>) => TSelected
172
- children: ((state: Accessor<TSelected>) => JSX.Element) | JSX.Element
173
- }) {
174
- const selected = useSelector(batcher.store, props.selector, {
175
- compare: shallow,
176
- })
177
-
178
- return typeof props.children === 'function'
179
- ? props.children(selected)
180
- : props.children
181
- }
182
-
183
- const state = useSelector(batcher.store, selector, { compare: shallow })
184
-
185
- createEffect(() => {
186
- onCleanup(() => {
187
- if (mergedOptions.onUnmount) {
188
- mergedOptions.onUnmount(batcher)
189
- } else {
190
- batcher.cancel()
191
- }
192
- })
193
- })
194
-
195
- return {
196
- ...batcher,
197
- state,
198
- } as SolidBatcher<TValue, TSelected>
199
- }
@@ -1,3 +0,0 @@
1
- export * from '@tanstack/pacer/batcher'
2
-
3
- export * from './createBatcher'
@@ -1,92 +0,0 @@
1
- import { createSignal } from 'solid-js'
2
- import { createDebouncer } from './createDebouncer'
3
- import type { SolidDebouncer, SolidDebouncerOptions } from './createDebouncer'
4
- import type { Accessor, Setter } from 'solid-js'
5
- import type { DebouncerState } from '@tanstack/pacer/debouncer'
6
-
7
- /**
8
- * A Solid hook that creates a debounced state value, combining Solid's createSignal with debouncing functionality.
9
- * This hook provides both the current debounced value and methods to update it.
10
- *
11
- * The state value is only updated after the specified wait time has elapsed since the last update attempt.
12
- * If another update is attempted before the wait time expires, the timer resets and starts waiting again.
13
- * This is useful for handling frequent state updates that should be throttled, like search input values
14
- * or window resize dimensions.
15
- *
16
- * The hook returns a tuple containing:
17
- * - The current debounced value accessor
18
- * - A function to update the debounced value
19
- * - The debouncer instance with additional control methods and state signals
20
- *
21
- * ## State Management and Selector
22
- *
23
- * The hook uses TanStack Store for reactive state management via the underlying debouncer instance.
24
- * The `selector` parameter allows you to specify which debouncer state changes will trigger reactive updates,
25
- * optimizing performance by preventing unnecessary subscriptions when irrelevant state changes occur.
26
- *
27
- * **By default, there will be no reactive state subscriptions** and you must opt-in to state
28
- * tracking by providing a selector function. This prevents unnecessary reactive updates and gives you
29
- * full control over when your component subscribes to state changes. Only when you provide a selector will
30
- * the reactive system track the selected state values.
31
- *
32
- * Available debouncer state properties:
33
- * - `canLeadingExecute`: Whether the debouncer can execute on the leading edge
34
- * - `executionCount`: Number of function executions that have been completed
35
- * - `isPending`: Whether the debouncer is waiting for the timeout to trigger execution
36
- * - `lastArgs`: The arguments from the most recent call to maybeExecute
37
- * - `status`: Current execution status ('disabled' | 'idle' | 'pending')
38
- *
39
- * @example
40
- * ```tsx
41
- * // Default behavior - no reactive state subscriptions
42
- * const [searchTerm, setSearchTerm, debouncer] = createDebouncedSignal('', {
43
- * wait: 500 // Wait 500ms after last keystroke
44
- * });
45
- *
46
- * // Opt-in to reactive updates when pending state changes (optimized for loading indicators)
47
- * const [searchTerm, setSearchTerm, debouncer] = createDebouncedSignal(
48
- * '',
49
- * { wait: 500 },
50
- * (state) => ({ isPending: state.isPending })
51
- * );
52
- *
53
- * // Opt-in to reactive updates when execution count changes (optimized for tracking executions)
54
- * const [searchTerm, setSearchTerm, debouncer] = createDebouncedSignal(
55
- * '',
56
- * { wait: 500 },
57
- * (state) => ({ executionCount: state.executionCount })
58
- * );
59
- *
60
- * // Update value - will be debounced
61
- * const handleChange = (e) => {
62
- * setSearchTerm(e.target.value);
63
- * };
64
- *
65
- * // Access debouncer state via signals
66
- * console.log('Executions:', debouncer.state().executionCount);
67
- * console.log('Is pending:', debouncer.state().isPending);
68
- *
69
- * // In onExecute callback, use get* methods
70
- * const [searchTerm, setSearchTerm, debouncer] = createDebouncedSignal('', {
71
- * wait: 500,
72
- * onExecute: (debouncer) => {
73
- * console.log('Total executions:', debouncer.getExecutionCount());
74
- * }
75
- * });
76
- * ```
77
- */
78
- export function createDebouncedSignal<TValue, TSelected = {}>(
79
- value: TValue,
80
- initialOptions: SolidDebouncerOptions<Setter<TValue>, TSelected>,
81
- selector?: (state: DebouncerState<Setter<TValue>>) => TSelected,
82
- ): [
83
- Accessor<TValue>,
84
- Setter<TValue>,
85
- SolidDebouncer<Setter<TValue>, TSelected>,
86
- ] {
87
- const [debouncedValue, setDebouncedValue] = createSignal<TValue>(value)
88
-
89
- const debouncer = createDebouncer(setDebouncedValue, initialOptions, selector)
90
-
91
- return [debouncedValue, debouncer.maybeExecute as Setter<TValue>, debouncer]
92
- }
@@ -1,85 +0,0 @@
1
- import { createEffect } from 'solid-js'
2
- import { createDebouncedSignal } from './createDebouncedSignal'
3
- import type { SolidDebouncer, SolidDebouncerOptions } from './createDebouncer'
4
- import type { Accessor, Setter } from 'solid-js'
5
- import type { DebouncerState } from '@tanstack/pacer/debouncer'
6
-
7
- /**
8
- * A Solid hook that creates a debounced value that updates only after a specified delay.
9
- * Unlike createDebouncedSignal, this hook automatically tracks changes to the input value
10
- * and updates the debounced value accordingly.
11
- *
12
- * The debounced value will only update after the specified wait time has elapsed since
13
- * the last change to the input value. If the input value changes again before the wait
14
- * time expires, the timer resets and starts waiting again.
15
- *
16
- * This is useful for deriving debounced values from props or state that change frequently,
17
- * like search queries or form inputs, where you want to limit how often downstream effects
18
- * or calculations occur.
19
- *
20
- * The hook returns a tuple containing:
21
- * - An Accessor that provides the current debounced value
22
- * - The debouncer instance with control methods
23
- *
24
- * ## State Management and Selector
25
- *
26
- * The hook uses TanStack Store for reactive state management via the underlying debouncer instance.
27
- * The `selector` parameter allows you to specify which debouncer state changes will trigger reactive updates,
28
- * optimizing performance by preventing unnecessary subscriptions when irrelevant state changes occur.
29
- *
30
- * **By default, there will be no reactive state subscriptions** and you must opt-in to state
31
- * tracking by providing a selector function. This prevents unnecessary reactive updates and gives you
32
- * full control over when your component subscribes to state changes. Only when you provide a selector will
33
- * the reactive system track the selected state values.
34
- *
35
- * Available debouncer state properties:
36
- * - `canLeadingExecute`: Whether the debouncer can execute on the leading edge
37
- * - `executionCount`: Number of function executions that have been completed
38
- * - `isPending`: Whether the debouncer is waiting for the timeout to trigger execution
39
- * - `lastArgs`: The arguments from the most recent call to maybeExecute
40
- * - `status`: Current execution status ('disabled' | 'idle' | 'pending')
41
- *
42
- * @example
43
- * ```tsx
44
- * // Default behavior - no reactive state subscriptions
45
- * const [searchQuery, setSearchQuery] = createSignal('');
46
- * const [debouncedQuery, debouncer] = createDebouncedValue(searchQuery, {
47
- * wait: 500 // Wait 500ms after last change
48
- * });
49
- *
50
- * // Opt-in to reactive updates when pending state changes (optimized for loading indicators)
51
- * const [debouncedQuery, debouncer] = createDebouncedValue(
52
- * searchQuery,
53
- * { wait: 500 },
54
- * (state) => ({ isPending: state.isPending })
55
- * );
56
- *
57
- * // debouncedQuery will update 500ms after searchQuery stops changing
58
- * createEffect(() => {
59
- * fetchSearchResults(debouncedQuery());
60
- * });
61
- *
62
- * // Access debouncer state via signals
63
- * console.log('Is pending:', debouncer.state().isPending);
64
- *
65
- * // Control the debouncer
66
- * debouncer.cancel(); // Cancel any pending updates
67
- * ```
68
- */
69
- export function createDebouncedValue<TValue, TSelected = {}>(
70
- value: Accessor<TValue>,
71
- initialOptions: SolidDebouncerOptions<Setter<TValue>, TSelected>,
72
- selector?: (state: DebouncerState<Setter<TValue>>) => TSelected,
73
- ): [Accessor<TValue>, SolidDebouncer<Setter<TValue>, TSelected>] {
74
- const [debouncedValue, setDebouncedValue, debouncer] = createDebouncedSignal(
75
- value(),
76
- initialOptions,
77
- selector,
78
- )
79
-
80
- createEffect(() => {
81
- setDebouncedValue(value() as any)
82
- })
83
-
84
- return [debouncedValue, debouncer]
85
- }
@@ -1,201 +0,0 @@
1
- import { Debouncer } from '@tanstack/pacer/debouncer'
2
- import { createEffect, onCleanup } from 'solid-js'
3
- import { shallow, useSelector } from '@tanstack/solid-store'
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 { AnyFunction } from '@tanstack/pacer/types'
8
- import type {
9
- DebouncerOptions,
10
- DebouncerState,
11
- } from '@tanstack/pacer/debouncer'
12
-
13
- export interface SolidDebouncerOptions<
14
- TFn extends AnyFunction,
15
- TSelected = {},
16
- > extends DebouncerOptions<TFn> {
17
- /**
18
- * Optional callback invoked when the owning component unmounts. Receives the debouncer instance.
19
- * When provided, replaces the default cleanup (cancel); use it to call flush(), reset(), cancel(), add logging, etc.
20
- */
21
- onUnmount?: (debouncer: SolidDebouncer<TFn, TSelected>) => void
22
- }
23
-
24
- export interface SolidDebouncer<
25
- TFn extends AnyFunction,
26
- TSelected = {},
27
- > extends Omit<Debouncer<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 })}>
36
- * {(state) => (
37
- * <div>{state().isPending ? 'Waiting...' : 'Ready'}</div>
38
- * )}
39
- * </debouncer.Subscribe>
40
- */
41
- Subscribe: <TSelected>(props: {
42
- selector: (state: DebouncerState<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<DebouncerState<TFn>>>
57
- }
58
-
59
- /**
60
- * A Solid hook that creates and manages a Debouncer instance.
61
- *
62
- * This is a lower-level hook that provides direct access to the Debouncer's functionality without
63
- * any built-in state management. This allows you to integrate it with any state management solution
64
- * you prefer (createSignal, Redux, Zustand, etc.).
65
- *
66
- * This hook provides debouncing functionality to limit how often a function can be called,
67
- * waiting for a specified delay before executing the latest call. This is useful for handling
68
- * frequent events like window resizing, scroll events, or real-time search inputs.
69
- *
70
- * The debouncer will only execute the function after the specified wait time has elapsed
71
- * since the last call. If the function is called again before the wait time expires, the
72
- * timer resets and starts waiting again.
73
- *
74
- * ## State Management and Selector
75
- *
76
- * The hook uses TanStack Store for reactive state management. You can subscribe to state changes
77
- * in two ways:
78
- *
79
- * **1. Using `debouncer.Subscribe` component (Recommended for component tree subscriptions)**
80
- *
81
- * Use the `Subscribe` component to subscribe to state changes deep in your component tree without
82
- * needing to pass a selector to the hook. This is ideal when you want to subscribe to state
83
- * in child components.
84
- *
85
- * **2. Using the `selector` parameter (For hook-level subscriptions)**
86
- *
87
- * The `selector` parameter allows you to specify which state changes will trigger reactive updates
88
- * at the hook level, optimizing performance by preventing unnecessary updates when irrelevant
89
- * state changes occur.
90
- *
91
- * **By default, there will be no reactive state subscriptions** and you must opt-in to state
92
- * tracking by providing a selector function or using the `Subscribe` component. This prevents unnecessary
93
- * updates and gives you full control over when your component tracks state changes.
94
- *
95
- * Available state properties:
96
- * - `canLeadingExecute`: Whether the debouncer can execute on the leading edge
97
- * - `executionCount`: Number of function executions that have been completed
98
- * - `isPending`: Whether the debouncer is waiting for the timeout to trigger execution
99
- * - `lastArgs`: The arguments from the most recent call to maybeExecute
100
- * - `status`: Current execution status ('disabled' | 'idle' | 'pending')
101
- *
102
- * ## Unmount behavior
103
- *
104
- * By default, the primitive cancels any pending execution when the owning component unmounts.
105
- * Use the `onUnmount` option to customize this. For example, to flush pending work instead:
106
- *
107
- * ```tsx
108
- * const debouncer = createDebouncer(fn, {
109
- * wait: 500,
110
- * onUnmount: (d) => d.flush()
111
- * });
112
- * ```
113
- *
114
- * @example
115
- * ```tsx
116
- * // Default behavior - no reactive state subscriptions
117
- * const debouncer = createDebouncer(
118
- * (query: string) => fetchSearchResults(query),
119
- * { wait: 500 }
120
- * );
121
- *
122
- * // Opt-in to track isPending changes (optimized for loading states)
123
- * const debouncer = createDebouncer(
124
- * (query: string) => fetchSearchResults(query),
125
- * { wait: 500 },
126
- * (state) => ({ isPending: state.isPending })
127
- * );
128
- *
129
- * // Opt-in to track executionCount changes (optimized for tracking execution)
130
- * const debouncer = createDebouncer(
131
- * (query: string) => fetchSearchResults(query),
132
- * { wait: 500 },
133
- * (state) => ({ executionCount: state.executionCount })
134
- * );
135
- *
136
- * // Multiple state properties - track when any of these change
137
- * const debouncer = createDebouncer(
138
- * (query: string) => fetchSearchResults(query),
139
- * { wait: 500 },
140
- * (state) => ({
141
- * isPending: state.isPending,
142
- * executionCount: state.executionCount,
143
- * status: state.status
144
- * })
145
- * );
146
- *
147
- * // In an event handler
148
- * const handleChange = (e) => {
149
- * debouncer.maybeExecute(e.target.value);
150
- * };
151
- *
152
- * // Access the selected state (will be empty object {} unless selector provided)
153
- * const { isPending } = debouncer.state();
154
- * ```
155
- */
156
- export function createDebouncer<TFn extends AnyFunction, TSelected = {}>(
157
- fn: TFn,
158
- options: SolidDebouncerOptions<TFn, TSelected>,
159
- selector: (state: DebouncerState<TFn>) => TSelected = () => ({}) as TSelected,
160
- ): SolidDebouncer<TFn, TSelected> {
161
- const mergedOptions = {
162
- ...useDefaultPacerOptions().debouncer,
163
- ...options,
164
- } as SolidDebouncerOptions<TFn, TSelected>
165
- const asyncDebouncer = new Debouncer<TFn>(
166
- fn,
167
- mergedOptions,
168
- ) as unknown as SolidDebouncer<TFn, TSelected>
169
-
170
- asyncDebouncer.Subscribe = function Subscribe<TSelected>(props: {
171
- selector: (state: DebouncerState<TFn>) => TSelected
172
- children: ((state: Accessor<TSelected>) => JSX.Element) | JSX.Element
173
- }) {
174
- const selected = useSelector(asyncDebouncer.store, props.selector, {
175
- compare: shallow,
176
- })
177
-
178
- return typeof props.children === 'function'
179
- ? props.children(selected)
180
- : props.children
181
- }
182
-
183
- const state = useSelector(asyncDebouncer.store, selector, {
184
- compare: shallow,
185
- })
186
-
187
- createEffect(() => {
188
- onCleanup(() => {
189
- if (mergedOptions.onUnmount) {
190
- mergedOptions.onUnmount(asyncDebouncer)
191
- } else {
192
- asyncDebouncer.cancel()
193
- }
194
- })
195
- })
196
-
197
- return {
198
- ...asyncDebouncer,
199
- state,
200
- } as SolidDebouncer<TFn, TSelected> // omit `store` in favor of `state`
201
- }
@@ -1,6 +0,0 @@
1
- // re-export everything from the core pacer package, BUT ONLY from the debouncer module
2
- export * from '@tanstack/pacer/debouncer'
3
-
4
- export * from './createDebouncedSignal'
5
- export * from './createDebouncedValue'
6
- export * from './createDebouncer'
package/src/index.ts DELETED
@@ -1,46 +0,0 @@
1
- // re-export everything from the core pacer package
2
- export * from '@tanstack/pacer'
3
-
4
- // provider
5
- export * from './provider/PacerProvider'
6
-
7
- /**
8
- * Export every hook individually - DON'T export from barrel files
9
- */
10
-
11
- // async-batcher
12
- export * from './async-batcher/createAsyncBatcher'
13
-
14
- // async-debouncer
15
- export * from './async-debouncer/createAsyncDebouncer'
16
-
17
- // async-queuer
18
- export * from './async-queuer/createAsyncQueuer'
19
-
20
- // async-rate-limiter
21
- export * from './async-rate-limiter/createAsyncRateLimiter'
22
-
23
- // async-throttler
24
- export * from './async-throttler/createAsyncThrottler'
25
-
26
- // batcher
27
- export * from './batcher/createBatcher'
28
-
29
- // debouncer
30
- export * from './debouncer/createDebouncedSignal'
31
- export * from './debouncer/createDebouncedValue'
32
- export * from './debouncer/createDebouncer'
33
-
34
- // queuer
35
- export * from './queuer/createQueuer'
36
- export * from './queuer/createQueuedSignal'
37
-
38
- // rate-limiter
39
- export * from './rate-limiter/createRateLimiter'
40
- export * from './rate-limiter/createRateLimitedSignal'
41
- export * from './rate-limiter/createRateLimitedValue'
42
-
43
- // throttler
44
- export * from './throttler/createThrottledSignal'
45
- export * from './throttler/createThrottledValue'
46
- export * from './throttler/createThrottler'