@tanstack/preact-pacer 0.19.4 → 0.21.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -6
- package/dist/async-batcher/index.cjs +1 -0
- package/dist/async-batcher/index.d.cts +2 -2
- package/dist/async-batcher/index.d.ts +2 -2
- package/dist/async-batcher/useAsyncBatchedCallback.cjs +2 -2
- package/dist/async-batcher/useAsyncBatchedCallback.cjs.map +1 -1
- package/dist/async-batcher/useAsyncBatchedCallback.d.cts +2 -3
- package/dist/async-batcher/useAsyncBatchedCallback.d.ts +2 -3
- package/dist/async-batcher/useAsyncBatchedCallback.js +2 -2
- package/dist/async-batcher/useAsyncBatchedCallback.js.map +1 -1
- package/dist/async-batcher/useAsyncBatcher.cjs +30 -2
- package/dist/async-batcher/useAsyncBatcher.cjs.map +1 -1
- package/dist/async-batcher/useAsyncBatcher.d.cts +28 -2
- package/dist/async-batcher/useAsyncBatcher.d.ts +28 -2
- package/dist/async-batcher/useAsyncBatcher.js +32 -4
- package/dist/async-batcher/useAsyncBatcher.js.map +1 -1
- package/dist/async-debouncer/index.cjs +1 -0
- package/dist/async-debouncer/index.d.cts +2 -2
- package/dist/async-debouncer/index.d.ts +2 -2
- package/dist/async-debouncer/useAsyncDebouncedCallback.cjs.map +1 -1
- package/dist/async-debouncer/useAsyncDebouncedCallback.d.cts +2 -2
- package/dist/async-debouncer/useAsyncDebouncedCallback.d.ts +2 -2
- package/dist/async-debouncer/useAsyncDebouncedCallback.js.map +1 -1
- package/dist/async-debouncer/useAsyncDebouncer.cjs +26 -4
- package/dist/async-debouncer/useAsyncDebouncer.cjs.map +1 -1
- package/dist/async-debouncer/useAsyncDebouncer.d.cts +28 -3
- package/dist/async-debouncer/useAsyncDebouncer.d.ts +27 -2
- package/dist/async-debouncer/useAsyncDebouncer.js +27 -5
- package/dist/async-debouncer/useAsyncDebouncer.js.map +1 -1
- package/dist/async-queuer/index.cjs +1 -0
- package/dist/async-queuer/index.d.cts +2 -2
- package/dist/async-queuer/index.d.ts +2 -2
- package/dist/async-queuer/useAsyncQueuedState.cjs.map +1 -1
- package/dist/async-queuer/useAsyncQueuedState.d.cts +3 -3
- package/dist/async-queuer/useAsyncQueuedState.d.ts +3 -3
- package/dist/async-queuer/useAsyncQueuedState.js.map +1 -1
- package/dist/async-queuer/useAsyncQueuer.cjs +30 -2
- package/dist/async-queuer/useAsyncQueuer.cjs.map +1 -1
- package/dist/async-queuer/useAsyncQueuer.d.cts +28 -2
- package/dist/async-queuer/useAsyncQueuer.d.ts +28 -2
- package/dist/async-queuer/useAsyncQueuer.js +32 -4
- package/dist/async-queuer/useAsyncQueuer.js.map +1 -1
- package/dist/async-rate-limiter/index.cjs +1 -0
- package/dist/async-rate-limiter/index.d.cts +2 -2
- package/dist/async-rate-limiter/index.d.ts +2 -2
- package/dist/async-rate-limiter/useAsyncRateLimitedCallback.cjs.map +1 -1
- package/dist/async-rate-limiter/useAsyncRateLimitedCallback.d.cts +2 -2
- package/dist/async-rate-limiter/useAsyncRateLimitedCallback.d.ts +2 -2
- package/dist/async-rate-limiter/useAsyncRateLimitedCallback.js.map +1 -1
- package/dist/async-rate-limiter/useAsyncRateLimiter.cjs +14 -2
- package/dist/async-rate-limiter/useAsyncRateLimiter.cjs.map +1 -1
- package/dist/async-rate-limiter/useAsyncRateLimiter.d.cts +15 -2
- package/dist/async-rate-limiter/useAsyncRateLimiter.d.ts +15 -2
- package/dist/async-rate-limiter/useAsyncRateLimiter.js +16 -4
- package/dist/async-rate-limiter/useAsyncRateLimiter.js.map +1 -1
- package/dist/async-throttler/index.cjs +1 -0
- package/dist/async-throttler/index.d.cts +2 -2
- package/dist/async-throttler/index.d.ts +2 -2
- package/dist/async-throttler/useAsyncThrottledCallback.cjs.map +1 -1
- package/dist/async-throttler/useAsyncThrottledCallback.d.cts +2 -2
- package/dist/async-throttler/useAsyncThrottledCallback.d.ts +2 -2
- package/dist/async-throttler/useAsyncThrottledCallback.js.map +1 -1
- package/dist/async-throttler/useAsyncThrottler.cjs +28 -4
- package/dist/async-throttler/useAsyncThrottler.cjs.map +1 -1
- package/dist/async-throttler/useAsyncThrottler.d.cts +27 -2
- package/dist/async-throttler/useAsyncThrottler.d.ts +27 -2
- package/dist/async-throttler/useAsyncThrottler.js +29 -5
- package/dist/async-throttler/useAsyncThrottler.js.map +1 -1
- package/dist/batcher/index.cjs +1 -0
- package/dist/batcher/index.d.cts +2 -2
- package/dist/batcher/index.d.ts +2 -2
- package/dist/batcher/useBatchedCallback.cjs +1 -1
- package/dist/batcher/useBatchedCallback.cjs.map +1 -1
- package/dist/batcher/useBatchedCallback.d.cts +2 -3
- package/dist/batcher/useBatchedCallback.d.ts +2 -3
- package/dist/batcher/useBatchedCallback.js +1 -1
- package/dist/batcher/useBatchedCallback.js.map +1 -1
- package/dist/batcher/useBatcher.cjs +21 -2
- package/dist/batcher/useBatcher.cjs.map +1 -1
- package/dist/batcher/useBatcher.d.cts +22 -2
- package/dist/batcher/useBatcher.d.ts +22 -2
- package/dist/batcher/useBatcher.js +23 -4
- package/dist/batcher/useBatcher.js.map +1 -1
- package/dist/debouncer/index.cjs +1 -0
- package/dist/debouncer/index.d.cts +2 -2
- package/dist/debouncer/index.d.ts +2 -2
- package/dist/debouncer/useDebouncedCallback.cjs.map +1 -1
- package/dist/debouncer/useDebouncedCallback.d.cts +2 -2
- package/dist/debouncer/useDebouncedCallback.d.ts +2 -2
- package/dist/debouncer/useDebouncedCallback.js.map +1 -1
- package/dist/debouncer/useDebouncedState.cjs.map +1 -1
- package/dist/debouncer/useDebouncedState.d.cts +3 -3
- package/dist/debouncer/useDebouncedState.d.ts +3 -3
- package/dist/debouncer/useDebouncedState.js.map +1 -1
- package/dist/debouncer/useDebouncedValue.cjs.map +1 -1
- package/dist/debouncer/useDebouncedValue.d.cts +3 -3
- package/dist/debouncer/useDebouncedValue.d.ts +3 -3
- package/dist/debouncer/useDebouncedValue.js.map +1 -1
- package/dist/debouncer/useDebouncer.cjs +17 -4
- package/dist/debouncer/useDebouncer.cjs.map +1 -1
- package/dist/debouncer/useDebouncer.d.cts +21 -2
- package/dist/debouncer/useDebouncer.d.ts +21 -2
- package/dist/debouncer/useDebouncer.js +18 -5
- package/dist/debouncer/useDebouncer.js.map +1 -1
- package/dist/index.cjs +1 -0
- package/dist/index.d.cts +11 -11
- package/dist/index.d.ts +11 -11
- package/dist/provider/PacerProvider.cjs.map +1 -1
- package/dist/provider/PacerProvider.d.cts +2 -2
- package/dist/provider/PacerProvider.d.ts +2 -2
- package/dist/provider/PacerProvider.js.map +1 -1
- package/dist/provider/index.cjs +1 -0
- package/dist/queuer/index.cjs +1 -0
- package/dist/queuer/index.d.cts +2 -2
- package/dist/queuer/index.d.ts +2 -2
- package/dist/queuer/useQueuedState.cjs.map +1 -1
- package/dist/queuer/useQueuedState.d.cts +3 -3
- package/dist/queuer/useQueuedState.d.ts +3 -3
- package/dist/queuer/useQueuedState.js.map +1 -1
- package/dist/queuer/useQueuedValue.cjs.map +1 -1
- package/dist/queuer/useQueuedValue.d.cts +3 -3
- package/dist/queuer/useQueuedValue.d.ts +3 -3
- package/dist/queuer/useQueuedValue.js.map +1 -1
- package/dist/queuer/useQueuer.cjs +21 -2
- package/dist/queuer/useQueuer.cjs.map +1 -1
- package/dist/queuer/useQueuer.d.cts +22 -2
- package/dist/queuer/useQueuer.d.ts +22 -2
- package/dist/queuer/useQueuer.js +23 -4
- package/dist/queuer/useQueuer.js.map +1 -1
- package/dist/rate-limiter/index.cjs +1 -0
- package/dist/rate-limiter/index.d.cts +2 -2
- package/dist/rate-limiter/index.d.ts +2 -2
- package/dist/rate-limiter/useRateLimitedCallback.cjs.map +1 -1
- package/dist/rate-limiter/useRateLimitedCallback.d.cts +2 -2
- package/dist/rate-limiter/useRateLimitedCallback.d.ts +2 -2
- package/dist/rate-limiter/useRateLimitedCallback.js.map +1 -1
- package/dist/rate-limiter/useRateLimitedState.cjs.map +1 -1
- package/dist/rate-limiter/useRateLimitedState.d.cts +3 -3
- package/dist/rate-limiter/useRateLimitedState.d.ts +3 -3
- package/dist/rate-limiter/useRateLimitedState.js.map +1 -1
- package/dist/rate-limiter/useRateLimitedValue.cjs.map +1 -1
- package/dist/rate-limiter/useRateLimitedValue.d.cts +3 -3
- package/dist/rate-limiter/useRateLimitedValue.d.ts +3 -3
- package/dist/rate-limiter/useRateLimitedValue.js.map +1 -1
- package/dist/rate-limiter/useRateLimiter.cjs +7 -2
- package/dist/rate-limiter/useRateLimiter.cjs.map +1 -1
- package/dist/rate-limiter/useRateLimiter.d.cts +9 -2
- package/dist/rate-limiter/useRateLimiter.d.ts +9 -2
- package/dist/rate-limiter/useRateLimiter.js +9 -4
- package/dist/rate-limiter/useRateLimiter.js.map +1 -1
- package/dist/throttler/index.cjs +1 -0
- package/dist/throttler/index.d.cts +2 -2
- package/dist/throttler/index.d.ts +2 -2
- package/dist/throttler/useThrottledCallback.cjs.map +1 -1
- package/dist/throttler/useThrottledCallback.d.cts +2 -2
- package/dist/throttler/useThrottledCallback.d.ts +2 -2
- package/dist/throttler/useThrottledCallback.js.map +1 -1
- package/dist/throttler/useThrottledState.cjs.map +1 -1
- package/dist/throttler/useThrottledState.d.cts +3 -3
- package/dist/throttler/useThrottledState.d.ts +3 -3
- package/dist/throttler/useThrottledState.js.map +1 -1
- package/dist/throttler/useThrottledValue.cjs.map +1 -1
- package/dist/throttler/useThrottledValue.d.cts +3 -3
- package/dist/throttler/useThrottledValue.d.ts +3 -3
- package/dist/throttler/useThrottledValue.js.map +1 -1
- package/dist/throttler/useThrottler.cjs +17 -4
- package/dist/throttler/useThrottler.cjs.map +1 -1
- package/dist/throttler/useThrottler.d.cts +21 -2
- package/dist/throttler/useThrottler.d.ts +21 -2
- package/dist/throttler/useThrottler.js +18 -5
- package/dist/throttler/useThrottler.js.map +1 -1
- package/package.json +36 -35
- package/src/async-batcher/useAsyncBatchedCallback.ts +7 -8
- package/src/async-batcher/useAsyncBatcher.ts +51 -7
- package/src/async-debouncer/useAsyncDebouncedCallback.ts +2 -2
- package/src/async-debouncer/useAsyncDebouncer.ts +45 -8
- package/src/async-queuer/useAsyncQueuedState.ts +5 -5
- package/src/async-queuer/useAsyncQueuer.ts +51 -7
- package/src/async-rate-limiter/useAsyncRateLimitedCallback.ts +2 -2
- package/src/async-rate-limiter/useAsyncRateLimiter.ts +37 -6
- package/src/async-throttler/useAsyncThrottledCallback.ts +2 -2
- package/src/async-throttler/useAsyncThrottler.ts +47 -8
- package/src/batcher/useBatchedCallback.ts +6 -10
- package/src/batcher/useBatcher.ts +44 -6
- package/src/debouncer/useDebouncedCallback.ts +2 -2
- package/src/debouncer/useDebouncedState.ts +3 -6
- package/src/debouncer/useDebouncedValue.ts +3 -6
- package/src/debouncer/useDebouncer.ts +38 -8
- package/src/queuer/useQueuedState.ts +3 -3
- package/src/queuer/useQueuedValue.ts +3 -3
- package/src/queuer/useQueuer.ts +44 -6
- package/src/rate-limiter/useRateLimitedCallback.ts +2 -2
- package/src/rate-limiter/useRateLimitedState.ts +6 -6
- package/src/rate-limiter/useRateLimitedValue.ts +6 -6
- package/src/rate-limiter/useRateLimiter.ts +29 -7
- package/src/throttler/useThrottledCallback.ts +2 -2
- package/src/throttler/useThrottledState.ts +3 -6
- package/src/throttler/useThrottledValue.ts +3 -6
- package/src/throttler/useThrottler.ts +38 -8
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { useMemo, useState } from 'preact/hooks'
|
|
1
|
+
import { useEffect, useMemo, useState } from 'preact/hooks'
|
|
2
2
|
import { AsyncRateLimiter } from '@tanstack/pacer/async-rate-limiter'
|
|
3
|
-
import { useStore } from '@tanstack/preact-store'
|
|
3
|
+
import { shallow, useStore } from '@tanstack/preact-store'
|
|
4
4
|
import { useDefaultPacerOptions } from '../provider/PacerProvider'
|
|
5
5
|
import type { Store } from '@tanstack/preact-store'
|
|
6
6
|
import type { AnyAsyncFunction } from '@tanstack/pacer/types'
|
|
@@ -10,6 +10,17 @@ import type {
|
|
|
10
10
|
} from '@tanstack/pacer/async-rate-limiter'
|
|
11
11
|
import type { ComponentChildren } from 'preact'
|
|
12
12
|
|
|
13
|
+
export interface PreactAsyncRateLimiterOptions<
|
|
14
|
+
TFn extends AnyAsyncFunction,
|
|
15
|
+
TSelected = {},
|
|
16
|
+
> extends AsyncRateLimiterOptions<TFn> {
|
|
17
|
+
/**
|
|
18
|
+
* Optional callback invoked when the component unmounts. Receives the rate limiter instance.
|
|
19
|
+
* When provided, replaces the default cleanup (abort); use it to call reset(), add logging, etc.
|
|
20
|
+
*/
|
|
21
|
+
onUnmount?: (rateLimiter: PreactAsyncRateLimiter<TFn, TSelected>) => void
|
|
22
|
+
}
|
|
23
|
+
|
|
13
24
|
export interface PreactAsyncRateLimiter<
|
|
14
25
|
TFn extends AnyAsyncFunction,
|
|
15
26
|
TSelected = {},
|
|
@@ -103,6 +114,12 @@ export interface PreactAsyncRateLimiter<
|
|
|
103
114
|
* - `settleCount`: Number of function executions that have completed (success or error)
|
|
104
115
|
* - `successCount`: Number of function executions that have completed successfully
|
|
105
116
|
*
|
|
117
|
+
* ## Unmount behavior
|
|
118
|
+
*
|
|
119
|
+
* By default, the hook aborts any in-flight execution when the component unmounts.
|
|
120
|
+
* Abort only cancels underlying operations (e.g. fetch) when the abort signal from `getAbortSignal()` is passed to them.
|
|
121
|
+
* Use the `onUnmount` option to customize this.
|
|
122
|
+
*
|
|
106
123
|
* @example
|
|
107
124
|
* ```tsx
|
|
108
125
|
* // Default behavior - no reactive state subscriptions
|
|
@@ -216,14 +233,14 @@ export function useAsyncRateLimiter<
|
|
|
216
233
|
TSelected = {},
|
|
217
234
|
>(
|
|
218
235
|
fn: TFn,
|
|
219
|
-
options:
|
|
236
|
+
options: PreactAsyncRateLimiterOptions<TFn, TSelected>,
|
|
220
237
|
selector: (state: AsyncRateLimiterState<TFn>) => TSelected = () =>
|
|
221
238
|
({}) as TSelected,
|
|
222
239
|
): PreactAsyncRateLimiter<TFn, TSelected> {
|
|
223
240
|
const mergedOptions = {
|
|
224
241
|
...useDefaultPacerOptions().asyncRateLimiter,
|
|
225
242
|
...options,
|
|
226
|
-
} as
|
|
243
|
+
} as PreactAsyncRateLimiterOptions<TFn, TSelected>
|
|
227
244
|
|
|
228
245
|
const [asyncRateLimiter] = useState(() => {
|
|
229
246
|
const rateLimiterInstance = new AsyncRateLimiter<TFn>(
|
|
@@ -235,7 +252,9 @@ export function useAsyncRateLimiter<
|
|
|
235
252
|
selector: (state: AsyncRateLimiterState<TFn>) => TSelected
|
|
236
253
|
children: ((state: TSelected) => ComponentChildren) | ComponentChildren
|
|
237
254
|
}) {
|
|
238
|
-
const selected = useStore(rateLimiterInstance.store, props.selector
|
|
255
|
+
const selected = useStore(rateLimiterInstance.store, props.selector, {
|
|
256
|
+
equal: shallow,
|
|
257
|
+
})
|
|
239
258
|
|
|
240
259
|
return typeof props.children === 'function'
|
|
241
260
|
? props.children(selected)
|
|
@@ -248,7 +267,19 @@ export function useAsyncRateLimiter<
|
|
|
248
267
|
asyncRateLimiter.fn = fn
|
|
249
268
|
asyncRateLimiter.setOptions(mergedOptions)
|
|
250
269
|
|
|
251
|
-
|
|
270
|
+
/* eslint-disable react-hooks/exhaustive-deps -- cleanup only; runs on unmount */
|
|
271
|
+
useEffect(() => {
|
|
272
|
+
return () => {
|
|
273
|
+
if (mergedOptions.onUnmount) {
|
|
274
|
+
mergedOptions.onUnmount(asyncRateLimiter)
|
|
275
|
+
} else {
|
|
276
|
+
asyncRateLimiter.abort()
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
}, [])
|
|
280
|
+
/* eslint-enable react-hooks/exhaustive-deps */
|
|
281
|
+
|
|
282
|
+
const state = useStore(asyncRateLimiter.store, selector, { equal: shallow })
|
|
252
283
|
|
|
253
284
|
return useMemo(
|
|
254
285
|
() =>
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { useCallback } from 'preact/hooks'
|
|
2
2
|
import { useAsyncThrottler } from './useAsyncThrottler'
|
|
3
|
-
import type {
|
|
3
|
+
import type { PreactAsyncThrottlerOptions } from './useAsyncThrottler'
|
|
4
4
|
import type { AnyAsyncFunction } from '@tanstack/pacer/types'
|
|
5
5
|
|
|
6
6
|
/**
|
|
@@ -41,7 +41,7 @@ import type { AnyAsyncFunction } from '@tanstack/pacer/types'
|
|
|
41
41
|
*/
|
|
42
42
|
export function useAsyncThrottledCallback<TFn extends AnyAsyncFunction>(
|
|
43
43
|
fn: TFn,
|
|
44
|
-
options:
|
|
44
|
+
options: PreactAsyncThrottlerOptions<TFn, {}>,
|
|
45
45
|
): (...args: Parameters<TFn>) => Promise<ReturnType<TFn>> {
|
|
46
46
|
const asyncThrottledFn = useAsyncThrottler(fn, options).maybeExecute
|
|
47
47
|
return useCallback(
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { useEffect, useMemo, useState } from 'preact/hooks'
|
|
2
2
|
import { AsyncThrottler } from '@tanstack/pacer/async-throttler'
|
|
3
|
-
import { useStore } from '@tanstack/preact-store'
|
|
3
|
+
import { shallow, useStore } from '@tanstack/preact-store'
|
|
4
4
|
import { useDefaultPacerOptions } from '../provider/PacerProvider'
|
|
5
5
|
import type { Store } from '@tanstack/preact-store'
|
|
6
6
|
import type { AnyAsyncFunction } from '@tanstack/pacer/types'
|
|
@@ -10,6 +10,17 @@ import type {
|
|
|
10
10
|
} from '@tanstack/pacer/async-throttler'
|
|
11
11
|
import type { ComponentChildren } from 'preact'
|
|
12
12
|
|
|
13
|
+
export interface PreactAsyncThrottlerOptions<
|
|
14
|
+
TFn extends AnyAsyncFunction,
|
|
15
|
+
TSelected = {},
|
|
16
|
+
> extends AsyncThrottlerOptions<TFn> {
|
|
17
|
+
/**
|
|
18
|
+
* Optional callback invoked when the component unmounts. Receives the throttler instance.
|
|
19
|
+
* When provided, replaces the default cleanup (cancel + abort); use it to call flush(), reset(), cancel(), add logging, etc.
|
|
20
|
+
*/
|
|
21
|
+
onUnmount?: (throttler: PreactAsyncThrottler<TFn, TSelected>) => void
|
|
22
|
+
}
|
|
23
|
+
|
|
13
24
|
export interface PreactAsyncThrottler<
|
|
14
25
|
TFn extends AnyAsyncFunction,
|
|
15
26
|
TSelected = {},
|
|
@@ -99,6 +110,24 @@ export interface PreactAsyncThrottler<
|
|
|
99
110
|
* - `status`: Current execution status ('disabled' | 'idle' | 'pending' | 'executing' | 'settled')
|
|
100
111
|
* - `successCount`: Number of function executions that have completed successfully
|
|
101
112
|
*
|
|
113
|
+
* ## Unmount behavior
|
|
114
|
+
*
|
|
115
|
+
* By default, the hook cancels any pending execution and aborts any in-flight execution when the component unmounts.
|
|
116
|
+
* Abort only cancels underlying operations (e.g. fetch) when the abort signal from `getAbortSignal()` is passed to them.
|
|
117
|
+
* Use the `onUnmount` option to customize this. For example, to flush pending work instead:
|
|
118
|
+
*
|
|
119
|
+
* ```tsx
|
|
120
|
+
* const throttler = useAsyncThrottler(fn, {
|
|
121
|
+
* wait: 1000,
|
|
122
|
+
* onUnmount: (t) => t.flush()
|
|
123
|
+
* });
|
|
124
|
+
* ```
|
|
125
|
+
*
|
|
126
|
+
* Note: For async utils, `flush()` returns a Promise and runs fire-and-forget in the cleanup.
|
|
127
|
+
* If your throttled function updates Preact state, those updates may run after the component has
|
|
128
|
+
* unmounted, which can cause "setState on unmounted component" warnings. Guard your callbacks
|
|
129
|
+
* accordingly when using onUnmount with flush.
|
|
130
|
+
*
|
|
102
131
|
* @example
|
|
103
132
|
* ```tsx
|
|
104
133
|
* // Default behavior - no reactive state subscriptions
|
|
@@ -195,15 +224,14 @@ export interface PreactAsyncThrottler<
|
|
|
195
224
|
*/
|
|
196
225
|
export function useAsyncThrottler<TFn extends AnyAsyncFunction, TSelected = {}>(
|
|
197
226
|
fn: TFn,
|
|
198
|
-
options:
|
|
227
|
+
options: PreactAsyncThrottlerOptions<TFn, TSelected>,
|
|
199
228
|
selector: (state: AsyncThrottlerState<TFn>) => TSelected = () =>
|
|
200
229
|
({}) as TSelected,
|
|
201
230
|
): PreactAsyncThrottler<TFn, TSelected> {
|
|
202
231
|
const mergedOptions = {
|
|
203
232
|
...useDefaultPacerOptions().asyncThrottler,
|
|
204
233
|
...options,
|
|
205
|
-
} as
|
|
206
|
-
|
|
234
|
+
} as PreactAsyncThrottlerOptions<TFn, TSelected>
|
|
207
235
|
const [asyncThrottler] = useState(() => {
|
|
208
236
|
const throttlerInstance = new AsyncThrottler<TFn>(
|
|
209
237
|
fn,
|
|
@@ -214,7 +242,9 @@ export function useAsyncThrottler<TFn extends AnyAsyncFunction, TSelected = {}>(
|
|
|
214
242
|
selector: (state: AsyncThrottlerState<TFn>) => TSelected
|
|
215
243
|
children: ((state: TSelected) => ComponentChildren) | ComponentChildren
|
|
216
244
|
}) {
|
|
217
|
-
const selected = useStore(throttlerInstance.store, props.selector
|
|
245
|
+
const selected = useStore(throttlerInstance.store, props.selector, {
|
|
246
|
+
equal: shallow,
|
|
247
|
+
})
|
|
218
248
|
|
|
219
249
|
return typeof props.children === 'function'
|
|
220
250
|
? props.children(selected)
|
|
@@ -227,11 +257,20 @@ export function useAsyncThrottler<TFn extends AnyAsyncFunction, TSelected = {}>(
|
|
|
227
257
|
asyncThrottler.fn = fn
|
|
228
258
|
asyncThrottler.setOptions(mergedOptions)
|
|
229
259
|
|
|
230
|
-
const state = useStore(asyncThrottler.store, selector)
|
|
260
|
+
const state = useStore(asyncThrottler.store, selector, { equal: shallow })
|
|
231
261
|
|
|
262
|
+
/* eslint-disable react-hooks/exhaustive-deps -- cleanup only; runs on unmount */
|
|
232
263
|
useEffect(() => {
|
|
233
|
-
return () =>
|
|
234
|
-
|
|
264
|
+
return () => {
|
|
265
|
+
if (mergedOptions.onUnmount) {
|
|
266
|
+
mergedOptions.onUnmount(asyncThrottler)
|
|
267
|
+
} else {
|
|
268
|
+
asyncThrottler.cancel()
|
|
269
|
+
asyncThrottler.abort()
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
}, [])
|
|
273
|
+
/* eslint-enable react-hooks/exhaustive-deps */
|
|
235
274
|
|
|
236
275
|
return useMemo(
|
|
237
276
|
() =>
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { useCallback } from 'preact/hooks'
|
|
2
2
|
import { useBatcher } from './useBatcher'
|
|
3
|
-
import type {
|
|
4
|
-
import type { AnyFunction } from '@tanstack/pacer/types'
|
|
3
|
+
import type { PreactBatcherOptions } from './useBatcher'
|
|
5
4
|
|
|
6
5
|
/**
|
|
7
6
|
* A Preact hook that creates a batched version of a callback function.
|
|
@@ -38,13 +37,10 @@ import type { AnyFunction } from '@tanstack/pacer/types'
|
|
|
38
37
|
* </button>
|
|
39
38
|
* ```
|
|
40
39
|
*/
|
|
41
|
-
export function useBatchedCallback<
|
|
42
|
-
fn: (items: Array<
|
|
43
|
-
options:
|
|
44
|
-
): (
|
|
40
|
+
export function useBatchedCallback<TValue>(
|
|
41
|
+
fn: (items: Array<TValue>) => void,
|
|
42
|
+
options: PreactBatcherOptions<TValue, {}>,
|
|
43
|
+
): (item: TValue) => void {
|
|
45
44
|
const batchedFn = useBatcher(fn, options).addItem
|
|
46
|
-
return useCallback(
|
|
47
|
-
(...args: Parameters<TFn>) => batchedFn(args[0]),
|
|
48
|
-
[batchedFn],
|
|
49
|
-
)
|
|
45
|
+
return useCallback((item: TValue) => batchedFn(item), [batchedFn])
|
|
50
46
|
}
|
|
@@ -1,11 +1,22 @@
|
|
|
1
|
-
import { useMemo, useState } from 'preact/hooks'
|
|
1
|
+
import { useEffect, useMemo, useState } from 'preact/hooks'
|
|
2
2
|
import { Batcher } from '@tanstack/pacer/batcher'
|
|
3
|
-
import { useStore } from '@tanstack/preact-store'
|
|
3
|
+
import { shallow, useStore } from '@tanstack/preact-store'
|
|
4
4
|
import { useDefaultPacerOptions } from '../provider/PacerProvider'
|
|
5
5
|
import type { Store } from '@tanstack/preact-store'
|
|
6
6
|
import type { BatcherOptions, BatcherState } from '@tanstack/pacer/batcher'
|
|
7
7
|
import type { ComponentChildren } from 'preact'
|
|
8
8
|
|
|
9
|
+
export interface PreactBatcherOptions<
|
|
10
|
+
TValue,
|
|
11
|
+
TSelected = {},
|
|
12
|
+
> extends BatcherOptions<TValue> {
|
|
13
|
+
/**
|
|
14
|
+
* Optional callback invoked when the 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: PreactBatcher<TValue, TSelected>) => void
|
|
18
|
+
}
|
|
19
|
+
|
|
9
20
|
export interface PreactBatcher<TValue, TSelected = {}> extends Omit<
|
|
10
21
|
Batcher<TValue>,
|
|
11
22
|
'store'
|
|
@@ -84,6 +95,19 @@ export interface PreactBatcher<TValue, TSelected = {}> extends Omit<
|
|
|
84
95
|
* - `status`: Current processing status ('idle' | 'pending')
|
|
85
96
|
* - `totalItemsProcessed`: Total number of items processed across all batches
|
|
86
97
|
*
|
|
98
|
+
* ## Unmount behavior
|
|
99
|
+
*
|
|
100
|
+
* By default, the hook cancels any pending batch when the component unmounts.
|
|
101
|
+
* Use the `onUnmount` option to customize this. For example, to flush pending work instead:
|
|
102
|
+
*
|
|
103
|
+
* ```tsx
|
|
104
|
+
* const batcher = useBatcher(fn, {
|
|
105
|
+
* maxSize: 10,
|
|
106
|
+
* wait: 2000,
|
|
107
|
+
* onUnmount: (b) => b.flush()
|
|
108
|
+
* });
|
|
109
|
+
* ```
|
|
110
|
+
*
|
|
87
111
|
* @example
|
|
88
112
|
* ```tsx
|
|
89
113
|
* // Default behavior - no reactive state subscriptions
|
|
@@ -158,14 +182,14 @@ export interface PreactBatcher<TValue, TSelected = {}> extends Omit<
|
|
|
158
182
|
*/
|
|
159
183
|
export function useBatcher<TValue, TSelected = {}>(
|
|
160
184
|
fn: (items: Array<TValue>) => void,
|
|
161
|
-
options:
|
|
185
|
+
options: PreactBatcherOptions<TValue, TSelected> = {},
|
|
162
186
|
selector: (state: BatcherState<TValue>) => TSelected = () =>
|
|
163
187
|
({}) as TSelected,
|
|
164
188
|
): PreactBatcher<TValue, TSelected> {
|
|
165
189
|
const mergedOptions = {
|
|
166
190
|
...useDefaultPacerOptions().batcher,
|
|
167
191
|
...options,
|
|
168
|
-
} as
|
|
192
|
+
} as PreactBatcherOptions<TValue, TSelected>
|
|
169
193
|
|
|
170
194
|
const [batcher] = useState(() => {
|
|
171
195
|
const batcherInstance = new Batcher<TValue>(
|
|
@@ -177,7 +201,9 @@ export function useBatcher<TValue, TSelected = {}>(
|
|
|
177
201
|
selector: (state: BatcherState<TValue>) => TSelected
|
|
178
202
|
children: ((state: TSelected) => ComponentChildren) | ComponentChildren
|
|
179
203
|
}) {
|
|
180
|
-
const selected = useStore(batcherInstance.store, props.selector
|
|
204
|
+
const selected = useStore(batcherInstance.store, props.selector, {
|
|
205
|
+
equal: shallow,
|
|
206
|
+
})
|
|
181
207
|
|
|
182
208
|
return typeof props.children === 'function'
|
|
183
209
|
? props.children(selected)
|
|
@@ -190,7 +216,19 @@ export function useBatcher<TValue, TSelected = {}>(
|
|
|
190
216
|
batcher.fn = fn
|
|
191
217
|
batcher.setOptions(mergedOptions)
|
|
192
218
|
|
|
193
|
-
|
|
219
|
+
/* eslint-disable react-hooks/exhaustive-deps -- cleanup only; runs on unmount */
|
|
220
|
+
useEffect(() => {
|
|
221
|
+
return () => {
|
|
222
|
+
if (mergedOptions.onUnmount) {
|
|
223
|
+
mergedOptions.onUnmount(batcher)
|
|
224
|
+
} else {
|
|
225
|
+
batcher.cancel()
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
}, [])
|
|
229
|
+
/* eslint-enable react-hooks/exhaustive-deps */
|
|
230
|
+
|
|
231
|
+
const state = useStore(batcher.store, selector, { equal: shallow })
|
|
194
232
|
|
|
195
233
|
return useMemo(
|
|
196
234
|
() =>
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { useCallback } from 'preact/hooks'
|
|
2
2
|
import { useDebouncer } from './useDebouncer'
|
|
3
|
-
import type {
|
|
3
|
+
import type { PreactDebouncerOptions } from './useDebouncer'
|
|
4
4
|
import type { AnyFunction } from '@tanstack/pacer/types'
|
|
5
5
|
|
|
6
6
|
/**
|
|
@@ -41,7 +41,7 @@ import type { AnyFunction } from '@tanstack/pacer/types'
|
|
|
41
41
|
*/
|
|
42
42
|
export function useDebouncedCallback<TFn extends AnyFunction>(
|
|
43
43
|
fn: TFn,
|
|
44
|
-
options:
|
|
44
|
+
options: PreactDebouncerOptions<TFn, {}>,
|
|
45
45
|
): (...args: Parameters<TFn>) => void {
|
|
46
46
|
const debouncedFn = useDebouncer(fn, options).maybeExecute
|
|
47
47
|
return useCallback(
|
|
@@ -1,11 +1,8 @@
|
|
|
1
1
|
import { useState } from 'preact/hooks'
|
|
2
2
|
import { useDebouncer } from './useDebouncer'
|
|
3
|
+
import type { PreactDebouncer, PreactDebouncerOptions } from './useDebouncer'
|
|
3
4
|
import type { Dispatch, StateUpdater } from 'preact/hooks'
|
|
4
|
-
import type {
|
|
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 state value, combining Preact's useState with debouncing functionality.
|
|
@@ -84,7 +81,7 @@ export function useDebouncedState<
|
|
|
84
81
|
TSelected = DebouncerState<Dispatch<StateUpdater<TValue>>>,
|
|
85
82
|
>(
|
|
86
83
|
value: TValue,
|
|
87
|
-
options:
|
|
84
|
+
options: PreactDebouncerOptions<Dispatch<StateUpdater<TValue>>, TSelected>,
|
|
88
85
|
selector?: (
|
|
89
86
|
state: DebouncerState<Dispatch<StateUpdater<TValue>>>,
|
|
90
87
|
) => TSelected,
|
|
@@ -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 {
|
|
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:
|
|
93
|
+
options: PreactDebouncerOptions<Dispatch<StateUpdater<TValue>>, TSelected>,
|
|
97
94
|
selector?: (
|
|
98
95
|
state: DebouncerState<Dispatch<StateUpdater<TValue>>>,
|
|
99
96
|
) => TSelected,
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { useEffect, useMemo, useState } from 'preact/hooks'
|
|
2
2
|
import { Debouncer } from '@tanstack/pacer/debouncer'
|
|
3
|
-
import { useStore } from '@tanstack/preact-store'
|
|
3
|
+
import { shallow, useStore } from '@tanstack/preact-store'
|
|
4
4
|
import { useDefaultPacerOptions } from '../provider/PacerProvider'
|
|
5
5
|
import type { Store } from '@tanstack/preact-store'
|
|
6
6
|
import type {
|
|
@@ -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:
|
|
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
|
|
149
|
-
|
|
171
|
+
} as PreactDebouncerOptions<TFn, TSelected>
|
|
150
172
|
const [debouncer] = useState(() => {
|
|
151
173
|
const debouncerInstance = new Debouncer(
|
|
152
174
|
fn,
|
|
@@ -157,7 +179,9 @@ export function useDebouncer<TFn extends AnyFunction, TSelected = {}>(
|
|
|
157
179
|
selector: (state: DebouncerState<TFn>) => TSelected
|
|
158
180
|
children: ((state: TSelected) => ComponentChildren) | ComponentChildren
|
|
159
181
|
}) {
|
|
160
|
-
const selected = useStore(debouncerInstance.store, props.selector
|
|
182
|
+
const selected = useStore(debouncerInstance.store, props.selector, {
|
|
183
|
+
equal: shallow,
|
|
184
|
+
})
|
|
161
185
|
|
|
162
186
|
return typeof props.children === 'function'
|
|
163
187
|
? props.children(selected)
|
|
@@ -170,13 +194,19 @@ export function useDebouncer<TFn extends AnyFunction, TSelected = {}>(
|
|
|
170
194
|
debouncer.fn = fn
|
|
171
195
|
debouncer.setOptions(mergedOptions)
|
|
172
196
|
|
|
197
|
+
/* eslint-disable react-hooks/exhaustive-deps -- cleanup only; runs on unmount */
|
|
173
198
|
useEffect(() => {
|
|
174
199
|
return () => {
|
|
175
|
-
|
|
200
|
+
if (mergedOptions.onUnmount) {
|
|
201
|
+
mergedOptions.onUnmount(debouncer)
|
|
202
|
+
} else {
|
|
203
|
+
debouncer.cancel()
|
|
204
|
+
}
|
|
176
205
|
}
|
|
177
|
-
}, [
|
|
206
|
+
}, [])
|
|
207
|
+
/* eslint-enable react-hooks/exhaustive-deps */
|
|
178
208
|
|
|
179
|
-
const state = useStore(debouncer.store, selector)
|
|
209
|
+
const state = useStore(debouncer.store, selector, { equal: shallow })
|
|
180
210
|
|
|
181
211
|
return useMemo(
|
|
182
212
|
() =>
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { useQueuer } from './useQueuer'
|
|
2
|
-
import type { PreactQueuer } from './useQueuer'
|
|
3
|
-
import type { 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:
|
|
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 {
|
|
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:
|
|
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)
|
package/src/queuer/useQueuer.ts
CHANGED
|
@@ -1,11 +1,22 @@
|
|
|
1
|
-
import { useMemo, useState } from 'preact/hooks'
|
|
1
|
+
import { useEffect, useMemo, useState } from 'preact/hooks'
|
|
2
2
|
import { Queuer } from '@tanstack/pacer/queuer'
|
|
3
|
-
import { useStore } from '@tanstack/preact-store'
|
|
3
|
+
import { shallow, useStore } from '@tanstack/preact-store'
|
|
4
4
|
import { useDefaultPacerOptions } from '../provider/PacerProvider'
|
|
5
5
|
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:
|
|
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
|
|
202
|
+
} as PreactQueuerOptions<TValue, TSelected>
|
|
179
203
|
|
|
180
204
|
const [queuer] = useState(() => {
|
|
181
205
|
const queuerInstance = new Queuer<TValue>(
|
|
@@ -187,7 +211,9 @@ export function useQueuer<TValue, TSelected = {}>(
|
|
|
187
211
|
selector: (state: QueuerState<TValue>) => TSelected
|
|
188
212
|
children: ((state: TSelected) => ComponentChildren) | ComponentChildren
|
|
189
213
|
}) {
|
|
190
|
-
const selected = useStore(queuerInstance.store, props.selector
|
|
214
|
+
const selected = useStore(queuerInstance.store, props.selector, {
|
|
215
|
+
equal: shallow,
|
|
216
|
+
})
|
|
191
217
|
|
|
192
218
|
return typeof props.children === 'function'
|
|
193
219
|
? props.children(selected)
|
|
@@ -200,7 +226,19 @@ export function useQueuer<TValue, TSelected = {}>(
|
|
|
200
226
|
queuer.fn = fn
|
|
201
227
|
queuer.setOptions(mergedOptions)
|
|
202
228
|
|
|
203
|
-
|
|
229
|
+
/* eslint-disable react-hooks/exhaustive-deps -- cleanup only; runs on unmount */
|
|
230
|
+
useEffect(() => {
|
|
231
|
+
return () => {
|
|
232
|
+
if (mergedOptions.onUnmount) {
|
|
233
|
+
mergedOptions.onUnmount(queuer)
|
|
234
|
+
} else {
|
|
235
|
+
queuer.stop()
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
}, [])
|
|
239
|
+
/* eslint-enable react-hooks/exhaustive-deps */
|
|
240
|
+
|
|
241
|
+
const state = useStore(queuer.store, selector, { equal: shallow })
|
|
204
242
|
|
|
205
243
|
return useMemo(
|
|
206
244
|
() =>
|
|
@@ -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 {
|
|
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:
|
|
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
|
-
|
|
7
|
-
|
|
8
|
-
} from '
|
|
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:
|
|
110
|
+
options: PreactRateLimiterOptions<Dispatch<StateUpdater<TValue>>, TSelected>,
|
|
111
111
|
selector?: (state: RateLimiterState) => TSelected,
|
|
112
112
|
): [
|
|
113
113
|
TValue,
|