@tanstack/preact-pacer 0.22.1 → 0.24.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 +17 -2
- package/dist/async-batcher/useAsyncBatchedCallback.d.ts +2 -5
- package/dist/async-batcher/useAsyncBatchedCallback.js +1 -2
- package/dist/async-batcher/useAsyncBatcher.d.ts +4 -8
- package/dist/async-batcher/useAsyncBatcher.js +1 -3
- package/dist/async-debouncer/useAsyncDebouncedCallback.d.ts +6 -7
- package/dist/async-debouncer/useAsyncDebouncedCallback.js +5 -4
- package/dist/async-debouncer/useAsyncDebouncer.d.ts +4 -7
- package/dist/async-debouncer/useAsyncDebouncer.js +1 -2
- package/dist/async-queuer/useAsyncQueuedState.d.ts +4 -7
- package/dist/async-queuer/useAsyncQueuedState.js +3 -4
- package/dist/async-queuer/useAsyncQueuer.d.ts +6 -9
- package/dist/async-queuer/useAsyncQueuer.js +3 -4
- package/dist/async-rate-limiter/useAsyncRateLimitedCallback.d.ts +2 -5
- package/dist/async-rate-limiter/useAsyncRateLimitedCallback.js +1 -2
- package/dist/async-rate-limiter/useAsyncRateLimiter.d.ts +4 -7
- package/dist/async-rate-limiter/useAsyncRateLimiter.js +1 -2
- package/dist/async-throttler/useAsyncThrottledCallback.d.ts +7 -9
- package/dist/async-throttler/useAsyncThrottledCallback.js +6 -6
- package/dist/async-throttler/useAsyncThrottler.d.ts +4 -7
- package/dist/async-throttler/useAsyncThrottler.js +1 -2
- package/dist/batcher/useBatchedCallback.d.ts +2 -5
- package/dist/batcher/useBatchedCallback.js +1 -2
- package/dist/batcher/useBatcher.d.ts +6 -11
- package/dist/batcher/useBatcher.js +3 -6
- package/dist/debouncer/useDebouncedCallback.d.ts +2 -5
- package/dist/debouncer/useDebouncedCallback.js +1 -2
- package/dist/debouncer/useDebouncedState.d.ts +2 -5
- package/dist/debouncer/useDebouncedState.js +1 -2
- package/dist/debouncer/useDebouncedValue.d.ts +2 -5
- package/dist/debouncer/useDebouncedValue.js +1 -2
- package/dist/debouncer/useDebouncer.d.ts +4 -7
- package/dist/debouncer/useDebouncer.js +1 -2
- package/dist/provider/PacerProvider.d.ts +6 -13
- package/dist/provider/PacerProvider.js +1 -2
- package/dist/queuer/useQueuedState.d.ts +2 -5
- package/dist/queuer/useQueuedState.js +1 -2
- package/dist/queuer/useQueuedValue.d.ts +2 -5
- package/dist/queuer/useQueuedValue.js +1 -2
- package/dist/queuer/useQueuer.d.ts +4 -7
- package/dist/queuer/useQueuer.js +1 -2
- package/dist/rate-limiter/useRateLimitedCallback.d.ts +2 -5
- package/dist/rate-limiter/useRateLimitedCallback.js +1 -2
- package/dist/rate-limiter/useRateLimitedState.d.ts +2 -5
- package/dist/rate-limiter/useRateLimitedState.js +1 -2
- package/dist/rate-limiter/useRateLimitedValue.d.ts +2 -5
- package/dist/rate-limiter/useRateLimitedValue.js +1 -2
- package/dist/rate-limiter/useRateLimiter.d.ts +4 -7
- package/dist/rate-limiter/useRateLimiter.js +1 -2
- package/dist/throttler/useThrottledCallback.d.ts +2 -5
- package/dist/throttler/useThrottledCallback.js +1 -2
- package/dist/throttler/useThrottledState.d.ts +2 -5
- package/dist/throttler/useThrottledState.js +1 -2
- package/dist/throttler/useThrottledValue.d.ts +2 -5
- package/dist/throttler/useThrottledValue.js +1 -2
- package/dist/throttler/useThrottler.d.ts +4 -7
- package/dist/throttler/useThrottler.js +1 -2
- package/package.json +24 -72
- package/dist/async-batcher/index.cjs +0 -13
- package/dist/async-batcher/index.d.cts +0 -4
- package/dist/async-batcher/useAsyncBatchedCallback.cjs +0 -51
- package/dist/async-batcher/useAsyncBatchedCallback.cjs.map +0 -1
- package/dist/async-batcher/useAsyncBatchedCallback.d.cts +0 -44
- package/dist/async-batcher/useAsyncBatchedCallback.js.map +0 -1
- package/dist/async-batcher/useAsyncBatcher.cjs +0 -217
- package/dist/async-batcher/useAsyncBatcher.cjs.map +0 -1
- package/dist/async-batcher/useAsyncBatcher.d.cts +0 -224
- package/dist/async-batcher/useAsyncBatcher.js.map +0 -1
- package/dist/async-debouncer/index.cjs +0 -13
- package/dist/async-debouncer/index.d.cts +0 -4
- package/dist/async-debouncer/useAsyncDebouncedCallback.cjs +0 -50
- package/dist/async-debouncer/useAsyncDebouncedCallback.cjs.map +0 -1
- package/dist/async-debouncer/useAsyncDebouncedCallback.d.cts +0 -46
- package/dist/async-debouncer/useAsyncDebouncedCallback.js.map +0 -1
- package/dist/async-debouncer/useAsyncDebouncer.cjs +0 -195
- package/dist/async-debouncer/useAsyncDebouncer.cjs.map +0 -1
- package/dist/async-debouncer/useAsyncDebouncer.d.cts +0 -203
- package/dist/async-debouncer/useAsyncDebouncer.js.map +0 -1
- package/dist/async-queuer/index.cjs +0 -13
- package/dist/async-queuer/index.d.cts +0 -4
- package/dist/async-queuer/useAsyncQueuedState.cjs +0 -154
- package/dist/async-queuer/useAsyncQueuedState.cjs.map +0 -1
- package/dist/async-queuer/useAsyncQueuedState.d.cts +0 -151
- package/dist/async-queuer/useAsyncQueuedState.js.map +0 -1
- package/dist/async-queuer/useAsyncQueuer.cjs +0 -217
- package/dist/async-queuer/useAsyncQueuer.cjs.map +0 -1
- package/dist/async-queuer/useAsyncQueuer.d.cts +0 -224
- package/dist/async-queuer/useAsyncQueuer.js.map +0 -1
- package/dist/async-rate-limiter/index.cjs +0 -13
- package/dist/async-rate-limiter/index.d.cts +0 -4
- package/dist/async-rate-limiter/useAsyncRateLimitedCallback.cjs +0 -65
- package/dist/async-rate-limiter/useAsyncRateLimitedCallback.cjs.map +0 -1
- package/dist/async-rate-limiter/useAsyncRateLimitedCallback.d.cts +0 -61
- package/dist/async-rate-limiter/useAsyncRateLimitedCallback.js.map +0 -1
- package/dist/async-rate-limiter/useAsyncRateLimiter.cjs +0 -209
- package/dist/async-rate-limiter/useAsyncRateLimiter.cjs.map +0 -1
- package/dist/async-rate-limiter/useAsyncRateLimiter.d.cts +0 -220
- package/dist/async-rate-limiter/useAsyncRateLimiter.js.map +0 -1
- package/dist/async-retryer/index.cjs +0 -9
- package/dist/async-retryer/index.d.cts +0 -1
- package/dist/async-throttler/index.cjs +0 -13
- package/dist/async-throttler/index.d.cts +0 -4
- package/dist/async-throttler/useAsyncThrottledCallback.cjs +0 -48
- package/dist/async-throttler/useAsyncThrottledCallback.cjs.map +0 -1
- package/dist/async-throttler/useAsyncThrottledCallback.d.cts +0 -44
- package/dist/async-throttler/useAsyncThrottledCallback.js.map +0 -1
- package/dist/async-throttler/useAsyncThrottler.cjs +0 -206
- package/dist/async-throttler/useAsyncThrottler.cjs.map +0 -1
- package/dist/async-throttler/useAsyncThrottler.d.cts +0 -214
- package/dist/async-throttler/useAsyncThrottler.js.map +0 -1
- package/dist/batcher/index.cjs +0 -13
- package/dist/batcher/index.d.cts +0 -4
- package/dist/batcher/useBatchedCallback.cjs +0 -47
- package/dist/batcher/useBatchedCallback.cjs.map +0 -1
- package/dist/batcher/useBatchedCallback.d.cts +0 -42
- package/dist/batcher/useBatchedCallback.js.map +0 -1
- package/dist/batcher/useBatcher.cjs +0 -165
- package/dist/batcher/useBatcher.cjs.map +0 -1
- package/dist/batcher/useBatcher.d.cts +0 -175
- package/dist/batcher/useBatcher.js.map +0 -1
- package/dist/debouncer/index.cjs +0 -17
- package/dist/debouncer/index.d.cts +0 -6
- package/dist/debouncer/useDebouncedCallback.cjs +0 -48
- package/dist/debouncer/useDebouncedCallback.cjs.map +0 -1
- package/dist/debouncer/useDebouncedCallback.d.cts +0 -44
- package/dist/debouncer/useDebouncedCallback.js.map +0 -1
- package/dist/debouncer/useDebouncedState.cjs +0 -89
- package/dist/debouncer/useDebouncedState.cjs.map +0 -1
- package/dist/debouncer/useDebouncedState.d.cts +0 -81
- package/dist/debouncer/useDebouncedState.js.map +0 -1
- package/dist/debouncer/useDebouncedValue.cjs +0 -96
- package/dist/debouncer/useDebouncedValue.cjs.map +0 -1
- package/dist/debouncer/useDebouncedValue.d.cts +0 -90
- package/dist/debouncer/useDebouncedValue.js.map +0 -1
- package/dist/debouncer/useDebouncer.cjs +0 -141
- package/dist/debouncer/useDebouncer.cjs.map +0 -1
- package/dist/debouncer/useDebouncer.d.cts +0 -152
- package/dist/debouncer/useDebouncer.js.map +0 -1
- package/dist/index.cjs +0 -67
- package/dist/index.d.cts +0 -30
- package/dist/provider/PacerProvider.cjs +0 -26
- package/dist/provider/PacerProvider.cjs.map +0 -1
- package/dist/provider/PacerProvider.d.cts +0 -33
- package/dist/provider/PacerProvider.js.map +0 -1
- package/dist/provider/index.cjs +0 -6
- package/dist/provider/index.d.cts +0 -2
- package/dist/queuer/index.cjs +0 -15
- package/dist/queuer/index.d.cts +0 -5
- package/dist/queuer/useQueuedState.cjs +0 -129
- package/dist/queuer/useQueuedState.cjs.map +0 -1
- package/dist/queuer/useQueuedState.d.cts +0 -122
- package/dist/queuer/useQueuedState.js.map +0 -1
- package/dist/queuer/useQueuedValue.cjs +0 -115
- package/dist/queuer/useQueuedValue.cjs.map +0 -1
- package/dist/queuer/useQueuedValue.d.cts +0 -105
- package/dist/queuer/useQueuedValue.js.map +0 -1
- package/dist/queuer/useQueuer.cjs +0 -176
- package/dist/queuer/useQueuer.cjs.map +0 -1
- package/dist/queuer/useQueuer.d.cts +0 -186
- package/dist/queuer/useQueuer.js.map +0 -1
- package/dist/rate-limiter/index.cjs +0 -17
- package/dist/rate-limiter/index.d.cts +0 -6
- package/dist/rate-limiter/useRateLimitedCallback.cjs +0 -65
- package/dist/rate-limiter/useRateLimitedCallback.cjs.map +0 -1
- package/dist/rate-limiter/useRateLimitedCallback.d.cts +0 -61
- package/dist/rate-limiter/useRateLimitedCallback.js.map +0 -1
- package/dist/rate-limiter/useRateLimitedState.cjs +0 -115
- package/dist/rate-limiter/useRateLimitedState.cjs.map +0 -1
- package/dist/rate-limiter/useRateLimitedState.d.cts +0 -107
- package/dist/rate-limiter/useRateLimitedState.js.map +0 -1
- package/dist/rate-limiter/useRateLimitedValue.cjs +0 -102
- package/dist/rate-limiter/useRateLimitedValue.cjs.map +0 -1
- package/dist/rate-limiter/useRateLimitedValue.d.cts +0 -96
- package/dist/rate-limiter/useRateLimitedValue.js.map +0 -1
- package/dist/rate-limiter/useRateLimiter.cjs +0 -167
- package/dist/rate-limiter/useRateLimiter.cjs.map +0 -1
- package/dist/rate-limiter/useRateLimiter.d.cts +0 -179
- package/dist/rate-limiter/useRateLimiter.js.map +0 -1
- package/dist/throttler/index.cjs +0 -17
- package/dist/throttler/index.d.cts +0 -6
- package/dist/throttler/useThrottledCallback.cjs +0 -49
- package/dist/throttler/useThrottledCallback.cjs.map +0 -1
- package/dist/throttler/useThrottledCallback.d.cts +0 -45
- package/dist/throttler/useThrottledCallback.js.map +0 -1
- package/dist/throttler/useThrottledState.cjs +0 -100
- package/dist/throttler/useThrottledState.cjs.map +0 -1
- package/dist/throttler/useThrottledState.d.cts +0 -92
- package/dist/throttler/useThrottledState.js.map +0 -1
- package/dist/throttler/useThrottledValue.cjs +0 -91
- package/dist/throttler/useThrottledValue.cjs.map +0 -1
- package/dist/throttler/useThrottledValue.d.cts +0 -85
- package/dist/throttler/useThrottledValue.js.map +0 -1
- package/dist/throttler/useThrottler.cjs +0 -146
- package/dist/throttler/useThrottler.cjs.map +0 -1
- package/dist/throttler/useThrottler.d.cts +0 -157
- package/dist/throttler/useThrottler.js.map +0 -1
- package/dist/types/index.cjs +0 -9
- package/dist/types/index.d.cts +0 -1
- package/dist/utils/index.cjs +0 -9
- package/dist/utils/index.d.cts +0 -1
- package/src/async-batcher/index.ts +0 -4
- package/src/async-batcher/useAsyncBatchedCallback.ts +0 -53
- package/src/async-batcher/useAsyncBatcher.ts +0 -293
- package/src/async-debouncer/index.ts +0 -5
- package/src/async-debouncer/useAsyncDebouncedCallback.ts +0 -54
- package/src/async-debouncer/useAsyncDebouncer.ts +0 -274
- package/src/async-queuer/index.ts +0 -4
- package/src/async-queuer/useAsyncQueuedState.ts +0 -165
- package/src/async-queuer/useAsyncQueuer.ts +0 -293
- package/src/async-rate-limiter/index.ts +0 -5
- package/src/async-rate-limiter/useAsyncRateLimitedCallback.ts +0 -69
- package/src/async-rate-limiter/useAsyncRateLimiter.ts +0 -294
- package/src/async-retryer/index.ts +0 -2
- package/src/async-throttler/index.ts +0 -5
- package/src/async-throttler/useAsyncThrottledCallback.ts +0 -52
- package/src/async-throttler/useAsyncThrottler.ts +0 -285
- package/src/batcher/index.ts +0 -4
- package/src/batcher/useBatchedCallback.ts +0 -46
- package/src/batcher/useBatcher.ts +0 -241
- package/src/debouncer/index.ts +0 -7
- package/src/debouncer/useDebouncedCallback.ts +0 -51
- package/src/debouncer/useDebouncedState.ts +0 -106
- package/src/debouncer/useDebouncedValue.ts +0 -109
- package/src/debouncer/useDebouncer.ts +0 -219
- package/src/index.ts +0 -56
- package/src/provider/PacerProvider.tsx +0 -70
- package/src/provider/index.ts +0 -1
- package/src/queuer/index.ts +0 -5
- package/src/queuer/useQueuedState.ts +0 -133
- package/src/queuer/useQueuedValue.ts +0 -129
- package/src/queuer/useQueuer.ts +0 -251
- package/src/rate-limiter/index.ts +0 -6
- package/src/rate-limiter/useRateLimitedCallback.ts +0 -68
- package/src/rate-limiter/useRateLimitedState.ts +0 -124
- package/src/rate-limiter/useRateLimitedValue.ts +0 -110
- package/src/rate-limiter/useRateLimiter.ts +0 -244
- package/src/throttler/index.ts +0 -7
- package/src/throttler/useThrottledCallback.ts +0 -52
- package/src/throttler/useThrottledState.ts +0 -118
- package/src/throttler/useThrottledValue.ts +0 -104
- package/src/throttler/useThrottler.ts +0 -224
- package/src/types/index.ts +0 -1
- package/src/utils/index.ts +0 -1
|
@@ -1,175 +0,0 @@
|
|
|
1
|
-
import { Store } from "@tanstack/preact-store";
|
|
2
|
-
import { ComponentChildren } from "preact";
|
|
3
|
-
import { Batcher, BatcherOptions, BatcherState } from "@tanstack/pacer/batcher";
|
|
4
|
-
|
|
5
|
-
//#region src/batcher/useBatcher.d.ts
|
|
6
|
-
interface PreactBatcherOptions<TValue, TSelected = {}> extends BatcherOptions<TValue> {
|
|
7
|
-
/**
|
|
8
|
-
* Optional callback invoked when the component unmounts. Receives the batcher instance.
|
|
9
|
-
* When provided, replaces the default cleanup (cancel); use it to call flush(), reset(), cancel(), add logging, etc.
|
|
10
|
-
*/
|
|
11
|
-
onUnmount?: (batcher: PreactBatcher<TValue, TSelected>) => void;
|
|
12
|
-
}
|
|
13
|
-
interface PreactBatcher<TValue, TSelected = {}> extends Omit<Batcher<TValue>, 'store'> {
|
|
14
|
-
/**
|
|
15
|
-
* A Preact HOC (Higher Order Component) that allows you to subscribe to the batcher state.
|
|
16
|
-
*
|
|
17
|
-
* This is useful for opting into state re-renders for specific parts of the batcher state
|
|
18
|
-
* deep in your component tree without needing to pass a selector to the hook.
|
|
19
|
-
*
|
|
20
|
-
* @example
|
|
21
|
-
* <batcher.Subscribe selector={(state) => ({ size: state.size })}>
|
|
22
|
-
* {({ size }) => (
|
|
23
|
-
* <div>Batch Size: {size}</div>
|
|
24
|
-
* )}
|
|
25
|
-
* </batcher.Subscribe>
|
|
26
|
-
*/
|
|
27
|
-
Subscribe: <TSelected>(props: {
|
|
28
|
-
selector: (state: BatcherState<TValue>) => TSelected;
|
|
29
|
-
children: ((state: TSelected) => ComponentChildren) | ComponentChildren;
|
|
30
|
-
}) => ComponentChildren;
|
|
31
|
-
/**
|
|
32
|
-
* Reactive state that will be updated and re-rendered when the batcher state changes
|
|
33
|
-
*
|
|
34
|
-
* Use this instead of `batcher.store.state`
|
|
35
|
-
*/
|
|
36
|
-
readonly state: Readonly<TSelected>;
|
|
37
|
-
/**
|
|
38
|
-
* @deprecated Use `batcher.state` instead of `batcher.store.state` if you want to read reactive state.
|
|
39
|
-
* The state on the store object is not reactive, as it has not been wrapped in a `useSelector` hook internally.
|
|
40
|
-
* Although, you can make the state reactive by using the `useSelector` in your own usage.
|
|
41
|
-
*/
|
|
42
|
-
readonly store: Store<Readonly<BatcherState<TValue>>>;
|
|
43
|
-
}
|
|
44
|
-
/**
|
|
45
|
-
* A Preact hook that creates and manages a Batcher instance.
|
|
46
|
-
*
|
|
47
|
-
* This is a lower-level hook that provides direct access to the Batcher's functionality without
|
|
48
|
-
* any built-in state management. This allows you to integrate it with any state management solution
|
|
49
|
-
* you prefer (useState, Redux, Zustand, etc.) by utilizing the onItemsChange callback.
|
|
50
|
-
*
|
|
51
|
-
* The Batcher collects items and processes them in batches based on configurable conditions:
|
|
52
|
-
* - Maximum batch size
|
|
53
|
-
* - Time-based batching (process after X milliseconds)
|
|
54
|
-
* - Custom batch processing logic via getShouldExecute
|
|
55
|
-
*
|
|
56
|
-
* ## State Management and Selector
|
|
57
|
-
*
|
|
58
|
-
* The hook uses TanStack Store for reactive state management. You can subscribe to state changes
|
|
59
|
-
* in two ways:
|
|
60
|
-
*
|
|
61
|
-
* **1. Using `batcher.Subscribe` HOC (Recommended for component tree subscriptions)**
|
|
62
|
-
*
|
|
63
|
-
* Use the `Subscribe` HOC to subscribe to state changes deep in your component tree without
|
|
64
|
-
* needing to pass a selector to the hook. This is ideal when you want to subscribe to state
|
|
65
|
-
* in child components.
|
|
66
|
-
*
|
|
67
|
-
* **2. Using the `selector` parameter (For hook-level subscriptions)**
|
|
68
|
-
*
|
|
69
|
-
* The `selector` parameter allows you to specify which state changes will trigger a re-render
|
|
70
|
-
* at the hook level, optimizing performance by preventing unnecessary re-renders when irrelevant
|
|
71
|
-
* state changes occur.
|
|
72
|
-
*
|
|
73
|
-
* **By default, there will be no reactive state subscriptions** and you must opt-in to state
|
|
74
|
-
* tracking by providing a selector function or using the `Subscribe` HOC. This prevents unnecessary
|
|
75
|
-
* re-renders and gives you full control over when your component updates.
|
|
76
|
-
*
|
|
77
|
-
* Available state properties:
|
|
78
|
-
* - `executionCount`: Number of batch executions that have been completed
|
|
79
|
-
* - `isEmpty`: Whether the batcher has no items to process
|
|
80
|
-
* - `isPending`: Whether the batcher is waiting for the timeout to trigger batch processing
|
|
81
|
-
* - `isRunning`: Whether the batcher is active and will process items automatically
|
|
82
|
-
* - `items`: Array of items currently queued for batch processing
|
|
83
|
-
* - `size`: Number of items currently in the batch queue
|
|
84
|
-
* - `status`: Current processing status ('idle' | 'pending')
|
|
85
|
-
* - `totalItemsProcessed`: Total number of items processed across all batches
|
|
86
|
-
*
|
|
87
|
-
* ## Unmount behavior
|
|
88
|
-
*
|
|
89
|
-
* By default, the hook cancels any pending batch when the component unmounts.
|
|
90
|
-
* Use the `onUnmount` option to customize this. For example, to flush pending work instead:
|
|
91
|
-
*
|
|
92
|
-
* ```tsx
|
|
93
|
-
* const batcher = useBatcher(fn, {
|
|
94
|
-
* maxSize: 10,
|
|
95
|
-
* wait: 2000,
|
|
96
|
-
* onUnmount: (b) => b.flush()
|
|
97
|
-
* });
|
|
98
|
-
* ```
|
|
99
|
-
*
|
|
100
|
-
* @example
|
|
101
|
-
* ```tsx
|
|
102
|
-
* // Default behavior - no reactive state subscriptions
|
|
103
|
-
* const batcher = useBatcher<number>(
|
|
104
|
-
* (items) => console.log('Processing batch:', items),
|
|
105
|
-
* { maxSize: 5, wait: 2000 }
|
|
106
|
-
* );
|
|
107
|
-
*
|
|
108
|
-
* // Subscribe to state changes deep in component tree using Subscribe HOC
|
|
109
|
-
* <batcher.Subscribe selector={(state) => ({ size: state.size })}>
|
|
110
|
-
* {({ size }) => (
|
|
111
|
-
* <div>Batch Size: {size}</div>
|
|
112
|
-
* )}
|
|
113
|
-
* </batcher.Subscribe>
|
|
114
|
-
*
|
|
115
|
-
* // Opt-in to re-render when batch size changes at hook level (optimized for displaying queue size)
|
|
116
|
-
* const batcher = useBatcher<number>(
|
|
117
|
-
* (items) => console.log('Processing batch:', items),
|
|
118
|
-
* { maxSize: 5, wait: 2000 },
|
|
119
|
-
* (state) => ({
|
|
120
|
-
* size: state.size,
|
|
121
|
-
* isEmpty: state.isEmpty
|
|
122
|
-
* })
|
|
123
|
-
* );
|
|
124
|
-
*
|
|
125
|
-
* // Opt-in to re-render when execution metrics change (optimized for stats display)
|
|
126
|
-
* const batcher = useBatcher<number>(
|
|
127
|
-
* (items) => console.log('Processing batch:', items),
|
|
128
|
-
* { maxSize: 5, wait: 2000 },
|
|
129
|
-
* (state) => ({
|
|
130
|
-
* executionCount: state.executionCount,
|
|
131
|
-
* totalItemsProcessed: state.totalItemsProcessed
|
|
132
|
-
* })
|
|
133
|
-
* );
|
|
134
|
-
*
|
|
135
|
-
* // Opt-in to re-render when processing state changes (optimized for loading indicators)
|
|
136
|
-
* const batcher = useBatcher<number>(
|
|
137
|
-
* (items) => console.log('Processing batch:', items),
|
|
138
|
-
* { maxSize: 5, wait: 2000 },
|
|
139
|
-
* (state) => ({
|
|
140
|
-
* isPending: state.isPending,
|
|
141
|
-
* isRunning: state.isRunning,
|
|
142
|
-
* status: state.status
|
|
143
|
-
* })
|
|
144
|
-
* );
|
|
145
|
-
*
|
|
146
|
-
* // Example with custom state management and batching
|
|
147
|
-
* const [items, setItems] = useState([]);
|
|
148
|
-
*
|
|
149
|
-
* const batcher = useBatcher<number>(
|
|
150
|
-
* (items) => console.log('Processing batch:', items),
|
|
151
|
-
* {
|
|
152
|
-
* maxSize: 5,
|
|
153
|
-
* wait: 2000,
|
|
154
|
-
* onItemsChange: (batcher) => setItems(batcher.peekAllItems()),
|
|
155
|
-
* getShouldExecute: (items) => items.length >= 3
|
|
156
|
-
* }
|
|
157
|
-
* );
|
|
158
|
-
*
|
|
159
|
-
* // Add items to batch - they'll be processed when conditions are met
|
|
160
|
-
* batcher.addItem(1);
|
|
161
|
-
* batcher.addItem(2);
|
|
162
|
-
* batcher.addItem(3); // Triggers batch processing
|
|
163
|
-
*
|
|
164
|
-
* // Control the batcher
|
|
165
|
-
* batcher.stop(); // Pause batching
|
|
166
|
-
* batcher.start(); // Resume batching
|
|
167
|
-
*
|
|
168
|
-
* // Access the selected state (will be empty object {} unless selector provided)
|
|
169
|
-
* const { size, isPending } = batcher.state;
|
|
170
|
-
* ```
|
|
171
|
-
*/
|
|
172
|
-
declare function useBatcher<TValue, TSelected = {}>(fn: (items: Array<TValue>) => void, options?: PreactBatcherOptions<TValue, TSelected>, selector?: (state: BatcherState<TValue>) => TSelected): PreactBatcher<TValue, TSelected>;
|
|
173
|
-
//#endregion
|
|
174
|
-
export { PreactBatcher, PreactBatcherOptions, useBatcher };
|
|
175
|
-
//# sourceMappingURL=useBatcher.d.cts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useBatcher.js","names":[],"sources":["../../src/batcher/useBatcher.ts"],"sourcesContent":["import { useEffect, useMemo, useState } from 'preact/hooks'\nimport { Batcher } from '@tanstack/pacer/batcher'\nimport { shallow, useSelector } from '@tanstack/preact-store'\nimport { useDefaultPacerOptions } from '../provider/PacerProvider'\nimport type { Store } from '@tanstack/preact-store'\nimport type { BatcherOptions, BatcherState } from '@tanstack/pacer/batcher'\nimport type { ComponentChildren } from 'preact'\n\nexport interface PreactBatcherOptions<\n TValue,\n TSelected = {},\n> extends BatcherOptions<TValue> {\n /**\n * Optional callback invoked when the component unmounts. Receives the batcher instance.\n * When provided, replaces the default cleanup (cancel); use it to call flush(), reset(), cancel(), add logging, etc.\n */\n onUnmount?: (batcher: PreactBatcher<TValue, TSelected>) => void\n}\n\nexport interface PreactBatcher<TValue, TSelected = {}> extends Omit<\n Batcher<TValue>,\n 'store'\n> {\n /**\n * A Preact HOC (Higher Order Component) that allows you to subscribe to the batcher state.\n *\n * This is useful for opting into state re-renders for specific parts of the batcher state\n * deep in your component tree without needing to pass a selector to the hook.\n *\n * @example\n * <batcher.Subscribe selector={(state) => ({ size: state.size })}>\n * {({ size }) => (\n * <div>Batch Size: {size}</div>\n * )}\n * </batcher.Subscribe>\n */\n Subscribe: <TSelected>(props: {\n selector: (state: BatcherState<TValue>) => TSelected\n children: ((state: TSelected) => ComponentChildren) | ComponentChildren\n }) => ComponentChildren\n /**\n * Reactive state that will be updated and re-rendered when the batcher state changes\n *\n * Use this instead of `batcher.store.state`\n */\n readonly state: Readonly<TSelected>\n /**\n * @deprecated Use `batcher.state` instead of `batcher.store.state` if you want to read reactive state.\n * The state on the store object is not reactive, as it has not been wrapped in a `useSelector` hook internally.\n * Although, you can make the state reactive by using the `useSelector` in your own usage.\n */\n readonly store: Store<Readonly<BatcherState<TValue>>>\n}\n\n/**\n * A Preact hook that creates and manages a Batcher instance.\n *\n * This is a lower-level hook that provides direct access to the Batcher's functionality without\n * any built-in state management. This allows you to integrate it with any state management solution\n * you prefer (useState, Redux, Zustand, etc.) by utilizing the onItemsChange callback.\n *\n * The Batcher collects items and processes them in batches based on configurable conditions:\n * - Maximum batch size\n * - Time-based batching (process after X milliseconds)\n * - Custom batch processing logic via getShouldExecute\n *\n * ## State Management and Selector\n *\n * The hook uses TanStack Store for reactive state management. You can subscribe to state changes\n * in two ways:\n *\n * **1. Using `batcher.Subscribe` HOC (Recommended for component tree subscriptions)**\n *\n * Use the `Subscribe` HOC to subscribe to state changes deep in your component tree without\n * needing to pass a selector to the hook. This is ideal when you want to subscribe to state\n * in child components.\n *\n * **2. Using the `selector` parameter (For hook-level subscriptions)**\n *\n * The `selector` parameter allows you to specify which state changes will trigger a re-render\n * at the hook level, optimizing performance by preventing unnecessary re-renders when irrelevant\n * state changes occur.\n *\n * **By default, there will be no reactive state subscriptions** and you must opt-in to state\n * tracking by providing a selector function or using the `Subscribe` HOC. This prevents unnecessary\n * re-renders and gives you full control over when your component updates.\n *\n * Available state properties:\n * - `executionCount`: Number of batch executions that have been completed\n * - `isEmpty`: Whether the batcher has no items to process\n * - `isPending`: Whether the batcher is waiting for the timeout to trigger batch processing\n * - `isRunning`: Whether the batcher is active and will process items automatically\n * - `items`: Array of items currently queued for batch processing\n * - `size`: Number of items currently in the batch queue\n * - `status`: Current processing status ('idle' | 'pending')\n * - `totalItemsProcessed`: Total number of items processed across all batches\n *\n * ## Unmount behavior\n *\n * By default, the hook cancels any pending batch when the component unmounts.\n * Use the `onUnmount` option to customize this. For example, to flush pending work instead:\n *\n * ```tsx\n * const batcher = useBatcher(fn, {\n * maxSize: 10,\n * wait: 2000,\n * onUnmount: (b) => b.flush()\n * });\n * ```\n *\n * @example\n * ```tsx\n * // Default behavior - no reactive state subscriptions\n * const batcher = useBatcher<number>(\n * (items) => console.log('Processing batch:', items),\n * { maxSize: 5, wait: 2000 }\n * );\n *\n * // Subscribe to state changes deep in component tree using Subscribe HOC\n * <batcher.Subscribe selector={(state) => ({ size: state.size })}>\n * {({ size }) => (\n * <div>Batch Size: {size}</div>\n * )}\n * </batcher.Subscribe>\n *\n * // Opt-in to re-render when batch size changes at hook level (optimized for displaying queue size)\n * const batcher = useBatcher<number>(\n * (items) => console.log('Processing batch:', items),\n * { maxSize: 5, wait: 2000 },\n * (state) => ({\n * size: state.size,\n * isEmpty: state.isEmpty\n * })\n * );\n *\n * // Opt-in to re-render when execution metrics change (optimized for stats display)\n * const batcher = useBatcher<number>(\n * (items) => console.log('Processing batch:', items),\n * { maxSize: 5, wait: 2000 },\n * (state) => ({\n * executionCount: state.executionCount,\n * totalItemsProcessed: state.totalItemsProcessed\n * })\n * );\n *\n * // Opt-in to re-render when processing state changes (optimized for loading indicators)\n * const batcher = useBatcher<number>(\n * (items) => console.log('Processing batch:', items),\n * { maxSize: 5, wait: 2000 },\n * (state) => ({\n * isPending: state.isPending,\n * isRunning: state.isRunning,\n * status: state.status\n * })\n * );\n *\n * // Example with custom state management and batching\n * const [items, setItems] = useState([]);\n *\n * const batcher = useBatcher<number>(\n * (items) => console.log('Processing batch:', items),\n * {\n * maxSize: 5,\n * wait: 2000,\n * onItemsChange: (batcher) => setItems(batcher.peekAllItems()),\n * getShouldExecute: (items) => items.length >= 3\n * }\n * );\n *\n * // Add items to batch - they'll be processed when conditions are met\n * batcher.addItem(1);\n * batcher.addItem(2);\n * batcher.addItem(3); // Triggers batch processing\n *\n * // Control the batcher\n * batcher.stop(); // Pause batching\n * batcher.start(); // Resume batching\n *\n * // Access the selected state (will be empty object {} unless selector provided)\n * const { size, isPending } = batcher.state;\n * ```\n */\nexport function useBatcher<TValue, TSelected = {}>(\n fn: (items: Array<TValue>) => void,\n options: PreactBatcherOptions<TValue, TSelected> = {},\n selector: (state: BatcherState<TValue>) => TSelected = () =>\n ({}) as TSelected,\n): PreactBatcher<TValue, TSelected> {\n const mergedOptions = {\n ...useDefaultPacerOptions().batcher,\n ...options,\n } as PreactBatcherOptions<TValue, TSelected>\n\n const [batcher] = useState(() => {\n const batcherInstance = new Batcher<TValue>(\n fn,\n mergedOptions,\n ) as unknown as PreactBatcher<TValue, TSelected>\n\n batcherInstance.Subscribe = function Subscribe<TSelected>(props: {\n selector: (state: BatcherState<TValue>) => TSelected\n children: ((state: TSelected) => ComponentChildren) | ComponentChildren\n }) {\n const selected = useSelector(batcherInstance.store, props.selector, {\n compare: shallow,\n })\n\n return typeof props.children === 'function'\n ? props.children(selected)\n : props.children\n }\n\n return batcherInstance\n })\n\n batcher.fn = fn\n batcher.setOptions(mergedOptions)\n\n /* eslint-disable react-hooks/exhaustive-deps -- cleanup only; runs on unmount */\n useEffect(() => {\n return () => {\n if (mergedOptions.onUnmount) {\n mergedOptions.onUnmount(batcher)\n } else {\n batcher.cancel()\n }\n }\n }, [])\n /* eslint-enable react-hooks/exhaustive-deps */\n\n const state = useSelector(batcher.store, selector, { compare: shallow })\n\n return useMemo(\n () =>\n ({\n ...batcher,\n state,\n }) as PreactBatcher<TValue, TSelected>, // omit `store` in favor of `state`\n [batcher, state],\n )\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAsLA,SAAgB,WACd,IACA,UAAmD,EAAE,EACrD,kBACG,EAAE,GAC6B;CAClC,MAAM,gBAAgB;EACpB,GAAG,wBAAwB,CAAC;EAC5B,GAAG;EACJ;CAED,MAAM,CAAC,WAAW,eAAe;EAC/B,MAAM,kBAAkB,IAAI,QAC1B,IACA,cACD;EAED,gBAAgB,YAAY,SAAS,UAAqB,OAGvD;GACD,MAAM,WAAW,YAAY,gBAAgB,OAAO,MAAM,UAAU,EAClE,SAAS,SACV,CAAC;GAEF,OAAO,OAAO,MAAM,aAAa,aAC7B,MAAM,SAAS,SAAS,GACxB,MAAM;;EAGZ,OAAO;GACP;CAEF,QAAQ,KAAK;CACb,QAAQ,WAAW,cAAc;CAGjC,gBAAgB;EACd,aAAa;GACX,IAAI,cAAc,WAChB,cAAc,UAAU,QAAQ;QAEhC,QAAQ,QAAQ;;IAGnB,EAAE,CAAC;CAGN,MAAM,QAAQ,YAAY,QAAQ,OAAO,UAAU,EAAE,SAAS,SAAS,CAAC;CAExE,OAAO,eAEF;EACC,GAAG;EACH;EACD,GACH,CAAC,SAAS,MAAM,CACjB"}
|
package/dist/debouncer/index.cjs
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
-
const require_useDebouncer = require('./useDebouncer.cjs');
|
|
3
|
-
const require_useDebouncedCallback = require('./useDebouncedCallback.cjs');
|
|
4
|
-
const require_useDebouncedState = require('./useDebouncedState.cjs');
|
|
5
|
-
const require_useDebouncedValue = require('./useDebouncedValue.cjs');
|
|
6
|
-
|
|
7
|
-
exports.useDebouncedCallback = require_useDebouncedCallback.useDebouncedCallback;
|
|
8
|
-
exports.useDebouncedState = require_useDebouncedState.useDebouncedState;
|
|
9
|
-
exports.useDebouncedValue = require_useDebouncedValue.useDebouncedValue;
|
|
10
|
-
exports.useDebouncer = require_useDebouncer.useDebouncer;
|
|
11
|
-
var _tanstack_pacer_debouncer = require("@tanstack/pacer/debouncer");
|
|
12
|
-
Object.keys(_tanstack_pacer_debouncer).forEach(function (k) {
|
|
13
|
-
if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
|
|
14
|
-
enumerable: true,
|
|
15
|
-
get: function () { return _tanstack_pacer_debouncer[k]; }
|
|
16
|
-
});
|
|
17
|
-
});
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import { PreactDebouncer, PreactDebouncerOptions, useDebouncer } from "./useDebouncer.cjs";
|
|
2
|
-
import { useDebouncedCallback } from "./useDebouncedCallback.cjs";
|
|
3
|
-
import { useDebouncedState } from "./useDebouncedState.cjs";
|
|
4
|
-
import { useDebouncedValue } from "./useDebouncedValue.cjs";
|
|
5
|
-
export * from "@tanstack/pacer/debouncer";
|
|
6
|
-
export { PreactDebouncer, PreactDebouncerOptions, useDebouncedCallback, useDebouncedState, useDebouncedValue, useDebouncer };
|
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
const require_useDebouncer = require('./useDebouncer.cjs');
|
|
2
|
-
let preact_hooks = require("preact/hooks");
|
|
3
|
-
|
|
4
|
-
//#region src/debouncer/useDebouncedCallback.ts
|
|
5
|
-
/**
|
|
6
|
-
* A Preact hook that creates a debounced version of a callback function.
|
|
7
|
-
* This hook is essentially a wrapper around the basic `debounce` function
|
|
8
|
-
* that is exported from `@tanstack/pacer`,
|
|
9
|
-
* but optimized for Preact with reactive options and a stable function reference.
|
|
10
|
-
*
|
|
11
|
-
* The debounced function will only execute after the specified wait time has elapsed
|
|
12
|
-
* since its last invocation. If called again before the wait time expires, the timer
|
|
13
|
-
* resets and starts waiting again.
|
|
14
|
-
*
|
|
15
|
-
* This hook provides a simpler API compared to `useDebouncer`, making it ideal for basic
|
|
16
|
-
* debouncing needs. However, it does not expose the underlying Debouncer instance.
|
|
17
|
-
*
|
|
18
|
-
* For advanced usage requiring features like:
|
|
19
|
-
* - Manual cancellation
|
|
20
|
-
* - Access to execution counts
|
|
21
|
-
* - Custom useCallback dependencies
|
|
22
|
-
*
|
|
23
|
-
* Consider using the `useDebouncer` hook instead.
|
|
24
|
-
*
|
|
25
|
-
* @example
|
|
26
|
-
* ```tsx
|
|
27
|
-
* // Debounce a search handler
|
|
28
|
-
* const handleSearch = useDebouncedCallback((query: string) => {
|
|
29
|
-
* fetchSearchResults(query);
|
|
30
|
-
* }, {
|
|
31
|
-
* wait: 500 // Wait 500ms between executions
|
|
32
|
-
* });
|
|
33
|
-
*
|
|
34
|
-
* // Use in an input
|
|
35
|
-
* <input
|
|
36
|
-
* type="search"
|
|
37
|
-
* onChange={(e) => handleSearch(e.target.value)}
|
|
38
|
-
* />
|
|
39
|
-
* ```
|
|
40
|
-
*/
|
|
41
|
-
function useDebouncedCallback(fn, options) {
|
|
42
|
-
const debouncedFn = require_useDebouncer.useDebouncer(fn, options).maybeExecute;
|
|
43
|
-
return (0, preact_hooks.useCallback)((...args) => debouncedFn(...args), [debouncedFn]);
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
//#endregion
|
|
47
|
-
exports.useDebouncedCallback = useDebouncedCallback;
|
|
48
|
-
//# sourceMappingURL=useDebouncedCallback.cjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useDebouncedCallback.cjs","names":["useDebouncer"],"sources":["../../src/debouncer/useDebouncedCallback.ts"],"sourcesContent":["import { useCallback } from 'preact/hooks'\nimport { useDebouncer } from './useDebouncer'\nimport type { PreactDebouncerOptions } from './useDebouncer'\nimport type { AnyFunction } from '@tanstack/pacer/types'\n\n/**\n * A Preact hook that creates a debounced version of a callback function.\n * This hook is essentially a wrapper around the basic `debounce` function\n * that is exported from `@tanstack/pacer`,\n * but optimized for Preact with reactive options and a stable function reference.\n *\n * The debounced function will only execute after the specified wait time has elapsed\n * since its last invocation. If called again before the wait time expires, the timer\n * resets and starts waiting again.\n *\n * This hook provides a simpler API compared to `useDebouncer`, making it ideal for basic\n * debouncing needs. However, it does not expose the underlying Debouncer instance.\n *\n * For advanced usage requiring features like:\n * - Manual cancellation\n * - Access to execution counts\n * - Custom useCallback dependencies\n *\n * Consider using the `useDebouncer` hook instead.\n *\n * @example\n * ```tsx\n * // Debounce a search handler\n * const handleSearch = useDebouncedCallback((query: string) => {\n * fetchSearchResults(query);\n * }, {\n * wait: 500 // Wait 500ms between executions\n * });\n *\n * // Use in an input\n * <input\n * type=\"search\"\n * onChange={(e) => handleSearch(e.target.value)}\n * />\n * ```\n */\nexport function useDebouncedCallback<TFn extends AnyFunction>(\n fn: TFn,\n options: PreactDebouncerOptions<TFn, {}>,\n): (...args: Parameters<TFn>) => void {\n const debouncedFn = useDebouncer(fn, options).maybeExecute\n return useCallback(\n (...args: Parameters<TFn>) => debouncedFn(...args),\n [debouncedFn],\n )\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAyCA,SAAgB,qBACd,IACA,SACoC;CACpC,MAAM,cAAcA,kCAAa,IAAI,QAAQ,CAAC;CAC9C,sCACG,GAAG,SAA0B,YAAY,GAAG,KAAK,EAClD,CAAC,YAAY,CACd"}
|
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
import { PreactDebouncerOptions } from "./useDebouncer.cjs";
|
|
2
|
-
import { AnyFunction } from "@tanstack/pacer/types";
|
|
3
|
-
|
|
4
|
-
//#region src/debouncer/useDebouncedCallback.d.ts
|
|
5
|
-
/**
|
|
6
|
-
* A Preact hook that creates a debounced version of a callback function.
|
|
7
|
-
* This hook is essentially a wrapper around the basic `debounce` function
|
|
8
|
-
* that is exported from `@tanstack/pacer`,
|
|
9
|
-
* but optimized for Preact with reactive options and a stable function reference.
|
|
10
|
-
*
|
|
11
|
-
* The debounced function will only execute after the specified wait time has elapsed
|
|
12
|
-
* since its last invocation. If called again before the wait time expires, the timer
|
|
13
|
-
* resets and starts waiting again.
|
|
14
|
-
*
|
|
15
|
-
* This hook provides a simpler API compared to `useDebouncer`, making it ideal for basic
|
|
16
|
-
* debouncing needs. However, it does not expose the underlying Debouncer instance.
|
|
17
|
-
*
|
|
18
|
-
* For advanced usage requiring features like:
|
|
19
|
-
* - Manual cancellation
|
|
20
|
-
* - Access to execution counts
|
|
21
|
-
* - Custom useCallback dependencies
|
|
22
|
-
*
|
|
23
|
-
* Consider using the `useDebouncer` hook instead.
|
|
24
|
-
*
|
|
25
|
-
* @example
|
|
26
|
-
* ```tsx
|
|
27
|
-
* // Debounce a search handler
|
|
28
|
-
* const handleSearch = useDebouncedCallback((query: string) => {
|
|
29
|
-
* fetchSearchResults(query);
|
|
30
|
-
* }, {
|
|
31
|
-
* wait: 500 // Wait 500ms between executions
|
|
32
|
-
* });
|
|
33
|
-
*
|
|
34
|
-
* // Use in an input
|
|
35
|
-
* <input
|
|
36
|
-
* type="search"
|
|
37
|
-
* onChange={(e) => handleSearch(e.target.value)}
|
|
38
|
-
* />
|
|
39
|
-
* ```
|
|
40
|
-
*/
|
|
41
|
-
declare function useDebouncedCallback<TFn extends AnyFunction>(fn: TFn, options: PreactDebouncerOptions<TFn, {}>): (...args: Parameters<TFn>) => void;
|
|
42
|
-
//#endregion
|
|
43
|
-
export { useDebouncedCallback };
|
|
44
|
-
//# sourceMappingURL=useDebouncedCallback.d.cts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useDebouncedCallback.js","names":[],"sources":["../../src/debouncer/useDebouncedCallback.ts"],"sourcesContent":["import { useCallback } from 'preact/hooks'\nimport { useDebouncer } from './useDebouncer'\nimport type { PreactDebouncerOptions } from './useDebouncer'\nimport type { AnyFunction } from '@tanstack/pacer/types'\n\n/**\n * A Preact hook that creates a debounced version of a callback function.\n * This hook is essentially a wrapper around the basic `debounce` function\n * that is exported from `@tanstack/pacer`,\n * but optimized for Preact with reactive options and a stable function reference.\n *\n * The debounced function will only execute after the specified wait time has elapsed\n * since its last invocation. If called again before the wait time expires, the timer\n * resets and starts waiting again.\n *\n * This hook provides a simpler API compared to `useDebouncer`, making it ideal for basic\n * debouncing needs. However, it does not expose the underlying Debouncer instance.\n *\n * For advanced usage requiring features like:\n * - Manual cancellation\n * - Access to execution counts\n * - Custom useCallback dependencies\n *\n * Consider using the `useDebouncer` hook instead.\n *\n * @example\n * ```tsx\n * // Debounce a search handler\n * const handleSearch = useDebouncedCallback((query: string) => {\n * fetchSearchResults(query);\n * }, {\n * wait: 500 // Wait 500ms between executions\n * });\n *\n * // Use in an input\n * <input\n * type=\"search\"\n * onChange={(e) => handleSearch(e.target.value)}\n * />\n * ```\n */\nexport function useDebouncedCallback<TFn extends AnyFunction>(\n fn: TFn,\n options: PreactDebouncerOptions<TFn, {}>,\n): (...args: Parameters<TFn>) => void {\n const debouncedFn = useDebouncer(fn, options).maybeExecute\n return useCallback(\n (...args: Parameters<TFn>) => debouncedFn(...args),\n [debouncedFn],\n )\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAyCA,SAAgB,qBACd,IACA,SACoC;CACpC,MAAM,cAAc,aAAa,IAAI,QAAQ,CAAC;CAC9C,OAAO,aACJ,GAAG,SAA0B,YAAY,GAAG,KAAK,EAClD,CAAC,YAAY,CACd"}
|
|
@@ -1,89 +0,0 @@
|
|
|
1
|
-
const require_useDebouncer = require('./useDebouncer.cjs');
|
|
2
|
-
let preact_hooks = require("preact/hooks");
|
|
3
|
-
|
|
4
|
-
//#region src/debouncer/useDebouncedState.ts
|
|
5
|
-
/**
|
|
6
|
-
* A Preact hook that creates a debounced state value, combining Preact's useState with debouncing functionality.
|
|
7
|
-
* This hook provides both the current debounced value and methods to update it.
|
|
8
|
-
*
|
|
9
|
-
* The state value is only updated after the specified wait time has elapsed since the last update attempt.
|
|
10
|
-
* If another update is attempted before the wait time expires, the timer resets and starts waiting again.
|
|
11
|
-
* This is useful for handling frequent state updates that should be throttled, like search input values
|
|
12
|
-
* or window resize dimensions.
|
|
13
|
-
*
|
|
14
|
-
* The hook returns a tuple containing:
|
|
15
|
-
* - The current debounced value
|
|
16
|
-
* - A function to update the debounced value
|
|
17
|
-
* - The debouncer instance with additional control methods
|
|
18
|
-
*
|
|
19
|
-
* ## State Management and Selector
|
|
20
|
-
*
|
|
21
|
-
* The hook uses TanStack Store for reactive state management via the underlying debouncer instance.
|
|
22
|
-
* The `selector` parameter allows you to specify which debouncer state changes will trigger a re-render,
|
|
23
|
-
* optimizing performance by preventing unnecessary re-renders when irrelevant state changes occur.
|
|
24
|
-
*
|
|
25
|
-
* **By default, there will be no reactive state subscriptions** and you must opt-in to state
|
|
26
|
-
* tracking by providing a selector function. This prevents unnecessary re-renders and gives you
|
|
27
|
-
* full control over when your component updates. Only when you provide a selector will the
|
|
28
|
-
* component re-render when the selected state values change.
|
|
29
|
-
*
|
|
30
|
-
* Available debouncer state properties:
|
|
31
|
-
* - `canLeadingExecute`: Whether the debouncer can execute on the leading edge
|
|
32
|
-
* - `executionCount`: Number of function executions that have been completed
|
|
33
|
-
* - `isPending`: Whether the debouncer is waiting for the timeout to trigger execution
|
|
34
|
-
* - `lastArgs`: The arguments from the most recent call to maybeExecute
|
|
35
|
-
* - `status`: Current execution status ('disabled' | 'idle' | 'pending')
|
|
36
|
-
*
|
|
37
|
-
* @example
|
|
38
|
-
* ```tsx
|
|
39
|
-
* // Default behavior - no reactive state subscriptions
|
|
40
|
-
* const [searchTerm, setSearchTerm, debouncer] = useDebouncedState('', {
|
|
41
|
-
* wait: 500 // Wait 500ms after last keystroke
|
|
42
|
-
* });
|
|
43
|
-
*
|
|
44
|
-
* // Opt-in to re-render when pending state changes (optimized for loading indicators)
|
|
45
|
-
* const [searchTerm, setSearchTerm, debouncer] = useDebouncedState(
|
|
46
|
-
* '',
|
|
47
|
-
* { wait: 500 },
|
|
48
|
-
* (state) => ({ isPending: state.isPending })
|
|
49
|
-
* );
|
|
50
|
-
*
|
|
51
|
-
* // Opt-in to re-render when execution count changes (optimized for tracking executions)
|
|
52
|
-
* const [searchTerm, setSearchTerm, debouncer] = useDebouncedState(
|
|
53
|
-
* '',
|
|
54
|
-
* { wait: 500 },
|
|
55
|
-
* (state) => ({ executionCount: state.executionCount })
|
|
56
|
-
* );
|
|
57
|
-
*
|
|
58
|
-
* // Opt-in to re-render when debouncing status changes (optimized for status display)
|
|
59
|
-
* const [searchTerm, setSearchTerm, debouncer] = useDebouncedState(
|
|
60
|
-
* '',
|
|
61
|
-
* { wait: 500 },
|
|
62
|
-
* (state) => ({
|
|
63
|
-
* status: state.status,
|
|
64
|
-
* canLeadingExecute: state.canLeadingExecute
|
|
65
|
-
* })
|
|
66
|
-
* );
|
|
67
|
-
*
|
|
68
|
-
* // Update value - will be debounced
|
|
69
|
-
* const handleChange = (e) => {
|
|
70
|
-
* setSearchTerm(e.target.value);
|
|
71
|
-
* };
|
|
72
|
-
*
|
|
73
|
-
* // Access the selected debouncer state (will be empty object {} unless selector provided)
|
|
74
|
-
* const { isPending, executionCount } = debouncer.state;
|
|
75
|
-
* ```
|
|
76
|
-
*/
|
|
77
|
-
function useDebouncedState(value, options, selector) {
|
|
78
|
-
const [debouncedValue, setDebouncedValue] = (0, preact_hooks.useState)(value);
|
|
79
|
-
const debouncer = require_useDebouncer.useDebouncer(setDebouncedValue, options, selector);
|
|
80
|
-
return [
|
|
81
|
-
debouncedValue,
|
|
82
|
-
debouncer.maybeExecute,
|
|
83
|
-
debouncer
|
|
84
|
-
];
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
//#endregion
|
|
88
|
-
exports.useDebouncedState = useDebouncedState;
|
|
89
|
-
//# sourceMappingURL=useDebouncedState.cjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useDebouncedState.cjs","names":["useDebouncer"],"sources":["../../src/debouncer/useDebouncedState.ts"],"sourcesContent":["import { useState } from 'preact/hooks'\nimport { useDebouncer } from './useDebouncer'\nimport type { PreactDebouncer, PreactDebouncerOptions } from './useDebouncer'\nimport type { Dispatch, StateUpdater } from 'preact/hooks'\nimport type { DebouncerState } from '@tanstack/pacer/debouncer'\n\n/**\n * A Preact hook that creates a debounced state value, combining Preact's useState with debouncing functionality.\n * This hook provides both the current debounced value and methods to update it.\n *\n * The state value is only updated after the specified wait time has elapsed since the last update attempt.\n * If another update is attempted before the wait time expires, the timer resets and starts waiting again.\n * This is useful for handling frequent state updates that should be throttled, like search input values\n * or window resize dimensions.\n *\n * The hook returns a tuple containing:\n * - The current debounced value\n * - A function to update the debounced value\n * - The debouncer instance with additional control methods\n *\n * ## State Management and Selector\n *\n * The hook uses TanStack Store for reactive state management via the underlying debouncer instance.\n * The `selector` parameter allows you to specify which debouncer state changes will trigger a re-render,\n * optimizing performance by preventing unnecessary re-renders when irrelevant state changes occur.\n *\n * **By default, there will be no reactive state subscriptions** and you must opt-in to state\n * tracking by providing a selector function. This prevents unnecessary re-renders and gives you\n * full control over when your component updates. Only when you provide a selector will the\n * component re-render when the selected state values change.\n *\n * Available debouncer state properties:\n * - `canLeadingExecute`: Whether the debouncer can execute on the leading edge\n * - `executionCount`: Number of function executions that have been completed\n * - `isPending`: Whether the debouncer is waiting for the timeout to trigger execution\n * - `lastArgs`: The arguments from the most recent call to maybeExecute\n * - `status`: Current execution status ('disabled' | 'idle' | 'pending')\n *\n * @example\n * ```tsx\n * // Default behavior - no reactive state subscriptions\n * const [searchTerm, setSearchTerm, debouncer] = useDebouncedState('', {\n * wait: 500 // Wait 500ms after last keystroke\n * });\n *\n * // Opt-in to re-render when pending state changes (optimized for loading indicators)\n * const [searchTerm, setSearchTerm, debouncer] = useDebouncedState(\n * '',\n * { wait: 500 },\n * (state) => ({ isPending: state.isPending })\n * );\n *\n * // Opt-in to re-render when execution count changes (optimized for tracking executions)\n * const [searchTerm, setSearchTerm, debouncer] = useDebouncedState(\n * '',\n * { wait: 500 },\n * (state) => ({ executionCount: state.executionCount })\n * );\n *\n * // Opt-in to re-render when debouncing status changes (optimized for status display)\n * const [searchTerm, setSearchTerm, debouncer] = useDebouncedState(\n * '',\n * { wait: 500 },\n * (state) => ({\n * status: state.status,\n * canLeadingExecute: state.canLeadingExecute\n * })\n * );\n *\n * // Update value - will be debounced\n * const handleChange = (e) => {\n * setSearchTerm(e.target.value);\n * };\n *\n * // Access the selected debouncer state (will be empty object {} unless selector provided)\n * const { isPending, executionCount } = debouncer.state;\n * ```\n */\nexport function useDebouncedState<\n TValue,\n TSelected = DebouncerState<Dispatch<StateUpdater<TValue>>>,\n>(\n value: TValue,\n options: PreactDebouncerOptions<Dispatch<StateUpdater<TValue>>, TSelected>,\n selector?: (\n state: DebouncerState<Dispatch<StateUpdater<TValue>>>,\n ) => TSelected,\n): [\n TValue,\n Dispatch<StateUpdater<TValue>>,\n PreactDebouncer<Dispatch<StateUpdater<TValue>>, TSelected>,\n] {\n const [debouncedValue, setDebouncedValue] = useState(value)\n const debouncer = useDebouncer(\n setDebouncedValue,\n options,\n selector as (\n state: DebouncerState<Dispatch<StateUpdater<TValue>>>,\n ) => TSelected,\n )\n return [\n debouncedValue,\n debouncer.maybeExecute as Dispatch<StateUpdater<TValue>>,\n debouncer,\n ]\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA8EA,SAAgB,kBAId,OACA,SACA,UAOA;CACA,MAAM,CAAC,gBAAgB,gDAA8B,MAAM;CAC3D,MAAM,YAAYA,kCAChB,mBACA,SACA,SAGD;CACD,OAAO;EACL;EACA,UAAU;EACV;EACD"}
|
|
@@ -1,81 +0,0 @@
|
|
|
1
|
-
import { PreactDebouncer, PreactDebouncerOptions } from "./useDebouncer.cjs";
|
|
2
|
-
import { DebouncerState } from "@tanstack/pacer/debouncer";
|
|
3
|
-
import { Dispatch, StateUpdater } from "preact/hooks";
|
|
4
|
-
|
|
5
|
-
//#region src/debouncer/useDebouncedState.d.ts
|
|
6
|
-
/**
|
|
7
|
-
* A Preact hook that creates a debounced state value, combining Preact's useState with debouncing functionality.
|
|
8
|
-
* This hook provides both the current debounced value and methods to update it.
|
|
9
|
-
*
|
|
10
|
-
* The state value is only updated after the specified wait time has elapsed since the last update attempt.
|
|
11
|
-
* If another update is attempted before the wait time expires, the timer resets and starts waiting again.
|
|
12
|
-
* This is useful for handling frequent state updates that should be throttled, like search input values
|
|
13
|
-
* or window resize dimensions.
|
|
14
|
-
*
|
|
15
|
-
* The hook returns a tuple containing:
|
|
16
|
-
* - The current debounced value
|
|
17
|
-
* - A function to update the debounced value
|
|
18
|
-
* - The debouncer instance with additional control methods
|
|
19
|
-
*
|
|
20
|
-
* ## State Management and Selector
|
|
21
|
-
*
|
|
22
|
-
* The hook uses TanStack Store for reactive state management via the underlying debouncer instance.
|
|
23
|
-
* The `selector` parameter allows you to specify which debouncer state changes will trigger a re-render,
|
|
24
|
-
* optimizing performance by preventing unnecessary re-renders when irrelevant state changes occur.
|
|
25
|
-
*
|
|
26
|
-
* **By default, there will be no reactive state subscriptions** and you must opt-in to state
|
|
27
|
-
* tracking by providing a selector function. This prevents unnecessary re-renders and gives you
|
|
28
|
-
* full control over when your component updates. Only when you provide a selector will the
|
|
29
|
-
* component re-render when the selected state values change.
|
|
30
|
-
*
|
|
31
|
-
* Available debouncer state properties:
|
|
32
|
-
* - `canLeadingExecute`: Whether the debouncer can execute on the leading edge
|
|
33
|
-
* - `executionCount`: Number of function executions that have been completed
|
|
34
|
-
* - `isPending`: Whether the debouncer is waiting for the timeout to trigger execution
|
|
35
|
-
* - `lastArgs`: The arguments from the most recent call to maybeExecute
|
|
36
|
-
* - `status`: Current execution status ('disabled' | 'idle' | 'pending')
|
|
37
|
-
*
|
|
38
|
-
* @example
|
|
39
|
-
* ```tsx
|
|
40
|
-
* // Default behavior - no reactive state subscriptions
|
|
41
|
-
* const [searchTerm, setSearchTerm, debouncer] = useDebouncedState('', {
|
|
42
|
-
* wait: 500 // Wait 500ms after last keystroke
|
|
43
|
-
* });
|
|
44
|
-
*
|
|
45
|
-
* // Opt-in to re-render when pending state changes (optimized for loading indicators)
|
|
46
|
-
* const [searchTerm, setSearchTerm, debouncer] = useDebouncedState(
|
|
47
|
-
* '',
|
|
48
|
-
* { wait: 500 },
|
|
49
|
-
* (state) => ({ isPending: state.isPending })
|
|
50
|
-
* );
|
|
51
|
-
*
|
|
52
|
-
* // Opt-in to re-render when execution count changes (optimized for tracking executions)
|
|
53
|
-
* const [searchTerm, setSearchTerm, debouncer] = useDebouncedState(
|
|
54
|
-
* '',
|
|
55
|
-
* { wait: 500 },
|
|
56
|
-
* (state) => ({ executionCount: state.executionCount })
|
|
57
|
-
* );
|
|
58
|
-
*
|
|
59
|
-
* // Opt-in to re-render when debouncing status changes (optimized for status display)
|
|
60
|
-
* const [searchTerm, setSearchTerm, debouncer] = useDebouncedState(
|
|
61
|
-
* '',
|
|
62
|
-
* { wait: 500 },
|
|
63
|
-
* (state) => ({
|
|
64
|
-
* status: state.status,
|
|
65
|
-
* canLeadingExecute: state.canLeadingExecute
|
|
66
|
-
* })
|
|
67
|
-
* );
|
|
68
|
-
*
|
|
69
|
-
* // Update value - will be debounced
|
|
70
|
-
* const handleChange = (e) => {
|
|
71
|
-
* setSearchTerm(e.target.value);
|
|
72
|
-
* };
|
|
73
|
-
*
|
|
74
|
-
* // Access the selected debouncer state (will be empty object {} unless selector provided)
|
|
75
|
-
* const { isPending, executionCount } = debouncer.state;
|
|
76
|
-
* ```
|
|
77
|
-
*/
|
|
78
|
-
declare function useDebouncedState<TValue, TSelected = DebouncerState<Dispatch<StateUpdater<TValue>>>>(value: TValue, options: PreactDebouncerOptions<Dispatch<StateUpdater<TValue>>, TSelected>, selector?: (state: DebouncerState<Dispatch<StateUpdater<TValue>>>) => TSelected): [TValue, Dispatch<StateUpdater<TValue>>, PreactDebouncer<Dispatch<StateUpdater<TValue>>, TSelected>];
|
|
79
|
-
//#endregion
|
|
80
|
-
export { useDebouncedState };
|
|
81
|
-
//# sourceMappingURL=useDebouncedState.d.cts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useDebouncedState.js","names":[],"sources":["../../src/debouncer/useDebouncedState.ts"],"sourcesContent":["import { useState } from 'preact/hooks'\nimport { useDebouncer } from './useDebouncer'\nimport type { PreactDebouncer, PreactDebouncerOptions } from './useDebouncer'\nimport type { Dispatch, StateUpdater } from 'preact/hooks'\nimport type { DebouncerState } from '@tanstack/pacer/debouncer'\n\n/**\n * A Preact hook that creates a debounced state value, combining Preact's useState with debouncing functionality.\n * This hook provides both the current debounced value and methods to update it.\n *\n * The state value is only updated after the specified wait time has elapsed since the last update attempt.\n * If another update is attempted before the wait time expires, the timer resets and starts waiting again.\n * This is useful for handling frequent state updates that should be throttled, like search input values\n * or window resize dimensions.\n *\n * The hook returns a tuple containing:\n * - The current debounced value\n * - A function to update the debounced value\n * - The debouncer instance with additional control methods\n *\n * ## State Management and Selector\n *\n * The hook uses TanStack Store for reactive state management via the underlying debouncer instance.\n * The `selector` parameter allows you to specify which debouncer state changes will trigger a re-render,\n * optimizing performance by preventing unnecessary re-renders when irrelevant state changes occur.\n *\n * **By default, there will be no reactive state subscriptions** and you must opt-in to state\n * tracking by providing a selector function. This prevents unnecessary re-renders and gives you\n * full control over when your component updates. Only when you provide a selector will the\n * component re-render when the selected state values change.\n *\n * Available debouncer state properties:\n * - `canLeadingExecute`: Whether the debouncer can execute on the leading edge\n * - `executionCount`: Number of function executions that have been completed\n * - `isPending`: Whether the debouncer is waiting for the timeout to trigger execution\n * - `lastArgs`: The arguments from the most recent call to maybeExecute\n * - `status`: Current execution status ('disabled' | 'idle' | 'pending')\n *\n * @example\n * ```tsx\n * // Default behavior - no reactive state subscriptions\n * const [searchTerm, setSearchTerm, debouncer] = useDebouncedState('', {\n * wait: 500 // Wait 500ms after last keystroke\n * });\n *\n * // Opt-in to re-render when pending state changes (optimized for loading indicators)\n * const [searchTerm, setSearchTerm, debouncer] = useDebouncedState(\n * '',\n * { wait: 500 },\n * (state) => ({ isPending: state.isPending })\n * );\n *\n * // Opt-in to re-render when execution count changes (optimized for tracking executions)\n * const [searchTerm, setSearchTerm, debouncer] = useDebouncedState(\n * '',\n * { wait: 500 },\n * (state) => ({ executionCount: state.executionCount })\n * );\n *\n * // Opt-in to re-render when debouncing status changes (optimized for status display)\n * const [searchTerm, setSearchTerm, debouncer] = useDebouncedState(\n * '',\n * { wait: 500 },\n * (state) => ({\n * status: state.status,\n * canLeadingExecute: state.canLeadingExecute\n * })\n * );\n *\n * // Update value - will be debounced\n * const handleChange = (e) => {\n * setSearchTerm(e.target.value);\n * };\n *\n * // Access the selected debouncer state (will be empty object {} unless selector provided)\n * const { isPending, executionCount } = debouncer.state;\n * ```\n */\nexport function useDebouncedState<\n TValue,\n TSelected = DebouncerState<Dispatch<StateUpdater<TValue>>>,\n>(\n value: TValue,\n options: PreactDebouncerOptions<Dispatch<StateUpdater<TValue>>, TSelected>,\n selector?: (\n state: DebouncerState<Dispatch<StateUpdater<TValue>>>,\n ) => TSelected,\n): [\n TValue,\n Dispatch<StateUpdater<TValue>>,\n PreactDebouncer<Dispatch<StateUpdater<TValue>>, TSelected>,\n] {\n const [debouncedValue, setDebouncedValue] = useState(value)\n const debouncer = useDebouncer(\n setDebouncedValue,\n options,\n selector as (\n state: DebouncerState<Dispatch<StateUpdater<TValue>>>,\n ) => TSelected,\n )\n return [\n debouncedValue,\n debouncer.maybeExecute as Dispatch<StateUpdater<TValue>>,\n debouncer,\n ]\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA8EA,SAAgB,kBAId,OACA,SACA,UAOA;CACA,MAAM,CAAC,gBAAgB,qBAAqB,SAAS,MAAM;CAC3D,MAAM,YAAY,aAChB,mBACA,SACA,SAGD;CACD,OAAO;EACL;EACA,UAAU;EACV;EACD"}
|
|
@@ -1,96 +0,0 @@
|
|
|
1
|
-
const require_useDebouncedState = require('./useDebouncedState.cjs');
|
|
2
|
-
let preact_hooks = require("preact/hooks");
|
|
3
|
-
|
|
4
|
-
//#region src/debouncer/useDebouncedValue.ts
|
|
5
|
-
/**
|
|
6
|
-
* A Preact hook that creates a debounced value that updates only after a specified delay.
|
|
7
|
-
* Unlike useDebouncedState, this hook automatically tracks changes to the input value
|
|
8
|
-
* and updates the debounced value accordingly.
|
|
9
|
-
*
|
|
10
|
-
* The debounced value will only update after the specified wait time has elapsed since
|
|
11
|
-
* the last change to the input value. If the input value changes again before the wait
|
|
12
|
-
* time expires, the timer resets and starts waiting again.
|
|
13
|
-
*
|
|
14
|
-
* This is useful for deriving debounced values from props or state that change frequently,
|
|
15
|
-
* like search queries or form inputs, where you want to limit how often downstream effects
|
|
16
|
-
* or calculations occur.
|
|
17
|
-
*
|
|
18
|
-
* The hook returns the current debounced value and the underlying debouncer instance.
|
|
19
|
-
* The debouncer instance can be used to access additional functionality like cancellation
|
|
20
|
-
* and execution counts.
|
|
21
|
-
*
|
|
22
|
-
* ## State Management and Selector
|
|
23
|
-
*
|
|
24
|
-
* The hook uses TanStack Store for reactive state management via the underlying debouncer instance.
|
|
25
|
-
* The `selector` parameter allows you to specify which debouncer state changes will trigger a re-render,
|
|
26
|
-
* optimizing performance by preventing unnecessary re-renders when irrelevant state changes occur.
|
|
27
|
-
*
|
|
28
|
-
* **By default, there will be no reactive state subscriptions** and you must opt-in to state
|
|
29
|
-
* tracking by providing a selector function. This prevents unnecessary re-renders and gives you
|
|
30
|
-
* full control over when your component updates. Only when you provide a selector will the
|
|
31
|
-
* component re-render when the selected state values change.
|
|
32
|
-
*
|
|
33
|
-
* Available debouncer state properties:
|
|
34
|
-
* - `canLeadingExecute`: Whether the debouncer can execute on the leading edge
|
|
35
|
-
* - `executionCount`: Number of function executions that have been completed
|
|
36
|
-
* - `isPending`: Whether the debouncer is waiting for the timeout to trigger execution
|
|
37
|
-
* - `lastArgs`: The arguments from the most recent call to maybeExecute
|
|
38
|
-
* - `status`: Current execution status ('disabled' | 'idle' | 'pending')
|
|
39
|
-
*
|
|
40
|
-
* @example
|
|
41
|
-
* ```tsx
|
|
42
|
-
* // Default behavior - no reactive state subscriptions
|
|
43
|
-
* const [searchQuery, setSearchQuery] = useState('');
|
|
44
|
-
* const [debouncedQuery, debouncer] = useDebouncedValue(searchQuery, {
|
|
45
|
-
* wait: 500 // Wait 500ms after last change
|
|
46
|
-
* });
|
|
47
|
-
*
|
|
48
|
-
* // Opt-in to re-render when pending state changes (optimized for loading indicators)
|
|
49
|
-
* const [debouncedQuery, debouncer] = useDebouncedValue(
|
|
50
|
-
* searchQuery,
|
|
51
|
-
* { wait: 500 },
|
|
52
|
-
* (state) => ({ isPending: state.isPending })
|
|
53
|
-
* );
|
|
54
|
-
*
|
|
55
|
-
* // Opt-in to re-render when execution count changes (optimized for tracking executions)
|
|
56
|
-
* const [debouncedQuery, debouncer] = useDebouncedValue(
|
|
57
|
-
* searchQuery,
|
|
58
|
-
* { wait: 500 },
|
|
59
|
-
* (state) => ({ executionCount: state.executionCount })
|
|
60
|
-
* );
|
|
61
|
-
*
|
|
62
|
-
* // Opt-in to re-render when debouncing status changes (optimized for status display)
|
|
63
|
-
* const [debouncedQuery, debouncer] = useDebouncedValue(
|
|
64
|
-
* searchQuery,
|
|
65
|
-
* { wait: 500 },
|
|
66
|
-
* (state) => ({
|
|
67
|
-
* status: state.status,
|
|
68
|
-
* canLeadingExecute: state.canLeadingExecute
|
|
69
|
-
* })
|
|
70
|
-
* );
|
|
71
|
-
*
|
|
72
|
-
* // debouncedQuery will update 500ms after searchQuery stops changing
|
|
73
|
-
* useEffect(() => {
|
|
74
|
-
* fetchSearchResults(debouncedQuery);
|
|
75
|
-
* }, [debouncedQuery]);
|
|
76
|
-
*
|
|
77
|
-
* // Handle input changes
|
|
78
|
-
* const handleChange = (e) => {
|
|
79
|
-
* setSearchQuery(e.target.value);
|
|
80
|
-
* };
|
|
81
|
-
*
|
|
82
|
-
* // Access the selected debouncer state (will be empty object {} unless selector provided)
|
|
83
|
-
* const { isPending, executionCount } = debouncer.state;
|
|
84
|
-
* ```
|
|
85
|
-
*/
|
|
86
|
-
function useDebouncedValue(value, options, selector) {
|
|
87
|
-
const [debouncedValue, setDebouncedValue, debouncer] = require_useDebouncedState.useDebouncedState(value, options, selector);
|
|
88
|
-
(0, preact_hooks.useEffect)(() => {
|
|
89
|
-
setDebouncedValue(value);
|
|
90
|
-
}, [value, setDebouncedValue]);
|
|
91
|
-
return [debouncedValue, debouncer];
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
//#endregion
|
|
95
|
-
exports.useDebouncedValue = useDebouncedValue;
|
|
96
|
-
//# sourceMappingURL=useDebouncedValue.cjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useDebouncedValue.cjs","names":["useDebouncedState"],"sources":["../../src/debouncer/useDebouncedValue.ts"],"sourcesContent":["import { useEffect } from 'preact/hooks'\nimport { useDebouncedState } from './useDebouncedState'\nimport type { PreactDebouncer, PreactDebouncerOptions } from './useDebouncer'\nimport type { Dispatch, StateUpdater } from 'preact/hooks'\nimport type { DebouncerState } from '@tanstack/pacer/debouncer'\n\n/**\n * A Preact hook that creates a debounced value that updates only after a specified delay.\n * Unlike useDebouncedState, this hook automatically tracks changes to the input value\n * and updates the debounced value accordingly.\n *\n * The debounced value will only update after the specified wait time has elapsed since\n * the last change to the input value. If the input value changes again before the wait\n * time expires, the timer resets and starts waiting again.\n *\n * This is useful for deriving debounced values from props or state that change frequently,\n * like search queries or form inputs, where you want to limit how often downstream effects\n * or calculations occur.\n *\n * The hook returns the current debounced value and the underlying debouncer instance.\n * The debouncer instance can be used to access additional functionality like cancellation\n * and execution counts.\n *\n * ## State Management and Selector\n *\n * The hook uses TanStack Store for reactive state management via the underlying debouncer instance.\n * The `selector` parameter allows you to specify which debouncer state changes will trigger a re-render,\n * optimizing performance by preventing unnecessary re-renders when irrelevant state changes occur.\n *\n * **By default, there will be no reactive state subscriptions** and you must opt-in to state\n * tracking by providing a selector function. This prevents unnecessary re-renders and gives you\n * full control over when your component updates. Only when you provide a selector will the\n * component re-render when the selected state values change.\n *\n * Available debouncer state properties:\n * - `canLeadingExecute`: Whether the debouncer can execute on the leading edge\n * - `executionCount`: Number of function executions that have been completed\n * - `isPending`: Whether the debouncer is waiting for the timeout to trigger execution\n * - `lastArgs`: The arguments from the most recent call to maybeExecute\n * - `status`: Current execution status ('disabled' | 'idle' | 'pending')\n *\n * @example\n * ```tsx\n * // Default behavior - no reactive state subscriptions\n * const [searchQuery, setSearchQuery] = useState('');\n * const [debouncedQuery, debouncer] = useDebouncedValue(searchQuery, {\n * wait: 500 // Wait 500ms after last change\n * });\n *\n * // Opt-in to re-render when pending state changes (optimized for loading indicators)\n * const [debouncedQuery, debouncer] = useDebouncedValue(\n * searchQuery,\n * { wait: 500 },\n * (state) => ({ isPending: state.isPending })\n * );\n *\n * // Opt-in to re-render when execution count changes (optimized for tracking executions)\n * const [debouncedQuery, debouncer] = useDebouncedValue(\n * searchQuery,\n * { wait: 500 },\n * (state) => ({ executionCount: state.executionCount })\n * );\n *\n * // Opt-in to re-render when debouncing status changes (optimized for status display)\n * const [debouncedQuery, debouncer] = useDebouncedValue(\n * searchQuery,\n * { wait: 500 },\n * (state) => ({\n * status: state.status,\n * canLeadingExecute: state.canLeadingExecute\n * })\n * );\n *\n * // debouncedQuery will update 500ms after searchQuery stops changing\n * useEffect(() => {\n * fetchSearchResults(debouncedQuery);\n * }, [debouncedQuery]);\n *\n * // Handle input changes\n * const handleChange = (e) => {\n * setSearchQuery(e.target.value);\n * };\n *\n * // Access the selected debouncer state (will be empty object {} unless selector provided)\n * const { isPending, executionCount } = debouncer.state;\n * ```\n */\nexport function useDebouncedValue<\n TValue,\n TSelected = DebouncerState<Dispatch<StateUpdater<TValue>>>,\n>(\n value: TValue,\n options: PreactDebouncerOptions<Dispatch<StateUpdater<TValue>>, TSelected>,\n selector?: (\n state: DebouncerState<Dispatch<StateUpdater<TValue>>>,\n ) => TSelected,\n): [TValue, PreactDebouncer<Dispatch<StateUpdater<TValue>>, TSelected>] {\n const [debouncedValue, setDebouncedValue, debouncer] = useDebouncedState(\n value,\n options,\n selector,\n )\n\n useEffect(() => {\n ;(setDebouncedValue as (value: TValue) => void)(value)\n }, [value, setDebouncedValue])\n\n return [debouncedValue, debouncer]\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAuFA,SAAgB,kBAId,OACA,SACA,UAGsE;CACtE,MAAM,CAAC,gBAAgB,mBAAmB,aAAaA,4CACrD,OACA,SACA,SACD;CAED,kCAAgB;EACb,AAAC,kBAA8C,MAAM;IACrD,CAAC,OAAO,kBAAkB,CAAC;CAE9B,OAAO,CAAC,gBAAgB,UAAU"}
|