@tanstack/preact-pacer 0.19.4 → 0.20.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 (196) hide show
  1. package/dist/async-batcher/index.cjs +1 -0
  2. package/dist/async-batcher/index.d.cts +2 -2
  3. package/dist/async-batcher/index.d.ts +2 -2
  4. package/dist/async-batcher/useAsyncBatchedCallback.cjs +2 -2
  5. package/dist/async-batcher/useAsyncBatchedCallback.cjs.map +1 -1
  6. package/dist/async-batcher/useAsyncBatchedCallback.d.cts +2 -3
  7. package/dist/async-batcher/useAsyncBatchedCallback.d.ts +2 -3
  8. package/dist/async-batcher/useAsyncBatchedCallback.js +2 -2
  9. package/dist/async-batcher/useAsyncBatchedCallback.js.map +1 -1
  10. package/dist/async-batcher/useAsyncBatcher.cjs +28 -0
  11. package/dist/async-batcher/useAsyncBatcher.cjs.map +1 -1
  12. package/dist/async-batcher/useAsyncBatcher.d.cts +28 -2
  13. package/dist/async-batcher/useAsyncBatcher.d.ts +28 -2
  14. package/dist/async-batcher/useAsyncBatcher.js +29 -1
  15. package/dist/async-batcher/useAsyncBatcher.js.map +1 -1
  16. package/dist/async-debouncer/index.cjs +1 -0
  17. package/dist/async-debouncer/index.d.cts +2 -2
  18. package/dist/async-debouncer/index.d.ts +2 -2
  19. package/dist/async-debouncer/useAsyncDebouncedCallback.cjs.map +1 -1
  20. package/dist/async-debouncer/useAsyncDebouncedCallback.d.cts +2 -2
  21. package/dist/async-debouncer/useAsyncDebouncedCallback.d.ts +2 -2
  22. package/dist/async-debouncer/useAsyncDebouncedCallback.js.map +1 -1
  23. package/dist/async-debouncer/useAsyncDebouncer.cjs +24 -2
  24. package/dist/async-debouncer/useAsyncDebouncer.cjs.map +1 -1
  25. package/dist/async-debouncer/useAsyncDebouncer.d.cts +28 -3
  26. package/dist/async-debouncer/useAsyncDebouncer.d.ts +27 -2
  27. package/dist/async-debouncer/useAsyncDebouncer.js +24 -2
  28. package/dist/async-debouncer/useAsyncDebouncer.js.map +1 -1
  29. package/dist/async-queuer/index.cjs +1 -0
  30. package/dist/async-queuer/index.d.cts +2 -2
  31. package/dist/async-queuer/index.d.ts +2 -2
  32. package/dist/async-queuer/useAsyncQueuedState.cjs.map +1 -1
  33. package/dist/async-queuer/useAsyncQueuedState.d.cts +3 -3
  34. package/dist/async-queuer/useAsyncQueuedState.d.ts +3 -3
  35. package/dist/async-queuer/useAsyncQueuedState.js.map +1 -1
  36. package/dist/async-queuer/useAsyncQueuer.cjs +28 -0
  37. package/dist/async-queuer/useAsyncQueuer.cjs.map +1 -1
  38. package/dist/async-queuer/useAsyncQueuer.d.cts +28 -2
  39. package/dist/async-queuer/useAsyncQueuer.d.ts +28 -2
  40. package/dist/async-queuer/useAsyncQueuer.js +29 -1
  41. package/dist/async-queuer/useAsyncQueuer.js.map +1 -1
  42. package/dist/async-rate-limiter/index.cjs +1 -0
  43. package/dist/async-rate-limiter/index.d.cts +2 -2
  44. package/dist/async-rate-limiter/index.d.ts +2 -2
  45. package/dist/async-rate-limiter/useAsyncRateLimitedCallback.cjs.map +1 -1
  46. package/dist/async-rate-limiter/useAsyncRateLimitedCallback.d.cts +2 -2
  47. package/dist/async-rate-limiter/useAsyncRateLimitedCallback.d.ts +2 -2
  48. package/dist/async-rate-limiter/useAsyncRateLimitedCallback.js.map +1 -1
  49. package/dist/async-rate-limiter/useAsyncRateLimiter.cjs +12 -0
  50. package/dist/async-rate-limiter/useAsyncRateLimiter.cjs.map +1 -1
  51. package/dist/async-rate-limiter/useAsyncRateLimiter.d.cts +15 -2
  52. package/dist/async-rate-limiter/useAsyncRateLimiter.d.ts +15 -2
  53. package/dist/async-rate-limiter/useAsyncRateLimiter.js +13 -1
  54. package/dist/async-rate-limiter/useAsyncRateLimiter.js.map +1 -1
  55. package/dist/async-throttler/index.cjs +1 -0
  56. package/dist/async-throttler/index.d.cts +2 -2
  57. package/dist/async-throttler/index.d.ts +2 -2
  58. package/dist/async-throttler/useAsyncThrottledCallback.cjs.map +1 -1
  59. package/dist/async-throttler/useAsyncThrottledCallback.d.cts +2 -2
  60. package/dist/async-throttler/useAsyncThrottledCallback.d.ts +2 -2
  61. package/dist/async-throttler/useAsyncThrottledCallback.js.map +1 -1
  62. package/dist/async-throttler/useAsyncThrottler.cjs +26 -2
  63. package/dist/async-throttler/useAsyncThrottler.cjs.map +1 -1
  64. package/dist/async-throttler/useAsyncThrottler.d.cts +27 -2
  65. package/dist/async-throttler/useAsyncThrottler.d.ts +27 -2
  66. package/dist/async-throttler/useAsyncThrottler.js +26 -2
  67. package/dist/async-throttler/useAsyncThrottler.js.map +1 -1
  68. package/dist/batcher/index.cjs +1 -0
  69. package/dist/batcher/index.d.cts +2 -2
  70. package/dist/batcher/index.d.ts +2 -2
  71. package/dist/batcher/useBatchedCallback.cjs +1 -1
  72. package/dist/batcher/useBatchedCallback.cjs.map +1 -1
  73. package/dist/batcher/useBatchedCallback.d.cts +2 -3
  74. package/dist/batcher/useBatchedCallback.d.ts +2 -3
  75. package/dist/batcher/useBatchedCallback.js +1 -1
  76. package/dist/batcher/useBatchedCallback.js.map +1 -1
  77. package/dist/batcher/useBatcher.cjs +19 -0
  78. package/dist/batcher/useBatcher.cjs.map +1 -1
  79. package/dist/batcher/useBatcher.d.cts +22 -2
  80. package/dist/batcher/useBatcher.d.ts +22 -2
  81. package/dist/batcher/useBatcher.js +20 -1
  82. package/dist/batcher/useBatcher.js.map +1 -1
  83. package/dist/debouncer/index.cjs +1 -0
  84. package/dist/debouncer/index.d.cts +2 -2
  85. package/dist/debouncer/index.d.ts +2 -2
  86. package/dist/debouncer/useDebouncedCallback.cjs.map +1 -1
  87. package/dist/debouncer/useDebouncedCallback.d.cts +2 -2
  88. package/dist/debouncer/useDebouncedCallback.d.ts +2 -2
  89. package/dist/debouncer/useDebouncedCallback.js.map +1 -1
  90. package/dist/debouncer/useDebouncedState.cjs.map +1 -1
  91. package/dist/debouncer/useDebouncedState.d.cts +3 -3
  92. package/dist/debouncer/useDebouncedState.d.ts +3 -3
  93. package/dist/debouncer/useDebouncedState.js.map +1 -1
  94. package/dist/debouncer/useDebouncedValue.cjs.map +1 -1
  95. package/dist/debouncer/useDebouncedValue.d.cts +3 -3
  96. package/dist/debouncer/useDebouncedValue.d.ts +3 -3
  97. package/dist/debouncer/useDebouncedValue.js.map +1 -1
  98. package/dist/debouncer/useDebouncer.cjs +15 -2
  99. package/dist/debouncer/useDebouncer.cjs.map +1 -1
  100. package/dist/debouncer/useDebouncer.d.cts +21 -2
  101. package/dist/debouncer/useDebouncer.d.ts +21 -2
  102. package/dist/debouncer/useDebouncer.js +15 -2
  103. package/dist/debouncer/useDebouncer.js.map +1 -1
  104. package/dist/index.cjs +1 -0
  105. package/dist/index.d.cts +11 -11
  106. package/dist/index.d.ts +11 -11
  107. package/dist/provider/PacerProvider.d.cts +2 -2
  108. package/dist/provider/PacerProvider.d.ts +2 -2
  109. package/dist/provider/index.cjs +1 -0
  110. package/dist/queuer/index.cjs +1 -0
  111. package/dist/queuer/index.d.cts +2 -2
  112. package/dist/queuer/index.d.ts +2 -2
  113. package/dist/queuer/useQueuedState.cjs.map +1 -1
  114. package/dist/queuer/useQueuedState.d.cts +3 -3
  115. package/dist/queuer/useQueuedState.d.ts +3 -3
  116. package/dist/queuer/useQueuedState.js.map +1 -1
  117. package/dist/queuer/useQueuedValue.cjs.map +1 -1
  118. package/dist/queuer/useQueuedValue.d.cts +3 -3
  119. package/dist/queuer/useQueuedValue.d.ts +3 -3
  120. package/dist/queuer/useQueuedValue.js.map +1 -1
  121. package/dist/queuer/useQueuer.cjs +19 -0
  122. package/dist/queuer/useQueuer.cjs.map +1 -1
  123. package/dist/queuer/useQueuer.d.cts +22 -2
  124. package/dist/queuer/useQueuer.d.ts +22 -2
  125. package/dist/queuer/useQueuer.js +20 -1
  126. package/dist/queuer/useQueuer.js.map +1 -1
  127. package/dist/rate-limiter/index.cjs +1 -0
  128. package/dist/rate-limiter/index.d.cts +2 -2
  129. package/dist/rate-limiter/index.d.ts +2 -2
  130. package/dist/rate-limiter/useRateLimitedCallback.cjs.map +1 -1
  131. package/dist/rate-limiter/useRateLimitedCallback.d.cts +2 -2
  132. package/dist/rate-limiter/useRateLimitedCallback.d.ts +2 -2
  133. package/dist/rate-limiter/useRateLimitedCallback.js.map +1 -1
  134. package/dist/rate-limiter/useRateLimitedState.cjs.map +1 -1
  135. package/dist/rate-limiter/useRateLimitedState.d.cts +3 -3
  136. package/dist/rate-limiter/useRateLimitedState.d.ts +3 -3
  137. package/dist/rate-limiter/useRateLimitedState.js.map +1 -1
  138. package/dist/rate-limiter/useRateLimitedValue.cjs.map +1 -1
  139. package/dist/rate-limiter/useRateLimitedValue.d.cts +3 -3
  140. package/dist/rate-limiter/useRateLimitedValue.d.ts +3 -3
  141. package/dist/rate-limiter/useRateLimitedValue.js.map +1 -1
  142. package/dist/rate-limiter/useRateLimiter.cjs +5 -0
  143. package/dist/rate-limiter/useRateLimiter.cjs.map +1 -1
  144. package/dist/rate-limiter/useRateLimiter.d.cts +9 -2
  145. package/dist/rate-limiter/useRateLimiter.d.ts +9 -2
  146. package/dist/rate-limiter/useRateLimiter.js +6 -1
  147. package/dist/rate-limiter/useRateLimiter.js.map +1 -1
  148. package/dist/throttler/index.cjs +1 -0
  149. package/dist/throttler/index.d.cts +2 -2
  150. package/dist/throttler/index.d.ts +2 -2
  151. package/dist/throttler/useThrottledCallback.cjs.map +1 -1
  152. package/dist/throttler/useThrottledCallback.d.cts +2 -2
  153. package/dist/throttler/useThrottledCallback.d.ts +2 -2
  154. package/dist/throttler/useThrottledCallback.js.map +1 -1
  155. package/dist/throttler/useThrottledState.cjs.map +1 -1
  156. package/dist/throttler/useThrottledState.d.cts +3 -3
  157. package/dist/throttler/useThrottledState.d.ts +3 -3
  158. package/dist/throttler/useThrottledState.js.map +1 -1
  159. package/dist/throttler/useThrottledValue.cjs.map +1 -1
  160. package/dist/throttler/useThrottledValue.d.cts +3 -3
  161. package/dist/throttler/useThrottledValue.d.ts +3 -3
  162. package/dist/throttler/useThrottledValue.js.map +1 -1
  163. package/dist/throttler/useThrottler.cjs +15 -2
  164. package/dist/throttler/useThrottler.cjs.map +1 -1
  165. package/dist/throttler/useThrottler.d.cts +21 -2
  166. package/dist/throttler/useThrottler.d.ts +21 -2
  167. package/dist/throttler/useThrottler.js +15 -2
  168. package/dist/throttler/useThrottler.js.map +1 -1
  169. package/package.json +36 -35
  170. package/src/async-batcher/useAsyncBatchedCallback.ts +7 -8
  171. package/src/async-batcher/useAsyncBatcher.ts +46 -4
  172. package/src/async-debouncer/useAsyncDebouncedCallback.ts +2 -2
  173. package/src/async-debouncer/useAsyncDebouncer.ts +40 -5
  174. package/src/async-queuer/useAsyncQueuedState.ts +5 -5
  175. package/src/async-queuer/useAsyncQueuer.ts +46 -4
  176. package/src/async-rate-limiter/useAsyncRateLimitedCallback.ts +2 -2
  177. package/src/async-rate-limiter/useAsyncRateLimiter.ts +32 -3
  178. package/src/async-throttler/useAsyncThrottledCallback.ts +2 -2
  179. package/src/async-throttler/useAsyncThrottler.ts +42 -5
  180. package/src/batcher/useBatchedCallback.ts +6 -10
  181. package/src/batcher/useBatcher.ts +39 -3
  182. package/src/debouncer/useDebouncedCallback.ts +2 -2
  183. package/src/debouncer/useDebouncedState.ts +3 -6
  184. package/src/debouncer/useDebouncedValue.ts +3 -6
  185. package/src/debouncer/useDebouncer.ts +33 -5
  186. package/src/queuer/useQueuedState.ts +3 -3
  187. package/src/queuer/useQueuedValue.ts +3 -3
  188. package/src/queuer/useQueuer.ts +39 -3
  189. package/src/rate-limiter/useRateLimitedCallback.ts +2 -2
  190. package/src/rate-limiter/useRateLimitedState.ts +6 -6
  191. package/src/rate-limiter/useRateLimitedValue.ts +6 -6
  192. package/src/rate-limiter/useRateLimiter.ts +24 -4
  193. package/src/throttler/useThrottledCallback.ts +2 -2
  194. package/src/throttler/useThrottledState.ts +3 -6
  195. package/src/throttler/useThrottledValue.ts +3 -6
  196. package/src/throttler/useThrottler.ts +33 -5
