@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,109 @@
|
|
|
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_throttler = require("@tanstack/pacer/throttler");
|
|
5
|
+
|
|
6
|
+
//#region src/throttler/useThrottler.ts
|
|
7
|
+
/**
|
|
8
|
+
* A low-level Preact hook that creates a `Throttler` instance that limits how often the provided function can execute.
|
|
9
|
+
*
|
|
10
|
+
* This hook is designed to be flexible and state-management agnostic - it simply returns a throttler instance that
|
|
11
|
+
* you can integrate with any state management solution (useState, Redux, Zustand, Jotai, etc). For a simpler and higher-level hook that
|
|
12
|
+
* integrates directly with Preact's useState, see useThrottledState.
|
|
13
|
+
*
|
|
14
|
+
* Throttling ensures a function executes at most once within a specified time window,
|
|
15
|
+
* regardless of how many times it is called. This is useful for rate-limiting
|
|
16
|
+
* expensive operations or UI updates.
|
|
17
|
+
*
|
|
18
|
+
* ## State Management and Selector
|
|
19
|
+
*
|
|
20
|
+
* The hook uses TanStack Store for reactive state management. The `selector` parameter allows you
|
|
21
|
+
* to specify which state changes will trigger a re-render, optimizing performance by preventing
|
|
22
|
+
* unnecessary re-renders when irrelevant state changes occur.
|
|
23
|
+
*
|
|
24
|
+
* **By default, there will be no reactive state subscriptions** and you must opt-in to state
|
|
25
|
+
* tracking by providing a selector function. This prevents unnecessary re-renders and gives you
|
|
26
|
+
* full control over when your component updates. Only when you provide a selector will the
|
|
27
|
+
* component re-render when the selected state values change.
|
|
28
|
+
*
|
|
29
|
+
* Available state properties:
|
|
30
|
+
* - `executionCount`: Number of function executions that have been completed
|
|
31
|
+
* - `lastArgs`: The arguments from the most recent call to maybeExecute
|
|
32
|
+
* - `lastExecutionTime`: Timestamp of the last function execution in milliseconds
|
|
33
|
+
* - `nextExecutionTime`: Timestamp when the next execution can occur in milliseconds
|
|
34
|
+
* - `isPending`: Whether the throttler is waiting for the timeout to trigger execution
|
|
35
|
+
* - `status`: Current execution status ('disabled' | 'idle' | 'pending')
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* ```tsx
|
|
39
|
+
* // Default behavior - no reactive state subscriptions
|
|
40
|
+
* const [value, setValue] = useState(0);
|
|
41
|
+
* const throttler = useThrottler(setValue, { wait: 1000 });
|
|
42
|
+
*
|
|
43
|
+
* // Opt-in to re-render when execution count changes (optimized for tracking executions)
|
|
44
|
+
* const [value, setValue] = useState(0);
|
|
45
|
+
* const throttler = useThrottler(
|
|
46
|
+
* setValue,
|
|
47
|
+
* { wait: 1000 },
|
|
48
|
+
* (state) => ({ executionCount: state.executionCount })
|
|
49
|
+
* );
|
|
50
|
+
*
|
|
51
|
+
* // Opt-in to re-render when throttling state changes (optimized for loading indicators)
|
|
52
|
+
* const [value, setValue] = useState(0);
|
|
53
|
+
* const throttler = useThrottler(
|
|
54
|
+
* setValue,
|
|
55
|
+
* { wait: 1000 },
|
|
56
|
+
* (state) => ({
|
|
57
|
+
* isPending: state.isPending,
|
|
58
|
+
* status: state.status
|
|
59
|
+
* })
|
|
60
|
+
* );
|
|
61
|
+
*
|
|
62
|
+
* // Opt-in to re-render when timing information changes (optimized for timing displays)
|
|
63
|
+
* const [value, setValue] = useState(0);
|
|
64
|
+
* const throttler = useThrottler(
|
|
65
|
+
* setValue,
|
|
66
|
+
* { wait: 1000 },
|
|
67
|
+
* (state) => ({
|
|
68
|
+
* lastExecutionTime: state.lastExecutionTime,
|
|
69
|
+
* nextExecutionTime: state.nextExecutionTime
|
|
70
|
+
* })
|
|
71
|
+
* );
|
|
72
|
+
*
|
|
73
|
+
* // With any state manager
|
|
74
|
+
* const throttler = useThrottler(
|
|
75
|
+
* (value) => stateManager.setState(value),
|
|
76
|
+
* {
|
|
77
|
+
* wait: 2000,
|
|
78
|
+
* leading: true, // Execute immediately on first call
|
|
79
|
+
* trailing: false // Skip trailing edge updates
|
|
80
|
+
* }
|
|
81
|
+
* );
|
|
82
|
+
*
|
|
83
|
+
* // Access the selected state (will be empty object {} unless selector provided)
|
|
84
|
+
* const { executionCount, isPending } = throttler.state;
|
|
85
|
+
* ```
|
|
86
|
+
*/
|
|
87
|
+
function useThrottler(fn, options, selector = () => ({})) {
|
|
88
|
+
const mergedOptions = {
|
|
89
|
+
...require_PacerProvider.useDefaultPacerOptions().throttler,
|
|
90
|
+
...options
|
|
91
|
+
};
|
|
92
|
+
const [throttler] = (0, preact_hooks.useState)(() => new __tanstack_pacer_throttler.Throttler(fn, mergedOptions));
|
|
93
|
+
throttler.fn = fn;
|
|
94
|
+
throttler.setOptions(mergedOptions);
|
|
95
|
+
const state = (0, __tanstack_preact_store.useStore)(throttler.store, selector);
|
|
96
|
+
(0, preact_hooks.useEffect)(() => {
|
|
97
|
+
return () => {
|
|
98
|
+
throttler.cancel();
|
|
99
|
+
};
|
|
100
|
+
}, [throttler]);
|
|
101
|
+
return (0, preact_hooks.useMemo)(() => ({
|
|
102
|
+
...throttler,
|
|
103
|
+
state
|
|
104
|
+
}), [throttler, state]);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
//#endregion
|
|
108
|
+
exports.useThrottler = useThrottler;
|
|
109
|
+
//# sourceMappingURL=useThrottler.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useThrottler.cjs","names":["useDefaultPacerOptions","Throttler"],"sources":["../../src/throttler/useThrottler.ts"],"sourcesContent":["import { useEffect, useMemo, useState } from 'preact/hooks'\nimport { Throttler } from '@tanstack/pacer/throttler'\nimport { useStore } from '@tanstack/preact-store'\nimport { useDefaultPacerOptions } from '../provider/PacerProvider'\nimport type { Store } from '@tanstack/preact-store'\nimport type { AnyFunction } from '@tanstack/pacer/types'\nimport type {\n ThrottlerOptions,\n ThrottlerState,\n} from '@tanstack/pacer/throttler'\n\nexport interface PreactThrottler<\n TFn extends AnyFunction,\n TSelected = {},\n> extends Omit<Throttler<TFn>, 'store'> {\n /**\n * Reactive state that will be updated and re-rendered when the throttler state changes\n *\n * Use this instead of `throttler.store.state`\n */\n readonly state: Readonly<TSelected>\n /**\n * @deprecated Use `throttler.state` instead of `throttler.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<ThrottlerState<TFn>>>\n}\n\n/**\n * A low-level Preact hook that creates a `Throttler` instance that limits how often the provided function can execute.\n *\n * This hook is designed to be flexible and state-management agnostic - it simply returns a throttler instance that\n * you can integrate with any state management solution (useState, Redux, Zustand, Jotai, etc). For a simpler and higher-level hook that\n * integrates directly with Preact's useState, see useThrottledState.\n *\n * Throttling ensures a function executes at most once within a specified time window,\n * regardless of how many times it is called. This is useful for rate-limiting\n * expensive operations or UI updates.\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 * - `executionCount`: Number of function executions that have been completed\n * - `lastArgs`: The arguments from the most recent call to maybeExecute\n * - `lastExecutionTime`: Timestamp of the last function execution in milliseconds\n * - `nextExecutionTime`: Timestamp when the next execution can occur in milliseconds\n * - `isPending`: Whether the throttler is waiting for the timeout to trigger execution\n * - `status`: Current execution status ('disabled' | 'idle' | 'pending')\n *\n * @example\n * ```tsx\n * // Default behavior - no reactive state subscriptions\n * const [value, setValue] = useState(0);\n * const throttler = useThrottler(setValue, { wait: 1000 });\n *\n * // Opt-in to re-render when execution count changes (optimized for tracking executions)\n * const [value, setValue] = useState(0);\n * const throttler = useThrottler(\n * setValue,\n * { wait: 1000 },\n * (state) => ({ executionCount: state.executionCount })\n * );\n *\n * // Opt-in to re-render when throttling state changes (optimized for loading indicators)\n * const [value, setValue] = useState(0);\n * const throttler = useThrottler(\n * setValue,\n * { wait: 1000 },\n * (state) => ({\n * isPending: state.isPending,\n * status: state.status\n * })\n * );\n *\n * // Opt-in to re-render when timing information changes (optimized for timing displays)\n * const [value, setValue] = useState(0);\n * const throttler = useThrottler(\n * setValue,\n * { wait: 1000 },\n * (state) => ({\n * lastExecutionTime: state.lastExecutionTime,\n * nextExecutionTime: state.nextExecutionTime\n * })\n * );\n *\n * // With any state manager\n * const throttler = useThrottler(\n * (value) => stateManager.setState(value),\n * {\n * wait: 2000,\n * leading: true, // Execute immediately on first call\n * trailing: false // Skip trailing edge updates\n * }\n * );\n *\n * // Access the selected state (will be empty object {} unless selector provided)\n * const { executionCount, isPending } = throttler.state;\n * ```\n */\nexport function useThrottler<TFn extends AnyFunction, TSelected = {}>(\n fn: TFn,\n options: ThrottlerOptions<TFn>,\n selector: (state: ThrottlerState<TFn>) => TSelected = () => ({}) as TSelected,\n): PreactThrottler<TFn, TSelected> {\n const mergedOptions = {\n ...useDefaultPacerOptions().throttler,\n ...options,\n } as ThrottlerOptions<TFn>\n\n const [throttler] = useState(() => new Throttler<TFn>(fn, mergedOptions))\n\n throttler.fn = fn\n throttler.setOptions(mergedOptions)\n\n const state = useStore(throttler.store, selector)\n\n useEffect(() => {\n return () => {\n throttler.cancel()\n }\n }, [throttler])\n\n return useMemo(\n () =>\n ({\n ...throttler,\n state,\n }) as PreactThrottler<TFn, TSelected>, // omit `store` in favor of `state`\n [throttler, state],\n )\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA6GA,SAAgB,aACd,IACA,SACA,kBAA6D,EAAE,GAC9B;CACjC,MAAM,gBAAgB;EACpB,GAAGA,8CAAwB,CAAC;EAC5B,GAAG;EACJ;CAED,MAAM,CAAC,8CAA4B,IAAIC,qCAAe,IAAI,cAAc,CAAC;AAEzE,WAAU,KAAK;AACf,WAAU,WAAW,cAAc;CAEnC,MAAM,8CAAiB,UAAU,OAAO,SAAS;AAEjD,mCAAgB;AACd,eAAa;AACX,aAAU,QAAQ;;IAEnB,CAAC,UAAU,CAAC;AAEf,yCAEK;EACC,GAAG;EACH;EACD,GACH,CAAC,WAAW,MAAM,CACnB"}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { Store } from "@tanstack/preact-store";
|
|
2
|
+
import { AnyFunction } from "@tanstack/pacer/types";
|
|
3
|
+
import { Throttler, ThrottlerOptions, ThrottlerState } from "@tanstack/pacer/throttler";
|
|
4
|
+
|
|
5
|
+
//#region src/throttler/useThrottler.d.ts
|
|
6
|
+
interface PreactThrottler<TFn extends AnyFunction, TSelected = {}> extends Omit<Throttler<TFn>, 'store'> {
|
|
7
|
+
/**
|
|
8
|
+
* Reactive state that will be updated and re-rendered when the throttler state changes
|
|
9
|
+
*
|
|
10
|
+
* Use this instead of `throttler.store.state`
|
|
11
|
+
*/
|
|
12
|
+
readonly state: Readonly<TSelected>;
|
|
13
|
+
/**
|
|
14
|
+
* @deprecated Use `throttler.state` instead of `throttler.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<ThrottlerState<TFn>>>;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* A low-level Preact hook that creates a `Throttler` instance that limits how often the provided function can execute.
|
|
22
|
+
*
|
|
23
|
+
* This hook is designed to be flexible and state-management agnostic - it simply returns a throttler instance that
|
|
24
|
+
* you can integrate with any state management solution (useState, Redux, Zustand, Jotai, etc). For a simpler and higher-level hook that
|
|
25
|
+
* integrates directly with Preact's useState, see useThrottledState.
|
|
26
|
+
*
|
|
27
|
+
* Throttling ensures a function executes at most once within a specified time window,
|
|
28
|
+
* regardless of how many times it is called. This is useful for rate-limiting
|
|
29
|
+
* expensive operations or UI updates.
|
|
30
|
+
*
|
|
31
|
+
* ## State Management and Selector
|
|
32
|
+
*
|
|
33
|
+
* The hook uses TanStack Store for reactive state management. The `selector` parameter allows you
|
|
34
|
+
* to specify which state changes will trigger a re-render, optimizing performance by preventing
|
|
35
|
+
* unnecessary re-renders when irrelevant state changes occur.
|
|
36
|
+
*
|
|
37
|
+
* **By default, there will be no reactive state subscriptions** and you must opt-in to state
|
|
38
|
+
* tracking by providing a selector function. This prevents unnecessary re-renders and gives you
|
|
39
|
+
* full control over when your component updates. Only when you provide a selector will the
|
|
40
|
+
* component re-render when the selected state values change.
|
|
41
|
+
*
|
|
42
|
+
* Available state properties:
|
|
43
|
+
* - `executionCount`: Number of function executions that have been completed
|
|
44
|
+
* - `lastArgs`: The arguments from the most recent call to maybeExecute
|
|
45
|
+
* - `lastExecutionTime`: Timestamp of the last function execution in milliseconds
|
|
46
|
+
* - `nextExecutionTime`: Timestamp when the next execution can occur in milliseconds
|
|
47
|
+
* - `isPending`: Whether the throttler is waiting for the timeout to trigger execution
|
|
48
|
+
* - `status`: Current execution status ('disabled' | 'idle' | 'pending')
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* ```tsx
|
|
52
|
+
* // Default behavior - no reactive state subscriptions
|
|
53
|
+
* const [value, setValue] = useState(0);
|
|
54
|
+
* const throttler = useThrottler(setValue, { wait: 1000 });
|
|
55
|
+
*
|
|
56
|
+
* // Opt-in to re-render when execution count changes (optimized for tracking executions)
|
|
57
|
+
* const [value, setValue] = useState(0);
|
|
58
|
+
* const throttler = useThrottler(
|
|
59
|
+
* setValue,
|
|
60
|
+
* { wait: 1000 },
|
|
61
|
+
* (state) => ({ executionCount: state.executionCount })
|
|
62
|
+
* );
|
|
63
|
+
*
|
|
64
|
+
* // Opt-in to re-render when throttling state changes (optimized for loading indicators)
|
|
65
|
+
* const [value, setValue] = useState(0);
|
|
66
|
+
* const throttler = useThrottler(
|
|
67
|
+
* setValue,
|
|
68
|
+
* { wait: 1000 },
|
|
69
|
+
* (state) => ({
|
|
70
|
+
* isPending: state.isPending,
|
|
71
|
+
* status: state.status
|
|
72
|
+
* })
|
|
73
|
+
* );
|
|
74
|
+
*
|
|
75
|
+
* // Opt-in to re-render when timing information changes (optimized for timing displays)
|
|
76
|
+
* const [value, setValue] = useState(0);
|
|
77
|
+
* const throttler = useThrottler(
|
|
78
|
+
* setValue,
|
|
79
|
+
* { wait: 1000 },
|
|
80
|
+
* (state) => ({
|
|
81
|
+
* lastExecutionTime: state.lastExecutionTime,
|
|
82
|
+
* nextExecutionTime: state.nextExecutionTime
|
|
83
|
+
* })
|
|
84
|
+
* );
|
|
85
|
+
*
|
|
86
|
+
* // With any state manager
|
|
87
|
+
* const throttler = useThrottler(
|
|
88
|
+
* (value) => stateManager.setState(value),
|
|
89
|
+
* {
|
|
90
|
+
* wait: 2000,
|
|
91
|
+
* leading: true, // Execute immediately on first call
|
|
92
|
+
* trailing: false // Skip trailing edge updates
|
|
93
|
+
* }
|
|
94
|
+
* );
|
|
95
|
+
*
|
|
96
|
+
* // Access the selected state (will be empty object {} unless selector provided)
|
|
97
|
+
* const { executionCount, isPending } = throttler.state;
|
|
98
|
+
* ```
|
|
99
|
+
*/
|
|
100
|
+
declare function useThrottler<TFn extends AnyFunction, TSelected = {}>(fn: TFn, options: ThrottlerOptions<TFn>, selector?: (state: ThrottlerState<TFn>) => TSelected): PreactThrottler<TFn, TSelected>;
|
|
101
|
+
//#endregion
|
|
102
|
+
export { PreactThrottler, useThrottler };
|
|
103
|
+
//# sourceMappingURL=useThrottler.d.cts.map
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { Store } from "@tanstack/preact-store";
|
|
2
|
+
import { Throttler, ThrottlerOptions, ThrottlerState } from "@tanstack/pacer/throttler";
|
|
3
|
+
import { AnyFunction } from "@tanstack/pacer/types";
|
|
4
|
+
|
|
5
|
+
//#region src/throttler/useThrottler.d.ts
|
|
6
|
+
interface PreactThrottler<TFn extends AnyFunction, TSelected = {}> extends Omit<Throttler<TFn>, 'store'> {
|
|
7
|
+
/**
|
|
8
|
+
* Reactive state that will be updated and re-rendered when the throttler state changes
|
|
9
|
+
*
|
|
10
|
+
* Use this instead of `throttler.store.state`
|
|
11
|
+
*/
|
|
12
|
+
readonly state: Readonly<TSelected>;
|
|
13
|
+
/**
|
|
14
|
+
* @deprecated Use `throttler.state` instead of `throttler.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<ThrottlerState<TFn>>>;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* A low-level Preact hook that creates a `Throttler` instance that limits how often the provided function can execute.
|
|
22
|
+
*
|
|
23
|
+
* This hook is designed to be flexible and state-management agnostic - it simply returns a throttler instance that
|
|
24
|
+
* you can integrate with any state management solution (useState, Redux, Zustand, Jotai, etc). For a simpler and higher-level hook that
|
|
25
|
+
* integrates directly with Preact's useState, see useThrottledState.
|
|
26
|
+
*
|
|
27
|
+
* Throttling ensures a function executes at most once within a specified time window,
|
|
28
|
+
* regardless of how many times it is called. This is useful for rate-limiting
|
|
29
|
+
* expensive operations or UI updates.
|
|
30
|
+
*
|
|
31
|
+
* ## State Management and Selector
|
|
32
|
+
*
|
|
33
|
+
* The hook uses TanStack Store for reactive state management. The `selector` parameter allows you
|
|
34
|
+
* to specify which state changes will trigger a re-render, optimizing performance by preventing
|
|
35
|
+
* unnecessary re-renders when irrelevant state changes occur.
|
|
36
|
+
*
|
|
37
|
+
* **By default, there will be no reactive state subscriptions** and you must opt-in to state
|
|
38
|
+
* tracking by providing a selector function. This prevents unnecessary re-renders and gives you
|
|
39
|
+
* full control over when your component updates. Only when you provide a selector will the
|
|
40
|
+
* component re-render when the selected state values change.
|
|
41
|
+
*
|
|
42
|
+
* Available state properties:
|
|
43
|
+
* - `executionCount`: Number of function executions that have been completed
|
|
44
|
+
* - `lastArgs`: The arguments from the most recent call to maybeExecute
|
|
45
|
+
* - `lastExecutionTime`: Timestamp of the last function execution in milliseconds
|
|
46
|
+
* - `nextExecutionTime`: Timestamp when the next execution can occur in milliseconds
|
|
47
|
+
* - `isPending`: Whether the throttler is waiting for the timeout to trigger execution
|
|
48
|
+
* - `status`: Current execution status ('disabled' | 'idle' | 'pending')
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* ```tsx
|
|
52
|
+
* // Default behavior - no reactive state subscriptions
|
|
53
|
+
* const [value, setValue] = useState(0);
|
|
54
|
+
* const throttler = useThrottler(setValue, { wait: 1000 });
|
|
55
|
+
*
|
|
56
|
+
* // Opt-in to re-render when execution count changes (optimized for tracking executions)
|
|
57
|
+
* const [value, setValue] = useState(0);
|
|
58
|
+
* const throttler = useThrottler(
|
|
59
|
+
* setValue,
|
|
60
|
+
* { wait: 1000 },
|
|
61
|
+
* (state) => ({ executionCount: state.executionCount })
|
|
62
|
+
* );
|
|
63
|
+
*
|
|
64
|
+
* // Opt-in to re-render when throttling state changes (optimized for loading indicators)
|
|
65
|
+
* const [value, setValue] = useState(0);
|
|
66
|
+
* const throttler = useThrottler(
|
|
67
|
+
* setValue,
|
|
68
|
+
* { wait: 1000 },
|
|
69
|
+
* (state) => ({
|
|
70
|
+
* isPending: state.isPending,
|
|
71
|
+
* status: state.status
|
|
72
|
+
* })
|
|
73
|
+
* );
|
|
74
|
+
*
|
|
75
|
+
* // Opt-in to re-render when timing information changes (optimized for timing displays)
|
|
76
|
+
* const [value, setValue] = useState(0);
|
|
77
|
+
* const throttler = useThrottler(
|
|
78
|
+
* setValue,
|
|
79
|
+
* { wait: 1000 },
|
|
80
|
+
* (state) => ({
|
|
81
|
+
* lastExecutionTime: state.lastExecutionTime,
|
|
82
|
+
* nextExecutionTime: state.nextExecutionTime
|
|
83
|
+
* })
|
|
84
|
+
* );
|
|
85
|
+
*
|
|
86
|
+
* // With any state manager
|
|
87
|
+
* const throttler = useThrottler(
|
|
88
|
+
* (value) => stateManager.setState(value),
|
|
89
|
+
* {
|
|
90
|
+
* wait: 2000,
|
|
91
|
+
* leading: true, // Execute immediately on first call
|
|
92
|
+
* trailing: false // Skip trailing edge updates
|
|
93
|
+
* }
|
|
94
|
+
* );
|
|
95
|
+
*
|
|
96
|
+
* // Access the selected state (will be empty object {} unless selector provided)
|
|
97
|
+
* const { executionCount, isPending } = throttler.state;
|
|
98
|
+
* ```
|
|
99
|
+
*/
|
|
100
|
+
declare function useThrottler<TFn extends AnyFunction, TSelected = {}>(fn: TFn, options: ThrottlerOptions<TFn>, selector?: (state: ThrottlerState<TFn>) => TSelected): PreactThrottler<TFn, TSelected>;
|
|
101
|
+
//#endregion
|
|
102
|
+
export { PreactThrottler, useThrottler };
|
|
103
|
+
//# sourceMappingURL=useThrottler.d.ts.map
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { useDefaultPacerOptions } from "../provider/PacerProvider.js";
|
|
2
|
+
import { useEffect, useMemo, useState } from "preact/hooks";
|
|
3
|
+
import { useStore } from "@tanstack/preact-store";
|
|
4
|
+
import { Throttler } from "@tanstack/pacer/throttler";
|
|
5
|
+
|
|
6
|
+
//#region src/throttler/useThrottler.ts
|
|
7
|
+
/**
|
|
8
|
+
* A low-level Preact hook that creates a `Throttler` instance that limits how often the provided function can execute.
|
|
9
|
+
*
|
|
10
|
+
* This hook is designed to be flexible and state-management agnostic - it simply returns a throttler instance that
|
|
11
|
+
* you can integrate with any state management solution (useState, Redux, Zustand, Jotai, etc). For a simpler and higher-level hook that
|
|
12
|
+
* integrates directly with Preact's useState, see useThrottledState.
|
|
13
|
+
*
|
|
14
|
+
* Throttling ensures a function executes at most once within a specified time window,
|
|
15
|
+
* regardless of how many times it is called. This is useful for rate-limiting
|
|
16
|
+
* expensive operations or UI updates.
|
|
17
|
+
*
|
|
18
|
+
* ## State Management and Selector
|
|
19
|
+
*
|
|
20
|
+
* The hook uses TanStack Store for reactive state management. The `selector` parameter allows you
|
|
21
|
+
* to specify which state changes will trigger a re-render, optimizing performance by preventing
|
|
22
|
+
* unnecessary re-renders when irrelevant state changes occur.
|
|
23
|
+
*
|
|
24
|
+
* **By default, there will be no reactive state subscriptions** and you must opt-in to state
|
|
25
|
+
* tracking by providing a selector function. This prevents unnecessary re-renders and gives you
|
|
26
|
+
* full control over when your component updates. Only when you provide a selector will the
|
|
27
|
+
* component re-render when the selected state values change.
|
|
28
|
+
*
|
|
29
|
+
* Available state properties:
|
|
30
|
+
* - `executionCount`: Number of function executions that have been completed
|
|
31
|
+
* - `lastArgs`: The arguments from the most recent call to maybeExecute
|
|
32
|
+
* - `lastExecutionTime`: Timestamp of the last function execution in milliseconds
|
|
33
|
+
* - `nextExecutionTime`: Timestamp when the next execution can occur in milliseconds
|
|
34
|
+
* - `isPending`: Whether the throttler is waiting for the timeout to trigger execution
|
|
35
|
+
* - `status`: Current execution status ('disabled' | 'idle' | 'pending')
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* ```tsx
|
|
39
|
+
* // Default behavior - no reactive state subscriptions
|
|
40
|
+
* const [value, setValue] = useState(0);
|
|
41
|
+
* const throttler = useThrottler(setValue, { wait: 1000 });
|
|
42
|
+
*
|
|
43
|
+
* // Opt-in to re-render when execution count changes (optimized for tracking executions)
|
|
44
|
+
* const [value, setValue] = useState(0);
|
|
45
|
+
* const throttler = useThrottler(
|
|
46
|
+
* setValue,
|
|
47
|
+
* { wait: 1000 },
|
|
48
|
+
* (state) => ({ executionCount: state.executionCount })
|
|
49
|
+
* );
|
|
50
|
+
*
|
|
51
|
+
* // Opt-in to re-render when throttling state changes (optimized for loading indicators)
|
|
52
|
+
* const [value, setValue] = useState(0);
|
|
53
|
+
* const throttler = useThrottler(
|
|
54
|
+
* setValue,
|
|
55
|
+
* { wait: 1000 },
|
|
56
|
+
* (state) => ({
|
|
57
|
+
* isPending: state.isPending,
|
|
58
|
+
* status: state.status
|
|
59
|
+
* })
|
|
60
|
+
* );
|
|
61
|
+
*
|
|
62
|
+
* // Opt-in to re-render when timing information changes (optimized for timing displays)
|
|
63
|
+
* const [value, setValue] = useState(0);
|
|
64
|
+
* const throttler = useThrottler(
|
|
65
|
+
* setValue,
|
|
66
|
+
* { wait: 1000 },
|
|
67
|
+
* (state) => ({
|
|
68
|
+
* lastExecutionTime: state.lastExecutionTime,
|
|
69
|
+
* nextExecutionTime: state.nextExecutionTime
|
|
70
|
+
* })
|
|
71
|
+
* );
|
|
72
|
+
*
|
|
73
|
+
* // With any state manager
|
|
74
|
+
* const throttler = useThrottler(
|
|
75
|
+
* (value) => stateManager.setState(value),
|
|
76
|
+
* {
|
|
77
|
+
* wait: 2000,
|
|
78
|
+
* leading: true, // Execute immediately on first call
|
|
79
|
+
* trailing: false // Skip trailing edge updates
|
|
80
|
+
* }
|
|
81
|
+
* );
|
|
82
|
+
*
|
|
83
|
+
* // Access the selected state (will be empty object {} unless selector provided)
|
|
84
|
+
* const { executionCount, isPending } = throttler.state;
|
|
85
|
+
* ```
|
|
86
|
+
*/
|
|
87
|
+
function useThrottler(fn, options, selector = () => ({})) {
|
|
88
|
+
const mergedOptions = {
|
|
89
|
+
...useDefaultPacerOptions().throttler,
|
|
90
|
+
...options
|
|
91
|
+
};
|
|
92
|
+
const [throttler] = useState(() => new Throttler(fn, mergedOptions));
|
|
93
|
+
throttler.fn = fn;
|
|
94
|
+
throttler.setOptions(mergedOptions);
|
|
95
|
+
const state = useStore(throttler.store, selector);
|
|
96
|
+
useEffect(() => {
|
|
97
|
+
return () => {
|
|
98
|
+
throttler.cancel();
|
|
99
|
+
};
|
|
100
|
+
}, [throttler]);
|
|
101
|
+
return useMemo(() => ({
|
|
102
|
+
...throttler,
|
|
103
|
+
state
|
|
104
|
+
}), [throttler, state]);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
//#endregion
|
|
108
|
+
export { useThrottler };
|
|
109
|
+
//# sourceMappingURL=useThrottler.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useThrottler.js","names":[],"sources":["../../src/throttler/useThrottler.ts"],"sourcesContent":["import { useEffect, useMemo, useState } from 'preact/hooks'\nimport { Throttler } from '@tanstack/pacer/throttler'\nimport { useStore } from '@tanstack/preact-store'\nimport { useDefaultPacerOptions } from '../provider/PacerProvider'\nimport type { Store } from '@tanstack/preact-store'\nimport type { AnyFunction } from '@tanstack/pacer/types'\nimport type {\n ThrottlerOptions,\n ThrottlerState,\n} from '@tanstack/pacer/throttler'\n\nexport interface PreactThrottler<\n TFn extends AnyFunction,\n TSelected = {},\n> extends Omit<Throttler<TFn>, 'store'> {\n /**\n * Reactive state that will be updated and re-rendered when the throttler state changes\n *\n * Use this instead of `throttler.store.state`\n */\n readonly state: Readonly<TSelected>\n /**\n * @deprecated Use `throttler.state` instead of `throttler.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<ThrottlerState<TFn>>>\n}\n\n/**\n * A low-level Preact hook that creates a `Throttler` instance that limits how often the provided function can execute.\n *\n * This hook is designed to be flexible and state-management agnostic - it simply returns a throttler instance that\n * you can integrate with any state management solution (useState, Redux, Zustand, Jotai, etc). For a simpler and higher-level hook that\n * integrates directly with Preact's useState, see useThrottledState.\n *\n * Throttling ensures a function executes at most once within a specified time window,\n * regardless of how many times it is called. This is useful for rate-limiting\n * expensive operations or UI updates.\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 * - `executionCount`: Number of function executions that have been completed\n * - `lastArgs`: The arguments from the most recent call to maybeExecute\n * - `lastExecutionTime`: Timestamp of the last function execution in milliseconds\n * - `nextExecutionTime`: Timestamp when the next execution can occur in milliseconds\n * - `isPending`: Whether the throttler is waiting for the timeout to trigger execution\n * - `status`: Current execution status ('disabled' | 'idle' | 'pending')\n *\n * @example\n * ```tsx\n * // Default behavior - no reactive state subscriptions\n * const [value, setValue] = useState(0);\n * const throttler = useThrottler(setValue, { wait: 1000 });\n *\n * // Opt-in to re-render when execution count changes (optimized for tracking executions)\n * const [value, setValue] = useState(0);\n * const throttler = useThrottler(\n * setValue,\n * { wait: 1000 },\n * (state) => ({ executionCount: state.executionCount })\n * );\n *\n * // Opt-in to re-render when throttling state changes (optimized for loading indicators)\n * const [value, setValue] = useState(0);\n * const throttler = useThrottler(\n * setValue,\n * { wait: 1000 },\n * (state) => ({\n * isPending: state.isPending,\n * status: state.status\n * })\n * );\n *\n * // Opt-in to re-render when timing information changes (optimized for timing displays)\n * const [value, setValue] = useState(0);\n * const throttler = useThrottler(\n * setValue,\n * { wait: 1000 },\n * (state) => ({\n * lastExecutionTime: state.lastExecutionTime,\n * nextExecutionTime: state.nextExecutionTime\n * })\n * );\n *\n * // With any state manager\n * const throttler = useThrottler(\n * (value) => stateManager.setState(value),\n * {\n * wait: 2000,\n * leading: true, // Execute immediately on first call\n * trailing: false // Skip trailing edge updates\n * }\n * );\n *\n * // Access the selected state (will be empty object {} unless selector provided)\n * const { executionCount, isPending } = throttler.state;\n * ```\n */\nexport function useThrottler<TFn extends AnyFunction, TSelected = {}>(\n fn: TFn,\n options: ThrottlerOptions<TFn>,\n selector: (state: ThrottlerState<TFn>) => TSelected = () => ({}) as TSelected,\n): PreactThrottler<TFn, TSelected> {\n const mergedOptions = {\n ...useDefaultPacerOptions().throttler,\n ...options,\n } as ThrottlerOptions<TFn>\n\n const [throttler] = useState(() => new Throttler<TFn>(fn, mergedOptions))\n\n throttler.fn = fn\n throttler.setOptions(mergedOptions)\n\n const state = useStore(throttler.store, selector)\n\n useEffect(() => {\n return () => {\n throttler.cancel()\n }\n }, [throttler])\n\n return useMemo(\n () =>\n ({\n ...throttler,\n state,\n }) as PreactThrottler<TFn, TSelected>, // omit `store` in favor of `state`\n [throttler, state],\n )\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA6GA,SAAgB,aACd,IACA,SACA,kBAA6D,EAAE,GAC9B;CACjC,MAAM,gBAAgB;EACpB,GAAG,wBAAwB,CAAC;EAC5B,GAAG;EACJ;CAED,MAAM,CAAC,aAAa,eAAe,IAAI,UAAe,IAAI,cAAc,CAAC;AAEzE,WAAU,KAAK;AACf,WAAU,WAAW,cAAc;CAEnC,MAAM,QAAQ,SAAS,UAAU,OAAO,SAAS;AAEjD,iBAAgB;AACd,eAAa;AACX,aAAU,QAAQ;;IAEnB,CAAC,UAAU,CAAC;AAEf,QAAO,eAEF;EACC,GAAG;EACH;EACD,GACH,CAAC,WAAW,MAAM,CACnB"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
var __tanstack_pacer_types = require("@tanstack/pacer/types");
|
|
4
|
+
Object.keys(__tanstack_pacer_types).forEach(function (k) {
|
|
5
|
+
if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: function () { return __tanstack_pacer_types[k]; }
|
|
8
|
+
});
|
|
9
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "@tanstack/pacer/types";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "@tanstack/pacer/types";
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
var __tanstack_pacer_utils = require("@tanstack/pacer/utils");
|
|
4
|
+
Object.keys(__tanstack_pacer_utils).forEach(function (k) {
|
|
5
|
+
if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: function () { return __tanstack_pacer_utils[k]; }
|
|
8
|
+
});
|
|
9
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "@tanstack/pacer/utils";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "@tanstack/pacer/utils";
|
package/package.json
CHANGED
|
@@ -1,10 +1,121 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/preact-pacer",
|
|
3
|
-
"version": "0.0
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "0.18.0",
|
|
4
|
+
"description": "Utilities for debouncing and throttling functions in Preact.",
|
|
5
|
+
"author": "Tanner Linsley",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/TanStack/pacer.git",
|
|
10
|
+
"directory": "packages/preact-pacer"
|
|
11
|
+
},
|
|
12
|
+
"homepage": "https://tanstack.com/pacer",
|
|
13
|
+
"funding": {
|
|
14
|
+
"type": "github",
|
|
15
|
+
"url": "https://github.com/sponsors/tannerlinsley"
|
|
16
|
+
},
|
|
5
17
|
"keywords": [
|
|
6
|
-
"
|
|
7
|
-
"
|
|
8
|
-
"
|
|
9
|
-
|
|
10
|
-
|
|
18
|
+
"preact",
|
|
19
|
+
"debounce",
|
|
20
|
+
"throttle",
|
|
21
|
+
"rate-limit",
|
|
22
|
+
"queue",
|
|
23
|
+
"async"
|
|
24
|
+
],
|
|
25
|
+
"type": "module",
|
|
26
|
+
"types": "./dist/index.d.cts",
|
|
27
|
+
"main": "./dist/index.cjs",
|
|
28
|
+
"module": "./dist/index.js",
|
|
29
|
+
"exports": {
|
|
30
|
+
".": {
|
|
31
|
+
"require": "./dist/index.cjs",
|
|
32
|
+
"import": "./dist/index.js"
|
|
33
|
+
},
|
|
34
|
+
"./async-batcher": {
|
|
35
|
+
"require": "./dist/async-batcher/index.cjs",
|
|
36
|
+
"import": "./dist/async-batcher/index.js"
|
|
37
|
+
},
|
|
38
|
+
"./async-debouncer": {
|
|
39
|
+
"require": "./dist/async-debouncer/index.cjs",
|
|
40
|
+
"import": "./dist/async-debouncer/index.js"
|
|
41
|
+
},
|
|
42
|
+
"./async-queuer": {
|
|
43
|
+
"require": "./dist/async-queuer/index.cjs",
|
|
44
|
+
"import": "./dist/async-queuer/index.js"
|
|
45
|
+
},
|
|
46
|
+
"./async-rate-limiter": {
|
|
47
|
+
"require": "./dist/async-rate-limiter/index.cjs",
|
|
48
|
+
"import": "./dist/async-rate-limiter/index.js"
|
|
49
|
+
},
|
|
50
|
+
"./async-retryer": {
|
|
51
|
+
"require": "./dist/async-retryer/index.cjs",
|
|
52
|
+
"import": "./dist/async-retryer/index.js"
|
|
53
|
+
},
|
|
54
|
+
"./async-throttler": {
|
|
55
|
+
"require": "./dist/async-throttler/index.cjs",
|
|
56
|
+
"import": "./dist/async-throttler/index.js"
|
|
57
|
+
},
|
|
58
|
+
"./batcher": {
|
|
59
|
+
"require": "./dist/batcher/index.cjs",
|
|
60
|
+
"import": "./dist/batcher/index.js"
|
|
61
|
+
},
|
|
62
|
+
"./debouncer": {
|
|
63
|
+
"require": "./dist/debouncer/index.cjs",
|
|
64
|
+
"import": "./dist/debouncer/index.js"
|
|
65
|
+
},
|
|
66
|
+
"./provider": {
|
|
67
|
+
"require": "./dist/provider/index.cjs",
|
|
68
|
+
"import": "./dist/provider/index.js"
|
|
69
|
+
},
|
|
70
|
+
"./queuer": {
|
|
71
|
+
"require": "./dist/queuer/index.cjs",
|
|
72
|
+
"import": "./dist/queuer/index.js"
|
|
73
|
+
},
|
|
74
|
+
"./rate-limiter": {
|
|
75
|
+
"require": "./dist/rate-limiter/index.cjs",
|
|
76
|
+
"import": "./dist/rate-limiter/index.js"
|
|
77
|
+
},
|
|
78
|
+
"./throttler": {
|
|
79
|
+
"require": "./dist/throttler/index.cjs",
|
|
80
|
+
"import": "./dist/throttler/index.js"
|
|
81
|
+
},
|
|
82
|
+
"./types": {
|
|
83
|
+
"require": "./dist/types/index.cjs",
|
|
84
|
+
"import": "./dist/types/index.js"
|
|
85
|
+
},
|
|
86
|
+
"./utils": {
|
|
87
|
+
"require": "./dist/utils/index.cjs",
|
|
88
|
+
"import": "./dist/utils/index.js"
|
|
89
|
+
},
|
|
90
|
+
"./package.json": "./package.json"
|
|
91
|
+
},
|
|
92
|
+
"sideEffects": false,
|
|
93
|
+
"engines": {
|
|
94
|
+
"node": ">=18"
|
|
95
|
+
},
|
|
96
|
+
"files": [
|
|
97
|
+
"dist",
|
|
98
|
+
"src"
|
|
99
|
+
],
|
|
100
|
+
"dependencies": {
|
|
101
|
+
"@tanstack/preact-store": "^0.10.1",
|
|
102
|
+
"@tanstack/pacer": "0.17.0"
|
|
103
|
+
},
|
|
104
|
+
"devDependencies": {
|
|
105
|
+
"@preact/preset-vite": "^2.10.2",
|
|
106
|
+
"eslint-plugin-react-hooks": "^7.0.1",
|
|
107
|
+
"preact": "^10.28.0"
|
|
108
|
+
},
|
|
109
|
+
"peerDependencies": {
|
|
110
|
+
"preact": ">=10.0.0"
|
|
111
|
+
},
|
|
112
|
+
"scripts": {
|
|
113
|
+
"clean": "premove ./build ./dist",
|
|
114
|
+
"test:eslint": "eslint ./src",
|
|
115
|
+
"test:lib": "vitest --passWithNoTests",
|
|
116
|
+
"test:lib:dev": "pnpm test:lib --watch",
|
|
117
|
+
"test:types": "tsc",
|
|
118
|
+
"test:build": "publint --strict",
|
|
119
|
+
"build": "tsdown"
|
|
120
|
+
}
|
|
121
|
+
}
|