@tanstack/solid-pacer 0.22.0 → 0.23.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -4
- package/dist/async-batcher/createAsyncBatcher.d.ts +5 -9
- package/dist/async-batcher/createAsyncBatcher.js +2 -5
- package/dist/async-debouncer/createAsyncDebouncer.d.ts +8 -11
- package/dist/async-debouncer/createAsyncDebouncer.js +5 -7
- package/dist/async-queuer/createAsyncQueuer.d.ts +9 -11
- package/dist/async-queuer/createAsyncQueuer.js +4 -5
- package/dist/async-rate-limiter/createAsyncRateLimiter.d.ts +10 -12
- package/dist/async-rate-limiter/createAsyncRateLimiter.js +7 -8
- package/dist/async-throttler/createAsyncThrottler.d.ts +8 -13
- package/dist/async-throttler/createAsyncThrottler.js +5 -9
- package/dist/batcher/createBatcher.d.ts +11 -14
- package/dist/batcher/createBatcher.js +6 -8
- package/dist/debouncer/createDebouncedSignal.d.ts +2 -4
- package/dist/debouncer/createDebouncedSignal.js +1 -2
- package/dist/debouncer/createDebouncedValue.d.ts +2 -4
- package/dist/debouncer/createDebouncedValue.js +1 -2
- package/dist/debouncer/createDebouncer.d.ts +4 -6
- package/dist/debouncer/createDebouncer.js +1 -2
- package/dist/provider/PacerProvider.d.ts +6 -8
- package/dist/provider/PacerProvider.js +1 -2
- package/dist/queuer/createQueuedSignal.d.ts +2 -4
- package/dist/queuer/createQueuedSignal.js +1 -2
- package/dist/queuer/createQueuer.d.ts +4 -6
- package/dist/queuer/createQueuer.js +1 -2
- package/dist/rate-limiter/createRateLimitedSignal.d.ts +12 -13
- package/dist/rate-limiter/createRateLimitedSignal.js +11 -11
- package/dist/rate-limiter/createRateLimitedValue.d.ts +2 -4
- package/dist/rate-limiter/createRateLimitedValue.js +1 -2
- package/dist/rate-limiter/createRateLimiter.d.ts +8 -9
- package/dist/rate-limiter/createRateLimiter.js +5 -5
- package/dist/throttler/createThrottledSignal.d.ts +2 -6
- package/dist/throttler/createThrottledSignal.js +1 -4
- package/dist/throttler/createThrottledValue.d.ts +2 -6
- package/dist/throttler/createThrottledValue.js +1 -4
- package/dist/throttler/createThrottler.d.ts +4 -8
- package/dist/throttler/createThrottler.js +1 -4
- package/package.json +23 -67
- package/dist/async-batcher/createAsyncBatcher.cjs +0 -166
- package/dist/async-batcher/createAsyncBatcher.cjs.map +0 -1
- package/dist/async-batcher/createAsyncBatcher.d.cts +0 -177
- package/dist/async-batcher/createAsyncBatcher.js.map +0 -1
- package/dist/async-batcher/index.cjs +0 -11
- package/dist/async-batcher/index.d.cts +0 -3
- package/dist/async-debouncer/createAsyncDebouncer.cjs +0 -151
- package/dist/async-debouncer/createAsyncDebouncer.cjs.map +0 -1
- package/dist/async-debouncer/createAsyncDebouncer.d.cts +0 -163
- package/dist/async-debouncer/createAsyncDebouncer.js.map +0 -1
- package/dist/async-debouncer/index.cjs +0 -11
- package/dist/async-debouncer/index.d.cts +0 -3
- package/dist/async-queuer/createAsyncQueuer.cjs +0 -158
- package/dist/async-queuer/createAsyncQueuer.cjs.map +0 -1
- package/dist/async-queuer/createAsyncQueuer.d.cts +0 -169
- package/dist/async-queuer/createAsyncQueuer.js.map +0 -1
- package/dist/async-queuer/index.cjs +0 -11
- package/dist/async-queuer/index.d.cts +0 -3
- package/dist/async-rate-limiter/createAsyncRateLimiter.cjs +0 -193
- package/dist/async-rate-limiter/createAsyncRateLimiter.cjs.map +0 -1
- package/dist/async-rate-limiter/createAsyncRateLimiter.d.cts +0 -208
- package/dist/async-rate-limiter/createAsyncRateLimiter.js.map +0 -1
- package/dist/async-rate-limiter/index.cjs +0 -11
- package/dist/async-rate-limiter/index.d.cts +0 -3
- package/dist/async-throttler/createAsyncThrottler.cjs +0 -151
- package/dist/async-throttler/createAsyncThrottler.cjs.map +0 -1
- package/dist/async-throttler/createAsyncThrottler.d.cts +0 -163
- package/dist/async-throttler/createAsyncThrottler.js.map +0 -1
- package/dist/async-throttler/index.cjs +0 -11
- package/dist/async-throttler/index.d.cts +0 -3
- package/dist/batcher/createBatcher.cjs +0 -132
- package/dist/batcher/createBatcher.cjs.map +0 -1
- package/dist/batcher/createBatcher.d.cts +0 -146
- package/dist/batcher/createBatcher.js.map +0 -1
- package/dist/batcher/index.cjs +0 -11
- package/dist/batcher/index.d.cts +0 -3
- package/dist/debouncer/createDebouncedSignal.cjs +0 -88
- package/dist/debouncer/createDebouncedSignal.cjs.map +0 -1
- package/dist/debouncer/createDebouncedSignal.d.cts +0 -79
- package/dist/debouncer/createDebouncedSignal.js.map +0 -1
- package/dist/debouncer/createDebouncedValue.cjs +0 -77
- package/dist/debouncer/createDebouncedValue.cjs.map +0 -1
- package/dist/debouncer/createDebouncedValue.d.cts +0 -70
- package/dist/debouncer/createDebouncedValue.js.map +0 -1
- package/dist/debouncer/createDebouncer.cjs +0 -129
- package/dist/debouncer/createDebouncer.cjs.map +0 -1
- package/dist/debouncer/createDebouncer.d.cts +0 -144
- package/dist/debouncer/createDebouncer.js.map +0 -1
- package/dist/debouncer/index.cjs +0 -15
- package/dist/debouncer/index.d.cts +0 -5
- package/dist/index.cjs +0 -47
- package/dist/index.d.cts +0 -20
- package/dist/provider/PacerProvider.cjs +0 -27
- package/dist/provider/PacerProvider.cjs.map +0 -1
- package/dist/provider/PacerProvider.d.cts +0 -28
- package/dist/provider/PacerProvider.js.map +0 -1
- package/dist/provider/index.cjs +0 -6
- package/dist/provider/index.d.cts +0 -2
- package/dist/queuer/createQueuedSignal.cjs +0 -129
- package/dist/queuer/createQueuedSignal.cjs.map +0 -1
- package/dist/queuer/createQueuedSignal.d.cts +0 -121
- package/dist/queuer/createQueuedSignal.js.map +0 -1
- package/dist/queuer/createQueuer.cjs +0 -133
- package/dist/queuer/createQueuer.cjs.map +0 -1
- package/dist/queuer/createQueuer.d.cts +0 -147
- package/dist/queuer/createQueuer.js.map +0 -1
- package/dist/queuer/index.cjs +0 -13
- package/dist/queuer/index.d.cts +0 -4
- package/dist/rate-limiter/createRateLimitedSignal.cjs +0 -102
- package/dist/rate-limiter/createRateLimitedSignal.cjs.map +0 -1
- package/dist/rate-limiter/createRateLimitedSignal.d.cts +0 -93
- package/dist/rate-limiter/createRateLimitedSignal.js.map +0 -1
- package/dist/rate-limiter/createRateLimitedValue.cjs +0 -102
- package/dist/rate-limiter/createRateLimitedValue.cjs.map +0 -1
- package/dist/rate-limiter/createRateLimitedValue.d.cts +0 -95
- package/dist/rate-limiter/createRateLimitedValue.js.map +0 -1
- package/dist/rate-limiter/createRateLimiter.cjs +0 -157
- package/dist/rate-limiter/createRateLimiter.cjs.map +0 -1
- package/dist/rate-limiter/createRateLimiter.d.cts +0 -173
- package/dist/rate-limiter/createRateLimiter.js.map +0 -1
- package/dist/rate-limiter/index.cjs +0 -15
- package/dist/rate-limiter/index.d.cts +0 -5
- package/dist/throttler/createThrottledSignal.cjs +0 -79
- package/dist/throttler/createThrottledSignal.cjs.map +0 -1
- package/dist/throttler/createThrottledSignal.d.cts +0 -70
- package/dist/throttler/createThrottledSignal.js.map +0 -1
- package/dist/throttler/createThrottledValue.cjs +0 -74
- package/dist/throttler/createThrottledValue.cjs.map +0 -1
- package/dist/throttler/createThrottledValue.d.cts +0 -67
- package/dist/throttler/createThrottledValue.js.map +0 -1
- package/dist/throttler/createThrottler.cjs +0 -132
- package/dist/throttler/createThrottler.cjs.map +0 -1
- package/dist/throttler/createThrottler.d.cts +0 -147
- package/dist/throttler/createThrottler.js.map +0 -1
- package/dist/throttler/index.cjs +0 -15
- package/dist/throttler/index.d.cts +0 -5
- package/dist/types/index.cjs +0 -8
- package/dist/types/index.d.cts +0 -1
- package/dist/utils/index.cjs +0 -8
- package/dist/utils/index.d.cts +0 -1
- package/src/async-batcher/createAsyncBatcher.ts +0 -234
- package/src/async-batcher/index.ts +0 -3
- package/src/async-debouncer/createAsyncDebouncer.ts +0 -225
- package/src/async-debouncer/index.ts +0 -4
- package/src/async-queuer/createAsyncQueuer.ts +0 -226
- package/src/async-queuer/index.ts +0 -3
- package/src/async-rate-limiter/createAsyncRateLimiter.ts +0 -269
- package/src/async-rate-limiter/index.ts +0 -4
- package/src/async-throttler/createAsyncThrottler.ts +0 -225
- package/src/async-throttler/index.ts +0 -4
- package/src/batcher/createBatcher.ts +0 -199
- package/src/batcher/index.ts +0 -3
- package/src/debouncer/createDebouncedSignal.ts +0 -92
- package/src/debouncer/createDebouncedValue.ts +0 -85
- package/src/debouncer/createDebouncer.ts +0 -201
- package/src/debouncer/index.ts +0 -6
- package/src/index.ts +0 -46
- package/src/provider/PacerProvider.tsx +0 -63
- package/src/provider/index.ts +0 -1
- package/src/queuer/createQueuedSignal.ts +0 -138
- package/src/queuer/createQueuer.ts +0 -199
- package/src/queuer/index.ts +0 -4
- package/src/rate-limiter/createRateLimitedSignal.ts +0 -117
- package/src/rate-limiter/createRateLimitedValue.ts +0 -110
- package/src/rate-limiter/createRateLimiter.ts +0 -226
- package/src/rate-limiter/index.ts +0 -5
- package/src/throttler/createThrottledSignal.ts +0 -81
- package/src/throttler/createThrottledValue.ts +0 -82
- package/src/throttler/createThrottler.ts +0 -204
- package/src/throttler/index.ts +0 -6
- package/src/types/index.ts +0 -1
- package/src/utils/index.ts +0 -1
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"createThrottledValue.cjs","names":["createThrottledSignal"],"sources":["../../src/throttler/createThrottledValue.ts"],"sourcesContent":["import { createEffect } from 'solid-js'\nimport { createThrottledSignal } from './createThrottledSignal'\nimport type { SolidThrottler, SolidThrottlerOptions } from './createThrottler'\nimport type { Accessor, Setter } from 'solid-js'\nimport type { ThrottlerState } from '@tanstack/pacer/throttler'\n\n/**\n * A high-level Solid hook that creates a throttled version of a value that updates at most once within a specified time window.\n * This hook uses Solid's createSignal internally to manage the throttled state.\n *\n * Throttling ensures the value updates occur at a controlled rate regardless of how frequently the input value changes.\n * This is useful for rate-limiting expensive updates or API calls that depend on rapidly changing values.\n *\n * The hook returns a tuple containing:\n * - An accessor function that provides the throttled value\n * - The throttler instance with control methods\n *\n * The throttled value will update according to the leading/trailing edge behavior specified in the options.\n *\n * For more direct control over throttling behavior without Solid state management,\n * consider using the lower-level createThrottler hook instead.\n *\n * ## State Management and Selector\n *\n * The hook uses TanStack Store for reactive state management via the underlying throttler instance.\n * The `selector` parameter allows you to specify which throttler state changes will trigger reactive updates,\n * optimizing performance by preventing unnecessary subscriptions 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 reactive updates and gives you\n * full control over when your component subscribes to state changes. Only when you provide a selector will\n * the reactive system track the selected state values.\n *\n * Available throttler state properties:\n * - `canLeadingExecute`: Whether the throttler can execute on the leading edge\n * - `canTrailingExecute`: Whether the throttler can execute on the trailing edge\n * - `executionCount`: Number of function executions that have been completed\n * - `isPending`: Whether the throttler is waiting for the timeout to trigger trailing execution\n * - `lastArgs`: The arguments from the most recent call to maybeExecute\n * - `lastExecutionTime`: Unix timestamp of the last execution\n * - `nextExecutionTime`: Unix timestamp of the next allowed execution\n * - `status`: Current execution status ('disabled' | 'idle' | 'pending')\n *\n * @example\n * ```tsx\n * // Default behavior - no reactive state subscriptions\n * const [throttledValue, throttler] = createThrottledValue(rawValue, { wait: 1000 });\n *\n * // Opt-in to reactive updates when pending state changes (optimized for loading indicators)\n * const [throttledValue, throttler] = createThrottledValue(\n * rawValue,\n * { wait: 1000 },\n * (state) => ({ isPending: state.isPending })\n * );\n *\n * // Use the throttled value\n * console.log(throttledValue()); // Access the current throttled value\n *\n * // Access throttler state via signals\n * console.log('Is pending:', throttler.state().isPending);\n *\n * // Control the throttler\n * throttler.cancel(); // Cancel any pending updates\n * ```\n */\nexport function createThrottledValue<TValue, TSelected = {}>(\n value: Accessor<TValue>,\n initialOptions: SolidThrottlerOptions<Setter<TValue>, TSelected>,\n selector?: (state: ThrottlerState<Setter<TValue>>) => TSelected,\n): [Accessor<TValue>, SolidThrottler<Setter<TValue>, TSelected>] {\n const [throttledValue, setThrottledValue, throttler] = createThrottledSignal(\n value(),\n initialOptions,\n selector,\n )\n\n createEffect(() => {\n setThrottledValue(value() as any)\n })\n\n return [throttledValue, throttler]\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAiEA,SAAgB,qBACd,OACA,gBACA,UAC+D;CAC/D,MAAM,CAAC,gBAAgB,mBAAmB,aAAaA,oDACrD,MAAM,GACN,gBACA,QACF;CAEA,iCAAmB;EACjB,kBAAkB,MAAM,CAAQ;CAClC,CAAC;CAED,OAAO,CAAC,gBAAgB,SAAS;AACnC"}
|
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
import { SolidThrottler, SolidThrottlerOptions } from "./createThrottler.cjs";
|
|
2
|
-
import { Accessor, Setter } from "solid-js";
|
|
3
|
-
import { ThrottlerState } from "@tanstack/pacer/throttler";
|
|
4
|
-
//#region src/throttler/createThrottledValue.d.ts
|
|
5
|
-
/**
|
|
6
|
-
* A high-level Solid hook that creates a throttled version of a value that updates at most once within a specified time window.
|
|
7
|
-
* This hook uses Solid's createSignal internally to manage the throttled state.
|
|
8
|
-
*
|
|
9
|
-
* Throttling ensures the value updates occur at a controlled rate regardless of how frequently the input value changes.
|
|
10
|
-
* This is useful for rate-limiting expensive updates or API calls that depend on rapidly changing values.
|
|
11
|
-
*
|
|
12
|
-
* The hook returns a tuple containing:
|
|
13
|
-
* - An accessor function that provides the throttled value
|
|
14
|
-
* - The throttler instance with control methods
|
|
15
|
-
*
|
|
16
|
-
* The throttled value will update according to the leading/trailing edge behavior specified in the options.
|
|
17
|
-
*
|
|
18
|
-
* For more direct control over throttling behavior without Solid state management,
|
|
19
|
-
* consider using the lower-level createThrottler hook instead.
|
|
20
|
-
*
|
|
21
|
-
* ## State Management and Selector
|
|
22
|
-
*
|
|
23
|
-
* The hook uses TanStack Store for reactive state management via the underlying throttler instance.
|
|
24
|
-
* The `selector` parameter allows you to specify which throttler state changes will trigger reactive updates,
|
|
25
|
-
* optimizing performance by preventing unnecessary subscriptions 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 reactive updates and gives you
|
|
29
|
-
* full control over when your component subscribes to state changes. Only when you provide a selector will
|
|
30
|
-
* the reactive system track the selected state values.
|
|
31
|
-
*
|
|
32
|
-
* Available throttler state properties:
|
|
33
|
-
* - `canLeadingExecute`: Whether the throttler can execute on the leading edge
|
|
34
|
-
* - `canTrailingExecute`: Whether the throttler can execute on the trailing edge
|
|
35
|
-
* - `executionCount`: Number of function executions that have been completed
|
|
36
|
-
* - `isPending`: Whether the throttler is waiting for the timeout to trigger trailing execution
|
|
37
|
-
* - `lastArgs`: The arguments from the most recent call to maybeExecute
|
|
38
|
-
* - `lastExecutionTime`: Unix timestamp of the last execution
|
|
39
|
-
* - `nextExecutionTime`: Unix timestamp of the next allowed execution
|
|
40
|
-
* - `status`: Current execution status ('disabled' | 'idle' | 'pending')
|
|
41
|
-
*
|
|
42
|
-
* @example
|
|
43
|
-
* ```tsx
|
|
44
|
-
* // Default behavior - no reactive state subscriptions
|
|
45
|
-
* const [throttledValue, throttler] = createThrottledValue(rawValue, { wait: 1000 });
|
|
46
|
-
*
|
|
47
|
-
* // Opt-in to reactive updates when pending state changes (optimized for loading indicators)
|
|
48
|
-
* const [throttledValue, throttler] = createThrottledValue(
|
|
49
|
-
* rawValue,
|
|
50
|
-
* { wait: 1000 },
|
|
51
|
-
* (state) => ({ isPending: state.isPending })
|
|
52
|
-
* );
|
|
53
|
-
*
|
|
54
|
-
* // Use the throttled value
|
|
55
|
-
* console.log(throttledValue()); // Access the current throttled value
|
|
56
|
-
*
|
|
57
|
-
* // Access throttler state via signals
|
|
58
|
-
* console.log('Is pending:', throttler.state().isPending);
|
|
59
|
-
*
|
|
60
|
-
* // Control the throttler
|
|
61
|
-
* throttler.cancel(); // Cancel any pending updates
|
|
62
|
-
* ```
|
|
63
|
-
*/
|
|
64
|
-
declare function createThrottledValue<TValue, TSelected = {}>(value: Accessor<TValue>, initialOptions: SolidThrottlerOptions<Setter<TValue>, TSelected>, selector?: (state: ThrottlerState<Setter<TValue>>) => TSelected): [Accessor<TValue>, SolidThrottler<Setter<TValue>, TSelected>];
|
|
65
|
-
//#endregion
|
|
66
|
-
export { createThrottledValue };
|
|
67
|
-
//# sourceMappingURL=createThrottledValue.d.cts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"createThrottledValue.js","names":[],"sources":["../../src/throttler/createThrottledValue.ts"],"sourcesContent":["import { createEffect } from 'solid-js'\nimport { createThrottledSignal } from './createThrottledSignal'\nimport type { SolidThrottler, SolidThrottlerOptions } from './createThrottler'\nimport type { Accessor, Setter } from 'solid-js'\nimport type { ThrottlerState } from '@tanstack/pacer/throttler'\n\n/**\n * A high-level Solid hook that creates a throttled version of a value that updates at most once within a specified time window.\n * This hook uses Solid's createSignal internally to manage the throttled state.\n *\n * Throttling ensures the value updates occur at a controlled rate regardless of how frequently the input value changes.\n * This is useful for rate-limiting expensive updates or API calls that depend on rapidly changing values.\n *\n * The hook returns a tuple containing:\n * - An accessor function that provides the throttled value\n * - The throttler instance with control methods\n *\n * The throttled value will update according to the leading/trailing edge behavior specified in the options.\n *\n * For more direct control over throttling behavior without Solid state management,\n * consider using the lower-level createThrottler hook instead.\n *\n * ## State Management and Selector\n *\n * The hook uses TanStack Store for reactive state management via the underlying throttler instance.\n * The `selector` parameter allows you to specify which throttler state changes will trigger reactive updates,\n * optimizing performance by preventing unnecessary subscriptions 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 reactive updates and gives you\n * full control over when your component subscribes to state changes. Only when you provide a selector will\n * the reactive system track the selected state values.\n *\n * Available throttler state properties:\n * - `canLeadingExecute`: Whether the throttler can execute on the leading edge\n * - `canTrailingExecute`: Whether the throttler can execute on the trailing edge\n * - `executionCount`: Number of function executions that have been completed\n * - `isPending`: Whether the throttler is waiting for the timeout to trigger trailing execution\n * - `lastArgs`: The arguments from the most recent call to maybeExecute\n * - `lastExecutionTime`: Unix timestamp of the last execution\n * - `nextExecutionTime`: Unix timestamp of the next allowed execution\n * - `status`: Current execution status ('disabled' | 'idle' | 'pending')\n *\n * @example\n * ```tsx\n * // Default behavior - no reactive state subscriptions\n * const [throttledValue, throttler] = createThrottledValue(rawValue, { wait: 1000 });\n *\n * // Opt-in to reactive updates when pending state changes (optimized for loading indicators)\n * const [throttledValue, throttler] = createThrottledValue(\n * rawValue,\n * { wait: 1000 },\n * (state) => ({ isPending: state.isPending })\n * );\n *\n * // Use the throttled value\n * console.log(throttledValue()); // Access the current throttled value\n *\n * // Access throttler state via signals\n * console.log('Is pending:', throttler.state().isPending);\n *\n * // Control the throttler\n * throttler.cancel(); // Cancel any pending updates\n * ```\n */\nexport function createThrottledValue<TValue, TSelected = {}>(\n value: Accessor<TValue>,\n initialOptions: SolidThrottlerOptions<Setter<TValue>, TSelected>,\n selector?: (state: ThrottlerState<Setter<TValue>>) => TSelected,\n): [Accessor<TValue>, SolidThrottler<Setter<TValue>, TSelected>] {\n const [throttledValue, setThrottledValue, throttler] = createThrottledSignal(\n value(),\n initialOptions,\n selector,\n )\n\n createEffect(() => {\n setThrottledValue(value() as any)\n })\n\n return [throttledValue, throttler]\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAiEA,SAAgB,qBACd,OACA,gBACA,UAC+D;CAC/D,MAAM,CAAC,gBAAgB,mBAAmB,aAAa,sBACrD,MAAM,GACN,gBACA,QACF;CAEA,mBAAmB;EACjB,kBAAkB,MAAM,CAAQ;CAClC,CAAC;CAED,OAAO,CAAC,gBAAgB,SAAS;AACnC"}
|
|
@@ -1,132 +0,0 @@
|
|
|
1
|
-
const require_PacerProvider = require('../provider/PacerProvider.cjs');
|
|
2
|
-
let solid_js = require("solid-js");
|
|
3
|
-
let _tanstack_solid_store = require("@tanstack/solid-store");
|
|
4
|
-
let _tanstack_pacer_throttler = require("@tanstack/pacer/throttler");
|
|
5
|
-
|
|
6
|
-
//#region src/throttler/createThrottler.ts
|
|
7
|
-
/**
|
|
8
|
-
* A low-level Solid 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 (createSignal, Redux, Zustand, Jotai, etc). For a simpler and higher-level hook that
|
|
12
|
-
* integrates directly with Solid's createSignal, see createThrottledSignal.
|
|
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. You can subscribe to state changes
|
|
21
|
-
* in two ways:
|
|
22
|
-
*
|
|
23
|
-
* **1. Using `throttler.Subscribe` component (Recommended for component tree subscriptions)**
|
|
24
|
-
*
|
|
25
|
-
* Use the `Subscribe` component to subscribe to state changes deep in your component tree without
|
|
26
|
-
* needing to pass a selector to the hook. This is ideal when you want to subscribe to state
|
|
27
|
-
* in child components.
|
|
28
|
-
*
|
|
29
|
-
* **2. Using the `selector` parameter (For hook-level subscriptions)**
|
|
30
|
-
*
|
|
31
|
-
* The `selector` parameter allows you to specify which state changes will trigger reactive updates
|
|
32
|
-
* at the hook level, optimizing performance by preventing unnecessary updates when irrelevant
|
|
33
|
-
* state changes occur.
|
|
34
|
-
*
|
|
35
|
-
* **By default, there will be no reactive state subscriptions** and you must opt-in to state
|
|
36
|
-
* tracking by providing a selector function or using the `Subscribe` component. This prevents unnecessary
|
|
37
|
-
* updates and gives you full control over when your component tracks state changes.
|
|
38
|
-
*
|
|
39
|
-
* Available state properties:
|
|
40
|
-
* - `canLeadingExecute`: Whether the throttler can execute on the leading edge
|
|
41
|
-
* - `canTrailingExecute`: Whether the throttler can execute on the trailing edge
|
|
42
|
-
* - `executionCount`: Number of function executions that have been completed
|
|
43
|
-
* - `isPending`: Whether the throttler is waiting for the timeout to trigger execution
|
|
44
|
-
* - `lastArgs`: The arguments from the most recent call to maybeExecute
|
|
45
|
-
* - `lastExecutionTime`: Timestamp of the last execution
|
|
46
|
-
* - `nextExecutionTime`: Timestamp of the next allowed execution
|
|
47
|
-
* - `status`: Current execution status ('disabled' | 'idle' | 'pending')
|
|
48
|
-
*
|
|
49
|
-
* ## Unmount behavior
|
|
50
|
-
*
|
|
51
|
-
* By default, the primitive cancels any pending execution when the owning component unmounts.
|
|
52
|
-
* Use the `onUnmount` option to customize this. For example, to flush pending work instead:
|
|
53
|
-
*
|
|
54
|
-
* ```tsx
|
|
55
|
-
* const throttler = createThrottler(fn, {
|
|
56
|
-
* wait: 1000,
|
|
57
|
-
* onUnmount: (t) => t.flush()
|
|
58
|
-
* });
|
|
59
|
-
* ```
|
|
60
|
-
*
|
|
61
|
-
* @example
|
|
62
|
-
* ```tsx
|
|
63
|
-
* // Default behavior - no reactive state subscriptions
|
|
64
|
-
* const throttler = createThrottler(setValue, { wait: 1000 });
|
|
65
|
-
*
|
|
66
|
-
* // Subscribe to state changes deep in component tree using Subscribe component
|
|
67
|
-
* <throttler.Subscribe selector={(state) => ({ isPending: state.isPending })}>
|
|
68
|
-
* {(state) => (
|
|
69
|
-
* <div>{state().isPending ? 'Loading...' : 'Ready'}</div>
|
|
70
|
-
* )}
|
|
71
|
-
* </throttler.Subscribe>
|
|
72
|
-
*
|
|
73
|
-
* // Opt-in to track isPending changes at hook level (optimized for loading states)
|
|
74
|
-
* const throttler = createThrottler(
|
|
75
|
-
* setValue,
|
|
76
|
-
* { wait: 1000 },
|
|
77
|
-
* (state) => ({ isPending: state.isPending })
|
|
78
|
-
* );
|
|
79
|
-
*
|
|
80
|
-
* // Opt-in to track executionCount changes (optimized for tracking execution)
|
|
81
|
-
* const throttler = createThrottler(
|
|
82
|
-
* setValue,
|
|
83
|
-
* { wait: 1000 },
|
|
84
|
-
* (state) => ({ executionCount: state.executionCount })
|
|
85
|
-
* );
|
|
86
|
-
*
|
|
87
|
-
* // Multiple state properties - track when any of these change
|
|
88
|
-
* const throttler = createThrottler(
|
|
89
|
-
* setValue,
|
|
90
|
-
* {
|
|
91
|
-
* wait: 2000,
|
|
92
|
-
* leading: true, // Execute immediately on first call
|
|
93
|
-
* trailing: false // Skip trailing edge updates
|
|
94
|
-
* },
|
|
95
|
-
* (state) => ({
|
|
96
|
-
* isPending: state.isPending,
|
|
97
|
-
* executionCount: state.executionCount,
|
|
98
|
-
* lastExecutionTime: state.lastExecutionTime,
|
|
99
|
-
* nextExecutionTime: state.nextExecutionTime
|
|
100
|
-
* })
|
|
101
|
-
* );
|
|
102
|
-
*
|
|
103
|
-
* // Access the selected state (will be empty object {} unless selector provided)
|
|
104
|
-
* const { isPending, executionCount } = throttler.state();
|
|
105
|
-
* ```
|
|
106
|
-
*/
|
|
107
|
-
function createThrottler(fn, options, selector = () => ({})) {
|
|
108
|
-
const mergedOptions = {
|
|
109
|
-
...require_PacerProvider.useDefaultPacerOptions().throttler,
|
|
110
|
-
...options
|
|
111
|
-
};
|
|
112
|
-
const asyncThrottler = new _tanstack_pacer_throttler.Throttler(fn, mergedOptions);
|
|
113
|
-
asyncThrottler.Subscribe = function Subscribe(props) {
|
|
114
|
-
const selected = (0, _tanstack_solid_store.useSelector)(asyncThrottler.store, props.selector, { compare: _tanstack_solid_store.shallow });
|
|
115
|
-
return typeof props.children === "function" ? props.children(selected) : props.children;
|
|
116
|
-
};
|
|
117
|
-
const state = (0, _tanstack_solid_store.useSelector)(asyncThrottler.store, selector, { compare: _tanstack_solid_store.shallow });
|
|
118
|
-
(0, solid_js.createEffect)(() => {
|
|
119
|
-
(0, solid_js.onCleanup)(() => {
|
|
120
|
-
if (mergedOptions.onUnmount) mergedOptions.onUnmount(asyncThrottler);
|
|
121
|
-
else asyncThrottler.cancel();
|
|
122
|
-
});
|
|
123
|
-
});
|
|
124
|
-
return {
|
|
125
|
-
...asyncThrottler,
|
|
126
|
-
state
|
|
127
|
-
};
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
//#endregion
|
|
131
|
-
exports.createThrottler = createThrottler;
|
|
132
|
-
//# sourceMappingURL=createThrottler.cjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"createThrottler.cjs","names":["useDefaultPacerOptions","Throttler","useSelector","shallow"],"sources":["../../src/throttler/createThrottler.ts"],"sourcesContent":["import { Throttler } from '@tanstack/pacer/throttler'\nimport { createEffect, onCleanup } from 'solid-js'\nimport { shallow, useSelector } from '@tanstack/solid-store'\nimport { useDefaultPacerOptions } from '../provider/PacerProvider'\nimport type { Store } from '@tanstack/solid-store'\nimport type { Accessor, JSX } from 'solid-js'\nimport type { AnyFunction } from '@tanstack/pacer/types'\nimport type {\n ThrottlerOptions,\n ThrottlerState,\n} from '@tanstack/pacer/throttler'\n\nexport interface SolidThrottlerOptions<\n TFn extends AnyFunction,\n TSelected = {},\n> extends ThrottlerOptions<TFn> {\n /**\n * Optional callback invoked when the owning component unmounts. Receives the throttler instance.\n * When provided, replaces the default cleanup (cancel); use it to call flush(), reset(), cancel(), add logging, etc.\n */\n onUnmount?: (throttler: SolidThrottler<TFn, TSelected>) => void\n}\n\nexport interface SolidThrottler<\n TFn extends AnyFunction,\n TSelected = {},\n> extends Omit<Throttler<TFn>, 'store'> {\n /**\n * A Solid component that allows you to subscribe to the throttler state.\n *\n * This is useful for tracking specific parts of the throttler state\n * deep in your component tree without needing to pass a selector to the hook.\n *\n * @example\n * <throttler.Subscribe selector={(state) => ({ isPending: state.isPending })}>\n * {(state) => (\n * <div>{state().isPending ? 'Loading...' : 'Ready'}</div>\n * )}\n * </throttler.Subscribe>\n */\n Subscribe: <TSelected>(props: {\n selector: (state: ThrottlerState<TFn>) => TSelected\n children: ((state: Accessor<TSelected>) => JSX.Element) | JSX.Element\n }) => JSX.Element\n /**\n * Reactive state that will be updated when the throttler state changes\n *\n * Use this instead of `throttler.store.state`\n */\n readonly state: Accessor<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 `useSelector` hook internally.\n * Although, you can make the state reactive by using the `useSelector` in your own usage.\n */\n readonly store: Store<Readonly<ThrottlerState<TFn>>>\n}\n\n/**\n * A low-level Solid 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 (createSignal, Redux, Zustand, Jotai, etc). For a simpler and higher-level hook that\n * integrates directly with Solid's createSignal, see createThrottledSignal.\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. You can subscribe to state changes\n * in two ways:\n *\n * **1. Using `throttler.Subscribe` component (Recommended for component tree subscriptions)**\n *\n * Use the `Subscribe` component to subscribe to state changes deep in your component tree without\n * needing to pass a selector to the hook. This is ideal when you want to subscribe to state\n * in child components.\n *\n * **2. Using the `selector` parameter (For hook-level subscriptions)**\n *\n * The `selector` parameter allows you to specify which state changes will trigger reactive updates\n * at the hook level, optimizing performance by preventing unnecessary updates when irrelevant\n * state changes occur.\n *\n * **By default, there will be no reactive state subscriptions** and you must opt-in to state\n * tracking by providing a selector function or using the `Subscribe` component. This prevents unnecessary\n * updates and gives you full control over when your component tracks state changes.\n *\n * Available state properties:\n * - `canLeadingExecute`: Whether the throttler can execute on the leading edge\n * - `canTrailingExecute`: Whether the throttler can execute on the trailing edge\n * - `executionCount`: Number of function executions that have been completed\n * - `isPending`: Whether the throttler is waiting for the timeout to trigger execution\n * - `lastArgs`: The arguments from the most recent call to maybeExecute\n * - `lastExecutionTime`: Timestamp of the last execution\n * - `nextExecutionTime`: Timestamp of the next allowed execution\n * - `status`: Current execution status ('disabled' | 'idle' | 'pending')\n *\n * ## Unmount behavior\n *\n * By default, the primitive cancels any pending execution when the owning component unmounts.\n * Use the `onUnmount` option to customize this. For example, to flush pending work instead:\n *\n * ```tsx\n * const throttler = createThrottler(fn, {\n * wait: 1000,\n * onUnmount: (t) => t.flush()\n * });\n * ```\n *\n * @example\n * ```tsx\n * // Default behavior - no reactive state subscriptions\n * const throttler = createThrottler(setValue, { wait: 1000 });\n *\n * // Subscribe to state changes deep in component tree using Subscribe component\n * <throttler.Subscribe selector={(state) => ({ isPending: state.isPending })}>\n * {(state) => (\n * <div>{state().isPending ? 'Loading...' : 'Ready'}</div>\n * )}\n * </throttler.Subscribe>\n *\n * // Opt-in to track isPending changes at hook level (optimized for loading states)\n * const throttler = createThrottler(\n * setValue,\n * { wait: 1000 },\n * (state) => ({ isPending: state.isPending })\n * );\n *\n * // Opt-in to track executionCount changes (optimized for tracking execution)\n * const throttler = createThrottler(\n * setValue,\n * { wait: 1000 },\n * (state) => ({ executionCount: state.executionCount })\n * );\n *\n * // Multiple state properties - track when any of these change\n * const throttler = createThrottler(\n * setValue,\n * {\n * wait: 2000,\n * leading: true, // Execute immediately on first call\n * trailing: false // Skip trailing edge updates\n * },\n * (state) => ({\n * isPending: state.isPending,\n * executionCount: state.executionCount,\n * lastExecutionTime: state.lastExecutionTime,\n * nextExecutionTime: state.nextExecutionTime\n * })\n * );\n *\n * // Access the selected state (will be empty object {} unless selector provided)\n * const { isPending, executionCount } = throttler.state();\n * ```\n */\nexport function createThrottler<TFn extends AnyFunction, TSelected = {}>(\n fn: TFn,\n options: SolidThrottlerOptions<TFn, TSelected>,\n selector: (state: ThrottlerState<TFn>) => TSelected = () => ({}) as TSelected,\n): SolidThrottler<TFn, TSelected> {\n const mergedOptions = {\n ...useDefaultPacerOptions().throttler,\n ...options,\n } as SolidThrottlerOptions<TFn, TSelected>\n const asyncThrottler = new Throttler<TFn>(\n fn,\n mergedOptions,\n ) as unknown as SolidThrottler<TFn, TSelected>\n\n asyncThrottler.Subscribe = function Subscribe<TSelected>(props: {\n selector: (state: ThrottlerState<TFn>) => TSelected\n children: ((state: Accessor<TSelected>) => JSX.Element) | JSX.Element\n }) {\n const selected = useSelector(asyncThrottler.store, props.selector, {\n compare: shallow,\n })\n\n return typeof props.children === 'function'\n ? props.children(selected)\n : props.children\n }\n\n const state = useSelector(asyncThrottler.store, selector, {\n compare: shallow,\n })\n\n createEffect(() => {\n onCleanup(() => {\n if (mergedOptions.onUnmount) {\n mergedOptions.onUnmount(asyncThrottler)\n } else {\n asyncThrottler.cancel()\n }\n })\n })\n\n return {\n ...asyncThrottler,\n state,\n } as SolidThrottler<TFn, TSelected> // omit `store` in favor of `state`\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA8JA,SAAgB,gBACd,IACA,SACA,kBAA6D,CAAC,IAC9B;CAChC,MAAM,gBAAgB;EACpB,GAAGA,6CAAuB,CAAC,CAAC;EAC5B,GAAG;CACL;CACA,MAAM,iBAAiB,IAAIC,oCACzB,IACA,aACF;CAEA,eAAe,YAAY,SAAS,UAAqB,OAGtD;EACD,MAAM,eAAWC,mCAAY,eAAe,OAAO,MAAM,UAAU,EACjE,SAASC,8BACX,CAAC;EAED,OAAO,OAAO,MAAM,aAAa,aAC7B,MAAM,SAAS,QAAQ,IACvB,MAAM;CACZ;CAEA,MAAM,YAAQD,mCAAY,eAAe,OAAO,UAAU,EACxD,SAASC,8BACX,CAAC;CAED,iCAAmB;EACjB,8BAAgB;GACd,IAAI,cAAc,WAChB,cAAc,UAAU,cAAc;QAEtC,eAAe,OAAO;EAE1B,CAAC;CACH,CAAC;CAED,OAAO;EACL,GAAG;EACH;CACF;AACF"}
|
|
@@ -1,147 +0,0 @@
|
|
|
1
|
-
import { Store } from "@tanstack/solid-store";
|
|
2
|
-
import { Accessor, JSX } from "solid-js";
|
|
3
|
-
import { AnyFunction } from "@tanstack/pacer/types";
|
|
4
|
-
import { Throttler, ThrottlerOptions, ThrottlerState } from "@tanstack/pacer/throttler";
|
|
5
|
-
//#region src/throttler/createThrottler.d.ts
|
|
6
|
-
interface SolidThrottlerOptions<TFn extends AnyFunction, TSelected = {}> extends ThrottlerOptions<TFn> {
|
|
7
|
-
/**
|
|
8
|
-
* Optional callback invoked when the owning component unmounts. Receives the throttler instance.
|
|
9
|
-
* When provided, replaces the default cleanup (cancel); use it to call flush(), reset(), cancel(), add logging, etc.
|
|
10
|
-
*/
|
|
11
|
-
onUnmount?: (throttler: SolidThrottler<TFn, TSelected>) => void;
|
|
12
|
-
}
|
|
13
|
-
interface SolidThrottler<TFn extends AnyFunction, TSelected = {}> extends Omit<Throttler<TFn>, 'store'> {
|
|
14
|
-
/**
|
|
15
|
-
* A Solid component that allows you to subscribe to the throttler state.
|
|
16
|
-
*
|
|
17
|
-
* This is useful for tracking specific parts of the throttler state
|
|
18
|
-
* deep in your component tree without needing to pass a selector to the hook.
|
|
19
|
-
*
|
|
20
|
-
* @example
|
|
21
|
-
* <throttler.Subscribe selector={(state) => ({ isPending: state.isPending })}>
|
|
22
|
-
* {(state) => (
|
|
23
|
-
* <div>{state().isPending ? 'Loading...' : 'Ready'}</div>
|
|
24
|
-
* )}
|
|
25
|
-
* </throttler.Subscribe>
|
|
26
|
-
*/
|
|
27
|
-
Subscribe: <TSelected>(props: {
|
|
28
|
-
selector: (state: ThrottlerState<TFn>) => TSelected;
|
|
29
|
-
children: ((state: Accessor<TSelected>) => JSX.Element) | JSX.Element;
|
|
30
|
-
}) => JSX.Element;
|
|
31
|
-
/**
|
|
32
|
-
* Reactive state that will be updated when the throttler state changes
|
|
33
|
-
*
|
|
34
|
-
* Use this instead of `throttler.store.state`
|
|
35
|
-
*/
|
|
36
|
-
readonly state: Accessor<Readonly<TSelected>>;
|
|
37
|
-
/**
|
|
38
|
-
* @deprecated Use `throttler.state` instead of `throttler.store.state` if you want to read reactive state.
|
|
39
|
-
* The state on the store object is not reactive, as it has not been wrapped in a `useSelector` hook internally.
|
|
40
|
-
* Although, you can make the state reactive by using the `useSelector` in your own usage.
|
|
41
|
-
*/
|
|
42
|
-
readonly store: Store<Readonly<ThrottlerState<TFn>>>;
|
|
43
|
-
}
|
|
44
|
-
/**
|
|
45
|
-
* A low-level Solid hook that creates a `Throttler` instance that limits how often the provided function can execute.
|
|
46
|
-
*
|
|
47
|
-
* This hook is designed to be flexible and state-management agnostic - it simply returns a throttler instance that
|
|
48
|
-
* you can integrate with any state management solution (createSignal, Redux, Zustand, Jotai, etc). For a simpler and higher-level hook that
|
|
49
|
-
* integrates directly with Solid's createSignal, see createThrottledSignal.
|
|
50
|
-
*
|
|
51
|
-
* Throttling ensures a function executes at most once within a specified time window,
|
|
52
|
-
* regardless of how many times it is called. This is useful for rate-limiting
|
|
53
|
-
* expensive operations or UI updates.
|
|
54
|
-
*
|
|
55
|
-
* ## State Management and Selector
|
|
56
|
-
*
|
|
57
|
-
* The hook uses TanStack Store for reactive state management. You can subscribe to state changes
|
|
58
|
-
* in two ways:
|
|
59
|
-
*
|
|
60
|
-
* **1. Using `throttler.Subscribe` component (Recommended for component tree subscriptions)**
|
|
61
|
-
*
|
|
62
|
-
* Use the `Subscribe` component to subscribe to state changes deep in your component tree without
|
|
63
|
-
* needing to pass a selector to the hook. This is ideal when you want to subscribe to state
|
|
64
|
-
* in child components.
|
|
65
|
-
*
|
|
66
|
-
* **2. Using the `selector` parameter (For hook-level subscriptions)**
|
|
67
|
-
*
|
|
68
|
-
* The `selector` parameter allows you to specify which state changes will trigger reactive updates
|
|
69
|
-
* at the hook level, optimizing performance by preventing unnecessary updates when irrelevant
|
|
70
|
-
* state changes occur.
|
|
71
|
-
*
|
|
72
|
-
* **By default, there will be no reactive state subscriptions** and you must opt-in to state
|
|
73
|
-
* tracking by providing a selector function or using the `Subscribe` component. This prevents unnecessary
|
|
74
|
-
* updates and gives you full control over when your component tracks state changes.
|
|
75
|
-
*
|
|
76
|
-
* Available state properties:
|
|
77
|
-
* - `canLeadingExecute`: Whether the throttler can execute on the leading edge
|
|
78
|
-
* - `canTrailingExecute`: Whether the throttler can execute on the trailing edge
|
|
79
|
-
* - `executionCount`: Number of function executions that have been completed
|
|
80
|
-
* - `isPending`: Whether the throttler is waiting for the timeout to trigger execution
|
|
81
|
-
* - `lastArgs`: The arguments from the most recent call to maybeExecute
|
|
82
|
-
* - `lastExecutionTime`: Timestamp of the last execution
|
|
83
|
-
* - `nextExecutionTime`: Timestamp of the next allowed execution
|
|
84
|
-
* - `status`: Current execution status ('disabled' | 'idle' | 'pending')
|
|
85
|
-
*
|
|
86
|
-
* ## Unmount behavior
|
|
87
|
-
*
|
|
88
|
-
* By default, the primitive cancels any pending execution when the owning component unmounts.
|
|
89
|
-
* Use the `onUnmount` option to customize this. For example, to flush pending work instead:
|
|
90
|
-
*
|
|
91
|
-
* ```tsx
|
|
92
|
-
* const throttler = createThrottler(fn, {
|
|
93
|
-
* wait: 1000,
|
|
94
|
-
* onUnmount: (t) => t.flush()
|
|
95
|
-
* });
|
|
96
|
-
* ```
|
|
97
|
-
*
|
|
98
|
-
* @example
|
|
99
|
-
* ```tsx
|
|
100
|
-
* // Default behavior - no reactive state subscriptions
|
|
101
|
-
* const throttler = createThrottler(setValue, { wait: 1000 });
|
|
102
|
-
*
|
|
103
|
-
* // Subscribe to state changes deep in component tree using Subscribe component
|
|
104
|
-
* <throttler.Subscribe selector={(state) => ({ isPending: state.isPending })}>
|
|
105
|
-
* {(state) => (
|
|
106
|
-
* <div>{state().isPending ? 'Loading...' : 'Ready'}</div>
|
|
107
|
-
* )}
|
|
108
|
-
* </throttler.Subscribe>
|
|
109
|
-
*
|
|
110
|
-
* // Opt-in to track isPending changes at hook level (optimized for loading states)
|
|
111
|
-
* const throttler = createThrottler(
|
|
112
|
-
* setValue,
|
|
113
|
-
* { wait: 1000 },
|
|
114
|
-
* (state) => ({ isPending: state.isPending })
|
|
115
|
-
* );
|
|
116
|
-
*
|
|
117
|
-
* // Opt-in to track executionCount changes (optimized for tracking execution)
|
|
118
|
-
* const throttler = createThrottler(
|
|
119
|
-
* setValue,
|
|
120
|
-
* { wait: 1000 },
|
|
121
|
-
* (state) => ({ executionCount: state.executionCount })
|
|
122
|
-
* );
|
|
123
|
-
*
|
|
124
|
-
* // Multiple state properties - track when any of these change
|
|
125
|
-
* const throttler = createThrottler(
|
|
126
|
-
* setValue,
|
|
127
|
-
* {
|
|
128
|
-
* wait: 2000,
|
|
129
|
-
* leading: true, // Execute immediately on first call
|
|
130
|
-
* trailing: false // Skip trailing edge updates
|
|
131
|
-
* },
|
|
132
|
-
* (state) => ({
|
|
133
|
-
* isPending: state.isPending,
|
|
134
|
-
* executionCount: state.executionCount,
|
|
135
|
-
* lastExecutionTime: state.lastExecutionTime,
|
|
136
|
-
* nextExecutionTime: state.nextExecutionTime
|
|
137
|
-
* })
|
|
138
|
-
* );
|
|
139
|
-
*
|
|
140
|
-
* // Access the selected state (will be empty object {} unless selector provided)
|
|
141
|
-
* const { isPending, executionCount } = throttler.state();
|
|
142
|
-
* ```
|
|
143
|
-
*/
|
|
144
|
-
declare function createThrottler<TFn extends AnyFunction, TSelected = {}>(fn: TFn, options: SolidThrottlerOptions<TFn, TSelected>, selector?: (state: ThrottlerState<TFn>) => TSelected): SolidThrottler<TFn, TSelected>;
|
|
145
|
-
//#endregion
|
|
146
|
-
export { SolidThrottler, SolidThrottlerOptions, createThrottler };
|
|
147
|
-
//# sourceMappingURL=createThrottler.d.cts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"createThrottler.js","names":[],"sources":["../../src/throttler/createThrottler.ts"],"sourcesContent":["import { Throttler } from '@tanstack/pacer/throttler'\nimport { createEffect, onCleanup } from 'solid-js'\nimport { shallow, useSelector } from '@tanstack/solid-store'\nimport { useDefaultPacerOptions } from '../provider/PacerProvider'\nimport type { Store } from '@tanstack/solid-store'\nimport type { Accessor, JSX } from 'solid-js'\nimport type { AnyFunction } from '@tanstack/pacer/types'\nimport type {\n ThrottlerOptions,\n ThrottlerState,\n} from '@tanstack/pacer/throttler'\n\nexport interface SolidThrottlerOptions<\n TFn extends AnyFunction,\n TSelected = {},\n> extends ThrottlerOptions<TFn> {\n /**\n * Optional callback invoked when the owning component unmounts. Receives the throttler instance.\n * When provided, replaces the default cleanup (cancel); use it to call flush(), reset(), cancel(), add logging, etc.\n */\n onUnmount?: (throttler: SolidThrottler<TFn, TSelected>) => void\n}\n\nexport interface SolidThrottler<\n TFn extends AnyFunction,\n TSelected = {},\n> extends Omit<Throttler<TFn>, 'store'> {\n /**\n * A Solid component that allows you to subscribe to the throttler state.\n *\n * This is useful for tracking specific parts of the throttler state\n * deep in your component tree without needing to pass a selector to the hook.\n *\n * @example\n * <throttler.Subscribe selector={(state) => ({ isPending: state.isPending })}>\n * {(state) => (\n * <div>{state().isPending ? 'Loading...' : 'Ready'}</div>\n * )}\n * </throttler.Subscribe>\n */\n Subscribe: <TSelected>(props: {\n selector: (state: ThrottlerState<TFn>) => TSelected\n children: ((state: Accessor<TSelected>) => JSX.Element) | JSX.Element\n }) => JSX.Element\n /**\n * Reactive state that will be updated when the throttler state changes\n *\n * Use this instead of `throttler.store.state`\n */\n readonly state: Accessor<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 `useSelector` hook internally.\n * Although, you can make the state reactive by using the `useSelector` in your own usage.\n */\n readonly store: Store<Readonly<ThrottlerState<TFn>>>\n}\n\n/**\n * A low-level Solid 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 (createSignal, Redux, Zustand, Jotai, etc). For a simpler and higher-level hook that\n * integrates directly with Solid's createSignal, see createThrottledSignal.\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. You can subscribe to state changes\n * in two ways:\n *\n * **1. Using `throttler.Subscribe` component (Recommended for component tree subscriptions)**\n *\n * Use the `Subscribe` component to subscribe to state changes deep in your component tree without\n * needing to pass a selector to the hook. This is ideal when you want to subscribe to state\n * in child components.\n *\n * **2. Using the `selector` parameter (For hook-level subscriptions)**\n *\n * The `selector` parameter allows you to specify which state changes will trigger reactive updates\n * at the hook level, optimizing performance by preventing unnecessary updates when irrelevant\n * state changes occur.\n *\n * **By default, there will be no reactive state subscriptions** and you must opt-in to state\n * tracking by providing a selector function or using the `Subscribe` component. This prevents unnecessary\n * updates and gives you full control over when your component tracks state changes.\n *\n * Available state properties:\n * - `canLeadingExecute`: Whether the throttler can execute on the leading edge\n * - `canTrailingExecute`: Whether the throttler can execute on the trailing edge\n * - `executionCount`: Number of function executions that have been completed\n * - `isPending`: Whether the throttler is waiting for the timeout to trigger execution\n * - `lastArgs`: The arguments from the most recent call to maybeExecute\n * - `lastExecutionTime`: Timestamp of the last execution\n * - `nextExecutionTime`: Timestamp of the next allowed execution\n * - `status`: Current execution status ('disabled' | 'idle' | 'pending')\n *\n * ## Unmount behavior\n *\n * By default, the primitive cancels any pending execution when the owning component unmounts.\n * Use the `onUnmount` option to customize this. For example, to flush pending work instead:\n *\n * ```tsx\n * const throttler = createThrottler(fn, {\n * wait: 1000,\n * onUnmount: (t) => t.flush()\n * });\n * ```\n *\n * @example\n * ```tsx\n * // Default behavior - no reactive state subscriptions\n * const throttler = createThrottler(setValue, { wait: 1000 });\n *\n * // Subscribe to state changes deep in component tree using Subscribe component\n * <throttler.Subscribe selector={(state) => ({ isPending: state.isPending })}>\n * {(state) => (\n * <div>{state().isPending ? 'Loading...' : 'Ready'}</div>\n * )}\n * </throttler.Subscribe>\n *\n * // Opt-in to track isPending changes at hook level (optimized for loading states)\n * const throttler = createThrottler(\n * setValue,\n * { wait: 1000 },\n * (state) => ({ isPending: state.isPending })\n * );\n *\n * // Opt-in to track executionCount changes (optimized for tracking execution)\n * const throttler = createThrottler(\n * setValue,\n * { wait: 1000 },\n * (state) => ({ executionCount: state.executionCount })\n * );\n *\n * // Multiple state properties - track when any of these change\n * const throttler = createThrottler(\n * setValue,\n * {\n * wait: 2000,\n * leading: true, // Execute immediately on first call\n * trailing: false // Skip trailing edge updates\n * },\n * (state) => ({\n * isPending: state.isPending,\n * executionCount: state.executionCount,\n * lastExecutionTime: state.lastExecutionTime,\n * nextExecutionTime: state.nextExecutionTime\n * })\n * );\n *\n * // Access the selected state (will be empty object {} unless selector provided)\n * const { isPending, executionCount } = throttler.state();\n * ```\n */\nexport function createThrottler<TFn extends AnyFunction, TSelected = {}>(\n fn: TFn,\n options: SolidThrottlerOptions<TFn, TSelected>,\n selector: (state: ThrottlerState<TFn>) => TSelected = () => ({}) as TSelected,\n): SolidThrottler<TFn, TSelected> {\n const mergedOptions = {\n ...useDefaultPacerOptions().throttler,\n ...options,\n } as SolidThrottlerOptions<TFn, TSelected>\n const asyncThrottler = new Throttler<TFn>(\n fn,\n mergedOptions,\n ) as unknown as SolidThrottler<TFn, TSelected>\n\n asyncThrottler.Subscribe = function Subscribe<TSelected>(props: {\n selector: (state: ThrottlerState<TFn>) => TSelected\n children: ((state: Accessor<TSelected>) => JSX.Element) | JSX.Element\n }) {\n const selected = useSelector(asyncThrottler.store, props.selector, {\n compare: shallow,\n })\n\n return typeof props.children === 'function'\n ? props.children(selected)\n : props.children\n }\n\n const state = useSelector(asyncThrottler.store, selector, {\n compare: shallow,\n })\n\n createEffect(() => {\n onCleanup(() => {\n if (mergedOptions.onUnmount) {\n mergedOptions.onUnmount(asyncThrottler)\n } else {\n asyncThrottler.cancel()\n }\n })\n })\n\n return {\n ...asyncThrottler,\n state,\n } as SolidThrottler<TFn, TSelected> // omit `store` in favor of `state`\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA8JA,SAAgB,gBACd,IACA,SACA,kBAA6D,CAAC,IAC9B;CAChC,MAAM,gBAAgB;EACpB,GAAG,uBAAuB,CAAC,CAAC;EAC5B,GAAG;CACL;CACA,MAAM,iBAAiB,IAAI,UACzB,IACA,aACF;CAEA,eAAe,YAAY,SAAS,UAAqB,OAGtD;EACD,MAAM,WAAW,YAAY,eAAe,OAAO,MAAM,UAAU,EACjE,SAAS,QACX,CAAC;EAED,OAAO,OAAO,MAAM,aAAa,aAC7B,MAAM,SAAS,QAAQ,IACvB,MAAM;CACZ;CAEA,MAAM,QAAQ,YAAY,eAAe,OAAO,UAAU,EACxD,SAAS,QACX,CAAC;CAED,mBAAmB;EACjB,gBAAgB;GACd,IAAI,cAAc,WAChB,cAAc,UAAU,cAAc;QAEtC,eAAe,OAAO;EAE1B,CAAC;CACH,CAAC;CAED,OAAO;EACL,GAAG;EACH;CACF;AACF"}
|
package/dist/throttler/index.cjs
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
-
const require_createThrottler = require('./createThrottler.cjs');
|
|
3
|
-
const require_createThrottledSignal = require('./createThrottledSignal.cjs');
|
|
4
|
-
const require_createThrottledValue = require('./createThrottledValue.cjs');
|
|
5
|
-
|
|
6
|
-
exports.createThrottledSignal = require_createThrottledSignal.createThrottledSignal;
|
|
7
|
-
exports.createThrottledValue = require_createThrottledValue.createThrottledValue;
|
|
8
|
-
exports.createThrottler = require_createThrottler.createThrottler;
|
|
9
|
-
var _tanstack_pacer_throttler = require("@tanstack/pacer/throttler");
|
|
10
|
-
Object.keys(_tanstack_pacer_throttler).forEach(function (k) {
|
|
11
|
-
if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
|
|
12
|
-
enumerable: true,
|
|
13
|
-
get: function () { return _tanstack_pacer_throttler[k]; }
|
|
14
|
-
});
|
|
15
|
-
});
|
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
import { SolidThrottler, SolidThrottlerOptions, createThrottler } from "./createThrottler.cjs";
|
|
2
|
-
import { createThrottledSignal } from "./createThrottledSignal.cjs";
|
|
3
|
-
import { createThrottledValue } from "./createThrottledValue.cjs";
|
|
4
|
-
export * from "@tanstack/pacer/throttler";
|
|
5
|
-
export { SolidThrottler, SolidThrottlerOptions, createThrottledSignal, createThrottledValue, createThrottler };
|
package/dist/types/index.cjs
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
var _tanstack_pacer_types = require("@tanstack/pacer/types");
|
|
3
|
-
Object.keys(_tanstack_pacer_types).forEach(function (k) {
|
|
4
|
-
if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
|
|
5
|
-
enumerable: true,
|
|
6
|
-
get: function () { return _tanstack_pacer_types[k]; }
|
|
7
|
-
});
|
|
8
|
-
});
|
package/dist/types/index.d.cts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from "@tanstack/pacer/types";
|
package/dist/utils/index.cjs
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
var _tanstack_pacer_utils = require("@tanstack/pacer/utils");
|
|
3
|
-
Object.keys(_tanstack_pacer_utils).forEach(function (k) {
|
|
4
|
-
if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
|
|
5
|
-
enumerable: true,
|
|
6
|
-
get: function () { return _tanstack_pacer_utils[k]; }
|
|
7
|
-
});
|
|
8
|
-
});
|
package/dist/utils/index.d.cts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from "@tanstack/pacer/utils";
|