@@ -1,11 +1,8 @@
1
1
  import { useEffect } from 'preact/hooks'
2
2
  import { useDebouncedState } from './useDebouncedState'
3
+ import type { PreactDebouncer, PreactDebouncerOptions } from './useDebouncer'
3
4
  import type { Dispatch, StateUpdater } from 'preact/hooks'
4
- import type { PreactDebouncer } from './useDebouncer'
5
- import type {
6
- DebouncerOptions,
7
- DebouncerState,
8
- } from '@tanstack/pacer/debouncer'
5
+ import type { DebouncerState } from '@tanstack/pacer/debouncer'
9
6
 
10
7
  /**
11
8
  * A Preact hook that creates a debounced value that updates only after a specified delay.
@@ -93,7 +90,7 @@ export function useDebouncedValue<
93
90
  TSelected = DebouncerState<Dispatch<StateUpdater<TValue>>>,
94
91
  >(
95
92
  value: TValue,
96
- options: DebouncerOptions<Dispatch<StateUpdater<TValue>>>,
93
+ options: PreactDebouncerOptions<Dispatch<StateUpdater<TValue>>, TSelected>,
97
94
  selector?: (
98
95
  state: DebouncerState<Dispatch<StateUpdater<TValue>>>,
99
96
  ) => TSelected,
@@ -10,6 +10,17 @@ import type {
10
10
  import type { AnyFunction } from '@tanstack/pacer/types'
11
11
  import type { ComponentChildren } from 'preact'
12
12
 
13
+ export interface PreactDebouncerOptions<
14
+ TFn extends AnyFunction,
15
+ TSelected = {},
16
+ > extends DebouncerOptions<TFn> {
17
+ /**
18
+ * Optional callback invoked when the 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: PreactDebouncer<TFn, TSelected>) => void
22
+ }
23
+
13
24
  export interface PreactDebouncer<
14
25
  TFn extends AnyFunction,
15
26
  TSelected = {},
@@ -88,6 +99,18 @@ export interface PreactDebouncer<
88
99
  * - `lastArgs`: The arguments from the most recent call to maybeExecute
89
100
  * - `status`: Current execution status ('disabled' | 'idle' | 'pending')
90
101
  *
102
+ * ## Unmount behavior
103
+ *
104
+ * By default, the hook cancels any pending execution when the component unmounts.
105
+ * Use the `onUnmount` option to customize this. For example, to flush pending work instead:
106
+ *
107
+ * ```tsx
108
+ * const debouncer = useDebouncer(fn, {
109
+ * wait: 500,
110
+ * onUnmount: (d) => d.flush()
111
+ * });
112
+ * ```
113
+ *
91
114
  * @example
92
115
  * ```tsx
93
116
  * // Default behavior - no reactive state subscriptions
@@ -139,14 +162,13 @@ export interface PreactDebouncer<
139
162
  */
