@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,107 @@
|
|
|
1
|
+
import { useEffect } from 'preact/hooks'
|
|
2
|
+
import { useThrottledState } from './useThrottledState'
|
|
3
|
+
import type { Dispatch, StateUpdater } from 'preact/hooks'
|
|
4
|
+
import type { PreactThrottler } from './useThrottler'
|
|
5
|
+
import type {
|
|
6
|
+
ThrottlerOptions,
|
|
7
|
+
ThrottlerState,
|
|
8
|
+
} from '@tanstack/pacer/throttler'
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* A high-level Preact hook that creates a throttled version of a value that updates at most once within a specified time window.
|
|
12
|
+
* This hook uses Preact's useState internally to manage the throttled state.
|
|
13
|
+
*
|
|
14
|
+
* Throttling ensures the value updates occur at a controlled rate regardless of how frequently the input value changes.
|
|
15
|
+
* This is useful for rate-limiting expensive re-renders or API calls that depend on rapidly changing values.
|
|
16
|
+
*
|
|
17
|
+
* The hook returns a tuple containing:
|
|
18
|
+
* - The throttled value that updates according to the leading/trailing edge behavior specified in the options
|
|
19
|
+
* - The throttler instance with control methods
|
|
20
|
+
*
|
|
21
|
+
* For more direct control over throttling behavior without Preact state management,
|
|
22
|
+
* consider using the lower-level useThrottler hook instead.
|
|
23
|
+
*
|
|
24
|
+
* ## State Management and Selector
|
|
25
|
+
*
|
|
26
|
+
* The hook uses TanStack Store for reactive state management via the underlying throttler instance.
|
|
27
|
+
* The `selector` parameter allows you to specify which throttler 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 throttler state properties:
|
|
36
|
+
* - `executionCount`: Number of function executions that have been completed
|
|
37
|
+
* - `lastArgs`: The arguments from the most recent call to maybeExecute
|
|
38
|
+
* - `lastExecutionTime`: Timestamp of the last function execution in milliseconds
|
|
39
|
+
* - `nextExecutionTime`: Timestamp when the next execution can occur in milliseconds
|
|
40
|
+
* - `isPending`: Whether the throttler is waiting for the timeout to trigger execution
|
|
41
|
+
* - `status`: Current execution status ('disabled' | 'idle' | 'pending')
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* ```tsx
|
|
45
|
+
* // Default behavior - no reactive state subscriptions
|
|
46
|
+
* const [throttledValue, throttler] = useThrottledValue(rawValue, { wait: 1000 });
|
|
47
|
+
*
|
|
48
|
+
* // Opt-in to re-render when execution count changes (optimized for tracking executions)
|
|
49
|
+
* const [throttledValue, throttler] = useThrottledValue(
|
|
50
|
+
* rawValue,
|
|
51
|
+
* { wait: 1000 },
|
|
52
|
+
* (state) => ({ executionCount: state.executionCount })
|
|
53
|
+
* );
|
|
54
|
+
*
|
|
55
|
+
* // Opt-in to re-render when throttling state changes (optimized for loading indicators)
|
|
56
|
+
* const [throttledValue, throttler] = useThrottledValue(
|
|
57
|
+
* rawValue,
|
|
58
|
+
* { wait: 1000 },
|
|
59
|
+
* (state) => ({
|
|
60
|
+
* isPending: state.isPending,
|
|
61
|
+
* status: state.status
|
|
62
|
+
* })
|
|
63
|
+
* );
|
|
64
|
+
*
|
|
65
|
+
* // Opt-in to re-render when timing information changes (optimized for timing displays)
|
|
66
|
+
* const [throttledValue, throttler] = useThrottledValue(
|
|
67
|
+
* rawValue,
|
|
68
|
+
* { wait: 1000 },
|
|
69
|
+
* (state) => ({
|
|
70
|
+
* lastExecutionTime: state.lastExecutionTime,
|
|
71
|
+
* nextExecutionTime: state.nextExecutionTime
|
|
72
|
+
* })
|
|
73
|
+
* );
|
|
74
|
+
*
|
|
75
|
+
* // With custom leading/trailing behavior
|
|
76
|
+
* const [throttledValue, throttler] = useThrottledValue(rawValue, {
|
|
77
|
+
* wait: 1000,
|
|
78
|
+
* leading: true, // Update immediately on first change
|
|
79
|
+
* trailing: false // Skip trailing edge updates
|
|
80
|
+
* });
|
|
81
|
+
*
|
|
82
|
+
* // Access the selected throttler state (will be empty object {} unless selector provided)
|
|
83
|
+
* const { executionCount, isPending } = throttler.state;
|
|
84
|
+
* ```
|
|
85
|
+
*/
|
|
86
|
+
export function useThrottledValue<
|
|
87
|
+
TValue,
|
|
88
|
+
TSelected = ThrottlerState<Dispatch<StateUpdater<TValue>>>,
|
|
89
|
+
>(
|
|
90
|
+
value: TValue,
|
|
91
|
+
options: ThrottlerOptions<Dispatch<StateUpdater<TValue>>>,
|
|
92
|
+
selector?: (
|
|
93
|
+
state: ThrottlerState<Dispatch<StateUpdater<TValue>>>,
|
|
94
|
+
) => TSelected,
|
|
95
|
+
): [TValue, PreactThrottler<Dispatch<StateUpdater<TValue>>, TSelected>] {
|
|
96
|
+
const [throttledValue, setThrottledValue, throttler] = useThrottledState(
|
|
97
|
+
value,
|
|
98
|
+
options,
|
|
99
|
+
selector,
|
|
100
|
+
)
|
|
101
|
+
|
|
102
|
+
useEffect(() => {
|
|
103
|
+
;(setThrottledValue as (value: TValue) => void)(value)
|
|
104
|
+
}, [value, setThrottledValue])
|
|
105
|
+
|
|
106
|
+
return [throttledValue, throttler]
|
|
107
|
+
}
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import { useEffect, useMemo, useState } from 'preact/hooks'
|
|
2
|
+
import { Throttler } from '@tanstack/pacer/throttler'
|
|
3
|
+
import { useStore } from '@tanstack/preact-store'
|
|
4
|
+
import { useDefaultPacerOptions } from '../provider/PacerProvider'
|
|
5
|
+
import type { Store } from '@tanstack/preact-store'
|
|
6
|
+
import type { AnyFunction } from '@tanstack/pacer/types'
|
|
7
|
+
import type {
|
|
8
|
+
ThrottlerOptions,
|
|
9
|
+
ThrottlerState,
|
|
10
|
+
} from '@tanstack/pacer/throttler'
|
|
11
|
+
|
|
12
|
+
export interface PreactThrottler<
|
|
13
|
+
TFn extends AnyFunction,
|
|
14
|
+
TSelected = {},
|
|
15
|
+
> extends Omit<Throttler<TFn>, 'store'> {
|
|
16
|
+
/**
|
|
17
|
+
* Reactive state that will be updated and re-rendered when the throttler state changes
|
|
18
|
+
*
|
|
19
|
+
* Use this instead of `throttler.store.state`
|
|
20
|
+
*/
|
|
21
|
+
readonly state: Readonly<TSelected>
|
|
22
|
+
/**
|
|
23
|
+
* @deprecated Use `throttler.state` instead of `throttler.store.state` if you want to read reactive state.
|
|
24
|
+
* The state on the store object is not reactive, as it has not been wrapped in a `useStore` hook internally.
|
|
25
|
+
* Although, you can make the state reactive by using the `useStore` in your own usage.
|
|
26
|
+
*/
|
|
27
|
+
readonly store: Store<Readonly<ThrottlerState<TFn>>>
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* A low-level Preact hook that creates a `Throttler` instance that limits how often the provided function can execute.
|
|
32
|
+
*
|
|
33
|
+
* This hook is designed to be flexible and state-management agnostic - it simply returns a throttler instance that
|
|
34
|
+
* you can integrate with any state management solution (useState, Redux, Zustand, Jotai, etc). For a simpler and higher-level hook that
|
|
35
|
+
* integrates directly with Preact's useState, see useThrottledState.
|
|
36
|
+
*
|
|
37
|
+
* Throttling ensures a function executes at most once within a specified time window,
|
|
38
|
+
* regardless of how many times it is called. This is useful for rate-limiting
|
|
39
|
+
* expensive operations or UI updates.
|
|
40
|
+
*
|
|
41
|
+
* ## State Management and Selector
|
|
42
|
+
*
|
|
43
|
+
* The hook uses TanStack Store for reactive state management. The `selector` parameter allows you
|
|
44
|
+
* to specify which state changes will trigger a re-render, optimizing performance by preventing
|
|
45
|
+
* unnecessary re-renders when irrelevant state changes occur.
|
|
46
|
+
*
|
|
47
|
+
* **By default, there will be no reactive state subscriptions** and you must opt-in to state
|
|
48
|
+
* tracking by providing a selector function. This prevents unnecessary re-renders and gives you
|
|
49
|
+
* full control over when your component updates. Only when you provide a selector will the
|
|
50
|
+
* component re-render when the selected state values change.
|
|
51
|
+
*
|
|
52
|
+
* Available state properties:
|
|
53
|
+
* - `executionCount`: Number of function executions that have been completed
|
|
54
|
+
* - `lastArgs`: The arguments from the most recent call to maybeExecute
|
|
55
|
+
* - `lastExecutionTime`: Timestamp of the last function execution in milliseconds
|
|
56
|
+
* - `nextExecutionTime`: Timestamp when the next execution can occur in milliseconds
|
|
57
|
+
* - `isPending`: Whether the throttler is waiting for the timeout to trigger execution
|
|
58
|
+
* - `status`: Current execution status ('disabled' | 'idle' | 'pending')
|
|
59
|
+
*
|
|
60
|
+
* @example
|
|
61
|
+
* ```tsx
|
|
62
|
+
* // Default behavior - no reactive state subscriptions
|
|
63
|
+
* const [value, setValue] = useState(0);
|
|
64
|
+
* const throttler = useThrottler(setValue, { wait: 1000 });
|
|
65
|
+
*
|
|
66
|
+
* // Opt-in to re-render when execution count changes (optimized for tracking executions)
|
|
67
|
+
* const [value, setValue] = useState(0);
|
|
68
|
+
* const throttler = useThrottler(
|
|
69
|
+
* setValue,
|
|
70
|
+
* { wait: 1000 },
|
|
71
|
+
* (state) => ({ executionCount: state.executionCount })
|
|
72
|
+
* );
|
|
73
|
+
*
|
|
74
|
+
* // Opt-in to re-render when throttling state changes (optimized for loading indicators)
|
|
75
|
+
* const [value, setValue] = useState(0);
|
|
76
|
+
* const throttler = useThrottler(
|
|
77
|
+
* setValue,
|
|
78
|
+
* { wait: 1000 },
|
|
79
|
+
* (state) => ({
|
|
80
|
+
* isPending: state.isPending,
|
|
81
|
+
* status: state.status
|
|
82
|
+
* })
|
|
83
|
+
* );
|
|
84
|
+
*
|
|
85
|
+
* // Opt-in to re-render when timing information changes (optimized for timing displays)
|
|
86
|
+
* const [value, setValue] = useState(0);
|
|
87
|
+
* const throttler = useThrottler(
|
|
88
|
+
* setValue,
|
|
89
|
+
* { wait: 1000 },
|
|
90
|
+
* (state) => ({
|
|
91
|
+
* lastExecutionTime: state.lastExecutionTime,
|
|
92
|
+
* nextExecutionTime: state.nextExecutionTime
|
|
93
|
+
* })
|
|
94
|
+
* );
|
|
95
|
+
*
|
|
96
|
+
* // With any state manager
|
|
97
|
+
* const throttler = useThrottler(
|
|
98
|
+
* (value) => stateManager.setState(value),
|
|
99
|
+
* {
|
|
100
|
+
* wait: 2000,
|
|
101
|
+
* leading: true, // Execute immediately on first call
|
|
102
|
+
* trailing: false // Skip trailing edge updates
|
|
103
|
+
* }
|
|
104
|
+
* );
|
|
105
|
+
*
|
|
106
|
+
* // Access the selected state (will be empty object {} unless selector provided)
|
|
107
|
+
* const { executionCount, isPending } = throttler.state;
|
|
108
|
+
* ```
|
|
109
|
+
*/
|
|
110
|
+
export function useThrottler<TFn extends AnyFunction, TSelected = {}>(
|
|
111
|
+
fn: TFn,
|
|
112
|
+
options: ThrottlerOptions<TFn>,
|
|
113
|
+
selector: (state: ThrottlerState<TFn>) => TSelected = () => ({}) as TSelected,
|
|
114
|
+
): PreactThrottler<TFn, TSelected> {
|
|
115
|
+
const mergedOptions = {
|
|
116
|
+
...useDefaultPacerOptions().throttler,
|
|
117
|
+
...options,
|
|
118
|
+
} as ThrottlerOptions<TFn>
|
|
119
|
+
|
|
120
|
+
const [throttler] = useState(() => new Throttler<TFn>(fn, mergedOptions))
|
|
121
|
+
|
|
122
|
+
throttler.fn = fn
|
|
123
|
+
throttler.setOptions(mergedOptions)
|
|
124
|
+
|
|
125
|
+
const state = useStore(throttler.store, selector)
|
|
126
|
+
|
|
127
|
+
useEffect(() => {
|
|
128
|
+
return () => {
|
|
129
|
+
throttler.cancel()
|
|
130
|
+
}
|
|
131
|
+
}, [throttler])
|
|
132
|
+
|
|
133
|
+
return useMemo(
|
|
134
|
+
() =>
|
|
135
|
+
({
|
|
136
|
+
...throttler,
|
|
137
|
+
state,
|
|
138
|
+
}) as PreactThrottler<TFn, TSelected>, // omit `store` in favor of `state`
|
|
139
|
+
[throttler, state],
|
|
140
|
+
)
|
|
141
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '@tanstack/pacer/types'
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '@tanstack/pacer/utils'
|