@tanstack/solid-pacer 0.16.1 → 0.18.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/async-batcher/createAsyncBatcher.cjs +26 -12
- package/dist/async-batcher/createAsyncBatcher.cjs.map +1 -1
- package/dist/async-batcher/createAsyncBatcher.d.cts +36 -9
- package/dist/async-batcher/createAsyncBatcher.d.ts +36 -9
- package/dist/async-batcher/createAsyncBatcher.js +22 -8
- package/dist/async-batcher/createAsyncBatcher.js.map +1 -1
- package/dist/async-batcher/index.cjs +3 -3
- package/dist/async-debouncer/createAsyncDebouncer.cjs +26 -12
- package/dist/async-debouncer/createAsyncDebouncer.cjs.map +1 -1
- package/dist/async-debouncer/createAsyncDebouncer.d.cts +36 -9
- package/dist/async-debouncer/createAsyncDebouncer.d.ts +36 -9
- package/dist/async-debouncer/createAsyncDebouncer.js +22 -8
- package/dist/async-debouncer/createAsyncDebouncer.js.map +1 -1
- package/dist/async-debouncer/index.cjs +3 -3
- package/dist/async-queuer/createAsyncQueuer.cjs +26 -12
- package/dist/async-queuer/createAsyncQueuer.cjs.map +1 -1
- package/dist/async-queuer/createAsyncQueuer.d.cts +36 -9
- package/dist/async-queuer/createAsyncQueuer.d.ts +36 -9
- package/dist/async-queuer/createAsyncQueuer.js +22 -8
- package/dist/async-queuer/createAsyncQueuer.js.map +1 -1
- package/dist/async-queuer/index.cjs +3 -3
- package/dist/async-rate-limiter/createAsyncRateLimiter.cjs +89 -30
- package/dist/async-rate-limiter/createAsyncRateLimiter.cjs.map +1 -1
- package/dist/async-rate-limiter/createAsyncRateLimiter.d.cts +99 -27
- package/dist/async-rate-limiter/createAsyncRateLimiter.d.ts +99 -27
- package/dist/async-rate-limiter/createAsyncRateLimiter.js +85 -26
- package/dist/async-rate-limiter/createAsyncRateLimiter.js.map +1 -1
- package/dist/async-rate-limiter/index.cjs +3 -3
- package/dist/async-throttler/createAsyncThrottler.cjs +26 -12
- package/dist/async-throttler/createAsyncThrottler.cjs.map +1 -1
- package/dist/async-throttler/createAsyncThrottler.d.cts +36 -9
- package/dist/async-throttler/createAsyncThrottler.d.ts +36 -9
- package/dist/async-throttler/createAsyncThrottler.js +22 -8
- package/dist/async-throttler/createAsyncThrottler.js.map +1 -1
- package/dist/async-throttler/index.cjs +3 -3
- package/dist/batcher/createBatcher.cjs +26 -12
- package/dist/batcher/createBatcher.cjs.map +1 -1
- package/dist/batcher/createBatcher.d.cts +36 -9
- package/dist/batcher/createBatcher.d.ts +36 -9
- package/dist/batcher/createBatcher.js +22 -8
- package/dist/batcher/createBatcher.js.map +1 -1
- package/dist/batcher/index.cjs +3 -3
- package/dist/debouncer/createDebouncer.cjs +27 -13
- package/dist/debouncer/createDebouncer.cjs.map +1 -1
- package/dist/debouncer/createDebouncer.d.cts +37 -10
- package/dist/debouncer/createDebouncer.d.ts +37 -10
- package/dist/debouncer/createDebouncer.js +23 -9
- package/dist/debouncer/createDebouncer.js.map +1 -1
- package/dist/debouncer/index.cjs +3 -3
- package/dist/index.cjs +3 -3
- package/dist/queuer/createQueuedSignal.cjs +8 -8
- package/dist/queuer/createQueuedSignal.cjs.map +1 -1
- package/dist/queuer/createQueuedSignal.d.cts +8 -8
- package/dist/queuer/createQueuedSignal.d.ts +8 -8
- package/dist/queuer/createQueuedSignal.js +8 -8
- package/dist/queuer/createQueuedSignal.js.map +1 -1
- package/dist/queuer/createQueuer.cjs +26 -12
- package/dist/queuer/createQueuer.cjs.map +1 -1
- package/dist/queuer/createQueuer.d.cts +36 -9
- package/dist/queuer/createQueuer.d.ts +36 -9
- package/dist/queuer/createQueuer.js +22 -8
- package/dist/queuer/createQueuer.js.map +1 -1
- package/dist/queuer/index.cjs +3 -3
- package/dist/rate-limiter/createRateLimiter.cjs +78 -23
- package/dist/rate-limiter/createRateLimiter.cjs.map +1 -1
- package/dist/rate-limiter/createRateLimiter.d.cts +88 -20
- package/dist/rate-limiter/createRateLimiter.d.ts +88 -20
- package/dist/rate-limiter/createRateLimiter.js +74 -19
- package/dist/rate-limiter/createRateLimiter.js.map +1 -1
- package/dist/rate-limiter/index.cjs +3 -3
- package/dist/throttler/createThrottledSignal.cjs +1 -1
- package/dist/throttler/createThrottledSignal.cjs.map +1 -1
- package/dist/throttler/createThrottledSignal.d.cts +1 -1
- package/dist/throttler/createThrottledSignal.d.ts +1 -1
- package/dist/throttler/createThrottledSignal.js +1 -1
- package/dist/throttler/createThrottledSignal.js.map +1 -1
- package/dist/throttler/createThrottledValue.cjs +1 -1
- package/dist/throttler/createThrottledValue.cjs.map +1 -1
- package/dist/throttler/createThrottledValue.d.cts +1 -1
- package/dist/throttler/createThrottledValue.d.ts +1 -1
- package/dist/throttler/createThrottledValue.js +1 -1
- package/dist/throttler/createThrottledValue.js.map +1 -1
- package/dist/throttler/createThrottler.cjs +34 -13
- package/dist/throttler/createThrottler.cjs.map +1 -1
- package/dist/throttler/createThrottler.d.cts +44 -10
- package/dist/throttler/createThrottler.d.ts +44 -10
- package/dist/throttler/createThrottler.js +30 -9
- package/dist/throttler/createThrottler.js.map +1 -1
- package/dist/throttler/index.cjs +3 -3
- package/dist/types/index.cjs +3 -3
- package/dist/utils/index.cjs +3 -3
- package/package.json +2 -2
- package/src/async-batcher/createAsyncBatcher.ts +51 -10
- package/src/async-debouncer/createAsyncDebouncer.ts +51 -10
- package/src/async-queuer/createAsyncQueuer.ts +51 -10
- package/src/async-rate-limiter/createAsyncRateLimiter.ts +114 -28
- package/src/async-throttler/createAsyncThrottler.ts +51 -10
- package/src/batcher/createBatcher.ts +51 -10
- package/src/debouncer/createDebouncer.ts +52 -11
- package/src/queuer/createQueuedSignal.ts +8 -8
- package/src/queuer/createQueuer.ts +51 -10
- package/src/rate-limiter/createRateLimiter.ts +103 -21
- package/src/throttler/createThrottledSignal.ts +1 -1
- package/src/throttler/createThrottledValue.ts +1 -1
- package/src/throttler/createThrottler.ts +59 -11
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createThrottler.cjs","names":["Throttler","useDefaultPacerOptions"],"sources":["../../src/throttler/createThrottler.ts"],"sourcesContent":["import { Throttler } from '@tanstack/pacer/throttler'\nimport { createEffect, onCleanup } from 'solid-js'\nimport { useStore } from '@tanstack/solid-store'\nimport { useDefaultPacerOptions } from '../provider/PacerProvider'\nimport type { Store } from '@tanstack/solid-store'\nimport type { Accessor } from 'solid-js'\nimport type { AnyFunction } from '@tanstack/pacer/types'\nimport type {\n ThrottlerOptions,\n ThrottlerState,\n} from '@tanstack/pacer/throttler'\n\nexport interface SolidThrottler<\n TFn extends AnyFunction,\n TSelected = {},\n> extends Omit<Throttler<TFn>, 'store'> {\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 `useStore` hook internally.\n * Although, you can make the state reactive by using the `useStore` in your own usage.\n */\n readonly store: Store<Readonly<ThrottlerState<TFn>>>\n}\n\n/**\n * A low-level 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. The `selector` parameter allows you
|
|
1
|
+
{"version":3,"file":"createThrottler.cjs","names":["Throttler","useDefaultPacerOptions"],"sources":["../../src/throttler/createThrottler.ts"],"sourcesContent":["import { Throttler } from '@tanstack/pacer/throttler'\nimport { createEffect, onCleanup } from 'solid-js'\nimport { useStore } 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 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 `useStore` hook internally.\n * Although, you can make the state reactive by using the `useStore` in your own usage.\n */\n readonly store: Store<Readonly<ThrottlerState<TFn>>>\n}\n\n/**\n * A low-level 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 * @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: ThrottlerOptions<TFn>,\n selector: (state: ThrottlerState<TFn>) => TSelected = () => ({}) as TSelected,\n): SolidThrottler<TFn, TSelected> {\n const mergedOptions = {\n ...useDefaultPacerOptions().throttler,\n ...options,\n } as ThrottlerOptions<TFn>\n\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 = useStore(asyncThrottler.store, props.selector)\n\n return typeof props.children === 'function'\n ? props.children(selected)\n : props.children\n }\n\n const state = useStore(asyncThrottler.store, selector)\n\n createEffect(() => {\n onCleanup(() => {\n asyncThrottler.cancel()\n })\n })\n\n return {\n ...asyncThrottler,\n state,\n } as SolidThrottler<TFn, TSelected> // omit `store` in favor of `state`\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAuIA,SAAgB,gBACd,IACA,SACA,kBAA6D,EAAE,GAC/B;CAMhC,MAAM,iBAAiB,IAAIA,oCACzB,IANoB;EACpB,GAAGC,8CAAwB,CAAC;EAC5B,GAAG;EACJ,CAKA;AAED,gBAAe,YAAY,SAAS,UAAqB,OAGtD;EACD,MAAM,+CAAoB,eAAe,OAAO,MAAM,SAAS;AAE/D,SAAO,OAAO,MAAM,aAAa,aAC7B,MAAM,SAAS,SAAS,GACxB,MAAM;;CAGZ,MAAM,4CAAiB,eAAe,OAAO,SAAS;AAEtD,kCAAmB;AACjB,gCAAgB;AACd,kBAAe,QAAQ;IACvB;GACF;AAEF,QAAO;EACL,GAAG;EACH;EACD"}
|
|
@@ -1,10 +1,27 @@
|
|
|
1
1
|
import { Store } from "@tanstack/solid-store";
|
|
2
|
-
import { Accessor } from "solid-js";
|
|
2
|
+
import { Accessor, JSX } from "solid-js";
|
|
3
3
|
import { AnyFunction } from "@tanstack/pacer/types";
|
|
4
4
|
import { Throttler, ThrottlerOptions, ThrottlerState } from "@tanstack/pacer/throttler";
|
|
5
5
|
|
|
6
6
|
//#region src/throttler/createThrottler.d.ts
|
|
7
7
|
interface SolidThrottler<TFn extends AnyFunction, TSelected = {}> extends Omit<Throttler<TFn>, 'store'> {
|
|
8
|
+
/**
|
|
9
|
+
* A Solid component that allows you to subscribe to the throttler state.
|
|
10
|
+
*
|
|
11
|
+
* This is useful for tracking specific parts of the throttler state
|
|
12
|
+
* deep in your component tree without needing to pass a selector to the hook.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* <throttler.Subscribe selector={(state) => ({ isPending: state.isPending })}>
|
|
16
|
+
* {(state) => (
|
|
17
|
+
* <div>{state().isPending ? 'Loading...' : 'Ready'}</div>
|
|
18
|
+
* )}
|
|
19
|
+
* </throttler.Subscribe>
|
|
20
|
+
*/
|
|
21
|
+
Subscribe: <TSelected>(props: {
|
|
22
|
+
selector: (state: ThrottlerState<TFn>) => TSelected;
|
|
23
|
+
children: ((state: Accessor<TSelected>) => JSX.Element) | JSX.Element;
|
|
24
|
+
}) => JSX.Element;
|
|
8
25
|
/**
|
|
9
26
|
* Reactive state that will be updated when the throttler state changes
|
|
10
27
|
*
|
|
@@ -31,14 +48,24 @@ interface SolidThrottler<TFn extends AnyFunction, TSelected = {}> extends Omit<T
|
|
|
31
48
|
*
|
|
32
49
|
* ## State Management and Selector
|
|
33
50
|
*
|
|
34
|
-
* The hook uses TanStack Store for reactive state management.
|
|
35
|
-
*
|
|
36
|
-
*
|
|
51
|
+
* The hook uses TanStack Store for reactive state management. You can subscribe to state changes
|
|
52
|
+
* in two ways:
|
|
53
|
+
*
|
|
54
|
+
* **1. Using `throttler.Subscribe` component (Recommended for component tree subscriptions)**
|
|
55
|
+
*
|
|
56
|
+
* Use the `Subscribe` component to subscribe to state changes deep in your component tree without
|
|
57
|
+
* needing to pass a selector to the hook. This is ideal when you want to subscribe to state
|
|
58
|
+
* in child components.
|
|
59
|
+
*
|
|
60
|
+
* **2. Using the `selector` parameter (For hook-level subscriptions)**
|
|
61
|
+
*
|
|
62
|
+
* The `selector` parameter allows you to specify which state changes will trigger reactive updates
|
|
63
|
+
* at the hook level, optimizing performance by preventing unnecessary updates when irrelevant
|
|
64
|
+
* state changes occur.
|
|
37
65
|
*
|
|
38
66
|
* **By default, there will be no reactive state subscriptions** and you must opt-in to state
|
|
39
|
-
* tracking by providing a selector function. This prevents unnecessary
|
|
40
|
-
* full control over when your component
|
|
41
|
-
* component re-render when the selected state values change.
|
|
67
|
+
* tracking by providing a selector function or using the `Subscribe` component. This prevents unnecessary
|
|
68
|
+
* updates and gives you full control over when your component tracks state changes.
|
|
42
69
|
*
|
|
43
70
|
* Available state properties:
|
|
44
71
|
* - `canLeadingExecute`: Whether the throttler can execute on the leading edge
|
|
@@ -55,21 +82,28 @@ interface SolidThrottler<TFn extends AnyFunction, TSelected = {}> extends Omit<T
|
|
|
55
82
|
* // Default behavior - no reactive state subscriptions
|
|
56
83
|
* const throttler = createThrottler(setValue, { wait: 1000 });
|
|
57
84
|
*
|
|
58
|
-
* //
|
|
85
|
+
* // Subscribe to state changes deep in component tree using Subscribe component
|
|
86
|
+
* <throttler.Subscribe selector={(state) => ({ isPending: state.isPending })}>
|
|
87
|
+
* {(state) => (
|
|
88
|
+
* <div>{state().isPending ? 'Loading...' : 'Ready'}</div>
|
|
89
|
+
* )}
|
|
90
|
+
* </throttler.Subscribe>
|
|
91
|
+
*
|
|
92
|
+
* // Opt-in to track isPending changes at hook level (optimized for loading states)
|
|
59
93
|
* const throttler = createThrottler(
|
|
60
94
|
* setValue,
|
|
61
95
|
* { wait: 1000 },
|
|
62
96
|
* (state) => ({ isPending: state.isPending })
|
|
63
97
|
* );
|
|
64
98
|
*
|
|
65
|
-
* // Opt-in to
|
|
99
|
+
* // Opt-in to track executionCount changes (optimized for tracking execution)
|
|
66
100
|
* const throttler = createThrottler(
|
|
67
101
|
* setValue,
|
|
68
102
|
* { wait: 1000 },
|
|
69
103
|
* (state) => ({ executionCount: state.executionCount })
|
|
70
104
|
* );
|
|
71
105
|
*
|
|
72
|
-
* // Multiple state properties -
|
|
106
|
+
* // Multiple state properties - track when any of these change
|
|
73
107
|
* const throttler = createThrottler(
|
|
74
108
|
* setValue,
|
|
75
109
|
* {
|
|
@@ -1,10 +1,27 @@
|
|
|
1
|
-
import { Accessor } from "solid-js";
|
|
1
|
+
import { Accessor, JSX } from "solid-js";
|
|
2
2
|
import { Store } from "@tanstack/solid-store";
|
|
3
3
|
import { Throttler, ThrottlerOptions, ThrottlerState } from "@tanstack/pacer/throttler";
|
|
4
4
|
import { AnyFunction } from "@tanstack/pacer/types";
|
|
5
5
|
|
|
6
6
|
//#region src/throttler/createThrottler.d.ts
|
|
7
7
|
interface SolidThrottler<TFn extends AnyFunction, TSelected = {}> extends Omit<Throttler<TFn>, 'store'> {
|
|
8
|
+
/**
|
|
9
|
+
* A Solid component that allows you to subscribe to the throttler state.
|
|
10
|
+
*
|
|
11
|
+
* This is useful for tracking specific parts of the throttler state
|
|
12
|
+
* deep in your component tree without needing to pass a selector to the hook.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* <throttler.Subscribe selector={(state) => ({ isPending: state.isPending })}>
|
|
16
|
+
* {(state) => (
|
|
17
|
+
* <div>{state().isPending ? 'Loading...' : 'Ready'}</div>
|
|
18
|
+
* )}
|
|
19
|
+
* </throttler.Subscribe>
|
|
20
|
+
*/
|
|
21
|
+
Subscribe: <TSelected>(props: {
|
|
22
|
+
selector: (state: ThrottlerState<TFn>) => TSelected;
|
|
23
|
+
children: ((state: Accessor<TSelected>) => JSX.Element) | JSX.Element;
|
|
24
|
+
}) => JSX.Element;
|
|
8
25
|
/**
|
|
9
26
|
* Reactive state that will be updated when the throttler state changes
|
|
10
27
|
*
|
|
@@ -31,14 +48,24 @@ interface SolidThrottler<TFn extends AnyFunction, TSelected = {}> extends Omit<T
|
|
|
31
48
|
*
|
|
32
49
|
* ## State Management and Selector
|
|
33
50
|
*
|
|
34
|
-
* The hook uses TanStack Store for reactive state management.
|
|
35
|
-
*
|
|
36
|
-
*
|
|
51
|
+
* The hook uses TanStack Store for reactive state management. You can subscribe to state changes
|
|
52
|
+
* in two ways:
|
|
53
|
+
*
|
|
54
|
+
* **1. Using `throttler.Subscribe` component (Recommended for component tree subscriptions)**
|
|
55
|
+
*
|
|
56
|
+
* Use the `Subscribe` component to subscribe to state changes deep in your component tree without
|
|
57
|
+
* needing to pass a selector to the hook. This is ideal when you want to subscribe to state
|
|
58
|
+
* in child components.
|
|
59
|
+
*
|
|
60
|
+
* **2. Using the `selector` parameter (For hook-level subscriptions)**
|
|
61
|
+
*
|
|
62
|
+
* The `selector` parameter allows you to specify which state changes will trigger reactive updates
|
|
63
|
+
* at the hook level, optimizing performance by preventing unnecessary updates when irrelevant
|
|
64
|
+
* state changes occur.
|
|
37
65
|
*
|
|
38
66
|
* **By default, there will be no reactive state subscriptions** and you must opt-in to state
|
|
39
|
-
* tracking by providing a selector function. This prevents unnecessary
|
|
40
|
-
* full control over when your component
|
|
41
|
-
* component re-render when the selected state values change.
|
|
67
|
+
* tracking by providing a selector function or using the `Subscribe` component. This prevents unnecessary
|
|
68
|
+
* updates and gives you full control over when your component tracks state changes.
|
|
42
69
|
*
|
|
43
70
|
* Available state properties:
|
|
44
71
|
* - `canLeadingExecute`: Whether the throttler can execute on the leading edge
|
|
@@ -55,21 +82,28 @@ interface SolidThrottler<TFn extends AnyFunction, TSelected = {}> extends Omit<T
|
|
|
55
82
|
* // Default behavior - no reactive state subscriptions
|
|
56
83
|
* const throttler = createThrottler(setValue, { wait: 1000 });
|
|
57
84
|
*
|
|
58
|
-
* //
|
|
85
|
+
* // Subscribe to state changes deep in component tree using Subscribe component
|
|
86
|
+
* <throttler.Subscribe selector={(state) => ({ isPending: state.isPending })}>
|
|
87
|
+
* {(state) => (
|
|
88
|
+
* <div>{state().isPending ? 'Loading...' : 'Ready'}</div>
|
|
89
|
+
* )}
|
|
90
|
+
* </throttler.Subscribe>
|
|
91
|
+
*
|
|
92
|
+
* // Opt-in to track isPending changes at hook level (optimized for loading states)
|
|
59
93
|
* const throttler = createThrottler(
|
|
60
94
|
* setValue,
|
|
61
95
|
* { wait: 1000 },
|
|
62
96
|
* (state) => ({ isPending: state.isPending })
|
|
63
97
|
* );
|
|
64
98
|
*
|
|
65
|
-
* // Opt-in to
|
|
99
|
+
* // Opt-in to track executionCount changes (optimized for tracking execution)
|
|
66
100
|
* const throttler = createThrottler(
|
|
67
101
|
* setValue,
|
|
68
102
|
* { wait: 1000 },
|
|
69
103
|
* (state) => ({ executionCount: state.executionCount })
|
|
70
104
|
* );
|
|
71
105
|
*
|
|
72
|
-
* // Multiple state properties -
|
|
106
|
+
* // Multiple state properties - track when any of these change
|
|
73
107
|
* const throttler = createThrottler(
|
|
74
108
|
* setValue,
|
|
75
109
|
* {
|
|
@@ -17,14 +17,24 @@ import { Throttler } from "@tanstack/pacer/throttler";
|
|
|
17
17
|
*
|
|
18
18
|
* ## State Management and Selector
|
|
19
19
|
*
|
|
20
|
-
* The hook uses TanStack Store for reactive state management.
|
|
21
|
-
*
|
|
22
|
-
*
|
|
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.
|
|
23
34
|
*
|
|
24
35
|
* **By default, there will be no reactive state subscriptions** and you must opt-in to state
|
|
25
|
-
* tracking by providing a selector function. This prevents unnecessary
|
|
26
|
-
* full control over when your component
|
|
27
|
-
* component re-render when the selected state values change.
|
|
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.
|
|
28
38
|
*
|
|
29
39
|
* Available state properties:
|
|
30
40
|
* - `canLeadingExecute`: Whether the throttler can execute on the leading edge
|
|
@@ -41,21 +51,28 @@ import { Throttler } from "@tanstack/pacer/throttler";
|
|
|
41
51
|
* // Default behavior - no reactive state subscriptions
|
|
42
52
|
* const throttler = createThrottler(setValue, { wait: 1000 });
|
|
43
53
|
*
|
|
44
|
-
* //
|
|
54
|
+
* // Subscribe to state changes deep in component tree using Subscribe component
|
|
55
|
+
* <throttler.Subscribe selector={(state) => ({ isPending: state.isPending })}>
|
|
56
|
+
* {(state) => (
|
|
57
|
+
* <div>{state().isPending ? 'Loading...' : 'Ready'}</div>
|
|
58
|
+
* )}
|
|
59
|
+
* </throttler.Subscribe>
|
|
60
|
+
*
|
|
61
|
+
* // Opt-in to track isPending changes at hook level (optimized for loading states)
|
|
45
62
|
* const throttler = createThrottler(
|
|
46
63
|
* setValue,
|
|
47
64
|
* { wait: 1000 },
|
|
48
65
|
* (state) => ({ isPending: state.isPending })
|
|
49
66
|
* );
|
|
50
67
|
*
|
|
51
|
-
* // Opt-in to
|
|
68
|
+
* // Opt-in to track executionCount changes (optimized for tracking execution)
|
|
52
69
|
* const throttler = createThrottler(
|
|
53
70
|
* setValue,
|
|
54
71
|
* { wait: 1000 },
|
|
55
72
|
* (state) => ({ executionCount: state.executionCount })
|
|
56
73
|
* );
|
|
57
74
|
*
|
|
58
|
-
* // Multiple state properties -
|
|
75
|
+
* // Multiple state properties - track when any of these change
|
|
59
76
|
* const throttler = createThrottler(
|
|
60
77
|
* setValue,
|
|
61
78
|
* {
|
|
@@ -80,6 +97,10 @@ function createThrottler(fn, options, selector = () => ({})) {
|
|
|
80
97
|
...useDefaultPacerOptions().throttler,
|
|
81
98
|
...options
|
|
82
99
|
});
|
|
100
|
+
asyncThrottler.Subscribe = function Subscribe(props) {
|
|
101
|
+
const selected = useStore(asyncThrottler.store, props.selector);
|
|
102
|
+
return typeof props.children === "function" ? props.children(selected) : props.children;
|
|
103
|
+
};
|
|
83
104
|
const state = useStore(asyncThrottler.store, selector);
|
|
84
105
|
createEffect(() => {
|
|
85
106
|
onCleanup(() => {
|
|
@@ -1 +1 @@
|
|
|
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 { useStore } from '@tanstack/solid-store'\nimport { useDefaultPacerOptions } from '../provider/PacerProvider'\nimport type { Store } from '@tanstack/solid-store'\nimport type { Accessor } from 'solid-js'\nimport type { AnyFunction } from '@tanstack/pacer/types'\nimport type {\n ThrottlerOptions,\n ThrottlerState,\n} from '@tanstack/pacer/throttler'\n\nexport interface SolidThrottler<\n TFn extends AnyFunction,\n TSelected = {},\n> extends Omit<Throttler<TFn>, 'store'> {\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 `useStore` hook internally.\n * Although, you can make the state reactive by using the `useStore` in your own usage.\n */\n readonly store: Store<Readonly<ThrottlerState<TFn>>>\n}\n\n/**\n * A low-level 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. The `selector` parameter allows you
|
|
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 { useStore } 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 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 `useStore` hook internally.\n * Although, you can make the state reactive by using the `useStore` in your own usage.\n */\n readonly store: Store<Readonly<ThrottlerState<TFn>>>\n}\n\n/**\n * A low-level 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 * @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: ThrottlerOptions<TFn>,\n selector: (state: ThrottlerState<TFn>) => TSelected = () => ({}) as TSelected,\n): SolidThrottler<TFn, TSelected> {\n const mergedOptions = {\n ...useDefaultPacerOptions().throttler,\n ...options,\n } as ThrottlerOptions<TFn>\n\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 = useStore(asyncThrottler.store, props.selector)\n\n return typeof props.children === 'function'\n ? props.children(selected)\n : props.children\n }\n\n const state = useStore(asyncThrottler.store, selector)\n\n createEffect(() => {\n onCleanup(() => {\n asyncThrottler.cancel()\n })\n })\n\n return {\n ...asyncThrottler,\n state,\n } as SolidThrottler<TFn, TSelected> // omit `store` in favor of `state`\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAuIA,SAAgB,gBACd,IACA,SACA,kBAA6D,EAAE,GAC/B;CAMhC,MAAM,iBAAiB,IAAI,UACzB,IANoB;EACpB,GAAG,wBAAwB,CAAC;EAC5B,GAAG;EACJ,CAKA;AAED,gBAAe,YAAY,SAAS,UAAqB,OAGtD;EACD,MAAM,WAAW,SAAS,eAAe,OAAO,MAAM,SAAS;AAE/D,SAAO,OAAO,MAAM,aAAa,aAC7B,MAAM,SAAS,SAAS,GACxB,MAAM;;CAGZ,MAAM,QAAQ,SAAS,eAAe,OAAO,SAAS;AAEtD,oBAAmB;AACjB,kBAAgB;AACd,kBAAe,QAAQ;IACvB;GACF;AAEF,QAAO;EACL,GAAG;EACH;EACD"}
|
package/dist/throttler/index.cjs
CHANGED
|
@@ -5,10 +5,10 @@ const require_createThrottledValue = require('./createThrottledValue.cjs');
|
|
|
5
5
|
exports.createThrottledSignal = require_createThrottledSignal.createThrottledSignal;
|
|
6
6
|
exports.createThrottledValue = require_createThrottledValue.createThrottledValue;
|
|
7
7
|
exports.createThrottler = require_createThrottler.createThrottler;
|
|
8
|
-
var
|
|
9
|
-
Object.keys(
|
|
8
|
+
var _tanstack_pacer_throttler = require("@tanstack/pacer/throttler");
|
|
9
|
+
Object.keys(_tanstack_pacer_throttler).forEach(function (k) {
|
|
10
10
|
if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
|
|
11
11
|
enumerable: true,
|
|
12
|
-
get: function () { return
|
|
12
|
+
get: function () { return _tanstack_pacer_throttler[k]; }
|
|
13
13
|
});
|
|
14
14
|
});
|
package/dist/types/index.cjs
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
|
|
2
2
|
|
|
3
|
-
var
|
|
4
|
-
Object.keys(
|
|
3
|
+
var _tanstack_pacer_types = require("@tanstack/pacer/types");
|
|
4
|
+
Object.keys(_tanstack_pacer_types).forEach(function (k) {
|
|
5
5
|
if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
|
|
6
6
|
enumerable: true,
|
|
7
|
-
get: function () { return
|
|
7
|
+
get: function () { return _tanstack_pacer_types[k]; }
|
|
8
8
|
});
|
|
9
9
|
});
|
package/dist/utils/index.cjs
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
|
|
2
2
|
|
|
3
|
-
var
|
|
4
|
-
Object.keys(
|
|
3
|
+
var _tanstack_pacer_utils = require("@tanstack/pacer/utils");
|
|
4
|
+
Object.keys(_tanstack_pacer_utils).forEach(function (k) {
|
|
5
5
|
if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
|
|
6
6
|
enumerable: true,
|
|
7
|
-
get: function () { return
|
|
7
|
+
get: function () { return _tanstack_pacer_utils[k]; }
|
|
8
8
|
});
|
|
9
9
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/solid-pacer",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.18.0",
|
|
4
4
|
"description": "Utilities for debouncing and throttling functions in Solid.",
|
|
5
5
|
"author": "Tanner Linsley",
|
|
6
6
|
"license": "MIT",
|
|
@@ -95,7 +95,7 @@
|
|
|
95
95
|
],
|
|
96
96
|
"dependencies": {
|
|
97
97
|
"@tanstack/solid-store": "^0.8.0",
|
|
98
|
-
"@tanstack/pacer": "0.
|
|
98
|
+
"@tanstack/pacer": "0.17.0"
|
|
99
99
|
},
|
|
100
100
|
"devDependencies": {
|
|
101
101
|
"solid-js": "^1.9.10",
|
|
@@ -2,7 +2,7 @@ import { AsyncBatcher } from '@tanstack/pacer/async-batcher'
|
|
|
2
2
|
import { useStore } from '@tanstack/solid-store'
|
|
3
3
|
import { useDefaultPacerOptions } from '../provider/PacerProvider'
|
|
4
4
|
import type { Store } from '@tanstack/solid-store'
|
|
5
|
-
import type { Accessor } from 'solid-js'
|
|
5
|
+
import type { Accessor, JSX } from 'solid-js'
|
|
6
6
|
import type {
|
|
7
7
|
AsyncBatcherOptions,
|
|
8
8
|
AsyncBatcherState,
|
|
@@ -12,6 +12,23 @@ export interface SolidAsyncBatcher<TValue, TSelected = {}> extends Omit<
|
|
|
12
12
|
AsyncBatcher<TValue>,
|
|
13
13
|
'store'
|
|
14
14
|
> {
|
|
15
|
+
/**
|
|
16
|
+
* A Solid component that allows you to subscribe to the batcher state.
|
|
17
|
+
*
|
|
18
|
+
* This is useful for tracking specific parts of the batcher state
|
|
19
|
+
* deep in your component tree without needing to pass a selector to the hook.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* <batcher.Subscribe selector={(state) => ({ size: state.size, isExecuting: state.isExecuting })}>
|
|
23
|
+
* {(state) => (
|
|
24
|
+
* <div>Batch: {state().size} items, {state().isExecuting ? 'Executing...' : 'Idle'}</div>
|
|
25
|
+
* )}
|
|
26
|
+
* </batcher.Subscribe>
|
|
27
|
+
*/
|
|
28
|
+
Subscribe: <TSelected>(props: {
|
|
29
|
+
selector: (state: AsyncBatcherState<TValue>) => TSelected
|
|
30
|
+
children: ((state: Accessor<TSelected>) => JSX.Element) | JSX.Element
|
|
31
|
+
}) => JSX.Element
|
|
15
32
|
/**
|
|
16
33
|
* Reactive state that will be updated when the batcher state changes
|
|
17
34
|
*
|
|
@@ -58,14 +75,24 @@ export interface SolidAsyncBatcher<TValue, TSelected = {}> extends Omit<
|
|
|
58
75
|
*
|
|
59
76
|
* ## State Management and Selector
|
|
60
77
|
*
|
|
61
|
-
* The hook uses TanStack Store for reactive state management.
|
|
62
|
-
*
|
|
63
|
-
*
|
|
78
|
+
* The hook uses TanStack Store for reactive state management. You can subscribe to state changes
|
|
79
|
+
* in two ways:
|
|
80
|
+
*
|
|
81
|
+
* **1. Using `batcher.Subscribe` component (Recommended for component tree subscriptions)**
|
|
82
|
+
*
|
|
83
|
+
* Use the `Subscribe` component to subscribe to state changes deep in your component tree without
|
|
84
|
+
* needing to pass a selector to the hook. This is ideal when you want to subscribe to state
|
|
85
|
+
* in child components.
|
|
86
|
+
*
|
|
87
|
+
* **2. Using the `selector` parameter (For hook-level subscriptions)**
|
|
88
|
+
*
|
|
89
|
+
* The `selector` parameter allows you to specify which state changes will trigger reactive updates
|
|
90
|
+
* at the hook level, optimizing performance by preventing unnecessary updates when irrelevant
|
|
91
|
+
* state changes occur.
|
|
64
92
|
*
|
|
65
93
|
* **By default, there will be no reactive state subscriptions** and you must opt-in to state
|
|
66
|
-
* tracking by providing a selector function. This prevents unnecessary
|
|
67
|
-
* full control over when your component
|
|
68
|
-
* component re-render when the selected state values change.
|
|
94
|
+
* tracking by providing a selector function or using the `Subscribe` component. This prevents unnecessary
|
|
95
|
+
* updates and gives you full control over when your component tracks state changes.
|
|
69
96
|
*
|
|
70
97
|
* Available state properties:
|
|
71
98
|
* - `errorCount`: Number of failed batch executions
|
|
@@ -98,7 +125,7 @@ export interface SolidAsyncBatcher<TValue, TSelected = {}> extends Omit<
|
|
|
98
125
|
* }
|
|
99
126
|
* );
|
|
100
127
|
*
|
|
101
|
-
* // Opt-in to
|
|
128
|
+
* // Opt-in to track items or isExecuting changes (optimized for UI updates)
|
|
102
129
|
* const asyncBatcher = createAsyncBatcher(
|
|
103
130
|
* async (items) => {
|
|
104
131
|
* const results = await Promise.all(items.map(item => processItem(item)));
|
|
@@ -108,7 +135,7 @@ export interface SolidAsyncBatcher<TValue, TSelected = {}> extends Omit<
|
|
|
108
135
|
* (state) => ({ items: state.items, isExecuting: state.isExecuting })
|
|
109
136
|
* );
|
|
110
137
|
*
|
|
111
|
-
* // Opt-in to
|
|
138
|
+
* // Opt-in to track error state changes (optimized for error handling)
|
|
112
139
|
* const asyncBatcher = createAsyncBatcher(
|
|
113
140
|
* async (items) => {
|
|
114
141
|
* const results = await Promise.all(items.map(item => processItem(item)));
|
|
@@ -139,7 +166,21 @@ export function createAsyncBatcher<TValue, TSelected = {}>(
|
|
|
139
166
|
...options,
|
|
140
167
|
} as AsyncBatcherOptions<TValue>
|
|
141
168
|
|
|
142
|
-
const asyncBatcher = new AsyncBatcher<TValue>(
|
|
169
|
+
const asyncBatcher = new AsyncBatcher<TValue>(
|
|
170
|
+
fn,
|
|
171
|
+
mergedOptions,
|
|
172
|
+
) as unknown as SolidAsyncBatcher<TValue, TSelected>
|
|
173
|
+
|
|
174
|
+
asyncBatcher.Subscribe = function Subscribe<TSelected>(props: {
|
|
175
|
+
selector: (state: AsyncBatcherState<TValue>) => TSelected
|
|
176
|
+
children: ((state: Accessor<TSelected>) => JSX.Element) | JSX.Element
|
|
177
|
+
}) {
|
|
178
|
+
const selected = useStore(asyncBatcher.store, props.selector)
|
|
179
|
+
|
|
180
|
+
return typeof props.children === 'function'
|
|
181
|
+
? props.children(selected)
|
|
182
|
+
: props.children
|
|
183
|
+
}
|
|
143
184
|
|
|
144
185
|
const state = useStore(asyncBatcher.store, selector)
|
|
145
186
|
|
|
@@ -3,7 +3,7 @@ import { useStore } from '@tanstack/solid-store'
|
|
|
3
3
|
import { createEffect, onCleanup } from 'solid-js'
|
|
4
4
|
import { useDefaultPacerOptions } from '../provider/PacerProvider'
|
|
5
5
|
import type { Store } from '@tanstack/solid-store'
|
|
6
|
-
import type { Accessor } from 'solid-js'
|
|
6
|
+
import type { Accessor, JSX } from 'solid-js'
|
|
7
7
|
import type {
|
|
8
8
|
AsyncDebouncerOptions,
|
|
9
9
|
AsyncDebouncerState,
|
|
@@ -14,6 +14,23 @@ export interface SolidAsyncDebouncer<
|
|
|
14
14
|
TFn extends AnyAsyncFunction,
|
|
15
15
|
TSelected = {},
|
|
16
16
|
> extends Omit<AsyncDebouncer<TFn>, 'store'> {
|
|
17
|
+
/**
|
|
18
|
+
* A Solid component that allows you to subscribe to the debouncer state.
|
|
19
|
+
*
|
|
20
|
+
* This is useful for tracking specific parts of the debouncer state
|
|
21
|
+
* deep in your component tree without needing to pass a selector to the hook.
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* <debouncer.Subscribe selector={(state) => ({ isPending: state.isPending, isExecuting: state.isExecuting })}>
|
|
25
|
+
* {(state) => (
|
|
26
|
+
* <div>{state().isPending ? 'Waiting...' : state().isExecuting ? 'Executing...' : 'Ready'}</div>
|
|
27
|
+
* )}
|
|
28
|
+
* </debouncer.Subscribe>
|
|
29
|
+
*/
|
|
30
|
+
Subscribe: <TSelected>(props: {
|
|
31
|
+
selector: (state: AsyncDebouncerState<TFn>) => TSelected
|
|
32
|
+
children: ((state: Accessor<TSelected>) => JSX.Element) | JSX.Element
|
|
33
|
+
}) => JSX.Element
|
|
17
34
|
/**
|
|
18
35
|
* Reactive state that will be updated when the debouncer state changes
|
|
19
36
|
*
|
|
@@ -54,14 +71,24 @@ export interface SolidAsyncDebouncer<
|
|
|
54
71
|
*
|
|
55
72
|
* ## State Management and Selector
|
|
56
73
|
*
|
|
57
|
-
* The hook uses TanStack Store for reactive state management.
|
|
58
|
-
*
|
|
59
|
-
*
|
|
74
|
+
* The hook uses TanStack Store for reactive state management. You can subscribe to state changes
|
|
75
|
+
* in two ways:
|
|
76
|
+
*
|
|
77
|
+
* **1. Using `debouncer.Subscribe` component (Recommended for component tree subscriptions)**
|
|
78
|
+
*
|
|
79
|
+
* Use the `Subscribe` component to subscribe to state changes deep in your component tree without
|
|
80
|
+
* needing to pass a selector to the hook. This is ideal when you want to subscribe to state
|
|
81
|
+
* in child components.
|
|
82
|
+
*
|
|
83
|
+
* **2. Using the `selector` parameter (For hook-level subscriptions)**
|
|
84
|
+
*
|
|
85
|
+
* The `selector` parameter allows you to specify which state changes will trigger reactive updates
|
|
86
|
+
* at the hook level, optimizing performance by preventing unnecessary updates when irrelevant
|
|
87
|
+
* state changes occur.
|
|
60
88
|
*
|
|
61
89
|
* **By default, there will be no reactive state subscriptions** and you must opt-in to state
|
|
62
|
-
* tracking by providing a selector function. This prevents unnecessary
|
|
63
|
-
* full control over when your component
|
|
64
|
-
* component re-render when the selected state values change.
|
|
90
|
+
* tracking by providing a selector function or using the `Subscribe` component. This prevents unnecessary
|
|
91
|
+
* updates and gives you full control over when your component tracks state changes.
|
|
65
92
|
*
|
|
66
93
|
* Available state properties:
|
|
67
94
|
* - `canLeadingExecute`: Whether the debouncer can execute on the leading edge
|
|
@@ -85,7 +112,7 @@ export interface SolidAsyncDebouncer<
|
|
|
85
112
|
* { wait: 500 }
|
|
86
113
|
* );
|
|
87
114
|
*
|
|
88
|
-
* // Opt-in to
|
|
115
|
+
* // Opt-in to track isPending or isExecuting changes (optimized for loading states)
|
|
89
116
|
* const debouncer = createAsyncDebouncer(
|
|
90
117
|
* async (query: string) => {
|
|
91
118
|
* const results = await api.search(query);
|
|
@@ -95,7 +122,7 @@ export interface SolidAsyncDebouncer<
|
|
|
95
122
|
* (state) => ({ isPending: state.isPending, isExecuting: state.isExecuting })
|
|
96
123
|
* );
|
|
97
124
|
*
|
|
98
|
-
* // Opt-in to
|
|
125
|
+
* // Opt-in to track error state changes (optimized for error handling)
|
|
99
126
|
* const debouncer = createAsyncDebouncer(
|
|
100
127
|
* async (searchTerm) => {
|
|
101
128
|
* const data = await searchAPI(searchTerm);
|
|
@@ -130,7 +157,21 @@ export function createAsyncDebouncer<
|
|
|
130
157
|
...options,
|
|
131
158
|
} as AsyncDebouncerOptions<TFn>
|
|
132
159
|
|
|
133
|
-
const asyncDebouncer = new AsyncDebouncer<TFn>(
|
|
160
|
+
const asyncDebouncer = new AsyncDebouncer<TFn>(
|
|
161
|
+
fn,
|
|
162
|
+
mergedOptions,
|
|
163
|
+
) as unknown as SolidAsyncDebouncer<TFn, TSelected>
|
|
164
|
+
|
|
165
|
+
asyncDebouncer.Subscribe = function Subscribe<TSelected>(props: {
|
|
166
|
+
selector: (state: AsyncDebouncerState<TFn>) => TSelected
|
|
167
|
+
children: ((state: Accessor<TSelected>) => JSX.Element) | JSX.Element
|
|
168
|
+
}) {
|
|
169
|
+
const selected = useStore(asyncDebouncer.store, props.selector)
|
|
170
|
+
|
|
171
|
+
return typeof props.children === 'function'
|
|
172
|
+
? props.children(selected)
|
|
173
|
+
: props.children
|
|
174
|
+
}
|
|
134
175
|
|
|
135
176
|
const state = useStore(asyncDebouncer.store, selector)
|
|
136
177
|
|