140
163
  export function useDebouncer<TFn extends AnyFunction, TSelected = {}>(
141
164
  fn: TFn,
142
- options: DebouncerOptions<TFn>,
165
+ options: PreactDebouncerOptions<TFn, TSelected>,
143
166
  selector: (state: DebouncerState<TFn>) => TSelected = () => ({}) as TSelected,
144
167
  ): PreactDebouncer<TFn, TSelected> {
145
168
  const mergedOptions = {
146
169
  ...useDefaultPacerOptions().debouncer,
147
170
  ...options,
148
- } as DebouncerOptions<TFn>
149
-
171
+ } as PreactDebouncerOptions<TFn, TSelected>
150
172
  const [debouncer] = useState(() => {
151
173
  const debouncerInstance = new Debouncer(
152
174
  fn,
@@ -170,11 +192,17 @@ export function useDebouncer<TFn extends AnyFunction, TSelected = {}>(
170
192
  debouncer.fn = fn
171
193
  debouncer.setOptions(mergedOptions)
172
194
 
195
+ /* eslint-disable react-hooks/exhaustive-deps -- cleanup only; runs on unmount */
173
196
  useEffect(() => {
174
197
  return () => {
175
- debouncer.cancel()
198
+ if (mergedOptions.onUnmount) {
199
+ mergedOptions.onUnmount(debouncer)
200
+ } else {
201
+ debouncer.cancel()
202
+ }
176
203
  }
177
- }, [debouncer])
204
+ }, [])
205
+ /* eslint-enable react-hooks/exhaustive-deps */
178
206
 
179
207
  const state = useStore(debouncer.store, selector)
180
208
 
@@ -1,6 +1,6 @@
1
1
  import { useQueuer } from './useQueuer'
2
- import type { PreactQueuer } from './useQueuer'
3
- import type { Queuer, QueuerOptions, QueuerState } from '@tanstack/pacer/queuer'
2
+ import type { PreactQueuer, PreactQueuerOptions } from './useQueuer'
3
+ import type { Queuer, QueuerState } from '@tanstack/pacer/queuer'
4
4
 
5
5
  /**
6
6
  * A Preact hook that creates a queuer with managed state, combining Preact's useState with queuing functionality.
@@ -124,7 +124,7 @@ export function useQueuedState<
124
124
  >,
125
125
  >(
126
126
  fn: (item: TValue) => void,
127
- options: QueuerOptions<TValue> = {},
127
+ options: PreactQueuerOptions<TValue, TSelected> = {},
128
128
  selector?: (state: QueuerState<TValue>) => TSelected,
129
129
  ): [Array<TValue>, Queuer<TValue>['addItem'], PreactQueuer<TValue, TSelected>] {
130
130
  const queue = useQueuer(fn, options, selector)
@@ -1,7 +1,7 @@
1
1
  import { useEffect, useState } from 'preact/hooks'
2
2
  import { useQueuedState } from './useQueuedState'
3
- import type { PreactQueuer } from './useQueuer'
4
- import type { QueuerOptions, QueuerState } from '@tanstack/pacer/queuer'
3
+ import type { PreactQueuer, PreactQueuerOptions } from './useQueuer'
4
+ import type { QueuerState } from '@tanstack/pacer/queuer'
5
5
 
6
6
  /**
7
7
  * A Preact hook that creates a queued value that processes state changes in order with an optional delay.
@@ -108,7 +108,7 @@ export function useQueuedValue<
108
108
  >,
109
109
  >(
110
110
  initialValue: TValue,
111
- options: QueuerOptions<TValue> = {},
111
+ options: PreactQueuerOptions<TValue, TSelected> = {},
112
112
  selector?: (state: QueuerState<TValue>) => TSelected,
113
113
  ): [TValue, PreactQueuer<TValue, TSelected>] {
114
114
  const [value, setValue] = useState<TValue>(initialValue)
@@ -1,4 +1,4 @@
1
- import { useMemo, useState } from 'preact/hooks'
1
+ import { useEffect, useMemo, useState } from 'preact/hooks'
2
2
  import { Queuer } from '@tanstack/pacer/queuer'
3
3
  import { useStore } from '@tanstack/preact-store'
4
4
  import { useDefaultPacerOptions } from '../provider/PacerProvider'
@@ -6,6 +6,17 @@ import type { Store } from '@tanstack/preact-store'
6
6
  import type { QueuerOptions, QueuerState } from '@tanstack/pacer/queuer'
7
7
  import type { ComponentChildren } from 'preact'
8
8
 
9
+ export interface PreactQueuerOptions<
10
+ TValue,
11
+ TSelected = {},
12
+ > extends QueuerOptions<TValue> {
13
+ /**
14
+ * Optional callback invoked when the component unmounts. Receives the queuer instance.
15
+ * When provided, replaces the default cleanup (stop); use it to call flush(), flushAsBatch(), stop(), add logging, etc.
16
+ */
17
+ onUnmount?: (queuer: PreactQueuer<TValue, TSelected>) => void
18
+ }
19
+
9
20
  export interface PreactQueuer<TValue, TSelected = {}> extends Omit<
10
21
  Queuer<TValue>,
11
22
  'store'
@@ -93,6 +104,19 @@ export interface PreactQueuer<TValue, TSelected = {}> extends Omit<
93
104
  * - `size`: Number of items currently in the queue
94
105
  * - `status`: Current processing status ('idle' | 'running' | 'stopped')
95
106
  *
107
+ * ## Unmount behavior
108
+ *
109
+ * By default, the hook stops the queuer when the component unmounts.
110
+ * Use the `onUnmount` option to customize this. For example, to flush pending items instead:
111
+ *
112
+ * ```tsx
113
+ * const queue = useQueuer(fn, {
114
+ * started: true,
115
+ * wait: 1000,
116
+ * onUnmount: (q) => q.flush()
117
+ * });
118
+ * ```
119
+ *
96
120
  * @example
97
121
  * ```tsx
98
122
  * // Default behavior - no reactive state subscriptions
@@ -169,13 +193,13 @@ export interface PreactQueuer<TValue, TSelected = {}> extends Omit<
169
193
  */
170
194
  export function useQueuer<TValue, TSelected = {}>(
171
195
  fn: (item: TValue) => void,
172
- options: QueuerOptions<TValue> = {},
196
+ options: PreactQueuerOptions<TValue, TSelected> = {},
173
197
  selector: (state: QueuerState<TValue>) => TSelected = () => ({}) as TSelected,
174
198
  ): PreactQueuer<TValue, TSelected> {
175
199
  const mergedOptions = {
176
200
  ...useDefaultPacerOptions().queuer,
177
201
  ...options,
178
- } as QueuerOptions<TValue>
202
+ } as PreactQueuerOptions<TValue, TSelected>
179
203
 
180
204
  const [queuer] = useState(() => {
181
205
  const queuerInstance = new Queuer<TValue>(
@@ -200,6 +224,18 @@ export function useQueuer<TValue, TSelected = {}>(
200
224
  queuer.fn = fn
201
225
  queuer.setOptions(mergedOptions)
202
226
 
227
+ /* eslint-disable react-hooks/exhaustive-deps -- cleanup only; runs on unmount */
228
+ useEffect(() => {
229
+ return () => {
230
+ if (mergedOptions.onUnmount) {
231
+ mergedOptions.onUnmount(queuer)
232
+ } else {
233
+ queuer.stop()
234
+ }
235
+ }
236
+ }, [])
237
+ /* eslint-enable react-hooks/exhaustive-deps */
238
+
203
239
  const state = useStore(queuer.store, selector)
204
240
 
205
241
  return useMemo(
@@ -1,7 +1,7 @@
1
1
  import { useCallback } from 'preact/hooks'
2
2
  import { useRateLimiter } from './useRateLimiter'
3
3
  import type { AnyFunction } from '@tanstack/pacer/types'
4
- import type { RateLimiterOptions } from '@tanstack/pacer/rate-limiter'
4
+ import type { PreactRateLimiterOptions } from './useRateLimiter'
5
5
 
6
6
  /**
7
7
  * A Preact hook that creates a rate-limited version of a callback function.
@@ -58,7 +58,7 @@ import type { RateLimiterOptions } from '@tanstack/pacer/rate-limiter'
58
58
  */
59
59
  export function useRateLimitedCallback<TFn extends AnyFunction>(
60
60
  fn: TFn,
61
- options: RateLimiterOptions<TFn>,
61
+ options: PreactRateLimiterOptions<TFn, {}>,
62
62
  ): (...args: Parameters<TFn>) => boolean {
63
63
  const rateLimitedFn = useRateLimiter(fn, options).maybeExecute
64
64
  return useCallback(
@@ -1,11 +1,11 @@
1
1
  import { useState } from 'preact/hooks'
2
2
  import { useRateLimiter } from './useRateLimiter'
3
- import type { Dispatch, StateUpdater } from 'preact/hooks'
4
- import type { PreactRateLimiter } from './useRateLimiter'
5
3
  import type {
6
- RateLimiterOptions,
7
- RateLimiterState,
8
- } from '@tanstack/pacer/rate-limiter'
4
+ PreactRateLimiter,
5
+ PreactRateLimiterOptions,
6
+ } from './useRateLimiter'
7
+ import type { Dispatch, StateUpdater } from 'preact/hooks'
8
+ import type { RateLimiterState } from '@tanstack/pacer/rate-limiter'
9
9
 
10
10
  /**
11
11
  * A Preact hook that creates a rate-limited state value that enforces a hard limit on state updates within a time window.
@@ -107,7 +107,7 @@ import type {
107
107
  */
108
108
  export function useRateLimitedState<TValue, TSelected = RateLimiterState>(
109
109
  value: TValue,
110
- options: RateLimiterOptions<Dispatch<StateUpdater<TValue>>>,
110
+ options: PreactRateLimiterOptions<Dispatch<StateUpdater<TValue>>, TSelected>,
111
111
  selector?: (state: RateLimiterState) => TSelected,
112
112
  ): [
113
113
  TValue,
@@ -1,11 +1,11 @@
1
1
  import { useEffect } from 'preact/hooks'
2
2
  import { useRateLimitedState } from './useRateLimitedState'
3
- import type { Dispatch, StateUpdater } from 'preact/hooks'
4
- import type { PreactRateLimiter } from './useRateLimiter'
5
3
  import type {
6
- RateLimiterOptions,
7
- RateLimiterState,
8
- } from '@tanstack/pacer/rate-limiter'
4
+ PreactRateLimiter,
5
+ PreactRateLimiterOptions,
6
+ } from './useRateLimiter'
7
+ import type { Dispatch, StateUpdater } from 'preact/hooks'
8
+ import type { RateLimiterState } from '@tanstack/pacer/rate-limiter'
9
9
 
10
10
  /**
11
11
  * A high-level Preact hook that creates a rate-limited version of a value that updates at most a certain number of times within a time window.
@@ -96,7 +96,7 @@ import type {
96
96
  */
97
97
  export function useRateLimitedValue<TValue, TSelected = RateLimiterState>(
98
98
  value: TValue,
99
- options: RateLimiterOptions<Dispatch<StateUpdater<TValue>>>,
99
+ options: PreactRateLimiterOptions<Dispatch<StateUpdater<TValue>>, TSelected>,
100
100
  selector?: (state: RateLimiterState) => TSelected,
101
101
  ): [TValue, PreactRateLimiter<Dispatch<StateUpdater<TValue>>, TSelected>] {
102
102
  const [rateLimitedValue, setRateLimitedValue, rateLimiter] =
@@ -1,4 +1,4 @@
1
- import { useMemo, useState } from 'preact/hooks'
1
+ import { useEffect, useMemo, useState } from 'preact/hooks'
2
2
  import { RateLimiter } from '@tanstack/pacer/rate-limiter'
3
3
  import { useStore } from '@tanstack/preact-store'
4
4
  import { useDefaultPacerOptions } from '../provider/PacerProvider'
@@ -10,6 +10,17 @@ import type {
10
10
  import type { AnyFunction } from '@tanstack/pacer/types'
11
11
  import type { ComponentChildren } from 'preact'
12
12
 
13
+ export interface PreactRateLimiterOptions<
14
+ TFn extends AnyFunction,
15
+ TSelected = {},
16
+ > extends RateLimiterOptions<TFn> {
17
+ /**
18
+ * Optional callback invoked when the component unmounts. Receives the rate limiter instance.
19
+ * When provided, replaces the default cleanup; use it to call reset(), add logging, etc.
20
+ */
21
+ onUnmount?: (rateLimiter: PreactRateLimiter<TFn, TSelected>) => void
22
+ }
23
+
13
24
  export interface PreactRateLimiter<
14
25
  TFn extends AnyFunction,
15
26
  TSelected = {},
@@ -178,14 +189,13 @@ export interface PreactRateLimiter<
178
189
  */
179
190
  export function useRateLimiter<TFn extends AnyFunction, TSelected = {}>(
180
191
  fn: TFn,
181
- options: RateLimiterOptions<TFn>,
192
+ options: PreactRateLimiterOptions<TFn, TSelected>,
182
193
  selector: (state: RateLimiterState) => TSelected = () => ({}) as TSelected,
183
194
  ): PreactRateLimiter<TFn, TSelected> {
184
195
  const mergedOptions = {
185
196
  ...useDefaultPacerOptions().rateLimiter,
186
197
  ...options,
187
- } as RateLimiterOptions<TFn>
188
-
198
+ } as PreactRateLimiterOptions<TFn, TSelected>
189
199
  const [rateLimiter] = useState(() => {
190
200
  const rateLimiterInstance = new RateLimiter<TFn>(
191
201
  fn,
@@ -209,6 +219,16 @@ export function useRateLimiter<TFn extends AnyFunction, TSelected = {}>(
209
219
  rateLimiter.fn = fn
210
220
  rateLimiter.setOptions(mergedOptions)
211
221
 
222
+ /* eslint-disable react-hooks/exhaustive-deps -- cleanup only; runs on unmount */
223
+ useEffect(() => {
224
+ return () => {
225
+ if (mergedOptions.onUnmount) {
226
+ mergedOptions.onUnmount(rateLimiter)
227
+ }
228
+ }
229
+ }, [])
230
+ /* eslint-enable react-hooks/exhaustive-deps */
231
+
212
232
  const state = useStore(rateLimiter.store, selector)
213
233
 
214
234
  return useMemo(
@@ -1,6 +1,6 @@
1
1
  import { useCallback } from 'preact/hooks'
2
2
  import { useThrottler } from './useThrottler'
3
- import type { ThrottlerOptions } from '@tanstack/pacer/throttler'
3
+ import type { PreactThrottlerOptions } from './useThrottler'
4
4
  import type { AnyFunction } from '@tanstack/pacer/types'
5
5
 
6
6
  /**
@@ -42,7 +42,7 @@ import type { AnyFunction } from '@tanstack/pacer/types'
42
42
  */
43
43
  export function useThrottledCallback<TFn extends AnyFunction>(
44
44
  fn: TFn,
45
- options: ThrottlerOptions<TFn>,
45
+ options: PreactThrottlerOptions<TFn, {}>,
46
46
  ): (...args: Parameters<TFn>) => void {
47
47
  const throttledFn = useThrottler(fn, options).maybeExecute
48
48
  return useCallback(
@@ -1,11 +1,8 @@
1
1
  import { useState } from 'preact/hooks'
2
2
  import { useThrottler } from './useThrottler'
3
3
  import type { Dispatch, StateUpdater } from 'preact/hooks'
4
- import type { PreactThrottler } from './useThrottler'
5
- import type {
6
- ThrottlerOptions,
7
- ThrottlerState,
8
- } from '@tanstack/pacer/throttler'
4
+ import type { PreactThrottler, PreactThrottlerOptions } from './useThrottler'
5
+ import type { ThrottlerState } from '@tanstack/pacer/throttler'
9
6
 
10
7
  /**
11
8
  * A Preact hook that creates a throttled state value that updates at most once within a specified time window.
@@ -96,7 +93,7 @@ export function useThrottledState<
96
93
  TSelected = ThrottlerState<Dispatch<StateUpdater<TValue>>>,
97
94
  >(
98
95
  value: TValue,
99
- options: ThrottlerOptions<Dispatch<StateUpdater<TValue>>>,
96
+ options: PreactThrottlerOptions<Dispatch<StateUpdater<TValue>>, TSelected>,
100
97
  selector?: (
101
98
  state: ThrottlerState<Dispatch<StateUpdater<TValue>>>,
102
99
  ) => TSelected,
@@ -1,11 +1,8 @@
1
1
  import { useEffect } from 'preact/hooks'
2
2
  import { useThrottledState } from './useThrottledState'
3
3
  import type { Dispatch, StateUpdater } from 'preact/hooks'
4
- import type { PreactThrottler } from './useThrottler'
5
- import type {
6
- ThrottlerOptions,
7
- ThrottlerState,
8
- } from '@tanstack/pacer/throttler'
4
+ import type { PreactThrottler, PreactThrottlerOptions } from './useThrottler'
5
+ import type { ThrottlerState } from '@tanstack/pacer/throttler'
9
6
 
10
7
  /**
11
8
  * A high-level Preact hook that creates a throttled version of a value that updates at most once within a specified time window.
@@ -88,7 +85,7 @@ export function useThrottledValue<
88
85
  TSelected = ThrottlerState<Dispatch<StateUpdater<TValue>>>,
89
86
  >(
90
87
  value: TValue,
91
- options: ThrottlerOptions<Dispatch<StateUpdater<TValue>>>,
88
+ options: PreactThrottlerOptions<Dispatch<StateUpdater<TValue>>, TSelected>,
92
89
  selector?: (
93
90
  state: ThrottlerState<Dispatch<StateUpdater<TValue>>>,
94
91
  ) => TSelected,
@@ -10,6 +10,17 @@ import type {
10
10
  } from '@tanstack/pacer/throttler'
11
11
  import type { ComponentChildren } from 'preact'
12
12
 
13
+ export interface PreactThrottlerOptions<
14
+ TFn extends AnyFunction,
15
+ TSelected = {},
16
+ > extends ThrottlerOptions<TFn> {
17
+ /**
18
+ * Optional callback invoked when the component unmounts. Receives the throttler instance.
19
+ * When provided, replaces the default cleanup (cancel); use it to call flush(), reset(), cancel(), add logging, etc.
20
+ */
21
+ onUnmount?: (throttler: PreactThrottler<TFn, TSelected>) => void
22
+ }
23
+
13
24
  export interface PreactThrottler<
14
25
  TFn extends AnyFunction,
15
26
  TSelected = {},
@@ -85,6 +96,18 @@ export interface PreactThrottler<
85
96
  * - `isPending`: Whether the throttler is waiting for the timeout to trigger execution
86
97
  * - `status`: Current execution status ('disabled' | 'idle' | 'pending')
87
98
  *
99
+ * ## Unmount behavior
100
+ *
101
+ * By default, the hook cancels any pending execution when the component unmounts.
102
+ * Use the `onUnmount` option to customize this. For example, to flush pending work instead:
103
+ *
104
+ * ```tsx
105
+ * const throttler = useThrottler(fn, {
106
+ * wait: 1000,
107
+ * onUnmount: (t) => t.flush()
108
+ * });
109
+ * ```
110
+ *
88
111
  * @example
89
112
  * ```tsx
90
113
  * // Default behavior - no reactive state subscriptions
@@ -144,14 +167,13 @@ export interface PreactThrottler<
144
167
  */
145
168
  export function useThrottler<TFn extends AnyFunction, TSelected = {}>(
146
169
  fn: TFn,
147
- options: ThrottlerOptions<TFn>,
170
+ options: PreactThrottlerOptions<TFn, TSelected>,
148
171
  selector: (state: ThrottlerState<TFn>) => TSelected = () => ({}) as TSelected,
149
172
  ): PreactThrottler<TFn, TSelected> {
150
173
  const mergedOptions = {
151
174
  ...useDefaultPacerOptions().throttler,
152
175
  ...options,
153
- } as ThrottlerOptions<TFn>
154
-
176
+ } as PreactThrottlerOptions<TFn, TSelected>
155
177
  const [throttler] = useState(() => {
156
178
  const throttlerInstance = new Throttler<TFn>(
157
179
  fn,
@@ -177,11 +199,17 @@ export function useThrottler<TFn extends AnyFunction, TSelected = {}>(
177
199
 
178
200
  const state = useStore(throttler.store, selector)
179
201
 
202
+ /* eslint-disable react-hooks/exhaustive-deps -- cleanup only; runs on unmount */
180
203
  useEffect(() => {
181
204
  return () => {
182
- throttler.cancel()
205
+ if (mergedOptions.onUnmount) {
206
+ mergedOptions.onUnmount(throttler)
207
+ } else {
208
+ throttler.cancel()
209
+ }
183
210
  }
184
- }, [throttler])
211
+ }, [])
212
+ /* eslint-enable react-hooks/exhaustive-deps */
185
213
 
186
214
  return useMemo(
187
215
  () =>