@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,6 +1,6 @@
|
|
|
1
1
|
const require_PacerProvider = require('../provider/PacerProvider.cjs');
|
|
2
|
-
let
|
|
3
|
-
let
|
|
2
|
+
let _tanstack_solid_store = require("@tanstack/solid-store");
|
|
3
|
+
let _tanstack_pacer_batcher = require("@tanstack/pacer/batcher");
|
|
4
4
|
|
|
5
5
|
//#region src/batcher/createBatcher.ts
|
|
6
6
|
/**
|
|
@@ -20,14 +20,24 @@ let __tanstack_pacer_batcher = require("@tanstack/pacer/batcher");
|
|
|
20
20
|
*
|
|
21
21
|
* ## State Management and Selector
|
|
22
22
|
*
|
|
23
|
-
* The hook uses TanStack Store for reactive state management.
|
|
24
|
-
*
|
|
25
|
-
*
|
|
23
|
+
* The hook uses TanStack Store for reactive state management. You can subscribe to state changes
|
|
24
|
+
* in two ways:
|
|
25
|
+
*
|
|
26
|
+
* **1. Using `batcher.Subscribe` component (Recommended for component tree subscriptions)**
|
|
27
|
+
*
|
|
28
|
+
* Use the `Subscribe` component to subscribe to state changes deep in your component tree without
|
|
29
|
+
* needing to pass a selector to the hook. This is ideal when you want to subscribe to state
|
|
30
|
+
* in child components.
|
|
31
|
+
*
|
|
32
|
+
* **2. Using the `selector` parameter (For hook-level subscriptions)**
|
|
33
|
+
*
|
|
34
|
+
* The `selector` parameter allows you to specify which state changes will trigger reactive updates
|
|
35
|
+
* at the hook level, optimizing performance by preventing unnecessary updates when irrelevant
|
|
36
|
+
* state changes occur.
|
|
26
37
|
*
|
|
27
38
|
* **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
|
|
29
|
-
* full control over when your component
|
|
30
|
-
* component re-render when the selected state values change.
|
|
39
|
+
* tracking by providing a selector function or using the `Subscribe` component. This prevents unnecessary
|
|
40
|
+
* updates and gives you full control over when your component tracks state changes.
|
|
31
41
|
*
|
|
32
42
|
* Available state properties:
|
|
33
43
|
* - `executionCount`: Number of batch executions that have been completed
|
|
@@ -51,14 +61,14 @@ let __tanstack_pacer_batcher = require("@tanstack/pacer/batcher");
|
|
|
51
61
|
* }
|
|
52
62
|
* );
|
|
53
63
|
*
|
|
54
|
-
* // Opt-in to
|
|
64
|
+
* // Opt-in to track items or isRunning changes (optimized for UI updates)
|
|
55
65
|
* const batcher = createBatcher(
|
|
56
66
|
* (items) => console.log('Processing batch:', items),
|
|
57
67
|
* { maxSize: 5, wait: 2000 },
|
|
58
68
|
* (state) => ({ items: state.items, isRunning: state.isRunning })
|
|
59
69
|
* );
|
|
60
70
|
*
|
|
61
|
-
* // Opt-in to
|
|
71
|
+
* // Opt-in to track execution metrics changes (optimized for tracking progress)
|
|
62
72
|
* const batcher = createBatcher(
|
|
63
73
|
* (items) => console.log('Processing batch:', items),
|
|
64
74
|
* { maxSize: 5, wait: 2000 },
|
|
@@ -81,11 +91,15 @@ let __tanstack_pacer_batcher = require("@tanstack/pacer/batcher");
|
|
|
81
91
|
* ```
|
|
82
92
|
*/
|
|
83
93
|
function createBatcher(fn, options = {}, selector = () => ({})) {
|
|
84
|
-
const batcher = new
|
|
94
|
+
const batcher = new _tanstack_pacer_batcher.Batcher(fn, {
|
|
85
95
|
...require_PacerProvider.useDefaultPacerOptions().batcher,
|
|
86
96
|
...options
|
|
87
97
|
});
|
|
88
|
-
|
|
98
|
+
batcher.Subscribe = function Subscribe(props) {
|
|
99
|
+
const selected = (0, _tanstack_solid_store.useStore)(batcher.store, props.selector);
|
|
100
|
+
return typeof props.children === "function" ? props.children(selected) : props.children;
|
|
101
|
+
};
|
|
102
|
+
const state = (0, _tanstack_solid_store.useStore)(batcher.store, selector);
|
|
89
103
|
return {
|
|
90
104
|
...batcher,
|
|
91
105
|
state
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createBatcher.cjs","names":["Batcher","useDefaultPacerOptions"],"sources":["../../src/batcher/createBatcher.ts"],"sourcesContent":["import { Batcher } from '@tanstack/pacer/batcher'\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 { BatcherOptions, BatcherState } from '@tanstack/pacer/batcher'\n\nexport interface SolidBatcher<TValue, TSelected = {}> extends Omit<\n Batcher<TValue>,\n 'store'\n> {\n /**\n * Reactive state that will be updated when the batcher state changes\n *\n * Use this instead of `batcher.store.state`\n */\n readonly state: Accessor<Readonly<TSelected>>\n /**\n * @deprecated Use `batcher.state` instead of `batcher.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<BatcherState<TValue>>>\n}\n\n/**\n * Creates a Solid-compatible Batcher instance for managing batches of items, exposing Solid signals for all stateful properties.\n *\n * Features:\n * - Batch processing of items using the provided `fn` function\n * - Configurable batch size and wait time\n * - Custom batch processing logic via getShouldExecute\n * - Event callbacks for monitoring batch operations\n * - All stateful properties (items, counts, etc.) are exposed as Solid signals for reactivity\n *\n * The batcher collects items and processes them in batches based on:\n * - Maximum batch size\n * - Time-based batching (process after X milliseconds)\n * - Custom batch processing logic via getShouldExecute\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":"createBatcher.cjs","names":["Batcher","useDefaultPacerOptions"],"sources":["../../src/batcher/createBatcher.ts"],"sourcesContent":["import { Batcher } from '@tanstack/pacer/batcher'\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 { BatcherOptions, BatcherState } from '@tanstack/pacer/batcher'\n\nexport interface SolidBatcher<TValue, TSelected = {}> extends Omit<\n Batcher<TValue>,\n 'store'\n> {\n /**\n * A Solid component that allows you to subscribe to the batcher state.\n *\n * This is useful for tracking specific parts of the batcher state\n * deep in your component tree without needing to pass a selector to the hook.\n *\n * @example\n * <batcher.Subscribe selector={(state) => ({ size: state.size, isRunning: state.isRunning })}>\n * {(state) => (\n * <div>Batch: {state().size} items, {state().isRunning ? 'Processing' : 'Idle'}</div>\n * )}\n * </batcher.Subscribe>\n */\n Subscribe: <TSelected>(props: {\n selector: (state: BatcherState<TValue>) => TSelected\n children: ((state: Accessor<TSelected>) => JSX.Element) | JSX.Element\n }) => JSX.Element\n /**\n * Reactive state that will be updated when the batcher state changes\n *\n * Use this instead of `batcher.store.state`\n */\n readonly state: Accessor<Readonly<TSelected>>\n /**\n * @deprecated Use `batcher.state` instead of `batcher.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<BatcherState<TValue>>>\n}\n\n/**\n * Creates a Solid-compatible Batcher instance for managing batches of items, exposing Solid signals for all stateful properties.\n *\n * Features:\n * - Batch processing of items using the provided `fn` function\n * - Configurable batch size and wait time\n * - Custom batch processing logic via getShouldExecute\n * - Event callbacks for monitoring batch operations\n * - All stateful properties (items, counts, etc.) are exposed as Solid signals for reactivity\n *\n * The batcher collects items and processes them in batches based on:\n * - Maximum batch size\n * - Time-based batching (process after X milliseconds)\n * - Custom batch processing logic via getShouldExecute\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 `batcher.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 * - `executionCount`: Number of batch executions that have been completed\n * - `isRunning`: Whether the batcher is currently running (not stopped)\n * - `items`: Array of items currently queued for batching\n * - `totalItemsProcessed`: Total number of individual items that have been processed across all batches\n *\n * Example usage:\n * ```tsx\n * // Default behavior - no reactive state subscriptions\n * const batcher = createBatcher(\n * (items) => {\n * // Process batch of items\n * console.log('Processing batch:', items);\n * },\n * {\n * maxSize: 5,\n * wait: 2000,\n * onExecute: (batcher) => console.log('Batch executed'),\n * getShouldExecute: (items) => items.length >= 3\n * }\n * );\n *\n * // Opt-in to track items or isRunning changes (optimized for UI updates)\n * const batcher = createBatcher(\n * (items) => console.log('Processing batch:', items),\n * { maxSize: 5, wait: 2000 },\n * (state) => ({ items: state.items, isRunning: state.isRunning })\n * );\n *\n * // Opt-in to track execution metrics changes (optimized for tracking progress)\n * const batcher = createBatcher(\n * (items) => console.log('Processing batch:', items),\n * { maxSize: 5, wait: 2000 },\n * (state) => ({\n * executionCount: state.executionCount,\n * totalItemsProcessed: state.totalItemsProcessed\n * })\n * );\n *\n * // Add items to batch\n * batcher.addItem('task1');\n * batcher.addItem('task2');\n *\n * // Control the batcher\n * batcher.stop(); // Pause processing\n * batcher.start(); // Resume processing\n *\n * // Access the selected state (will be empty object {} unless selector provided)\n * const { items, isRunning } = batcher.state();\n * ```\n */\nexport function createBatcher<TValue, TSelected = {}>(\n fn: (items: Array<TValue>) => void,\n options: BatcherOptions<TValue> = {},\n selector: (state: BatcherState<TValue>) => TSelected = () =>\n ({}) as TSelected,\n): SolidBatcher<TValue, TSelected> {\n const mergedOptions = {\n ...useDefaultPacerOptions().batcher,\n ...options,\n } as BatcherOptions<TValue>\n\n const batcher = new Batcher(fn, mergedOptions) as unknown as SolidBatcher<\n TValue,\n TSelected\n >\n\n batcher.Subscribe = function Subscribe<TSelected>(props: {\n selector: (state: BatcherState<TValue>) => TSelected\n children: ((state: Accessor<TSelected>) => JSX.Element) | JSX.Element\n }) {\n const selected = useStore(batcher.store, props.selector)\n\n return typeof props.children === 'function'\n ? props.children(selected)\n : props.children\n }\n\n const state = useStore(batcher.store, selector)\n return {\n ...batcher,\n state,\n } as SolidBatcher<TValue, TSelected>\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAiIA,SAAgB,cACd,IACA,UAAkC,EAAE,EACpC,kBACG,EAAE,GAC4B;CAMjC,MAAM,UAAU,IAAIA,gCAAQ,IALN;EACpB,GAAGC,8CAAwB,CAAC;EAC5B,GAAG;EACJ,CAE6C;AAK9C,SAAQ,YAAY,SAAS,UAAqB,OAG/C;EACD,MAAM,+CAAoB,QAAQ,OAAO,MAAM,SAAS;AAExD,SAAO,OAAO,MAAM,aAAa,aAC7B,MAAM,SAAS,SAAS,GACxB,MAAM;;CAGZ,MAAM,4CAAiB,QAAQ,OAAO,SAAS;AAC/C,QAAO;EACL,GAAG;EACH;EACD"}
|
|
@@ -1,9 +1,26 @@
|
|
|
1
1
|
import { Store } from "@tanstack/solid-store";
|
|
2
|
-
import { Accessor } from "solid-js";
|
|
2
|
+
import { Accessor, JSX } from "solid-js";
|
|
3
3
|
import { Batcher, BatcherOptions, BatcherState } from "@tanstack/pacer/batcher";
|
|
4
4
|
|
|
5
5
|
//#region src/batcher/createBatcher.d.ts
|
|
6
6
|
interface SolidBatcher<TValue, TSelected = {}> extends Omit<Batcher<TValue>, 'store'> {
|
|
7
|
+
/**
|
|
8
|
+
* A Solid component that allows you to subscribe to the batcher state.
|
|
9
|
+
*
|
|
10
|
+
* This is useful for tracking specific parts of the batcher state
|
|
11
|
+
* deep in your component tree without needing to pass a selector to the hook.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* <batcher.Subscribe selector={(state) => ({ size: state.size, isRunning: state.isRunning })}>
|
|
15
|
+
* {(state) => (
|
|
16
|
+
* <div>Batch: {state().size} items, {state().isRunning ? 'Processing' : 'Idle'}</div>
|
|
17
|
+
* )}
|
|
18
|
+
* </batcher.Subscribe>
|
|
19
|
+
*/
|
|
20
|
+
Subscribe: <TSelected>(props: {
|
|
21
|
+
selector: (state: BatcherState<TValue>) => TSelected;
|
|
22
|
+
children: ((state: Accessor<TSelected>) => JSX.Element) | JSX.Element;
|
|
23
|
+
}) => JSX.Element;
|
|
7
24
|
/**
|
|
8
25
|
* Reactive state that will be updated when the batcher state changes
|
|
9
26
|
*
|
|
@@ -34,14 +51,24 @@ interface SolidBatcher<TValue, TSelected = {}> extends Omit<Batcher<TValue>, 'st
|
|
|
34
51
|
*
|
|
35
52
|
* ## State Management and Selector
|
|
36
53
|
*
|
|
37
|
-
* The hook uses TanStack Store for reactive state management.
|
|
38
|
-
*
|
|
39
|
-
*
|
|
54
|
+
* The hook uses TanStack Store for reactive state management. You can subscribe to state changes
|
|
55
|
+
* in two ways:
|
|
56
|
+
*
|
|
57
|
+
* **1. Using `batcher.Subscribe` component (Recommended for component tree subscriptions)**
|
|
58
|
+
*
|
|
59
|
+
* Use the `Subscribe` component to subscribe to state changes deep in your component tree without
|
|
60
|
+
* needing to pass a selector to the hook. This is ideal when you want to subscribe to state
|
|
61
|
+
* in child components.
|
|
62
|
+
*
|
|
63
|
+
* **2. Using the `selector` parameter (For hook-level subscriptions)**
|
|
64
|
+
*
|
|
65
|
+
* The `selector` parameter allows you to specify which state changes will trigger reactive updates
|
|
66
|
+
* at the hook level, optimizing performance by preventing unnecessary updates when irrelevant
|
|
67
|
+
* state changes occur.
|
|
40
68
|
*
|
|
41
69
|
* **By default, there will be no reactive state subscriptions** and you must opt-in to state
|
|
42
|
-
* tracking by providing a selector function. This prevents unnecessary
|
|
43
|
-
* full control over when your component
|
|
44
|
-
* component re-render when the selected state values change.
|
|
70
|
+
* tracking by providing a selector function or using the `Subscribe` component. This prevents unnecessary
|
|
71
|
+
* updates and gives you full control over when your component tracks state changes.
|
|
45
72
|
*
|
|
46
73
|
* Available state properties:
|
|
47
74
|
* - `executionCount`: Number of batch executions that have been completed
|
|
@@ -65,14 +92,14 @@ interface SolidBatcher<TValue, TSelected = {}> extends Omit<Batcher<TValue>, 'st
|
|
|
65
92
|
* }
|
|
66
93
|
* );
|
|
67
94
|
*
|
|
68
|
-
* // Opt-in to
|
|
95
|
+
* // Opt-in to track items or isRunning changes (optimized for UI updates)
|
|
69
96
|
* const batcher = createBatcher(
|
|
70
97
|
* (items) => console.log('Processing batch:', items),
|
|
71
98
|
* { maxSize: 5, wait: 2000 },
|
|
72
99
|
* (state) => ({ items: state.items, isRunning: state.isRunning })
|
|
73
100
|
* );
|
|
74
101
|
*
|
|
75
|
-
* // Opt-in to
|
|
102
|
+
* // Opt-in to track execution metrics changes (optimized for tracking progress)
|
|
76
103
|
* const batcher = createBatcher(
|
|
77
104
|
* (items) => console.log('Processing batch:', items),
|
|
78
105
|
* { maxSize: 5, wait: 2000 },
|
|
@@ -1,9 +1,26 @@
|
|
|
1
|
-
import { Accessor } from "solid-js";
|
|
1
|
+
import { Accessor, JSX } from "solid-js";
|
|
2
2
|
import { Store } from "@tanstack/solid-store";
|
|
3
3
|
import { Batcher, BatcherOptions, BatcherState } from "@tanstack/pacer/batcher";
|
|
4
4
|
|
|
5
5
|
//#region src/batcher/createBatcher.d.ts
|
|
6
6
|
interface SolidBatcher<TValue, TSelected = {}> extends Omit<Batcher<TValue>, 'store'> {
|
|
7
|
+
/**
|
|
8
|
+
* A Solid component that allows you to subscribe to the batcher state.
|
|
9
|
+
*
|
|
10
|
+
* This is useful for tracking specific parts of the batcher state
|
|
11
|
+
* deep in your component tree without needing to pass a selector to the hook.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* <batcher.Subscribe selector={(state) => ({ size: state.size, isRunning: state.isRunning })}>
|
|
15
|
+
* {(state) => (
|
|
16
|
+
* <div>Batch: {state().size} items, {state().isRunning ? 'Processing' : 'Idle'}</div>
|
|
17
|
+
* )}
|
|
18
|
+
* </batcher.Subscribe>
|
|
19
|
+
*/
|
|
20
|
+
Subscribe: <TSelected>(props: {
|
|
21
|
+
selector: (state: BatcherState<TValue>) => TSelected;
|
|
22
|
+
children: ((state: Accessor<TSelected>) => JSX.Element) | JSX.Element;
|
|
23
|
+
}) => JSX.Element;
|
|
7
24
|
/**
|
|
8
25
|
* Reactive state that will be updated when the batcher state changes
|
|
9
26
|
*
|
|
@@ -34,14 +51,24 @@ interface SolidBatcher<TValue, TSelected = {}> extends Omit<Batcher<TValue>, 'st
|
|
|
34
51
|
*
|
|
35
52
|
* ## State Management and Selector
|
|
36
53
|
*
|
|
37
|
-
* The hook uses TanStack Store for reactive state management.
|
|
38
|
-
*
|
|
39
|
-
*
|
|
54
|
+
* The hook uses TanStack Store for reactive state management. You can subscribe to state changes
|
|
55
|
+
* in two ways:
|
|
56
|
+
*
|
|
57
|
+
* **1. Using `batcher.Subscribe` component (Recommended for component tree subscriptions)**
|
|
58
|
+
*
|
|
59
|
+
* Use the `Subscribe` component to subscribe to state changes deep in your component tree without
|
|
60
|
+
* needing to pass a selector to the hook. This is ideal when you want to subscribe to state
|
|
61
|
+
* in child components.
|
|
62
|
+
*
|
|
63
|
+
* **2. Using the `selector` parameter (For hook-level subscriptions)**
|
|
64
|
+
*
|
|
65
|
+
* The `selector` parameter allows you to specify which state changes will trigger reactive updates
|
|
66
|
+
* at the hook level, optimizing performance by preventing unnecessary updates when irrelevant
|
|
67
|
+
* state changes occur.
|
|
40
68
|
*
|
|
41
69
|
* **By default, there will be no reactive state subscriptions** and you must opt-in to state
|
|
42
|
-
* tracking by providing a selector function. This prevents unnecessary
|
|
43
|
-
* full control over when your component
|
|
44
|
-
* component re-render when the selected state values change.
|
|
70
|
+
* tracking by providing a selector function or using the `Subscribe` component. This prevents unnecessary
|
|
71
|
+
* updates and gives you full control over when your component tracks state changes.
|
|
45
72
|
*
|
|
46
73
|
* Available state properties:
|
|
47
74
|
* - `executionCount`: Number of batch executions that have been completed
|
|
@@ -65,14 +92,14 @@ interface SolidBatcher<TValue, TSelected = {}> extends Omit<Batcher<TValue>, 'st
|
|
|
65
92
|
* }
|
|
66
93
|
* );
|
|
67
94
|
*
|
|
68
|
-
* // Opt-in to
|
|
95
|
+
* // Opt-in to track items or isRunning changes (optimized for UI updates)
|
|
69
96
|
* const batcher = createBatcher(
|
|
70
97
|
* (items) => console.log('Processing batch:', items),
|
|
71
98
|
* { maxSize: 5, wait: 2000 },
|
|
72
99
|
* (state) => ({ items: state.items, isRunning: state.isRunning })
|
|
73
100
|
* );
|
|
74
101
|
*
|
|
75
|
-
* // Opt-in to
|
|
102
|
+
* // Opt-in to track execution metrics changes (optimized for tracking progress)
|
|
76
103
|
* const batcher = createBatcher(
|
|
77
104
|
* (items) => console.log('Processing batch:', items),
|
|
78
105
|
* { maxSize: 5, wait: 2000 },
|
|
@@ -20,14 +20,24 @@ import { Batcher } from "@tanstack/pacer/batcher";
|
|
|
20
20
|
*
|
|
21
21
|
* ## State Management and Selector
|
|
22
22
|
*
|
|
23
|
-
* The hook uses TanStack Store for reactive state management.
|
|
24
|
-
*
|
|
25
|
-
*
|
|
23
|
+
* The hook uses TanStack Store for reactive state management. You can subscribe to state changes
|
|
24
|
+
* in two ways:
|
|
25
|
+
*
|
|
26
|
+
* **1. Using `batcher.Subscribe` component (Recommended for component tree subscriptions)**
|
|
27
|
+
*
|
|
28
|
+
* Use the `Subscribe` component to subscribe to state changes deep in your component tree without
|
|
29
|
+
* needing to pass a selector to the hook. This is ideal when you want to subscribe to state
|
|
30
|
+
* in child components.
|
|
31
|
+
*
|
|
32
|
+
* **2. Using the `selector` parameter (For hook-level subscriptions)**
|
|
33
|
+
*
|
|
34
|
+
* The `selector` parameter allows you to specify which state changes will trigger reactive updates
|
|
35
|
+
* at the hook level, optimizing performance by preventing unnecessary updates when irrelevant
|
|
36
|
+
* state changes occur.
|
|
26
37
|
*
|
|
27
38
|
* **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
|
|
29
|
-
* full control over when your component
|
|
30
|
-
* component re-render when the selected state values change.
|
|
39
|
+
* tracking by providing a selector function or using the `Subscribe` component. This prevents unnecessary
|
|
40
|
+
* updates and gives you full control over when your component tracks state changes.
|
|
31
41
|
*
|
|
32
42
|
* Available state properties:
|
|
33
43
|
* - `executionCount`: Number of batch executions that have been completed
|
|
@@ -51,14 +61,14 @@ import { Batcher } from "@tanstack/pacer/batcher";
|
|
|
51
61
|
* }
|
|
52
62
|
* );
|
|
53
63
|
*
|
|
54
|
-
* // Opt-in to
|
|
64
|
+
* // Opt-in to track items or isRunning changes (optimized for UI updates)
|
|
55
65
|
* const batcher = createBatcher(
|
|
56
66
|
* (items) => console.log('Processing batch:', items),
|
|
57
67
|
* { maxSize: 5, wait: 2000 },
|
|
58
68
|
* (state) => ({ items: state.items, isRunning: state.isRunning })
|
|
59
69
|
* );
|
|
60
70
|
*
|
|
61
|
-
* // Opt-in to
|
|
71
|
+
* // Opt-in to track execution metrics changes (optimized for tracking progress)
|
|
62
72
|
* const batcher = createBatcher(
|
|
63
73
|
* (items) => console.log('Processing batch:', items),
|
|
64
74
|
* { maxSize: 5, wait: 2000 },
|
|
@@ -85,6 +95,10 @@ function createBatcher(fn, options = {}, selector = () => ({})) {
|
|
|
85
95
|
...useDefaultPacerOptions().batcher,
|
|
86
96
|
...options
|
|
87
97
|
});
|
|
98
|
+
batcher.Subscribe = function Subscribe(props) {
|
|
99
|
+
const selected = useStore(batcher.store, props.selector);
|
|
100
|
+
return typeof props.children === "function" ? props.children(selected) : props.children;
|
|
101
|
+
};
|
|
88
102
|
const state = useStore(batcher.store, selector);
|
|
89
103
|
return {
|
|
90
104
|
...batcher,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createBatcher.js","names":[],"sources":["../../src/batcher/createBatcher.ts"],"sourcesContent":["import { Batcher } from '@tanstack/pacer/batcher'\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 { BatcherOptions, BatcherState } from '@tanstack/pacer/batcher'\n\nexport interface SolidBatcher<TValue, TSelected = {}> extends Omit<\n Batcher<TValue>,\n 'store'\n> {\n /**\n * Reactive state that will be updated when the batcher state changes\n *\n * Use this instead of `batcher.store.state`\n */\n readonly state: Accessor<Readonly<TSelected>>\n /**\n * @deprecated Use `batcher.state` instead of `batcher.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<BatcherState<TValue>>>\n}\n\n/**\n * Creates a Solid-compatible Batcher instance for managing batches of items, exposing Solid signals for all stateful properties.\n *\n * Features:\n * - Batch processing of items using the provided `fn` function\n * - Configurable batch size and wait time\n * - Custom batch processing logic via getShouldExecute\n * - Event callbacks for monitoring batch operations\n * - All stateful properties (items, counts, etc.) are exposed as Solid signals for reactivity\n *\n * The batcher collects items and processes them in batches based on:\n * - Maximum batch size\n * - Time-based batching (process after X milliseconds)\n * - Custom batch processing logic via getShouldExecute\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":"createBatcher.js","names":[],"sources":["../../src/batcher/createBatcher.ts"],"sourcesContent":["import { Batcher } from '@tanstack/pacer/batcher'\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 { BatcherOptions, BatcherState } from '@tanstack/pacer/batcher'\n\nexport interface SolidBatcher<TValue, TSelected = {}> extends Omit<\n Batcher<TValue>,\n 'store'\n> {\n /**\n * A Solid component that allows you to subscribe to the batcher state.\n *\n * This is useful for tracking specific parts of the batcher state\n * deep in your component tree without needing to pass a selector to the hook.\n *\n * @example\n * <batcher.Subscribe selector={(state) => ({ size: state.size, isRunning: state.isRunning })}>\n * {(state) => (\n * <div>Batch: {state().size} items, {state().isRunning ? 'Processing' : 'Idle'}</div>\n * )}\n * </batcher.Subscribe>\n */\n Subscribe: <TSelected>(props: {\n selector: (state: BatcherState<TValue>) => TSelected\n children: ((state: Accessor<TSelected>) => JSX.Element) | JSX.Element\n }) => JSX.Element\n /**\n * Reactive state that will be updated when the batcher state changes\n *\n * Use this instead of `batcher.store.state`\n */\n readonly state: Accessor<Readonly<TSelected>>\n /**\n * @deprecated Use `batcher.state` instead of `batcher.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<BatcherState<TValue>>>\n}\n\n/**\n * Creates a Solid-compatible Batcher instance for managing batches of items, exposing Solid signals for all stateful properties.\n *\n * Features:\n * - Batch processing of items using the provided `fn` function\n * - Configurable batch size and wait time\n * - Custom batch processing logic via getShouldExecute\n * - Event callbacks for monitoring batch operations\n * - All stateful properties (items, counts, etc.) are exposed as Solid signals for reactivity\n *\n * The batcher collects items and processes them in batches based on:\n * - Maximum batch size\n * - Time-based batching (process after X milliseconds)\n * - Custom batch processing logic via getShouldExecute\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 `batcher.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 * - `executionCount`: Number of batch executions that have been completed\n * - `isRunning`: Whether the batcher is currently running (not stopped)\n * - `items`: Array of items currently queued for batching\n * - `totalItemsProcessed`: Total number of individual items that have been processed across all batches\n *\n * Example usage:\n * ```tsx\n * // Default behavior - no reactive state subscriptions\n * const batcher = createBatcher(\n * (items) => {\n * // Process batch of items\n * console.log('Processing batch:', items);\n * },\n * {\n * maxSize: 5,\n * wait: 2000,\n * onExecute: (batcher) => console.log('Batch executed'),\n * getShouldExecute: (items) => items.length >= 3\n * }\n * );\n *\n * // Opt-in to track items or isRunning changes (optimized for UI updates)\n * const batcher = createBatcher(\n * (items) => console.log('Processing batch:', items),\n * { maxSize: 5, wait: 2000 },\n * (state) => ({ items: state.items, isRunning: state.isRunning })\n * );\n *\n * // Opt-in to track execution metrics changes (optimized for tracking progress)\n * const batcher = createBatcher(\n * (items) => console.log('Processing batch:', items),\n * { maxSize: 5, wait: 2000 },\n * (state) => ({\n * executionCount: state.executionCount,\n * totalItemsProcessed: state.totalItemsProcessed\n * })\n * );\n *\n * // Add items to batch\n * batcher.addItem('task1');\n * batcher.addItem('task2');\n *\n * // Control the batcher\n * batcher.stop(); // Pause processing\n * batcher.start(); // Resume processing\n *\n * // Access the selected state (will be empty object {} unless selector provided)\n * const { items, isRunning } = batcher.state();\n * ```\n */\nexport function createBatcher<TValue, TSelected = {}>(\n fn: (items: Array<TValue>) => void,\n options: BatcherOptions<TValue> = {},\n selector: (state: BatcherState<TValue>) => TSelected = () =>\n ({}) as TSelected,\n): SolidBatcher<TValue, TSelected> {\n const mergedOptions = {\n ...useDefaultPacerOptions().batcher,\n ...options,\n } as BatcherOptions<TValue>\n\n const batcher = new Batcher(fn, mergedOptions) as unknown as SolidBatcher<\n TValue,\n TSelected\n >\n\n batcher.Subscribe = function Subscribe<TSelected>(props: {\n selector: (state: BatcherState<TValue>) => TSelected\n children: ((state: Accessor<TSelected>) => JSX.Element) | JSX.Element\n }) {\n const selected = useStore(batcher.store, props.selector)\n\n return typeof props.children === 'function'\n ? props.children(selected)\n : props.children\n }\n\n const state = useStore(batcher.store, selector)\n return {\n ...batcher,\n state,\n } as SolidBatcher<TValue, TSelected>\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAiIA,SAAgB,cACd,IACA,UAAkC,EAAE,EACpC,kBACG,EAAE,GAC4B;CAMjC,MAAM,UAAU,IAAI,QAAQ,IALN;EACpB,GAAG,wBAAwB,CAAC;EAC5B,GAAG;EACJ,CAE6C;AAK9C,SAAQ,YAAY,SAAS,UAAqB,OAG/C;EACD,MAAM,WAAW,SAAS,QAAQ,OAAO,MAAM,SAAS;AAExD,SAAO,OAAO,MAAM,aAAa,aAC7B,MAAM,SAAS,SAAS,GACxB,MAAM;;CAGZ,MAAM,QAAQ,SAAS,QAAQ,OAAO,SAAS;AAC/C,QAAO;EACL,GAAG;EACH;EACD"}
|
package/dist/batcher/index.cjs
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
const require_createBatcher = require('./createBatcher.cjs');
|
|
2
2
|
|
|
3
3
|
exports.createBatcher = require_createBatcher.createBatcher;
|
|
4
|
-
var
|
|
5
|
-
Object.keys(
|
|
4
|
+
var _tanstack_pacer_batcher = require("@tanstack/pacer/batcher");
|
|
5
|
+
Object.keys(_tanstack_pacer_batcher).forEach(function (k) {
|
|
6
6
|
if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
|
|
7
7
|
enumerable: true,
|
|
8
|
-
get: function () { return
|
|
8
|
+
get: function () { return _tanstack_pacer_batcher[k]; }
|
|
9
9
|
});
|
|
10
10
|
});
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
const require_PacerProvider = require('../provider/PacerProvider.cjs');
|
|
2
2
|
let solid_js = require("solid-js");
|
|
3
|
-
let
|
|
4
|
-
let
|
|
3
|
+
let _tanstack_solid_store = require("@tanstack/solid-store");
|
|
4
|
+
let _tanstack_pacer_debouncer = require("@tanstack/pacer/debouncer");
|
|
5
5
|
|
|
6
6
|
//#region src/debouncer/createDebouncer.ts
|
|
7
7
|
/**
|
|
@@ -21,14 +21,24 @@ let __tanstack_pacer_debouncer = require("@tanstack/pacer/debouncer");
|
|
|
21
21
|
*
|
|
22
22
|
* ## State Management and Selector
|
|
23
23
|
*
|
|
24
|
-
* The hook uses TanStack Store for reactive state management.
|
|
25
|
-
*
|
|
26
|
-
*
|
|
24
|
+
* The hook uses TanStack Store for reactive state management. You can subscribe to state changes
|
|
25
|
+
* in two ways:
|
|
26
|
+
*
|
|
27
|
+
* **1. Using `debouncer.Subscribe` component (Recommended for component tree subscriptions)**
|
|
28
|
+
*
|
|
29
|
+
* Use the `Subscribe` component to subscribe to state changes deep in your component tree without
|
|
30
|
+
* needing to pass a selector to the hook. This is ideal when you want to subscribe to state
|
|
31
|
+
* in child components.
|
|
32
|
+
*
|
|
33
|
+
* **2. Using the `selector` parameter (For hook-level subscriptions)**
|
|
34
|
+
*
|
|
35
|
+
* The `selector` parameter allows you to specify which state changes will trigger reactive updates
|
|
36
|
+
* at the hook level, optimizing performance by preventing unnecessary updates when irrelevant
|
|
37
|
+
* state changes occur.
|
|
27
38
|
*
|
|
28
39
|
* **By default, there will be no reactive state subscriptions** and you must opt-in to state
|
|
29
|
-
* tracking by providing a selector function. This prevents unnecessary
|
|
30
|
-
* full control over when your component
|
|
31
|
-
* component re-render when the selected state values change.
|
|
40
|
+
* tracking by providing a selector function or using the `Subscribe` component. This prevents unnecessary
|
|
41
|
+
* updates and gives you full control over when your component tracks state changes.
|
|
32
42
|
*
|
|
33
43
|
* Available state properties:
|
|
34
44
|
* - `canLeadingExecute`: Whether the debouncer can execute on the leading edge
|
|
@@ -45,21 +55,21 @@ let __tanstack_pacer_debouncer = require("@tanstack/pacer/debouncer");
|
|
|
45
55
|
* { wait: 500 }
|
|
46
56
|
* );
|
|
47
57
|
*
|
|
48
|
-
* // Opt-in to
|
|
58
|
+
* // Opt-in to track isPending changes (optimized for loading states)
|
|
49
59
|
* const debouncer = createDebouncer(
|
|
50
60
|
* (query: string) => fetchSearchResults(query),
|
|
51
61
|
* { wait: 500 },
|
|
52
62
|
* (state) => ({ isPending: state.isPending })
|
|
53
63
|
* );
|
|
54
64
|
*
|
|
55
|
-
* // Opt-in to
|
|
65
|
+
* // Opt-in to track executionCount changes (optimized for tracking execution)
|
|
56
66
|
* const debouncer = createDebouncer(
|
|
57
67
|
* (query: string) => fetchSearchResults(query),
|
|
58
68
|
* { wait: 500 },
|
|
59
69
|
* (state) => ({ executionCount: state.executionCount })
|
|
60
70
|
* );
|
|
61
71
|
*
|
|
62
|
-
* // Multiple state properties -
|
|
72
|
+
* // Multiple state properties - track when any of these change
|
|
63
73
|
* const debouncer = createDebouncer(
|
|
64
74
|
* (query: string) => fetchSearchResults(query),
|
|
65
75
|
* { wait: 500 },
|
|
@@ -80,11 +90,15 @@ let __tanstack_pacer_debouncer = require("@tanstack/pacer/debouncer");
|
|
|
80
90
|
* ```
|
|
81
91
|
*/
|
|
82
92
|
function createDebouncer(fn, options, selector = () => ({})) {
|
|
83
|
-
const asyncDebouncer = new
|
|
93
|
+
const asyncDebouncer = new _tanstack_pacer_debouncer.Debouncer(fn, {
|
|
84
94
|
...require_PacerProvider.useDefaultPacerOptions().debouncer,
|
|
85
95
|
...options
|
|
86
96
|
});
|
|
87
|
-
|
|
97
|
+
asyncDebouncer.Subscribe = function Subscribe(props) {
|
|
98
|
+
const selected = (0, _tanstack_solid_store.useStore)(asyncDebouncer.store, props.selector);
|
|
99
|
+
return typeof props.children === "function" ? props.children(selected) : props.children;
|
|
100
|
+
};
|
|
101
|
+
const state = (0, _tanstack_solid_store.useStore)(asyncDebouncer.store, selector);
|
|
88
102
|
(0, solid_js.createEffect)(() => {
|
|
89
103
|
(0, solid_js.onCleanup)(() => {
|
|
90
104
|
asyncDebouncer.cancel();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createDebouncer.cjs","names":["Debouncer","useDefaultPacerOptions"],"sources":["../../src/debouncer/createDebouncer.ts"],"sourcesContent":["import { Debouncer } from '@tanstack/pacer/debouncer'\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 DebouncerOptions,\n DebouncerState,\n} from '@tanstack/pacer/debouncer'\n\nexport interface SolidDebouncer<\n TFn extends AnyFunction,\n TSelected = {},\n> extends Omit<Debouncer<TFn>, 'store'> {\n /**\n * Reactive state that will be updated when the debouncer state changes\n *\n * Use this instead of `debouncer.store.state`\n */\n readonly state: Accessor<Readonly<TSelected>>\n /**\n * @deprecated Use `debouncer.state` instead of `debouncer.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<DebouncerState<TFn>>>\n}\n\n/**\n * A Solid hook that creates and manages a Debouncer instance.\n *\n * This is a lower-level hook that provides direct access to the Debouncer's functionality without\n * any built-in state management. This allows you to integrate it with any state management solution\n * you prefer (createSignal, Redux, Zustand, etc.).\n *\n * This hook provides debouncing functionality to limit how often a function can be called,\n * waiting for a specified delay before executing the latest call. This is useful for handling\n * frequent events like window resizing, scroll events, or real-time search inputs.\n *\n * The debouncer will only execute the function after the specified wait time has elapsed\n * since the last call. If the function is called again before the wait time expires, the\n * timer resets and starts waiting again.\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":"createDebouncer.cjs","names":["Debouncer","useDefaultPacerOptions"],"sources":["../../src/debouncer/createDebouncer.ts"],"sourcesContent":["import { Debouncer } from '@tanstack/pacer/debouncer'\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 DebouncerOptions,\n DebouncerState,\n} from '@tanstack/pacer/debouncer'\n\nexport interface SolidDebouncer<\n TFn extends AnyFunction,\n TSelected = {},\n> extends Omit<Debouncer<TFn>, 'store'> {\n /**\n * A Solid component that allows you to subscribe to the debouncer state.\n *\n * This is useful for tracking specific parts of the debouncer state\n * deep in your component tree without needing to pass a selector to the hook.\n *\n * @example\n * <debouncer.Subscribe selector={(state) => ({ isPending: state.isPending })}>\n * {(state) => (\n * <div>{state().isPending ? 'Waiting...' : 'Ready'}</div>\n * )}\n * </debouncer.Subscribe>\n */\n Subscribe: <TSelected>(props: {\n selector: (state: DebouncerState<TFn>) => TSelected\n children: ((state: Accessor<TSelected>) => JSX.Element) | JSX.Element\n }) => JSX.Element\n /**\n * Reactive state that will be updated when the debouncer state changes\n *\n * Use this instead of `debouncer.store.state`\n */\n readonly state: Accessor<Readonly<TSelected>>\n /**\n * @deprecated Use `debouncer.state` instead of `debouncer.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<DebouncerState<TFn>>>\n}\n\n/**\n * A Solid hook that creates and manages a Debouncer instance.\n *\n * This is a lower-level hook that provides direct access to the Debouncer's functionality without\n * any built-in state management. This allows you to integrate it with any state management solution\n * you prefer (createSignal, Redux, Zustand, etc.).\n *\n * This hook provides debouncing functionality to limit how often a function can be called,\n * waiting for a specified delay before executing the latest call. This is useful for handling\n * frequent events like window resizing, scroll events, or real-time search inputs.\n *\n * The debouncer will only execute the function after the specified wait time has elapsed\n * since the last call. If the function is called again before the wait time expires, the\n * timer resets and starts waiting again.\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 `debouncer.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 debouncer can execute on the leading edge\n * - `executionCount`: Number of function executions that have been completed\n * - `isPending`: Whether the debouncer is waiting for the timeout to trigger execution\n * - `lastArgs`: The arguments from the most recent call to maybeExecute\n * - `status`: Current execution status ('disabled' | 'idle' | 'pending')\n *\n * @example\n * ```tsx\n * // Default behavior - no reactive state subscriptions\n * const debouncer = createDebouncer(\n * (query: string) => fetchSearchResults(query),\n * { wait: 500 }\n * );\n *\n * // Opt-in to track isPending changes (optimized for loading states)\n * const debouncer = createDebouncer(\n * (query: string) => fetchSearchResults(query),\n * { wait: 500 },\n * (state) => ({ isPending: state.isPending })\n * );\n *\n * // Opt-in to track executionCount changes (optimized for tracking execution)\n * const debouncer = createDebouncer(\n * (query: string) => fetchSearchResults(query),\n * { wait: 500 },\n * (state) => ({ executionCount: state.executionCount })\n * );\n *\n * // Multiple state properties - track when any of these change\n * const debouncer = createDebouncer(\n * (query: string) => fetchSearchResults(query),\n * { wait: 500 },\n * (state) => ({\n * isPending: state.isPending,\n * executionCount: state.executionCount,\n * status: state.status\n * })\n * );\n *\n * // In an event handler\n * const handleChange = (e) => {\n * debouncer.maybeExecute(e.target.value);\n * };\n *\n * // Access the selected state (will be empty object {} unless selector provided)\n * const { isPending } = debouncer.state();\n * ```\n */\nexport function createDebouncer<TFn extends AnyFunction, TSelected = {}>(\n fn: TFn,\n options: DebouncerOptions<TFn>,\n selector: (state: DebouncerState<TFn>) => TSelected = () => ({}) as TSelected,\n): SolidDebouncer<TFn, TSelected> {\n const mergedOptions = {\n ...useDefaultPacerOptions().debouncer,\n ...options,\n } as DebouncerOptions<TFn>\n\n const asyncDebouncer = new Debouncer<TFn>(\n fn,\n mergedOptions,\n ) as unknown as SolidDebouncer<TFn, TSelected>\n\n asyncDebouncer.Subscribe = function Subscribe<TSelected>(props: {\n selector: (state: DebouncerState<TFn>) => TSelected\n children: ((state: Accessor<TSelected>) => JSX.Element) | JSX.Element\n }) {\n const selected = useStore(asyncDebouncer.store, props.selector)\n\n return typeof props.children === 'function'\n ? props.children(selected)\n : props.children\n }\n\n const state = useStore(asyncDebouncer.store, selector)\n\n createEffect(() => {\n onCleanup(() => {\n asyncDebouncer.cancel()\n })\n })\n\n return {\n ...asyncDebouncer,\n state,\n } as SolidDebouncer<TFn, TSelected> // omit `store` in favor of `state`\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAoIA,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 { Debouncer, DebouncerOptions, DebouncerState } from "@tanstack/pacer/debouncer";
|
|
5
5
|
|
|
6
6
|
//#region src/debouncer/createDebouncer.d.ts
|
|
7
7
|
interface SolidDebouncer<TFn extends AnyFunction, TSelected = {}> extends Omit<Debouncer<TFn>, 'store'> {
|
|
8
|
+
/**
|
|
9
|
+
* A Solid component that allows you to subscribe to the debouncer state.
|
|
10
|
+
*
|
|
11
|
+
* This is useful for tracking specific parts of the debouncer state
|
|
12
|
+
* deep in your component tree without needing to pass a selector to the hook.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* <debouncer.Subscribe selector={(state) => ({ isPending: state.isPending })}>
|
|
16
|
+
* {(state) => (
|
|
17
|
+
* <div>{state().isPending ? 'Waiting...' : 'Ready'}</div>
|
|
18
|
+
* )}
|
|
19
|
+
* </debouncer.Subscribe>
|
|
20
|
+
*/
|
|
21
|
+
Subscribe: <TSelected>(props: {
|
|
22
|
+
selector: (state: DebouncerState<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 debouncer state changes
|
|
10
27
|
*
|
|
@@ -35,14 +52,24 @@ interface SolidDebouncer<TFn extends AnyFunction, TSelected = {}> extends Omit<D
|
|
|
35
52
|
*
|
|
36
53
|
* ## State Management and Selector
|
|
37
54
|
*
|
|
38
|
-
* The hook uses TanStack Store for reactive state management.
|
|
39
|
-
*
|
|
40
|
-
*
|
|
55
|
+
* The hook uses TanStack Store for reactive state management. You can subscribe to state changes
|
|
56
|
+
* in two ways:
|
|
57
|
+
*
|
|
58
|
+
* **1. Using `debouncer.Subscribe` component (Recommended for component tree subscriptions)**
|
|
59
|
+
*
|
|
60
|
+
* Use the `Subscribe` component to subscribe to state changes deep in your component tree without
|
|
61
|
+
* needing to pass a selector to the hook. This is ideal when you want to subscribe to state
|
|
62
|
+
* in child components.
|
|
63
|
+
*
|
|
64
|
+
* **2. Using the `selector` parameter (For hook-level subscriptions)**
|
|
65
|
+
*
|
|
66
|
+
* The `selector` parameter allows you to specify which state changes will trigger reactive updates
|
|
67
|
+
* at the hook level, optimizing performance by preventing unnecessary updates when irrelevant
|
|
68
|
+
* state changes occur.
|
|
41
69
|
*
|
|
42
70
|
* **By default, there will be no reactive state subscriptions** and you must opt-in to state
|
|
43
|
-
* tracking by providing a selector function. This prevents unnecessary
|
|
44
|
-
* full control over when your component
|
|
45
|
-
* component re-render when the selected state values change.
|
|
71
|
+
* tracking by providing a selector function or using the `Subscribe` component. This prevents unnecessary
|
|
72
|
+
* updates and gives you full control over when your component tracks state changes.
|
|
46
73
|
*
|
|
47
74
|
* Available state properties:
|
|
48
75
|
* - `canLeadingExecute`: Whether the debouncer can execute on the leading edge
|
|
@@ -59,21 +86,21 @@ interface SolidDebouncer<TFn extends AnyFunction, TSelected = {}> extends Omit<D
|
|
|
59
86
|
* { wait: 500 }
|
|
60
87
|
* );
|
|
61
88
|
*
|
|
62
|
-
* // Opt-in to
|
|
89
|
+
* // Opt-in to track isPending changes (optimized for loading states)
|
|
63
90
|
* const debouncer = createDebouncer(
|
|
64
91
|
* (query: string) => fetchSearchResults(query),
|
|
65
92
|
* { wait: 500 },
|
|
66
93
|
* (state) => ({ isPending: state.isPending })
|
|
67
94
|
* );
|
|
68
95
|
*
|
|
69
|
-
* // Opt-in to
|
|
96
|
+
* // Opt-in to track executionCount changes (optimized for tracking execution)
|
|
70
97
|
* const debouncer = createDebouncer(
|
|
71
98
|
* (query: string) => fetchSearchResults(query),
|
|
72
99
|
* { wait: 500 },
|
|
73
100
|
* (state) => ({ executionCount: state.executionCount })
|
|
74
101
|
* );
|
|
75
102
|
*
|
|
76
|
-
* // Multiple state properties -
|
|
103
|
+
* // Multiple state properties - track when any of these change
|
|
77
104
|
* const debouncer = createDebouncer(
|
|
78
105
|
* (query: string) => fetchSearchResults(query),
|
|
79
106
|
* { wait: 500 },
|