@tanstack/preact-pacer 0.0.1 → 0.18.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/LICENSE +21 -0
- package/README.md +165 -45
- package/dist/async-batcher/index.cjs +12 -0
- package/dist/async-batcher/index.d.cts +4 -0
- package/dist/async-batcher/index.d.ts +4 -0
- package/dist/async-batcher/index.js +6 -0
- package/dist/async-batcher/useAsyncBatchedCallback.cjs +51 -0
- package/dist/async-batcher/useAsyncBatchedCallback.cjs.map +1 -0
- package/dist/async-batcher/useAsyncBatchedCallback.d.cts +46 -0
- package/dist/async-batcher/useAsyncBatchedCallback.d.ts +46 -0
- package/dist/async-batcher/useAsyncBatchedCallback.js +51 -0
- package/dist/async-batcher/useAsyncBatchedCallback.js.map +1 -0
- package/dist/async-batcher/useAsyncBatcher.cjs +165 -0
- package/dist/async-batcher/useAsyncBatcher.cjs.map +1 -0
- package/dist/async-batcher/useAsyncBatcher.d.cts +163 -0
- package/dist/async-batcher/useAsyncBatcher.d.ts +163 -0
- package/dist/async-batcher/useAsyncBatcher.js +165 -0
- package/dist/async-batcher/useAsyncBatcher.js.map +1 -0
- package/dist/async-debouncer/index.cjs +12 -0
- package/dist/async-debouncer/index.d.cts +4 -0
- package/dist/async-debouncer/index.d.ts +4 -0
- package/dist/async-debouncer/index.js +6 -0
- package/dist/async-debouncer/useAsyncDebouncedCallback.cjs +50 -0
- package/dist/async-debouncer/useAsyncDebouncedCallback.cjs.map +1 -0
- package/dist/async-debouncer/useAsyncDebouncedCallback.d.cts +47 -0
- package/dist/async-debouncer/useAsyncDebouncedCallback.d.ts +47 -0
- package/dist/async-debouncer/useAsyncDebouncedCallback.js +50 -0
- package/dist/async-debouncer/useAsyncDebouncedCallback.js.map +1 -0
- package/dist/async-debouncer/useAsyncDebouncer.cjs +149 -0
- package/dist/async-debouncer/useAsyncDebouncer.cjs.map +1 -0
- package/dist/async-debouncer/useAsyncDebouncer.d.cts +143 -0
- package/dist/async-debouncer/useAsyncDebouncer.d.ts +143 -0
- package/dist/async-debouncer/useAsyncDebouncer.js +149 -0
- package/dist/async-debouncer/useAsyncDebouncer.js.map +1 -0
- package/dist/async-queuer/index.cjs +12 -0
- package/dist/async-queuer/index.d.cts +4 -0
- package/dist/async-queuer/index.d.ts +4 -0
- package/dist/async-queuer/index.js +6 -0
- package/dist/async-queuer/useAsyncQueuedState.cjs +154 -0
- package/dist/async-queuer/useAsyncQueuedState.cjs.map +1 -0
- package/dist/async-queuer/useAsyncQueuedState.d.cts +152 -0
- package/dist/async-queuer/useAsyncQueuedState.d.ts +152 -0
- package/dist/async-queuer/useAsyncQueuedState.js +154 -0
- package/dist/async-queuer/useAsyncQueuedState.js.map +1 -0
- package/dist/async-queuer/useAsyncQueuer.cjs +165 -0
- package/dist/async-queuer/useAsyncQueuer.cjs.map +1 -0
- package/dist/async-queuer/useAsyncQueuer.d.cts +163 -0
- package/dist/async-queuer/useAsyncQueuer.d.ts +163 -0
- package/dist/async-queuer/useAsyncQueuer.js +165 -0
- package/dist/async-queuer/useAsyncQueuer.js.map +1 -0
- package/dist/async-rate-limiter/index.cjs +12 -0
- package/dist/async-rate-limiter/index.d.cts +4 -0
- package/dist/async-rate-limiter/index.d.ts +4 -0
- package/dist/async-rate-limiter/index.js +6 -0
- package/dist/async-rate-limiter/useAsyncRateLimitedCallback.cjs +65 -0
- package/dist/async-rate-limiter/useAsyncRateLimitedCallback.cjs.map +1 -0
- package/dist/async-rate-limiter/useAsyncRateLimitedCallback.d.cts +62 -0
- package/dist/async-rate-limiter/useAsyncRateLimitedCallback.d.ts +62 -0
- package/dist/async-rate-limiter/useAsyncRateLimitedCallback.js +65 -0
- package/dist/async-rate-limiter/useAsyncRateLimitedCallback.js.map +1 -0
- package/dist/async-rate-limiter/useAsyncRateLimiter.cjs +173 -0
- package/dist/async-rate-limiter/useAsyncRateLimiter.cjs.map +1 -0
- package/dist/async-rate-limiter/useAsyncRateLimiter.d.cts +172 -0
- package/dist/async-rate-limiter/useAsyncRateLimiter.d.ts +172 -0
- package/dist/async-rate-limiter/useAsyncRateLimiter.js +173 -0
- package/dist/async-rate-limiter/useAsyncRateLimiter.js.map +1 -0
- package/dist/async-retryer/index.cjs +9 -0
- package/dist/async-retryer/index.d.cts +1 -0
- package/dist/async-retryer/index.d.ts +1 -0
- package/dist/async-retryer/index.js +3 -0
- package/dist/async-throttler/index.cjs +12 -0
- package/dist/async-throttler/index.d.cts +4 -0
- package/dist/async-throttler/index.d.ts +4 -0
- package/dist/async-throttler/index.js +6 -0
- package/dist/async-throttler/useAsyncThrottledCallback.cjs +48 -0
- package/dist/async-throttler/useAsyncThrottledCallback.cjs.map +1 -0
- package/dist/async-throttler/useAsyncThrottledCallback.d.cts +45 -0
- package/dist/async-throttler/useAsyncThrottledCallback.d.ts +45 -0
- package/dist/async-throttler/useAsyncThrottledCallback.js +48 -0
- package/dist/async-throttler/useAsyncThrottledCallback.js.map +1 -0
- package/dist/async-throttler/useAsyncThrottler.cjs +158 -0
- package/dist/async-throttler/useAsyncThrottler.cjs.map +1 -0
- package/dist/async-throttler/useAsyncThrottler.d.cts +154 -0
- package/dist/async-throttler/useAsyncThrottler.d.ts +154 -0
- package/dist/async-throttler/useAsyncThrottler.js +158 -0
- package/dist/async-throttler/useAsyncThrottler.js.map +1 -0
- package/dist/batcher/index.cjs +12 -0
- package/dist/batcher/index.d.cts +4 -0
- package/dist/batcher/index.d.ts +4 -0
- package/dist/batcher/index.js +6 -0
- package/dist/batcher/useBatchedCallback.cjs +47 -0
- package/dist/batcher/useBatchedCallback.cjs.map +1 -0
- package/dist/batcher/useBatchedCallback.d.cts +44 -0
- package/dist/batcher/useBatchedCallback.d.ts +44 -0
- package/dist/batcher/useBatchedCallback.js +47 -0
- package/dist/batcher/useBatchedCallback.js.map +1 -0
- package/dist/batcher/useBatcher.cjs +122 -0
- package/dist/batcher/useBatcher.cjs.map +1 -0
- package/dist/batcher/useBatcher.d.cts +120 -0
- package/dist/batcher/useBatcher.d.ts +120 -0
- package/dist/batcher/useBatcher.js +122 -0
- package/dist/batcher/useBatcher.js.map +1 -0
- package/dist/debouncer/index.cjs +16 -0
- package/dist/debouncer/index.d.cts +6 -0
- package/dist/debouncer/index.d.ts +6 -0
- package/dist/debouncer/index.js +8 -0
- package/dist/debouncer/useDebouncedCallback.cjs +48 -0
- package/dist/debouncer/useDebouncedCallback.cjs.map +1 -0
- package/dist/debouncer/useDebouncedCallback.d.cts +45 -0
- package/dist/debouncer/useDebouncedCallback.d.ts +45 -0
- package/dist/debouncer/useDebouncedCallback.js +48 -0
- package/dist/debouncer/useDebouncedCallback.js.map +1 -0
- package/dist/debouncer/useDebouncedState.cjs +89 -0
- package/dist/debouncer/useDebouncedState.cjs.map +1 -0
- package/dist/debouncer/useDebouncedState.d.cts +82 -0
- package/dist/debouncer/useDebouncedState.d.ts +82 -0
- package/dist/debouncer/useDebouncedState.js +89 -0
- package/dist/debouncer/useDebouncedState.js.map +1 -0
- package/dist/debouncer/useDebouncedValue.cjs +96 -0
- package/dist/debouncer/useDebouncedValue.cjs.map +1 -0
- package/dist/debouncer/useDebouncedValue.d.cts +91 -0
- package/dist/debouncer/useDebouncedValue.d.ts +91 -0
- package/dist/debouncer/useDebouncedValue.js +96 -0
- package/dist/debouncer/useDebouncedValue.js.map +1 -0
- package/dist/debouncer/useDebouncer.cjs +104 -0
- package/dist/debouncer/useDebouncer.cjs.map +1 -0
- package/dist/debouncer/useDebouncer.d.cts +98 -0
- package/dist/debouncer/useDebouncer.d.ts +98 -0
- package/dist/debouncer/useDebouncer.js +104 -0
- package/dist/debouncer/useDebouncer.js.map +1 -0
- package/dist/index.cjs +66 -0
- package/dist/index.d.cts +30 -0
- package/dist/index.d.ts +30 -0
- package/dist/index.js +32 -0
- package/dist/provider/PacerProvider.cjs +26 -0
- package/dist/provider/PacerProvider.cjs.map +1 -0
- package/dist/provider/PacerProvider.d.cts +33 -0
- package/dist/provider/PacerProvider.d.ts +33 -0
- package/dist/provider/PacerProvider.js +24 -0
- package/dist/provider/PacerProvider.js.map +1 -0
- package/dist/provider/index.cjs +5 -0
- package/dist/provider/index.d.cts +2 -0
- package/dist/provider/index.d.ts +2 -0
- package/dist/provider/index.js +3 -0
- package/dist/queuer/index.cjs +14 -0
- package/dist/queuer/index.d.cts +5 -0
- package/dist/queuer/index.d.ts +5 -0
- package/dist/queuer/index.js +7 -0
- package/dist/queuer/useQueuedState.cjs +129 -0
- package/dist/queuer/useQueuedState.cjs.map +1 -0
- package/dist/queuer/useQueuedState.d.cts +123 -0
- package/dist/queuer/useQueuedState.d.ts +123 -0
- package/dist/queuer/useQueuedState.js +129 -0
- package/dist/queuer/useQueuedState.js.map +1 -0
- package/dist/queuer/useQueuedValue.cjs +115 -0
- package/dist/queuer/useQueuedValue.cjs.map +1 -0
- package/dist/queuer/useQueuedValue.d.cts +106 -0
- package/dist/queuer/useQueuedValue.d.ts +106 -0
- package/dist/queuer/useQueuedValue.js +115 -0
- package/dist/queuer/useQueuedValue.js.map +1 -0
- package/dist/queuer/useQueuer.cjs +133 -0
- package/dist/queuer/useQueuer.cjs.map +1 -0
- package/dist/queuer/useQueuer.d.cts +131 -0
- package/dist/queuer/useQueuer.d.ts +131 -0
- package/dist/queuer/useQueuer.js +133 -0
- package/dist/queuer/useQueuer.js.map +1 -0
- package/dist/rate-limiter/index.cjs +16 -0
- package/dist/rate-limiter/index.d.cts +6 -0
- package/dist/rate-limiter/index.d.ts +6 -0
- package/dist/rate-limiter/index.js +8 -0
- package/dist/rate-limiter/useRateLimitedCallback.cjs +65 -0
- package/dist/rate-limiter/useRateLimitedCallback.cjs.map +1 -0
- package/dist/rate-limiter/useRateLimitedCallback.d.cts +62 -0
- package/dist/rate-limiter/useRateLimitedCallback.d.ts +62 -0
- package/dist/rate-limiter/useRateLimitedCallback.js +65 -0
- package/dist/rate-limiter/useRateLimitedCallback.js.map +1 -0
- package/dist/rate-limiter/useRateLimitedState.cjs +115 -0
- package/dist/rate-limiter/useRateLimitedState.cjs.map +1 -0
- package/dist/rate-limiter/useRateLimitedState.d.cts +108 -0
- package/dist/rate-limiter/useRateLimitedState.d.ts +108 -0
- package/dist/rate-limiter/useRateLimitedState.js +115 -0
- package/dist/rate-limiter/useRateLimitedState.js.map +1 -0
- package/dist/rate-limiter/useRateLimitedValue.cjs +102 -0
- package/dist/rate-limiter/useRateLimitedValue.cjs.map +1 -0
- package/dist/rate-limiter/useRateLimitedValue.d.cts +97 -0
- package/dist/rate-limiter/useRateLimitedValue.d.ts +97 -0
- package/dist/rate-limiter/useRateLimitedValue.js +102 -0
- package/dist/rate-limiter/useRateLimitedValue.js.map +1 -0
- package/dist/rate-limiter/useRateLimiter.cjs +138 -0
- package/dist/rate-limiter/useRateLimiter.cjs.map +1 -0
- package/dist/rate-limiter/useRateLimiter.d.cts +137 -0
- package/dist/rate-limiter/useRateLimiter.d.ts +137 -0
- package/dist/rate-limiter/useRateLimiter.js +138 -0
- package/dist/rate-limiter/useRateLimiter.js.map +1 -0
- package/dist/throttler/index.cjs +16 -0
- package/dist/throttler/index.d.cts +6 -0
- package/dist/throttler/index.d.ts +6 -0
- package/dist/throttler/index.js +8 -0
- package/dist/throttler/useThrottledCallback.cjs +49 -0
- package/dist/throttler/useThrottledCallback.cjs.map +1 -0
- package/dist/throttler/useThrottledCallback.d.cts +46 -0
- package/dist/throttler/useThrottledCallback.d.ts +46 -0
- package/dist/throttler/useThrottledCallback.js +49 -0
- package/dist/throttler/useThrottledCallback.js.map +1 -0
- package/dist/throttler/useThrottledState.cjs +100 -0
- package/dist/throttler/useThrottledState.cjs.map +1 -0
- package/dist/throttler/useThrottledState.d.cts +93 -0
- package/dist/throttler/useThrottledState.d.ts +93 -0
- package/dist/throttler/useThrottledState.js +100 -0
- package/dist/throttler/useThrottledState.js.map +1 -0
- package/dist/throttler/useThrottledValue.cjs +91 -0
- package/dist/throttler/useThrottledValue.cjs.map +1 -0
- package/dist/throttler/useThrottledValue.d.cts +86 -0
- package/dist/throttler/useThrottledValue.d.ts +86 -0
- package/dist/throttler/useThrottledValue.js +91 -0
- package/dist/throttler/useThrottledValue.js.map +1 -0
- package/dist/throttler/useThrottler.cjs +109 -0
- package/dist/throttler/useThrottler.cjs.map +1 -0
- package/dist/throttler/useThrottler.d.cts +103 -0
- package/dist/throttler/useThrottler.d.ts +103 -0
- package/dist/throttler/useThrottler.js +109 -0
- package/dist/throttler/useThrottler.js.map +1 -0
- package/dist/types/index.cjs +9 -0
- package/dist/types/index.d.cts +1 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/index.js +3 -0
- package/dist/utils/index.cjs +9 -0
- package/dist/utils/index.d.cts +1 -0
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/index.js +3 -0
- package/package.json +118 -7
- package/src/async-batcher/index.ts +4 -0
- package/src/async-batcher/useAsyncBatchedCallback.ts +54 -0
- package/src/async-batcher/useAsyncBatcher.ts +198 -0
- package/src/async-debouncer/index.ts +5 -0
- package/src/async-debouncer/useAsyncDebouncedCallback.ts +54 -0
- package/src/async-debouncer/useAsyncDebouncer.ts +184 -0
- package/src/async-queuer/index.ts +4 -0
- package/src/async-queuer/useAsyncQueuedState.ts +165 -0
- package/src/async-queuer/useAsyncQueuer.ts +198 -0
- package/src/async-rate-limiter/index.ts +5 -0
- package/src/async-rate-limiter/useAsyncRateLimitedCallback.ts +69 -0
- package/src/async-rate-limiter/useAsyncRateLimiter.ts +210 -0
- package/src/async-retryer/index.ts +2 -0
- package/src/async-throttler/index.ts +5 -0
- package/src/async-throttler/useAsyncThrottledCallback.ts +52 -0
- package/src/async-throttler/useAsyncThrottler.ts +193 -0
- package/src/batcher/index.ts +4 -0
- package/src/batcher/useBatchedCallback.ts +50 -0
- package/src/batcher/useBatcher.ts +150 -0
- package/src/debouncer/index.ts +7 -0
- package/src/debouncer/useDebouncedCallback.ts +51 -0
- package/src/debouncer/useDebouncedState.ts +109 -0
- package/src/debouncer/useDebouncedValue.ts +112 -0
- package/src/debouncer/useDebouncer.ts +136 -0
- package/src/index.ts +56 -0
- package/src/provider/PacerProvider.tsx +70 -0
- package/src/provider/index.ts +1 -0
- package/src/queuer/index.ts +5 -0
- package/src/queuer/useQueuedState.ts +133 -0
- package/src/queuer/useQueuedValue.ts +129 -0
- package/src/queuer/useQueuer.ts +160 -0
- package/src/rate-limiter/index.ts +6 -0
- package/src/rate-limiter/useRateLimitedCallback.ts +68 -0
- package/src/rate-limiter/useRateLimitedState.ts +124 -0
- package/src/rate-limiter/useRateLimitedValue.ts +110 -0
- package/src/rate-limiter/useRateLimiter.ts +169 -0
- package/src/throttler/index.ts +7 -0
- package/src/throttler/useThrottledCallback.ts +52 -0
- package/src/throttler/useThrottledState.ts +121 -0
- package/src/throttler/useThrottledValue.ts +107 -0
- package/src/throttler/useThrottler.ts +141 -0
- package/src/types/index.ts +1 -0
- package/src/utils/index.ts +1 -0
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { PreactDebouncer } from "./useDebouncer.cjs";
|
|
2
|
+
import { DebouncerOptions, DebouncerState } from "@tanstack/pacer/debouncer";
|
|
3
|
+
import { Dispatch, StateUpdater } from "preact/hooks";
|
|
4
|
+
|
|
5
|
+
//#region src/debouncer/useDebouncedValue.d.ts
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* A Preact hook that creates a debounced value that updates only after a specified delay.
|
|
9
|
+
* Unlike useDebouncedState, this hook automatically tracks changes to the input value
|
|
10
|
+
* and updates the debounced value accordingly.
|
|
11
|
+
*
|
|
12
|
+
* The debounced value will only update after the specified wait time has elapsed since
|
|
13
|
+
* the last change to the input value. If the input value changes again before the wait
|
|
14
|
+
* time expires, the timer resets and starts waiting again.
|
|
15
|
+
*
|
|
16
|
+
* This is useful for deriving debounced values from props or state that change frequently,
|
|
17
|
+
* like search queries or form inputs, where you want to limit how often downstream effects
|
|
18
|
+
* or calculations occur.
|
|
19
|
+
*
|
|
20
|
+
* The hook returns the current debounced value and the underlying debouncer instance.
|
|
21
|
+
* The debouncer instance can be used to access additional functionality like cancellation
|
|
22
|
+
* and execution counts.
|
|
23
|
+
*
|
|
24
|
+
* ## State Management and Selector
|
|
25
|
+
*
|
|
26
|
+
* The hook uses TanStack Store for reactive state management via the underlying debouncer instance.
|
|
27
|
+
* The `selector` parameter allows you to specify which debouncer state changes will trigger a re-render,
|
|
28
|
+
* optimizing performance by preventing unnecessary re-renders when irrelevant state changes occur.
|
|
29
|
+
*
|
|
30
|
+
* **By default, there will be no reactive state subscriptions** and you must opt-in to state
|
|
31
|
+
* tracking by providing a selector function. This prevents unnecessary re-renders and gives you
|
|
32
|
+
* full control over when your component updates. Only when you provide a selector will the
|
|
33
|
+
* component re-render when the selected state values change.
|
|
34
|
+
*
|
|
35
|
+
* Available debouncer state properties:
|
|
36
|
+
* - `canLeadingExecute`: Whether the debouncer can execute on the leading edge
|
|
37
|
+
* - `executionCount`: Number of function executions that have been completed
|
|
38
|
+
* - `isPending`: Whether the debouncer is waiting for the timeout to trigger execution
|
|
39
|
+
* - `lastArgs`: The arguments from the most recent call to maybeExecute
|
|
40
|
+
* - `status`: Current execution status ('disabled' | 'idle' | 'pending')
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```tsx
|
|
44
|
+
* // Default behavior - no reactive state subscriptions
|
|
45
|
+
* const [searchQuery, setSearchQuery] = useState('');
|
|
46
|
+
* const [debouncedQuery, debouncer] = useDebouncedValue(searchQuery, {
|
|
47
|
+
* wait: 500 // Wait 500ms after last change
|
|
48
|
+
* });
|
|
49
|
+
*
|
|
50
|
+
* // Opt-in to re-render when pending state changes (optimized for loading indicators)
|
|
51
|
+
* const [debouncedQuery, debouncer] = useDebouncedValue(
|
|
52
|
+
* searchQuery,
|
|
53
|
+
* { wait: 500 },
|
|
54
|
+
* (state) => ({ isPending: state.isPending })
|
|
55
|
+
* );
|
|
56
|
+
*
|
|
57
|
+
* // Opt-in to re-render when execution count changes (optimized for tracking executions)
|
|
58
|
+
* const [debouncedQuery, debouncer] = useDebouncedValue(
|
|
59
|
+
* searchQuery,
|
|
60
|
+
* { wait: 500 },
|
|
61
|
+
* (state) => ({ executionCount: state.executionCount })
|
|
62
|
+
* );
|
|
63
|
+
*
|
|
64
|
+
* // Opt-in to re-render when debouncing status changes (optimized for status display)
|
|
65
|
+
* const [debouncedQuery, debouncer] = useDebouncedValue(
|
|
66
|
+
* searchQuery,
|
|
67
|
+
* { wait: 500 },
|
|
68
|
+
* (state) => ({
|
|
69
|
+
* status: state.status,
|
|
70
|
+
* canLeadingExecute: state.canLeadingExecute
|
|
71
|
+
* })
|
|
72
|
+
* );
|
|
73
|
+
*
|
|
74
|
+
* // debouncedQuery will update 500ms after searchQuery stops changing
|
|
75
|
+
* useEffect(() => {
|
|
76
|
+
* fetchSearchResults(debouncedQuery);
|
|
77
|
+
* }, [debouncedQuery]);
|
|
78
|
+
*
|
|
79
|
+
* // Handle input changes
|
|
80
|
+
* const handleChange = (e) => {
|
|
81
|
+
* setSearchQuery(e.target.value);
|
|
82
|
+
* };
|
|
83
|
+
*
|
|
84
|
+
* // Access the selected debouncer state (will be empty object {} unless selector provided)
|
|
85
|
+
* const { isPending, executionCount } = debouncer.state;
|
|
86
|
+
* ```
|
|
87
|
+
*/
|
|
88
|
+
declare function useDebouncedValue<TValue, TSelected = DebouncerState<Dispatch<StateUpdater<TValue>>>>(value: TValue, options: DebouncerOptions<Dispatch<StateUpdater<TValue>>>, selector?: (state: DebouncerState<Dispatch<StateUpdater<TValue>>>) => TSelected): [TValue, PreactDebouncer<Dispatch<StateUpdater<TValue>>, TSelected>];
|
|
89
|
+
//#endregion
|
|
90
|
+
export { useDebouncedValue };
|
|
91
|
+
//# sourceMappingURL=useDebouncedValue.d.cts.map
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { PreactDebouncer } from "./useDebouncer.js";
|
|
2
|
+
import { Dispatch, StateUpdater } from "preact/hooks";
|
|
3
|
+
import { DebouncerOptions, DebouncerState } from "@tanstack/pacer/debouncer";
|
|
4
|
+
|
|
5
|
+
//#region src/debouncer/useDebouncedValue.d.ts
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* A Preact hook that creates a debounced value that updates only after a specified delay.
|
|
9
|
+
* Unlike useDebouncedState, this hook automatically tracks changes to the input value
|
|
10
|
+
* and updates the debounced value accordingly.
|
|
11
|
+
*
|
|
12
|
+
* The debounced value will only update after the specified wait time has elapsed since
|
|
13
|
+
* the last change to the input value. If the input value changes again before the wait
|
|
14
|
+
* time expires, the timer resets and starts waiting again.
|
|
15
|
+
*
|
|
16
|
+
* This is useful for deriving debounced values from props or state that change frequently,
|
|
17
|
+
* like search queries or form inputs, where you want to limit how often downstream effects
|
|
18
|
+
* or calculations occur.
|
|
19
|
+
*
|
|
20
|
+
* The hook returns the current debounced value and the underlying debouncer instance.
|
|
21
|
+
* The debouncer instance can be used to access additional functionality like cancellation
|
|
22
|
+
* and execution counts.
|
|
23
|
+
*
|
|
24
|
+
* ## State Management and Selector
|
|
25
|
+
*
|
|
26
|
+
* The hook uses TanStack Store for reactive state management via the underlying debouncer instance.
|
|
27
|
+
* The `selector` parameter allows you to specify which debouncer state changes will trigger a re-render,
|
|
28
|
+
* optimizing performance by preventing unnecessary re-renders when irrelevant state changes occur.
|
|
29
|
+
*
|
|
30
|
+
* **By default, there will be no reactive state subscriptions** and you must opt-in to state
|
|
31
|
+
* tracking by providing a selector function. This prevents unnecessary re-renders and gives you
|
|
32
|
+
* full control over when your component updates. Only when you provide a selector will the
|
|
33
|
+
* component re-render when the selected state values change.
|
|
34
|
+
*
|
|
35
|
+
* Available debouncer state properties:
|
|
36
|
+
* - `canLeadingExecute`: Whether the debouncer can execute on the leading edge
|
|
37
|
+
* - `executionCount`: Number of function executions that have been completed
|
|
38
|
+
* - `isPending`: Whether the debouncer is waiting for the timeout to trigger execution
|
|
39
|
+
* - `lastArgs`: The arguments from the most recent call to maybeExecute
|
|
40
|
+
* - `status`: Current execution status ('disabled' | 'idle' | 'pending')
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```tsx
|
|
44
|
+
* // Default behavior - no reactive state subscriptions
|
|
45
|
+
* const [searchQuery, setSearchQuery] = useState('');
|
|
46
|
+
* const [debouncedQuery, debouncer] = useDebouncedValue(searchQuery, {
|
|
47
|
+
* wait: 500 // Wait 500ms after last change
|
|
48
|
+
* });
|
|
49
|
+
*
|
|
50
|
+
* // Opt-in to re-render when pending state changes (optimized for loading indicators)
|
|
51
|
+
* const [debouncedQuery, debouncer] = useDebouncedValue(
|
|
52
|
+
* searchQuery,
|
|
53
|
+
* { wait: 500 },
|
|
54
|
+
* (state) => ({ isPending: state.isPending })
|
|
55
|
+
* );
|
|
56
|
+
*
|
|
57
|
+
* // Opt-in to re-render when execution count changes (optimized for tracking executions)
|
|
58
|
+
* const [debouncedQuery, debouncer] = useDebouncedValue(
|
|
59
|
+
* searchQuery,
|
|
60
|
+
* { wait: 500 },
|
|
61
|
+
* (state) => ({ executionCount: state.executionCount })
|
|
62
|
+
* );
|
|
63
|
+
*
|
|
64
|
+
* // Opt-in to re-render when debouncing status changes (optimized for status display)
|
|
65
|
+
* const [debouncedQuery, debouncer] = useDebouncedValue(
|
|
66
|
+
* searchQuery,
|
|
67
|
+
* { wait: 500 },
|
|
68
|
+
* (state) => ({
|
|
69
|
+
* status: state.status,
|
|
70
|
+
* canLeadingExecute: state.canLeadingExecute
|
|
71
|
+
* })
|
|
72
|
+
* );
|
|
73
|
+
*
|
|
74
|
+
* // debouncedQuery will update 500ms after searchQuery stops changing
|
|
75
|
+
* useEffect(() => {
|
|
76
|
+
* fetchSearchResults(debouncedQuery);
|
|
77
|
+
* }, [debouncedQuery]);
|
|
78
|
+
*
|
|
79
|
+
* // Handle input changes
|
|
80
|
+
* const handleChange = (e) => {
|
|
81
|
+
* setSearchQuery(e.target.value);
|
|
82
|
+
* };
|
|
83
|
+
*
|
|
84
|
+
* // Access the selected debouncer state (will be empty object {} unless selector provided)
|
|
85
|
+
* const { isPending, executionCount } = debouncer.state;
|
|
86
|
+
* ```
|
|
87
|
+
*/
|
|
88
|
+
declare function useDebouncedValue<TValue, TSelected = DebouncerState<Dispatch<StateUpdater<TValue>>>>(value: TValue, options: DebouncerOptions<Dispatch<StateUpdater<TValue>>>, selector?: (state: DebouncerState<Dispatch<StateUpdater<TValue>>>) => TSelected): [TValue, PreactDebouncer<Dispatch<StateUpdater<TValue>>, TSelected>];
|
|
89
|
+
//#endregion
|
|
90
|
+
export { useDebouncedValue };
|
|
91
|
+
//# sourceMappingURL=useDebouncedValue.d.ts.map
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { useDebouncedState } from "./useDebouncedState.js";
|
|
2
|
+
import { useEffect } from "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] = useDebouncedState(value, options, selector);
|
|
88
|
+
useEffect(() => {
|
|
89
|
+
setDebouncedValue(value);
|
|
90
|
+
}, [value, setDebouncedValue]);
|
|
91
|
+
return [debouncedValue, debouncer];
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
//#endregion
|
|
95
|
+
export { useDebouncedValue };
|
|
96
|
+
//# sourceMappingURL=useDebouncedValue.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useDebouncedValue.js","names":[],"sources":["../../src/debouncer/useDebouncedValue.ts"],"sourcesContent":["import { useEffect } from 'preact/hooks'\nimport { useDebouncedState } from './useDebouncedState'\nimport type { Dispatch, StateUpdater } from 'preact/hooks'\nimport type { PreactDebouncer } from './useDebouncer'\nimport type {\n DebouncerOptions,\n DebouncerState,\n} 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: DebouncerOptions<Dispatch<StateUpdater<TValue>>>,\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":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA0FA,SAAgB,kBAId,OACA,SACA,UAGsE;CACtE,MAAM,CAAC,gBAAgB,mBAAmB,aAAa,kBACrD,OACA,SACA,SACD;AAED,iBAAgB;AACb,EAAC,kBAA8C,MAAM;IACrD,CAAC,OAAO,kBAAkB,CAAC;AAE9B,QAAO,CAAC,gBAAgB,UAAU"}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
const require_PacerProvider = require('../provider/PacerProvider.cjs');
|
|
2
|
+
let preact_hooks = require("preact/hooks");
|
|
3
|
+
let __tanstack_preact_store = require("@tanstack/preact-store");
|
|
4
|
+
let __tanstack_pacer_debouncer = require("@tanstack/pacer/debouncer");
|
|
5
|
+
|
|
6
|
+
//#region src/debouncer/useDebouncer.ts
|
|
7
|
+
/**
|
|
8
|
+
* A Preact hook that creates and manages a Debouncer instance.
|
|
9
|
+
*
|
|
10
|
+
* This is a lower-level hook that provides direct access to the Debouncer's functionality without
|
|
11
|
+
* any built-in state management. This allows you to integrate it with any state management solution
|
|
12
|
+
* you prefer (useState, Redux, Zustand, etc.).
|
|
13
|
+
*
|
|
14
|
+
* This hook provides debouncing functionality to limit how often a function can be called,
|
|
15
|
+
* waiting for a specified delay before executing the latest call. This is useful for handling
|
|
16
|
+
* frequent events like window resizing, scroll events, or real-time search inputs.
|
|
17
|
+
*
|
|
18
|
+
* The debouncer will only execute the function after the specified wait time has elapsed
|
|
19
|
+
* since the last call. If the function is called again before the wait time expires, the
|
|
20
|
+
* timer resets and starts waiting again.
|
|
21
|
+
*
|
|
22
|
+
* ## State Management and Selector
|
|
23
|
+
*
|
|
24
|
+
* The hook uses TanStack Store for reactive state management. The `selector` parameter allows you
|
|
25
|
+
* to specify which state changes will trigger a re-render, optimizing performance by preventing
|
|
26
|
+
* 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 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 searchDebouncer = useDebouncer(
|
|
44
|
+
* (query: string) => fetchSearchResults(query),
|
|
45
|
+
* { wait: 500 }
|
|
46
|
+
* );
|
|
47
|
+
*
|
|
48
|
+
* // Opt-in to re-render when isPending changes (optimized for loading states)
|
|
49
|
+
* const searchDebouncer = useDebouncer(
|
|
50
|
+
* (query: string) => fetchSearchResults(query),
|
|
51
|
+
* { wait: 500 },
|
|
52
|
+
* (state) => ({ isPending: state.isPending })
|
|
53
|
+
* );
|
|
54
|
+
*
|
|
55
|
+
* // Opt-in to re-render when executionCount changes (optimized for tracking execution)
|
|
56
|
+
* const searchDebouncer = useDebouncer(
|
|
57
|
+
* (query: string) => fetchSearchResults(query),
|
|
58
|
+
* { wait: 500 },
|
|
59
|
+
* (state) => ({ executionCount: state.executionCount })
|
|
60
|
+
* );
|
|
61
|
+
*
|
|
62
|
+
* // Multiple state properties - re-render when any of these change
|
|
63
|
+
* const searchDebouncer = useDebouncer(
|
|
64
|
+
* (query: string) => fetchSearchResults(query),
|
|
65
|
+
* { wait: 500 },
|
|
66
|
+
* (state) => ({
|
|
67
|
+
* isPending: state.isPending,
|
|
68
|
+
* executionCount: state.executionCount,
|
|
69
|
+
* status: state.status
|
|
70
|
+
* })
|
|
71
|
+
* );
|
|
72
|
+
*
|
|
73
|
+
* // In an event handler
|
|
74
|
+
* const handleChange = (e) => {
|
|
75
|
+
* searchDebouncer.maybeExecute(e.target.value);
|
|
76
|
+
* };
|
|
77
|
+
*
|
|
78
|
+
* // Access the selected state (will be empty object {} unless selector provided)
|
|
79
|
+
* const { isPending } = searchDebouncer.state;
|
|
80
|
+
* ```
|
|
81
|
+
*/
|
|
82
|
+
function useDebouncer(fn, options, selector = () => ({})) {
|
|
83
|
+
const mergedOptions = {
|
|
84
|
+
...require_PacerProvider.useDefaultPacerOptions().debouncer,
|
|
85
|
+
...options
|
|
86
|
+
};
|
|
87
|
+
const [debouncer] = (0, preact_hooks.useState)(() => new __tanstack_pacer_debouncer.Debouncer(fn, mergedOptions));
|
|
88
|
+
debouncer.fn = fn;
|
|
89
|
+
debouncer.setOptions(mergedOptions);
|
|
90
|
+
(0, preact_hooks.useEffect)(() => {
|
|
91
|
+
return () => {
|
|
92
|
+
debouncer.cancel();
|
|
93
|
+
};
|
|
94
|
+
}, [debouncer]);
|
|
95
|
+
const state = (0, __tanstack_preact_store.useStore)(debouncer.store, selector);
|
|
96
|
+
return (0, preact_hooks.useMemo)(() => ({
|
|
97
|
+
...debouncer,
|
|
98
|
+
state
|
|
99
|
+
}), [debouncer, state]);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
//#endregion
|
|
103
|
+
exports.useDebouncer = useDebouncer;
|
|
104
|
+
//# sourceMappingURL=useDebouncer.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useDebouncer.cjs","names":["useDefaultPacerOptions","Debouncer"],"sources":["../../src/debouncer/useDebouncer.ts"],"sourcesContent":["import { useEffect, useMemo, useState } from 'preact/hooks'\nimport { Debouncer } from '@tanstack/pacer/debouncer'\nimport { useStore } from '@tanstack/preact-store'\nimport { useDefaultPacerOptions } from '../provider/PacerProvider'\nimport type { Store } from '@tanstack/preact-store'\nimport type {\n DebouncerOptions,\n DebouncerState,\n} from '@tanstack/pacer/debouncer'\nimport type { AnyFunction } from '@tanstack/pacer/types'\n\nexport interface PreactDebouncer<\n TFn extends AnyFunction,\n TSelected = {},\n> extends Omit<Debouncer<TFn>, 'store'> {\n /**\n * Reactive state that will be updated and re-rendered when the debouncer state changes\n *\n * Use this instead of `debouncer.store.state`\n */\n readonly state: Readonly<TSelected>\n /**\n * @deprecated Use `debouncer.state` instead of `debouncer.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 `useStore` hook internally.\n * Although, you can make the state reactive by using the `useStore` in your own usage.\n */\n readonly store: Store<Readonly<DebouncerState<TFn>>>\n}\n\n/**\n * A Preact hook that creates and manages a Debouncer instance.\n *\n * This is a lower-level hook that provides direct access to the Debouncer'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.).\n *\n * This hook provides debouncing functionality to limit how often a function can be called,\n * waiting for a specified delay before executing the latest call. This is useful for handling\n * frequent events like window resizing, scroll events, or real-time search inputs.\n *\n * The debouncer will only execute the function after the specified wait time has elapsed\n * since the last call. If the function is called again before the wait time expires, the\n * timer resets and starts waiting again.\n *\n * ## State Management and Selector\n *\n * The hook uses TanStack Store for reactive state management. The `selector` parameter allows you\n * to specify which state changes will trigger a re-render, optimizing performance by preventing\n * 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 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 searchDebouncer = useDebouncer(\n * (query: string) => fetchSearchResults(query),\n * { wait: 500 }\n * );\n *\n * // Opt-in to re-render when isPending changes (optimized for loading states)\n * const searchDebouncer = useDebouncer(\n * (query: string) => fetchSearchResults(query),\n * { wait: 500 },\n * (state) => ({ isPending: state.isPending })\n * );\n *\n * // Opt-in to re-render when executionCount changes (optimized for tracking execution)\n * const searchDebouncer = useDebouncer(\n * (query: string) => fetchSearchResults(query),\n * { wait: 500 },\n * (state) => ({ executionCount: state.executionCount })\n * );\n *\n * // Multiple state properties - re-render when any of these change\n * const searchDebouncer = useDebouncer(\n * (query: string) => fetchSearchResults(query),\n * { wait: 500 },\n * (state) => ({\n * isPending: state.isPending,\n * executionCount: state.executionCount,\n * status: state.status\n * })\n * );\n *\n * // In an event handler\n * const handleChange = (e) => {\n * searchDebouncer.maybeExecute(e.target.value);\n * };\n *\n * // Access the selected state (will be empty object {} unless selector provided)\n * const { isPending } = searchDebouncer.state;\n * ```\n */\nexport function useDebouncer<TFn extends AnyFunction, TSelected = {}>(\n fn: TFn,\n options: DebouncerOptions<TFn>,\n selector: (state: DebouncerState<TFn>) => TSelected = () => ({}) as TSelected,\n): PreactDebouncer<TFn, TSelected> {\n const mergedOptions = {\n ...useDefaultPacerOptions().debouncer,\n ...options,\n } as DebouncerOptions<TFn>\n\n const [debouncer] = useState(() => new Debouncer(fn, mergedOptions))\n\n debouncer.fn = fn\n debouncer.setOptions(mergedOptions)\n\n useEffect(() => {\n return () => {\n debouncer.cancel()\n }\n }, [debouncer])\n\n const state = useStore(debouncer.store, selector)\n\n return useMemo(\n () =>\n ({\n ...debouncer,\n state,\n }) as PreactDebouncer<TFn, TSelected>, // omit `store` in favor of `state`\n [debouncer, state],\n )\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAwGA,SAAgB,aACd,IACA,SACA,kBAA6D,EAAE,GAC9B;CACjC,MAAM,gBAAgB;EACpB,GAAGA,8CAAwB,CAAC;EAC5B,GAAG;EACJ;CAED,MAAM,CAAC,8CAA4B,IAAIC,qCAAU,IAAI,cAAc,CAAC;AAEpE,WAAU,KAAK;AACf,WAAU,WAAW,cAAc;AAEnC,mCAAgB;AACd,eAAa;AACX,aAAU,QAAQ;;IAEnB,CAAC,UAAU,CAAC;CAEf,MAAM,8CAAiB,UAAU,OAAO,SAAS;AAEjD,yCAEK;EACC,GAAG;EACH;EACD,GACH,CAAC,WAAW,MAAM,CACnB"}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { Store } from "@tanstack/preact-store";
|
|
2
|
+
import { AnyFunction } from "@tanstack/pacer/types";
|
|
3
|
+
import { Debouncer, DebouncerOptions, DebouncerState } from "@tanstack/pacer/debouncer";
|
|
4
|
+
|
|
5
|
+
//#region src/debouncer/useDebouncer.d.ts
|
|
6
|
+
interface PreactDebouncer<TFn extends AnyFunction, TSelected = {}> extends Omit<Debouncer<TFn>, 'store'> {
|
|
7
|
+
/**
|
|
8
|
+
* Reactive state that will be updated and re-rendered when the debouncer state changes
|
|
9
|
+
*
|
|
10
|
+
* Use this instead of `debouncer.store.state`
|
|
11
|
+
*/
|
|
12
|
+
readonly state: Readonly<TSelected>;
|
|
13
|
+
/**
|
|
14
|
+
* @deprecated Use `debouncer.state` instead of `debouncer.store.state` if you want to read reactive state.
|
|
15
|
+
* The state on the store object is not reactive, as it has not been wrapped in a `useStore` hook internally.
|
|
16
|
+
* Although, you can make the state reactive by using the `useStore` in your own usage.
|
|
17
|
+
*/
|
|
18
|
+
readonly store: Store<Readonly<DebouncerState<TFn>>>;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* A Preact hook that creates and manages a Debouncer instance.
|
|
22
|
+
*
|
|
23
|
+
* This is a lower-level hook that provides direct access to the Debouncer's functionality without
|
|
24
|
+
* any built-in state management. This allows you to integrate it with any state management solution
|
|
25
|
+
* you prefer (useState, Redux, Zustand, etc.).
|
|
26
|
+
*
|
|
27
|
+
* This hook provides debouncing functionality to limit how often a function can be called,
|
|
28
|
+
* waiting for a specified delay before executing the latest call. This is useful for handling
|
|
29
|
+
* frequent events like window resizing, scroll events, or real-time search inputs.
|
|
30
|
+
*
|
|
31
|
+
* The debouncer will only execute the function after the specified wait time has elapsed
|
|
32
|
+
* since the last call. If the function is called again before the wait time expires, the
|
|
33
|
+
* timer resets and starts waiting again.
|
|
34
|
+
*
|
|
35
|
+
* ## State Management and Selector
|
|
36
|
+
*
|
|
37
|
+
* The hook uses TanStack Store for reactive state management. The `selector` parameter allows you
|
|
38
|
+
* to specify which state changes will trigger a re-render, optimizing performance by preventing
|
|
39
|
+
* unnecessary re-renders when irrelevant state changes occur.
|
|
40
|
+
*
|
|
41
|
+
* **By default, there will be no reactive state subscriptions** and you must opt-in to state
|
|
42
|
+
* tracking by providing a selector function. This prevents unnecessary re-renders and gives you
|
|
43
|
+
* full control over when your component updates. Only when you provide a selector will the
|
|
44
|
+
* component re-render when the selected state values change.
|
|
45
|
+
*
|
|
46
|
+
* Available state properties:
|
|
47
|
+
* - `canLeadingExecute`: Whether the debouncer can execute on the leading edge
|
|
48
|
+
* - `executionCount`: Number of function executions that have been completed
|
|
49
|
+
* - `isPending`: Whether the debouncer is waiting for the timeout to trigger execution
|
|
50
|
+
* - `lastArgs`: The arguments from the most recent call to maybeExecute
|
|
51
|
+
* - `status`: Current execution status ('disabled' | 'idle' | 'pending')
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* ```tsx
|
|
55
|
+
* // Default behavior - no reactive state subscriptions
|
|
56
|
+
* const searchDebouncer = useDebouncer(
|
|
57
|
+
* (query: string) => fetchSearchResults(query),
|
|
58
|
+
* { wait: 500 }
|
|
59
|
+
* );
|
|
60
|
+
*
|
|
61
|
+
* // Opt-in to re-render when isPending changes (optimized for loading states)
|
|
62
|
+
* const searchDebouncer = useDebouncer(
|
|
63
|
+
* (query: string) => fetchSearchResults(query),
|
|
64
|
+
* { wait: 500 },
|
|
65
|
+
* (state) => ({ isPending: state.isPending })
|
|
66
|
+
* );
|
|
67
|
+
*
|
|
68
|
+
* // Opt-in to re-render when executionCount changes (optimized for tracking execution)
|
|
69
|
+
* const searchDebouncer = useDebouncer(
|
|
70
|
+
* (query: string) => fetchSearchResults(query),
|
|
71
|
+
* { wait: 500 },
|
|
72
|
+
* (state) => ({ executionCount: state.executionCount })
|
|
73
|
+
* );
|
|
74
|
+
*
|
|
75
|
+
* // Multiple state properties - re-render when any of these change
|
|
76
|
+
* const searchDebouncer = useDebouncer(
|
|
77
|
+
* (query: string) => fetchSearchResults(query),
|
|
78
|
+
* { wait: 500 },
|
|
79
|
+
* (state) => ({
|
|
80
|
+
* isPending: state.isPending,
|
|
81
|
+
* executionCount: state.executionCount,
|
|
82
|
+
* status: state.status
|
|
83
|
+
* })
|
|
84
|
+
* );
|
|
85
|
+
*
|
|
86
|
+
* // In an event handler
|
|
87
|
+
* const handleChange = (e) => {
|
|
88
|
+
* searchDebouncer.maybeExecute(e.target.value);
|
|
89
|
+
* };
|
|
90
|
+
*
|
|
91
|
+
* // Access the selected state (will be empty object {} unless selector provided)
|
|
92
|
+
* const { isPending } = searchDebouncer.state;
|
|
93
|
+
* ```
|
|
94
|
+
*/
|
|
95
|
+
declare function useDebouncer<TFn extends AnyFunction, TSelected = {}>(fn: TFn, options: DebouncerOptions<TFn>, selector?: (state: DebouncerState<TFn>) => TSelected): PreactDebouncer<TFn, TSelected>;
|
|
96
|
+
//#endregion
|
|
97
|
+
export { PreactDebouncer, useDebouncer };
|
|
98
|
+
//# sourceMappingURL=useDebouncer.d.cts.map
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { Store } from "@tanstack/preact-store";
|
|
2
|
+
import { Debouncer, DebouncerOptions, DebouncerState } from "@tanstack/pacer/debouncer";
|
|
3
|
+
import { AnyFunction } from "@tanstack/pacer/types";
|
|
4
|
+
|
|
5
|
+
//#region src/debouncer/useDebouncer.d.ts
|
|
6
|
+
interface PreactDebouncer<TFn extends AnyFunction, TSelected = {}> extends Omit<Debouncer<TFn>, 'store'> {
|
|
7
|
+
/**
|
|
8
|
+
* Reactive state that will be updated and re-rendered when the debouncer state changes
|
|
9
|
+
*
|
|
10
|
+
* Use this instead of `debouncer.store.state`
|
|
11
|
+
*/
|
|
12
|
+
readonly state: Readonly<TSelected>;
|
|
13
|
+
/**
|
|
14
|
+
* @deprecated Use `debouncer.state` instead of `debouncer.store.state` if you want to read reactive state.
|
|
15
|
+
* The state on the store object is not reactive, as it has not been wrapped in a `useStore` hook internally.
|
|
16
|
+
* Although, you can make the state reactive by using the `useStore` in your own usage.
|
|
17
|
+
*/
|
|
18
|
+
readonly store: Store<Readonly<DebouncerState<TFn>>>;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* A Preact hook that creates and manages a Debouncer instance.
|
|
22
|
+
*
|
|
23
|
+
* This is a lower-level hook that provides direct access to the Debouncer's functionality without
|
|
24
|
+
* any built-in state management. This allows you to integrate it with any state management solution
|
|
25
|
+
* you prefer (useState, Redux, Zustand, etc.).
|
|
26
|
+
*
|
|
27
|
+
* This hook provides debouncing functionality to limit how often a function can be called,
|
|
28
|
+
* waiting for a specified delay before executing the latest call. This is useful for handling
|
|
29
|
+
* frequent events like window resizing, scroll events, or real-time search inputs.
|
|
30
|
+
*
|
|
31
|
+
* The debouncer will only execute the function after the specified wait time has elapsed
|
|
32
|
+
* since the last call. If the function is called again before the wait time expires, the
|
|
33
|
+
* timer resets and starts waiting again.
|
|
34
|
+
*
|
|
35
|
+
* ## State Management and Selector
|
|
36
|
+
*
|
|
37
|
+
* The hook uses TanStack Store for reactive state management. The `selector` parameter allows you
|
|
38
|
+
* to specify which state changes will trigger a re-render, optimizing performance by preventing
|
|
39
|
+
* unnecessary re-renders when irrelevant state changes occur.
|
|
40
|
+
*
|
|
41
|
+
* **By default, there will be no reactive state subscriptions** and you must opt-in to state
|
|
42
|
+
* tracking by providing a selector function. This prevents unnecessary re-renders and gives you
|
|
43
|
+
* full control over when your component updates. Only when you provide a selector will the
|
|
44
|
+
* component re-render when the selected state values change.
|
|
45
|
+
*
|
|
46
|
+
* Available state properties:
|
|
47
|
+
* - `canLeadingExecute`: Whether the debouncer can execute on the leading edge
|
|
48
|
+
* - `executionCount`: Number of function executions that have been completed
|
|
49
|
+
* - `isPending`: Whether the debouncer is waiting for the timeout to trigger execution
|
|
50
|
+
* - `lastArgs`: The arguments from the most recent call to maybeExecute
|
|
51
|
+
* - `status`: Current execution status ('disabled' | 'idle' | 'pending')
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* ```tsx
|
|
55
|
+
* // Default behavior - no reactive state subscriptions
|
|
56
|
+
* const searchDebouncer = useDebouncer(
|
|
57
|
+
* (query: string) => fetchSearchResults(query),
|
|
58
|
+
* { wait: 500 }
|
|
59
|
+
* );
|
|
60
|
+
*
|
|
61
|
+
* // Opt-in to re-render when isPending changes (optimized for loading states)
|
|
62
|
+
* const searchDebouncer = useDebouncer(
|
|
63
|
+
* (query: string) => fetchSearchResults(query),
|
|
64
|
+
* { wait: 500 },
|
|
65
|
+
* (state) => ({ isPending: state.isPending })
|
|
66
|
+
* );
|
|
67
|
+
*
|
|
68
|
+
* // Opt-in to re-render when executionCount changes (optimized for tracking execution)
|
|
69
|
+
* const searchDebouncer = useDebouncer(
|
|
70
|
+
* (query: string) => fetchSearchResults(query),
|
|
71
|
+
* { wait: 500 },
|
|
72
|
+
* (state) => ({ executionCount: state.executionCount })
|
|
73
|
+
* );
|
|
74
|
+
*
|
|
75
|
+
* // Multiple state properties - re-render when any of these change
|
|
76
|
+
* const searchDebouncer = useDebouncer(
|
|
77
|
+
* (query: string) => fetchSearchResults(query),
|
|
78
|
+
* { wait: 500 },
|
|
79
|
+
* (state) => ({
|
|
80
|
+
* isPending: state.isPending,
|
|
81
|
+
* executionCount: state.executionCount,
|
|
82
|
+
* status: state.status
|
|
83
|
+
* })
|
|
84
|
+
* );
|
|
85
|
+
*
|
|
86
|
+
* // In an event handler
|
|
87
|
+
* const handleChange = (e) => {
|
|
88
|
+
* searchDebouncer.maybeExecute(e.target.value);
|
|
89
|
+
* };
|
|
90
|
+
*
|
|
91
|
+
* // Access the selected state (will be empty object {} unless selector provided)
|
|
92
|
+
* const { isPending } = searchDebouncer.state;
|
|
93
|
+
* ```
|
|
94
|
+
*/
|
|
95
|
+
declare function useDebouncer<TFn extends AnyFunction, TSelected = {}>(fn: TFn, options: DebouncerOptions<TFn>, selector?: (state: DebouncerState<TFn>) => TSelected): PreactDebouncer<TFn, TSelected>;
|
|
96
|
+
//#endregion
|
|
97
|
+
export { PreactDebouncer, useDebouncer };
|
|
98
|
+
//# sourceMappingURL=useDebouncer.d.ts.map
|