@tanstack/preact-pacer 0.0.1 → 0.19.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 +189 -0
- package/dist/async-batcher/useAsyncBatcher.cjs.map +1 -0
- package/dist/async-batcher/useAsyncBatcher.d.cts +198 -0
- package/dist/async-batcher/useAsyncBatcher.d.ts +198 -0
- package/dist/async-batcher/useAsyncBatcher.js +189 -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 +173 -0
- package/dist/async-debouncer/useAsyncDebouncer.cjs.map +1 -0
- package/dist/async-debouncer/useAsyncDebouncer.d.cts +178 -0
- package/dist/async-debouncer/useAsyncDebouncer.d.ts +178 -0
- package/dist/async-debouncer/useAsyncDebouncer.js +173 -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 +189 -0
- package/dist/async-queuer/useAsyncQueuer.cjs.map +1 -0
- package/dist/async-queuer/useAsyncQueuer.d.cts +198 -0
- package/dist/async-queuer/useAsyncQueuer.d.ts +198 -0
- package/dist/async-queuer/useAsyncQueuer.js +189 -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 +197 -0
- package/dist/async-rate-limiter/useAsyncRateLimiter.cjs.map +1 -0
- package/dist/async-rate-limiter/useAsyncRateLimiter.d.cts +207 -0
- package/dist/async-rate-limiter/useAsyncRateLimiter.d.ts +207 -0
- package/dist/async-rate-limiter/useAsyncRateLimiter.js +197 -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 +182 -0
- package/dist/async-throttler/useAsyncThrottler.cjs.map +1 -0
- package/dist/async-throttler/useAsyncThrottler.d.cts +189 -0
- package/dist/async-throttler/useAsyncThrottler.d.ts +189 -0
- package/dist/async-throttler/useAsyncThrottler.js +182 -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 +146 -0
- package/dist/batcher/useBatcher.cjs.map +1 -0
- package/dist/batcher/useBatcher.d.cts +155 -0
- package/dist/batcher/useBatcher.d.ts +155 -0
- package/dist/batcher/useBatcher.js +146 -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 +128 -0
- package/dist/debouncer/useDebouncer.cjs.map +1 -0
- package/dist/debouncer/useDebouncer.d.cts +133 -0
- package/dist/debouncer/useDebouncer.d.ts +133 -0
- package/dist/debouncer/useDebouncer.js +128 -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 +157 -0
- package/dist/queuer/useQueuer.cjs.map +1 -0
- package/dist/queuer/useQueuer.d.cts +166 -0
- package/dist/queuer/useQueuer.d.ts +166 -0
- package/dist/queuer/useQueuer.js +157 -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 +162 -0
- package/dist/rate-limiter/useRateLimiter.cjs.map +1 -0
- package/dist/rate-limiter/useRateLimiter.d.cts +172 -0
- package/dist/rate-limiter/useRateLimiter.d.ts +172 -0
- package/dist/rate-limiter/useRateLimiter.js +162 -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 +133 -0
- package/dist/throttler/useThrottler.cjs.map +1 -0
- package/dist/throttler/useThrottler.d.cts +138 -0
- package/dist/throttler/useThrottler.d.ts +138 -0
- package/dist/throttler/useThrottler.js +133 -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 +249 -0
- package/src/async-debouncer/index.ts +5 -0
- package/src/async-debouncer/useAsyncDebouncedCallback.ts +54 -0
- package/src/async-debouncer/useAsyncDebouncer.ts +235 -0
- package/src/async-queuer/index.ts +4 -0
- package/src/async-queuer/useAsyncQueuedState.ts +165 -0
- package/src/async-queuer/useAsyncQueuer.ts +249 -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 +261 -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 +244 -0
- package/src/batcher/index.ts +4 -0
- package/src/batcher/useBatchedCallback.ts +50 -0
- package/src/batcher/useBatcher.ts +203 -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 +189 -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 +213 -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 +222 -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 +194 -0
- package/src/types/index.ts +1 -0
- package/src/utils/index.ts +1 -0
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { useDebouncer } from "./useDebouncer.js";
|
|
2
|
+
import { useCallback } from "preact/hooks";
|
|
3
|
+
|
|
4
|
+
//#region src/debouncer/useDebouncedCallback.ts
|
|
5
|
+
/**
|
|
6
|
+
* A Preact hook that creates a debounced version of a callback function.
|
|
7
|
+
* This hook is essentially a wrapper around the basic `debounce` function
|
|
8
|
+
* that is exported from `@tanstack/pacer`,
|
|
9
|
+
* but optimized for Preact with reactive options and a stable function reference.
|
|
10
|
+
*
|
|
11
|
+
* The debounced function will only execute after the specified wait time has elapsed
|
|
12
|
+
* since its last invocation. If called again before the wait time expires, the timer
|
|
13
|
+
* resets and starts waiting again.
|
|
14
|
+
*
|
|
15
|
+
* This hook provides a simpler API compared to `useDebouncer`, making it ideal for basic
|
|
16
|
+
* debouncing needs. However, it does not expose the underlying Debouncer instance.
|
|
17
|
+
*
|
|
18
|
+
* For advanced usage requiring features like:
|
|
19
|
+
* - Manual cancellation
|
|
20
|
+
* - Access to execution counts
|
|
21
|
+
* - Custom useCallback dependencies
|
|
22
|
+
*
|
|
23
|
+
* Consider using the `useDebouncer` hook instead.
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```tsx
|
|
27
|
+
* // Debounce a search handler
|
|
28
|
+
* const handleSearch = useDebouncedCallback((query: string) => {
|
|
29
|
+
* fetchSearchResults(query);
|
|
30
|
+
* }, {
|
|
31
|
+
* wait: 500 // Wait 500ms between executions
|
|
32
|
+
* });
|
|
33
|
+
*
|
|
34
|
+
* // Use in an input
|
|
35
|
+
* <input
|
|
36
|
+
* type="search"
|
|
37
|
+
* onChange={(e) => handleSearch(e.target.value)}
|
|
38
|
+
* />
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
41
|
+
function useDebouncedCallback(fn, options) {
|
|
42
|
+
const debouncedFn = useDebouncer(fn, options).maybeExecute;
|
|
43
|
+
return useCallback((...args) => debouncedFn(...args), [debouncedFn]);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
//#endregion
|
|
47
|
+
export { useDebouncedCallback };
|
|
48
|
+
//# sourceMappingURL=useDebouncedCallback.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useDebouncedCallback.js","names":[],"sources":["../../src/debouncer/useDebouncedCallback.ts"],"sourcesContent":["import { useCallback } from 'preact/hooks'\nimport { useDebouncer } from './useDebouncer'\nimport type { DebouncerOptions } from '@tanstack/pacer/debouncer'\nimport type { AnyFunction } from '@tanstack/pacer/types'\n\n/**\n * A Preact hook that creates a debounced version of a callback function.\n * This hook is essentially a wrapper around the basic `debounce` function\n * that is exported from `@tanstack/pacer`,\n * but optimized for Preact with reactive options and a stable function reference.\n *\n * The debounced function will only execute after the specified wait time has elapsed\n * since its last invocation. If called again before the wait time expires, the timer\n * resets and starts waiting again.\n *\n * This hook provides a simpler API compared to `useDebouncer`, making it ideal for basic\n * debouncing needs. However, it does not expose the underlying Debouncer instance.\n *\n * For advanced usage requiring features like:\n * - Manual cancellation\n * - Access to execution counts\n * - Custom useCallback dependencies\n *\n * Consider using the `useDebouncer` hook instead.\n *\n * @example\n * ```tsx\n * // Debounce a search handler\n * const handleSearch = useDebouncedCallback((query: string) => {\n * fetchSearchResults(query);\n * }, {\n * wait: 500 // Wait 500ms between executions\n * });\n *\n * // Use in an input\n * <input\n * type=\"search\"\n * onChange={(e) => handleSearch(e.target.value)}\n * />\n * ```\n */\nexport function useDebouncedCallback<TFn extends AnyFunction>(\n fn: TFn,\n options: DebouncerOptions<TFn>,\n): (...args: Parameters<TFn>) => void {\n const debouncedFn = useDebouncer(fn, options).maybeExecute\n return useCallback(\n (...args: Parameters<TFn>) => debouncedFn(...args),\n [debouncedFn],\n )\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAyCA,SAAgB,qBACd,IACA,SACoC;CACpC,MAAM,cAAc,aAAa,IAAI,QAAQ,CAAC;AAC9C,QAAO,aACJ,GAAG,SAA0B,YAAY,GAAG,KAAK,EAClD,CAAC,YAAY,CACd"}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
const require_useDebouncer = require('./useDebouncer.cjs');
|
|
2
|
+
let preact_hooks = require("preact/hooks");
|
|
3
|
+
|
|
4
|
+
//#region src/debouncer/useDebouncedState.ts
|
|
5
|
+
/**
|
|
6
|
+
* A Preact hook that creates a debounced state value, combining Preact's useState with debouncing functionality.
|
|
7
|
+
* This hook provides both the current debounced value and methods to update it.
|
|
8
|
+
*
|
|
9
|
+
* The state value is only updated after the specified wait time has elapsed since the last update attempt.
|
|
10
|
+
* If another update is attempted before the wait time expires, the timer resets and starts waiting again.
|
|
11
|
+
* This is useful for handling frequent state updates that should be throttled, like search input values
|
|
12
|
+
* or window resize dimensions.
|
|
13
|
+
*
|
|
14
|
+
* The hook returns a tuple containing:
|
|
15
|
+
* - The current debounced value
|
|
16
|
+
* - A function to update the debounced value
|
|
17
|
+
* - The debouncer instance with additional control methods
|
|
18
|
+
*
|
|
19
|
+
* ## State Management and Selector
|
|
20
|
+
*
|
|
21
|
+
* The hook uses TanStack Store for reactive state management via the underlying debouncer instance.
|
|
22
|
+
* The `selector` parameter allows you to specify which debouncer state changes will trigger a re-render,
|
|
23
|
+
* optimizing performance by preventing unnecessary re-renders when irrelevant state changes occur.
|
|
24
|
+
*
|
|
25
|
+
* **By default, there will be no reactive state subscriptions** and you must opt-in to state
|
|
26
|
+
* tracking by providing a selector function. This prevents unnecessary re-renders and gives you
|
|
27
|
+
* full control over when your component updates. Only when you provide a selector will the
|
|
28
|
+
* component re-render when the selected state values change.
|
|
29
|
+
*
|
|
30
|
+
* Available debouncer state properties:
|
|
31
|
+
* - `canLeadingExecute`: Whether the debouncer can execute on the leading edge
|
|
32
|
+
* - `executionCount`: Number of function executions that have been completed
|
|
33
|
+
* - `isPending`: Whether the debouncer is waiting for the timeout to trigger execution
|
|
34
|
+
* - `lastArgs`: The arguments from the most recent call to maybeExecute
|
|
35
|
+
* - `status`: Current execution status ('disabled' | 'idle' | 'pending')
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* ```tsx
|
|
39
|
+
* // Default behavior - no reactive state subscriptions
|
|
40
|
+
* const [searchTerm, setSearchTerm, debouncer] = useDebouncedState('', {
|
|
41
|
+
* wait: 500 // Wait 500ms after last keystroke
|
|
42
|
+
* });
|
|
43
|
+
*
|
|
44
|
+
* // Opt-in to re-render when pending state changes (optimized for loading indicators)
|
|
45
|
+
* const [searchTerm, setSearchTerm, debouncer] = useDebouncedState(
|
|
46
|
+
* '',
|
|
47
|
+
* { wait: 500 },
|
|
48
|
+
* (state) => ({ isPending: state.isPending })
|
|
49
|
+
* );
|
|
50
|
+
*
|
|
51
|
+
* // Opt-in to re-render when execution count changes (optimized for tracking executions)
|
|
52
|
+
* const [searchTerm, setSearchTerm, debouncer] = useDebouncedState(
|
|
53
|
+
* '',
|
|
54
|
+
* { wait: 500 },
|
|
55
|
+
* (state) => ({ executionCount: state.executionCount })
|
|
56
|
+
* );
|
|
57
|
+
*
|
|
58
|
+
* // Opt-in to re-render when debouncing status changes (optimized for status display)
|
|
59
|
+
* const [searchTerm, setSearchTerm, debouncer] = useDebouncedState(
|
|
60
|
+
* '',
|
|
61
|
+
* { wait: 500 },
|
|
62
|
+
* (state) => ({
|
|
63
|
+
* status: state.status,
|
|
64
|
+
* canLeadingExecute: state.canLeadingExecute
|
|
65
|
+
* })
|
|
66
|
+
* );
|
|
67
|
+
*
|
|
68
|
+
* // Update value - will be debounced
|
|
69
|
+
* const handleChange = (e) => {
|
|
70
|
+
* setSearchTerm(e.target.value);
|
|
71
|
+
* };
|
|
72
|
+
*
|
|
73
|
+
* // Access the selected debouncer state (will be empty object {} unless selector provided)
|
|
74
|
+
* const { isPending, executionCount } = debouncer.state;
|
|
75
|
+
* ```
|
|
76
|
+
*/
|
|
77
|
+
function useDebouncedState(value, options, selector) {
|
|
78
|
+
const [debouncedValue, setDebouncedValue] = (0, preact_hooks.useState)(value);
|
|
79
|
+
const debouncer = require_useDebouncer.useDebouncer(setDebouncedValue, options, selector);
|
|
80
|
+
return [
|
|
81
|
+
debouncedValue,
|
|
82
|
+
debouncer.maybeExecute,
|
|
83
|
+
debouncer
|
|
84
|
+
];
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
//#endregion
|
|
88
|
+
exports.useDebouncedState = useDebouncedState;
|
|
89
|
+
//# sourceMappingURL=useDebouncedState.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useDebouncedState.cjs","names":["useDebouncer"],"sources":["../../src/debouncer/useDebouncedState.ts"],"sourcesContent":["import { useState } from 'preact/hooks'\nimport { useDebouncer } from './useDebouncer'\nimport type { 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 state value, combining Preact's useState with debouncing functionality.\n * This hook provides both the current debounced value and methods to update it.\n *\n * The state value is only updated after the specified wait time has elapsed since the last update attempt.\n * If another update is attempted before the wait time expires, the timer resets and starts waiting again.\n * This is useful for handling frequent state updates that should be throttled, like search input values\n * or window resize dimensions.\n *\n * The hook returns a tuple containing:\n * - The current debounced value\n * - A function to update the debounced value\n * - The debouncer instance with additional control methods\n *\n * ## State Management and Selector\n *\n * The hook uses TanStack Store for reactive state management via the underlying debouncer instance.\n * The `selector` parameter allows you to specify which debouncer state changes will trigger a re-render,\n * optimizing performance by preventing unnecessary re-renders when irrelevant state changes occur.\n *\n * **By default, there will be no reactive state subscriptions** and you must opt-in to state\n * tracking by providing a selector function. This prevents unnecessary re-renders and gives you\n * full control over when your component updates. Only when you provide a selector will the\n * component re-render when the selected state values change.\n *\n * Available debouncer state properties:\n * - `canLeadingExecute`: Whether the debouncer can execute on the leading edge\n * - `executionCount`: Number of function executions that have been completed\n * - `isPending`: Whether the debouncer is waiting for the timeout to trigger execution\n * - `lastArgs`: The arguments from the most recent call to maybeExecute\n * - `status`: Current execution status ('disabled' | 'idle' | 'pending')\n *\n * @example\n * ```tsx\n * // Default behavior - no reactive state subscriptions\n * const [searchTerm, setSearchTerm, debouncer] = useDebouncedState('', {\n * wait: 500 // Wait 500ms after last keystroke\n * });\n *\n * // Opt-in to re-render when pending state changes (optimized for loading indicators)\n * const [searchTerm, setSearchTerm, debouncer] = useDebouncedState(\n * '',\n * { wait: 500 },\n * (state) => ({ isPending: state.isPending })\n * );\n *\n * // Opt-in to re-render when execution count changes (optimized for tracking executions)\n * const [searchTerm, setSearchTerm, debouncer] = useDebouncedState(\n * '',\n * { wait: 500 },\n * (state) => ({ executionCount: state.executionCount })\n * );\n *\n * // Opt-in to re-render when debouncing status changes (optimized for status display)\n * const [searchTerm, setSearchTerm, debouncer] = useDebouncedState(\n * '',\n * { wait: 500 },\n * (state) => ({\n * status: state.status,\n * canLeadingExecute: state.canLeadingExecute\n * })\n * );\n *\n * // Update value - will be debounced\n * const handleChange = (e) => {\n * setSearchTerm(e.target.value);\n * };\n *\n * // Access the selected debouncer state (will be empty object {} unless selector provided)\n * const { isPending, executionCount } = debouncer.state;\n * ```\n */\nexport function useDebouncedState<\n TValue,\n TSelected = DebouncerState<Dispatch<StateUpdater<TValue>>>,\n>(\n value: TValue,\n options: DebouncerOptions<Dispatch<StateUpdater<TValue>>>,\n selector?: (\n state: DebouncerState<Dispatch<StateUpdater<TValue>>>,\n ) => TSelected,\n): [\n TValue,\n Dispatch<StateUpdater<TValue>>,\n PreactDebouncer<Dispatch<StateUpdater<TValue>>, TSelected>,\n] {\n const [debouncedValue, setDebouncedValue] = useState(value)\n const debouncer = useDebouncer(\n setDebouncedValue,\n options,\n selector as (\n state: DebouncerState<Dispatch<StateUpdater<TValue>>>,\n ) => TSelected,\n )\n return [\n debouncedValue,\n debouncer.maybeExecute as Dispatch<StateUpdater<TValue>>,\n debouncer,\n ]\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAiFA,SAAgB,kBAId,OACA,SACA,UAOA;CACA,MAAM,CAAC,gBAAgB,gDAA8B,MAAM;CAC3D,MAAM,YAAYA,kCAChB,mBACA,SACA,SAGD;AACD,QAAO;EACL;EACA,UAAU;EACV;EACD"}
|
|
@@ -0,0 +1,82 @@
|
|
|
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/useDebouncedState.d.ts
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* A Preact hook that creates a debounced state value, combining Preact's useState with debouncing functionality.
|
|
9
|
+
* This hook provides both the current debounced value and methods to update it.
|
|
10
|
+
*
|
|
11
|
+
* The state value is only updated after the specified wait time has elapsed since the last update attempt.
|
|
12
|
+
* If another update is attempted before the wait time expires, the timer resets and starts waiting again.
|
|
13
|
+
* This is useful for handling frequent state updates that should be throttled, like search input values
|
|
14
|
+
* or window resize dimensions.
|
|
15
|
+
*
|
|
16
|
+
* The hook returns a tuple containing:
|
|
17
|
+
* - The current debounced value
|
|
18
|
+
* - A function to update the debounced value
|
|
19
|
+
* - The debouncer instance with additional control methods
|
|
20
|
+
*
|
|
21
|
+
* ## State Management and Selector
|
|
22
|
+
*
|
|
23
|
+
* The hook uses TanStack Store for reactive state management via the underlying debouncer instance.
|
|
24
|
+
* The `selector` parameter allows you to specify which debouncer state changes will trigger a re-render,
|
|
25
|
+
* optimizing performance by preventing unnecessary re-renders when irrelevant state changes occur.
|
|
26
|
+
*
|
|
27
|
+
* **By default, there will be no reactive state subscriptions** and you must opt-in to state
|
|
28
|
+
* tracking by providing a selector function. This prevents unnecessary re-renders and gives you
|
|
29
|
+
* full control over when your component updates. Only when you provide a selector will the
|
|
30
|
+
* component re-render when the selected state values change.
|
|
31
|
+
*
|
|
32
|
+
* Available debouncer state properties:
|
|
33
|
+
* - `canLeadingExecute`: Whether the debouncer can execute on the leading edge
|
|
34
|
+
* - `executionCount`: Number of function executions that have been completed
|
|
35
|
+
* - `isPending`: Whether the debouncer is waiting for the timeout to trigger execution
|
|
36
|
+
* - `lastArgs`: The arguments from the most recent call to maybeExecute
|
|
37
|
+
* - `status`: Current execution status ('disabled' | 'idle' | 'pending')
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* ```tsx
|
|
41
|
+
* // Default behavior - no reactive state subscriptions
|
|
42
|
+
* const [searchTerm, setSearchTerm, debouncer] = useDebouncedState('', {
|
|
43
|
+
* wait: 500 // Wait 500ms after last keystroke
|
|
44
|
+
* });
|
|
45
|
+
*
|
|
46
|
+
* // Opt-in to re-render when pending state changes (optimized for loading indicators)
|
|
47
|
+
* const [searchTerm, setSearchTerm, debouncer] = useDebouncedState(
|
|
48
|
+
* '',
|
|
49
|
+
* { wait: 500 },
|
|
50
|
+
* (state) => ({ isPending: state.isPending })
|
|
51
|
+
* );
|
|
52
|
+
*
|
|
53
|
+
* // Opt-in to re-render when execution count changes (optimized for tracking executions)
|
|
54
|
+
* const [searchTerm, setSearchTerm, debouncer] = useDebouncedState(
|
|
55
|
+
* '',
|
|
56
|
+
* { wait: 500 },
|
|
57
|
+
* (state) => ({ executionCount: state.executionCount })
|
|
58
|
+
* );
|
|
59
|
+
*
|
|
60
|
+
* // Opt-in to re-render when debouncing status changes (optimized for status display)
|
|
61
|
+
* const [searchTerm, setSearchTerm, debouncer] = useDebouncedState(
|
|
62
|
+
* '',
|
|
63
|
+
* { wait: 500 },
|
|
64
|
+
* (state) => ({
|
|
65
|
+
* status: state.status,
|
|
66
|
+
* canLeadingExecute: state.canLeadingExecute
|
|
67
|
+
* })
|
|
68
|
+
* );
|
|
69
|
+
*
|
|
70
|
+
* // Update value - will be debounced
|
|
71
|
+
* const handleChange = (e) => {
|
|
72
|
+
* setSearchTerm(e.target.value);
|
|
73
|
+
* };
|
|
74
|
+
*
|
|
75
|
+
* // Access the selected debouncer state (will be empty object {} unless selector provided)
|
|
76
|
+
* const { isPending, executionCount } = debouncer.state;
|
|
77
|
+
* ```
|
|
78
|
+
*/
|
|
79
|
+
declare function useDebouncedState<TValue, TSelected = DebouncerState<Dispatch<StateUpdater<TValue>>>>(value: TValue, options: DebouncerOptions<Dispatch<StateUpdater<TValue>>>, selector?: (state: DebouncerState<Dispatch<StateUpdater<TValue>>>) => TSelected): [TValue, Dispatch<StateUpdater<TValue>>, PreactDebouncer<Dispatch<StateUpdater<TValue>>, TSelected>];
|
|
80
|
+
//#endregion
|
|
81
|
+
export { useDebouncedState };
|
|
82
|
+
//# sourceMappingURL=useDebouncedState.d.cts.map
|
|
@@ -0,0 +1,82 @@
|
|
|
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/useDebouncedState.d.ts
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* A Preact hook that creates a debounced state value, combining Preact's useState with debouncing functionality.
|
|
9
|
+
* This hook provides both the current debounced value and methods to update it.
|
|
10
|
+
*
|
|
11
|
+
* The state value is only updated after the specified wait time has elapsed since the last update attempt.
|
|
12
|
+
* If another update is attempted before the wait time expires, the timer resets and starts waiting again.
|
|
13
|
+
* This is useful for handling frequent state updates that should be throttled, like search input values
|
|
14
|
+
* or window resize dimensions.
|
|
15
|
+
*
|
|
16
|
+
* The hook returns a tuple containing:
|
|
17
|
+
* - The current debounced value
|
|
18
|
+
* - A function to update the debounced value
|
|
19
|
+
* - The debouncer instance with additional control methods
|
|
20
|
+
*
|
|
21
|
+
* ## State Management and Selector
|
|
22
|
+
*
|
|
23
|
+
* The hook uses TanStack Store for reactive state management via the underlying debouncer instance.
|
|
24
|
+
* The `selector` parameter allows you to specify which debouncer state changes will trigger a re-render,
|
|
25
|
+
* optimizing performance by preventing unnecessary re-renders when irrelevant state changes occur.
|
|
26
|
+
*
|
|
27
|
+
* **By default, there will be no reactive state subscriptions** and you must opt-in to state
|
|
28
|
+
* tracking by providing a selector function. This prevents unnecessary re-renders and gives you
|
|
29
|
+
* full control over when your component updates. Only when you provide a selector will the
|
|
30
|
+
* component re-render when the selected state values change.
|
|
31
|
+
*
|
|
32
|
+
* Available debouncer state properties:
|
|
33
|
+
* - `canLeadingExecute`: Whether the debouncer can execute on the leading edge
|
|
34
|
+
* - `executionCount`: Number of function executions that have been completed
|
|
35
|
+
* - `isPending`: Whether the debouncer is waiting for the timeout to trigger execution
|
|
36
|
+
* - `lastArgs`: The arguments from the most recent call to maybeExecute
|
|
37
|
+
* - `status`: Current execution status ('disabled' | 'idle' | 'pending')
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* ```tsx
|
|
41
|
+
* // Default behavior - no reactive state subscriptions
|
|
42
|
+
* const [searchTerm, setSearchTerm, debouncer] = useDebouncedState('', {
|
|
43
|
+
* wait: 500 // Wait 500ms after last keystroke
|
|
44
|
+
* });
|
|
45
|
+
*
|
|
46
|
+
* // Opt-in to re-render when pending state changes (optimized for loading indicators)
|
|
47
|
+
* const [searchTerm, setSearchTerm, debouncer] = useDebouncedState(
|
|
48
|
+
* '',
|
|
49
|
+
* { wait: 500 },
|
|
50
|
+
* (state) => ({ isPending: state.isPending })
|
|
51
|
+
* );
|
|
52
|
+
*
|
|
53
|
+
* // Opt-in to re-render when execution count changes (optimized for tracking executions)
|
|
54
|
+
* const [searchTerm, setSearchTerm, debouncer] = useDebouncedState(
|
|
55
|
+
* '',
|
|
56
|
+
* { wait: 500 },
|
|
57
|
+
* (state) => ({ executionCount: state.executionCount })
|
|
58
|
+
* );
|
|
59
|
+
*
|
|
60
|
+
* // Opt-in to re-render when debouncing status changes (optimized for status display)
|
|
61
|
+
* const [searchTerm, setSearchTerm, debouncer] = useDebouncedState(
|
|
62
|
+
* '',
|
|
63
|
+
* { wait: 500 },
|
|
64
|
+
* (state) => ({
|
|
65
|
+
* status: state.status,
|
|
66
|
+
* canLeadingExecute: state.canLeadingExecute
|
|
67
|
+
* })
|
|
68
|
+
* );
|
|
69
|
+
*
|
|
70
|
+
* // Update value - will be debounced
|
|
71
|
+
* const handleChange = (e) => {
|
|
72
|
+
* setSearchTerm(e.target.value);
|
|
73
|
+
* };
|
|
74
|
+
*
|
|
75
|
+
* // Access the selected debouncer state (will be empty object {} unless selector provided)
|
|
76
|
+
* const { isPending, executionCount } = debouncer.state;
|
|
77
|
+
* ```
|
|
78
|
+
*/
|
|
79
|
+
declare function useDebouncedState<TValue, TSelected = DebouncerState<Dispatch<StateUpdater<TValue>>>>(value: TValue, options: DebouncerOptions<Dispatch<StateUpdater<TValue>>>, selector?: (state: DebouncerState<Dispatch<StateUpdater<TValue>>>) => TSelected): [TValue, Dispatch<StateUpdater<TValue>>, PreactDebouncer<Dispatch<StateUpdater<TValue>>, TSelected>];
|
|
80
|
+
//#endregion
|
|
81
|
+
export { useDebouncedState };
|
|
82
|
+
//# sourceMappingURL=useDebouncedState.d.ts.map
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { useDebouncer } from "./useDebouncer.js";
|
|
2
|
+
import { useState } from "preact/hooks";
|
|
3
|
+
|
|
4
|
+
//#region src/debouncer/useDebouncedState.ts
|
|
5
|
+
/**
|
|
6
|
+
* A Preact hook that creates a debounced state value, combining Preact's useState with debouncing functionality.
|
|
7
|
+
* This hook provides both the current debounced value and methods to update it.
|
|
8
|
+
*
|
|
9
|
+
* The state value is only updated after the specified wait time has elapsed since the last update attempt.
|
|
10
|
+
* If another update is attempted before the wait time expires, the timer resets and starts waiting again.
|
|
11
|
+
* This is useful for handling frequent state updates that should be throttled, like search input values
|
|
12
|
+
* or window resize dimensions.
|
|
13
|
+
*
|
|
14
|
+
* The hook returns a tuple containing:
|
|
15
|
+
* - The current debounced value
|
|
16
|
+
* - A function to update the debounced value
|
|
17
|
+
* - The debouncer instance with additional control methods
|
|
18
|
+
*
|
|
19
|
+
* ## State Management and Selector
|
|
20
|
+
*
|
|
21
|
+
* The hook uses TanStack Store for reactive state management via the underlying debouncer instance.
|
|
22
|
+
* The `selector` parameter allows you to specify which debouncer state changes will trigger a re-render,
|
|
23
|
+
* optimizing performance by preventing unnecessary re-renders when irrelevant state changes occur.
|
|
24
|
+
*
|
|
25
|
+
* **By default, there will be no reactive state subscriptions** and you must opt-in to state
|
|
26
|
+
* tracking by providing a selector function. This prevents unnecessary re-renders and gives you
|
|
27
|
+
* full control over when your component updates. Only when you provide a selector will the
|
|
28
|
+
* component re-render when the selected state values change.
|
|
29
|
+
*
|
|
30
|
+
* Available debouncer state properties:
|
|
31
|
+
* - `canLeadingExecute`: Whether the debouncer can execute on the leading edge
|
|
32
|
+
* - `executionCount`: Number of function executions that have been completed
|
|
33
|
+
* - `isPending`: Whether the debouncer is waiting for the timeout to trigger execution
|
|
34
|
+
* - `lastArgs`: The arguments from the most recent call to maybeExecute
|
|
35
|
+
* - `status`: Current execution status ('disabled' | 'idle' | 'pending')
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* ```tsx
|
|
39
|
+
* // Default behavior - no reactive state subscriptions
|
|
40
|
+
* const [searchTerm, setSearchTerm, debouncer] = useDebouncedState('', {
|
|
41
|
+
* wait: 500 // Wait 500ms after last keystroke
|
|
42
|
+
* });
|
|
43
|
+
*
|
|
44
|
+
* // Opt-in to re-render when pending state changes (optimized for loading indicators)
|
|
45
|
+
* const [searchTerm, setSearchTerm, debouncer] = useDebouncedState(
|
|
46
|
+
* '',
|
|
47
|
+
* { wait: 500 },
|
|
48
|
+
* (state) => ({ isPending: state.isPending })
|
|
49
|
+
* );
|
|
50
|
+
*
|
|
51
|
+
* // Opt-in to re-render when execution count changes (optimized for tracking executions)
|
|
52
|
+
* const [searchTerm, setSearchTerm, debouncer] = useDebouncedState(
|
|
53
|
+
* '',
|
|
54
|
+
* { wait: 500 },
|
|
55
|
+
* (state) => ({ executionCount: state.executionCount })
|
|
56
|
+
* );
|
|
57
|
+
*
|
|
58
|
+
* // Opt-in to re-render when debouncing status changes (optimized for status display)
|
|
59
|
+
* const [searchTerm, setSearchTerm, debouncer] = useDebouncedState(
|
|
60
|
+
* '',
|
|
61
|
+
* { wait: 500 },
|
|
62
|
+
* (state) => ({
|
|
63
|
+
* status: state.status,
|
|
64
|
+
* canLeadingExecute: state.canLeadingExecute
|
|
65
|
+
* })
|
|
66
|
+
* );
|
|
67
|
+
*
|
|
68
|
+
* // Update value - will be debounced
|
|
69
|
+
* const handleChange = (e) => {
|
|
70
|
+
* setSearchTerm(e.target.value);
|
|
71
|
+
* };
|
|
72
|
+
*
|
|
73
|
+
* // Access the selected debouncer state (will be empty object {} unless selector provided)
|
|
74
|
+
* const { isPending, executionCount } = debouncer.state;
|
|
75
|
+
* ```
|
|
76
|
+
*/
|
|
77
|
+
function useDebouncedState(value, options, selector) {
|
|
78
|
+
const [debouncedValue, setDebouncedValue] = useState(value);
|
|
79
|
+
const debouncer = useDebouncer(setDebouncedValue, options, selector);
|
|
80
|
+
return [
|
|
81
|
+
debouncedValue,
|
|
82
|
+
debouncer.maybeExecute,
|
|
83
|
+
debouncer
|
|
84
|
+
];
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
//#endregion
|
|
88
|
+
export { useDebouncedState };
|
|
89
|
+
//# sourceMappingURL=useDebouncedState.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useDebouncedState.js","names":[],"sources":["../../src/debouncer/useDebouncedState.ts"],"sourcesContent":["import { useState } from 'preact/hooks'\nimport { useDebouncer } from './useDebouncer'\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 state value, combining Preact's useState with debouncing functionality.\n * This hook provides both the current debounced value and methods to update it.\n *\n * The state value is only updated after the specified wait time has elapsed since the last update attempt.\n * If another update is attempted before the wait time expires, the timer resets and starts waiting again.\n * This is useful for handling frequent state updates that should be throttled, like search input values\n * or window resize dimensions.\n *\n * The hook returns a tuple containing:\n * - The current debounced value\n * - A function to update the debounced value\n * - The debouncer instance with additional control methods\n *\n * ## State Management and Selector\n *\n * The hook uses TanStack Store for reactive state management via the underlying debouncer instance.\n * The `selector` parameter allows you to specify which debouncer state changes will trigger a re-render,\n * optimizing performance by preventing unnecessary re-renders when irrelevant state changes occur.\n *\n * **By default, there will be no reactive state subscriptions** and you must opt-in to state\n * tracking by providing a selector function. This prevents unnecessary re-renders and gives you\n * full control over when your component updates. Only when you provide a selector will the\n * component re-render when the selected state values change.\n *\n * Available debouncer state properties:\n * - `canLeadingExecute`: Whether the debouncer can execute on the leading edge\n * - `executionCount`: Number of function executions that have been completed\n * - `isPending`: Whether the debouncer is waiting for the timeout to trigger execution\n * - `lastArgs`: The arguments from the most recent call to maybeExecute\n * - `status`: Current execution status ('disabled' | 'idle' | 'pending')\n *\n * @example\n * ```tsx\n * // Default behavior - no reactive state subscriptions\n * const [searchTerm, setSearchTerm, debouncer] = useDebouncedState('', {\n * wait: 500 // Wait 500ms after last keystroke\n * });\n *\n * // Opt-in to re-render when pending state changes (optimized for loading indicators)\n * const [searchTerm, setSearchTerm, debouncer] = useDebouncedState(\n * '',\n * { wait: 500 },\n * (state) => ({ isPending: state.isPending })\n * );\n *\n * // Opt-in to re-render when execution count changes (optimized for tracking executions)\n * const [searchTerm, setSearchTerm, debouncer] = useDebouncedState(\n * '',\n * { wait: 500 },\n * (state) => ({ executionCount: state.executionCount })\n * );\n *\n * // Opt-in to re-render when debouncing status changes (optimized for status display)\n * const [searchTerm, setSearchTerm, debouncer] = useDebouncedState(\n * '',\n * { wait: 500 },\n * (state) => ({\n * status: state.status,\n * canLeadingExecute: state.canLeadingExecute\n * })\n * );\n *\n * // Update value - will be debounced\n * const handleChange = (e) => {\n * setSearchTerm(e.target.value);\n * };\n *\n * // Access the selected debouncer state (will be empty object {} unless selector provided)\n * const { isPending, executionCount } = debouncer.state;\n * ```\n */\nexport function useDebouncedState<\n TValue,\n TSelected = DebouncerState<Dispatch<StateUpdater<TValue>>>,\n>(\n value: TValue,\n options: DebouncerOptions<Dispatch<StateUpdater<TValue>>>,\n selector?: (\n state: DebouncerState<Dispatch<StateUpdater<TValue>>>,\n ) => TSelected,\n): [\n TValue,\n Dispatch<StateUpdater<TValue>>,\n PreactDebouncer<Dispatch<StateUpdater<TValue>>, TSelected>,\n] {\n const [debouncedValue, setDebouncedValue] = useState(value)\n const debouncer = useDebouncer(\n setDebouncedValue,\n options,\n selector as (\n state: DebouncerState<Dispatch<StateUpdater<TValue>>>,\n ) => TSelected,\n )\n return [\n debouncedValue,\n debouncer.maybeExecute as Dispatch<StateUpdater<TValue>>,\n debouncer,\n ]\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAiFA,SAAgB,kBAId,OACA,SACA,UAOA;CACA,MAAM,CAAC,gBAAgB,qBAAqB,SAAS,MAAM;CAC3D,MAAM,YAAY,aAChB,mBACA,SACA,SAGD;AACD,QAAO;EACL;EACA,UAAU;EACV;EACD"}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
const require_useDebouncedState = require('./useDebouncedState.cjs');
|
|
2
|
+
let preact_hooks = require("preact/hooks");
|
|
3
|
+
|
|
4
|
+
//#region src/debouncer/useDebouncedValue.ts
|
|
5
|
+
/**
|
|
6
|
+
* A Preact hook that creates a debounced value that updates only after a specified delay.
|
|
7
|
+
* Unlike useDebouncedState, this hook automatically tracks changes to the input value
|
|
8
|
+
* and updates the debounced value accordingly.
|
|
9
|
+
*
|
|
10
|
+
* The debounced value will only update after the specified wait time has elapsed since
|
|
11
|
+
* the last change to the input value. If the input value changes again before the wait
|
|
12
|
+
* time expires, the timer resets and starts waiting again.
|
|
13
|
+
*
|
|
14
|
+
* This is useful for deriving debounced values from props or state that change frequently,
|
|
15
|
+
* like search queries or form inputs, where you want to limit how often downstream effects
|
|
16
|
+
* or calculations occur.
|
|
17
|
+
*
|
|
18
|
+
* The hook returns the current debounced value and the underlying debouncer instance.
|
|
19
|
+
* The debouncer instance can be used to access additional functionality like cancellation
|
|
20
|
+
* and execution counts.
|
|
21
|
+
*
|
|
22
|
+
* ## State Management and Selector
|
|
23
|
+
*
|
|
24
|
+
* The hook uses TanStack Store for reactive state management via the underlying debouncer instance.
|
|
25
|
+
* The `selector` parameter allows you to specify which debouncer state changes will trigger a re-render,
|
|
26
|
+
* optimizing performance by preventing unnecessary re-renders when irrelevant state changes occur.
|
|
27
|
+
*
|
|
28
|
+
* **By default, there will be no reactive state subscriptions** and you must opt-in to state
|
|
29
|
+
* tracking by providing a selector function. This prevents unnecessary re-renders and gives you
|
|
30
|
+
* full control over when your component updates. Only when you provide a selector will the
|
|
31
|
+
* component re-render when the selected state values change.
|
|
32
|
+
*
|
|
33
|
+
* Available debouncer state properties:
|
|
34
|
+
* - `canLeadingExecute`: Whether the debouncer can execute on the leading edge
|
|
35
|
+
* - `executionCount`: Number of function executions that have been completed
|
|
36
|
+
* - `isPending`: Whether the debouncer is waiting for the timeout to trigger execution
|
|
37
|
+
* - `lastArgs`: The arguments from the most recent call to maybeExecute
|
|
38
|
+
* - `status`: Current execution status ('disabled' | 'idle' | 'pending')
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* ```tsx
|
|
42
|
+
* // Default behavior - no reactive state subscriptions
|
|
43
|
+
* const [searchQuery, setSearchQuery] = useState('');
|
|
44
|
+
* const [debouncedQuery, debouncer] = useDebouncedValue(searchQuery, {
|
|
45
|
+
* wait: 500 // Wait 500ms after last change
|
|
46
|
+
* });
|
|
47
|
+
*
|
|
48
|
+
* // Opt-in to re-render when pending state changes (optimized for loading indicators)
|
|
49
|
+
* const [debouncedQuery, debouncer] = useDebouncedValue(
|
|
50
|
+
* searchQuery,
|
|
51
|
+
* { wait: 500 },
|
|
52
|
+
* (state) => ({ isPending: state.isPending })
|
|
53
|
+
* );
|
|
54
|
+
*
|
|
55
|
+
* // Opt-in to re-render when execution count changes (optimized for tracking executions)
|
|
56
|
+
* const [debouncedQuery, debouncer] = useDebouncedValue(
|
|
57
|
+
* searchQuery,
|
|
58
|
+
* { wait: 500 },
|
|
59
|
+
* (state) => ({ executionCount: state.executionCount })
|
|
60
|
+
* );
|
|
61
|
+
*
|
|
62
|
+
* // Opt-in to re-render when debouncing status changes (optimized for status display)
|
|
63
|
+
* const [debouncedQuery, debouncer] = useDebouncedValue(
|
|
64
|
+
* searchQuery,
|
|
65
|
+
* { wait: 500 },
|
|
66
|
+
* (state) => ({
|
|
67
|
+
* status: state.status,
|
|
68
|
+
* canLeadingExecute: state.canLeadingExecute
|
|
69
|
+
* })
|
|
70
|
+
* );
|
|
71
|
+
*
|
|
72
|
+
* // debouncedQuery will update 500ms after searchQuery stops changing
|
|
73
|
+
* useEffect(() => {
|
|
74
|
+
* fetchSearchResults(debouncedQuery);
|
|
75
|
+
* }, [debouncedQuery]);
|
|
76
|
+
*
|
|
77
|
+
* // Handle input changes
|
|
78
|
+
* const handleChange = (e) => {
|
|
79
|
+
* setSearchQuery(e.target.value);
|
|
80
|
+
* };
|
|
81
|
+
*
|
|
82
|
+
* // Access the selected debouncer state (will be empty object {} unless selector provided)
|
|
83
|
+
* const { isPending, executionCount } = debouncer.state;
|
|
84
|
+
* ```
|
|
85
|
+
*/
|
|
86
|
+
function useDebouncedValue(value, options, selector) {
|
|
87
|
+
const [debouncedValue, setDebouncedValue, debouncer] = require_useDebouncedState.useDebouncedState(value, options, selector);
|
|
88
|
+
(0, preact_hooks.useEffect)(() => {
|
|
89
|
+
setDebouncedValue(value);
|
|
90
|
+
}, [value, setDebouncedValue]);
|
|
91
|
+
return [debouncedValue, debouncer];
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
//#endregion
|
|
95
|
+
exports.useDebouncedValue = useDebouncedValue;
|
|
96
|
+
//# sourceMappingURL=useDebouncedValue.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useDebouncedValue.cjs","names":["useDebouncedState"],"sources":["../../src/debouncer/useDebouncedValue.ts"],"sourcesContent":["import { useEffect } from 'preact/hooks'\nimport { useDebouncedState } from './useDebouncedState'\nimport type { 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,aAAaA,4CACrD,OACA,SACA,SACD;AAED,mCAAgB;AACb,EAAC,kBAA8C,MAAM;IACrD,CAAC,OAAO,kBAAkB,CAAC;AAE9B,QAAO,CAAC,gBAAgB,UAAU"}
|
|
@@ -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
|