@tanstack/solid-pacer 0.10.0 → 0.11.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/dist/cjs/async-batcher/createAsyncBatcher.cjs +1 -1
- package/dist/cjs/async-batcher/createAsyncBatcher.cjs.map +1 -1
- package/dist/cjs/async-batcher/createAsyncBatcher.d.cts +54 -6
- package/dist/cjs/async-debouncer/createAsyncDebouncer.cjs +1 -1
- package/dist/cjs/async-debouncer/createAsyncDebouncer.cjs.map +1 -1
- package/dist/cjs/async-debouncer/createAsyncDebouncer.d.cts +50 -8
- package/dist/cjs/async-queuer/createAsyncQueuer.cjs +1 -1
- package/dist/cjs/async-queuer/createAsyncQueuer.cjs.map +1 -1
- package/dist/cjs/async-queuer/createAsyncQueuer.d.cts +55 -6
- package/dist/cjs/async-rate-limiter/createAsyncRateLimiter.cjs +1 -1
- package/dist/cjs/async-rate-limiter/createAsyncRateLimiter.cjs.map +1 -1
- package/dist/cjs/async-rate-limiter/createAsyncRateLimiter.d.cts +59 -9
- package/dist/cjs/async-throttler/createAsyncThrottler.cjs +1 -1
- package/dist/cjs/async-throttler/createAsyncThrottler.cjs.map +1 -1
- package/dist/cjs/async-throttler/createAsyncThrottler.d.cts +54 -9
- package/dist/cjs/batcher/createBatcher.cjs +1 -1
- package/dist/cjs/batcher/createBatcher.cjs.map +1 -1
- package/dist/cjs/batcher/createBatcher.d.cts +46 -9
- package/dist/cjs/debouncer/createDebouncedSignal.cjs.map +1 -1
- package/dist/cjs/debouncer/createDebouncedSignal.d.cts +36 -4
- package/dist/cjs/debouncer/createDebouncedValue.cjs.map +1 -1
- package/dist/cjs/debouncer/createDebouncedValue.d.cts +30 -2
- package/dist/cjs/debouncer/createDebouncer.cjs +1 -1
- package/dist/cjs/debouncer/createDebouncer.cjs.map +1 -1
- package/dist/cjs/debouncer/createDebouncer.d.cts +56 -10
- package/dist/cjs/queuer/createQueuer.cjs +1 -1
- package/dist/cjs/queuer/createQueuer.cjs.map +1 -1
- package/dist/cjs/queuer/createQueuer.d.cts +46 -12
- package/dist/cjs/rate-limiter/createRateLimitedSignal.cjs.map +1 -1
- package/dist/cjs/rate-limiter/createRateLimitedSignal.d.cts +30 -3
- package/dist/cjs/rate-limiter/createRateLimitedValue.cjs.map +1 -1
- package/dist/cjs/rate-limiter/createRateLimitedValue.d.cts +32 -2
- package/dist/cjs/rate-limiter/createRateLimiter.cjs +1 -1
- package/dist/cjs/rate-limiter/createRateLimiter.cjs.map +1 -1
- package/dist/cjs/rate-limiter/createRateLimiter.d.cts +50 -8
- package/dist/cjs/throttler/createThrottledSignal.cjs.map +1 -1
- package/dist/cjs/throttler/createThrottledSignal.d.cts +34 -6
- package/dist/cjs/throttler/createThrottledValue.cjs.map +1 -1
- package/dist/cjs/throttler/createThrottledValue.d.cts +33 -2
- package/dist/cjs/throttler/createThrottler.cjs +1 -1
- package/dist/cjs/throttler/createThrottler.cjs.map +1 -1
- package/dist/cjs/throttler/createThrottler.d.cts +56 -12
- package/dist/esm/async-batcher/createAsyncBatcher.d.ts +54 -6
- package/dist/esm/async-batcher/createAsyncBatcher.js +1 -1
- package/dist/esm/async-batcher/createAsyncBatcher.js.map +1 -1
- package/dist/esm/async-debouncer/createAsyncDebouncer.d.ts +50 -8
- package/dist/esm/async-debouncer/createAsyncDebouncer.js +1 -1
- package/dist/esm/async-debouncer/createAsyncDebouncer.js.map +1 -1
- package/dist/esm/async-queuer/createAsyncQueuer.d.ts +55 -6
- package/dist/esm/async-queuer/createAsyncQueuer.js +1 -1
- package/dist/esm/async-queuer/createAsyncQueuer.js.map +1 -1
- package/dist/esm/async-rate-limiter/createAsyncRateLimiter.d.ts +59 -9
- package/dist/esm/async-rate-limiter/createAsyncRateLimiter.js +1 -1
- package/dist/esm/async-rate-limiter/createAsyncRateLimiter.js.map +1 -1
- package/dist/esm/async-throttler/createAsyncThrottler.d.ts +54 -9
- package/dist/esm/async-throttler/createAsyncThrottler.js +1 -1
- package/dist/esm/async-throttler/createAsyncThrottler.js.map +1 -1
- package/dist/esm/batcher/createBatcher.d.ts +46 -9
- package/dist/esm/batcher/createBatcher.js +1 -1
- package/dist/esm/batcher/createBatcher.js.map +1 -1
- package/dist/esm/debouncer/createDebouncedSignal.d.ts +36 -4
- package/dist/esm/debouncer/createDebouncedSignal.js.map +1 -1
- package/dist/esm/debouncer/createDebouncedValue.d.ts +30 -2
- package/dist/esm/debouncer/createDebouncedValue.js.map +1 -1
- package/dist/esm/debouncer/createDebouncer.d.ts +56 -10
- package/dist/esm/debouncer/createDebouncer.js +1 -1
- package/dist/esm/debouncer/createDebouncer.js.map +1 -1
- package/dist/esm/queuer/createQueuer.d.ts +46 -12
- package/dist/esm/queuer/createQueuer.js +1 -1
- package/dist/esm/queuer/createQueuer.js.map +1 -1
- package/dist/esm/rate-limiter/createRateLimitedSignal.d.ts +30 -3
- package/dist/esm/rate-limiter/createRateLimitedSignal.js.map +1 -1
- package/dist/esm/rate-limiter/createRateLimitedValue.d.ts +32 -2
- package/dist/esm/rate-limiter/createRateLimitedValue.js.map +1 -1
- package/dist/esm/rate-limiter/createRateLimiter.d.ts +50 -8
- package/dist/esm/rate-limiter/createRateLimiter.js +1 -1
- package/dist/esm/rate-limiter/createRateLimiter.js.map +1 -1
- package/dist/esm/throttler/createThrottledSignal.d.ts +34 -6
- package/dist/esm/throttler/createThrottledSignal.js.map +1 -1
- package/dist/esm/throttler/createThrottledValue.d.ts +33 -2
- package/dist/esm/throttler/createThrottledValue.js.map +1 -1
- package/dist/esm/throttler/createThrottler.d.ts +56 -12
- package/dist/esm/throttler/createThrottler.js +1 -1
- package/dist/esm/throttler/createThrottler.js.map +1 -1
- package/package.json +2 -2
- package/src/async-batcher/createAsyncBatcher.ts +58 -14
- package/src/async-debouncer/createAsyncDebouncer.ts +53 -10
- package/src/async-queuer/createAsyncQueuer.ts +58 -8
- package/src/async-rate-limiter/createAsyncRateLimiter.ts +61 -10
- package/src/async-throttler/createAsyncThrottler.ts +56 -10
- package/src/batcher/createBatcher.ts +48 -10
- package/src/debouncer/createDebouncedSignal.ts +36 -7
- package/src/debouncer/createDebouncedValue.ts +30 -5
- package/src/debouncer/createDebouncer.ts +58 -17
- package/src/queuer/createQueuer.ts +48 -14
- package/src/rate-limiter/createRateLimitedSignal.ts +30 -3
- package/src/rate-limiter/createRateLimitedValue.ts +32 -2
- package/src/rate-limiter/createRateLimiter.ts +53 -16
- package/src/throttler/createThrottledSignal.ts +34 -9
- package/src/throttler/createThrottledValue.ts +33 -5
- package/src/throttler/createThrottler.ts +58 -19
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
3
|
const asyncBatcher = require("@tanstack/pacer/async-batcher");
|
|
4
4
|
const solidStore = require("@tanstack/solid-store");
|
|
5
|
-
function createAsyncBatcher(fn, initialOptions = {}, selector) {
|
|
5
|
+
function createAsyncBatcher(fn, initialOptions = {}, selector = () => ({})) {
|
|
6
6
|
const asyncBatcher$1 = new asyncBatcher.AsyncBatcher(fn, initialOptions);
|
|
7
7
|
const state = solidStore.useStore(asyncBatcher$1.store, selector);
|
|
8
8
|
return {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createAsyncBatcher.cjs","sources":["../../../src/async-batcher/createAsyncBatcher.ts"],"sourcesContent":["import { AsyncBatcher } from '@tanstack/pacer/async-batcher'\nimport { useStore } from '@tanstack/solid-store'\nimport type { Accessor } from 'solid-js'\nimport type {\n AsyncBatcherOptions,\n AsyncBatcherState,\n} from '@tanstack/pacer/async-batcher'\n\nexport interface SolidAsyncBatcher
|
|
1
|
+
{"version":3,"file":"createAsyncBatcher.cjs","sources":["../../../src/async-batcher/createAsyncBatcher.ts"],"sourcesContent":["import { AsyncBatcher } from '@tanstack/pacer/async-batcher'\nimport { useStore } from '@tanstack/solid-store'\nimport type { Store } from '@tanstack/solid-store'\nimport type { Accessor } from 'solid-js'\nimport type {\n AsyncBatcherOptions,\n AsyncBatcherState,\n} from '@tanstack/pacer/async-batcher'\n\nexport interface SolidAsyncBatcher<TValue, TSelected = {}>\n extends Omit<AsyncBatcher<TValue>, 'store'> {\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<AsyncBatcherState<TValue>>>\n}\n\n/**\n * Creates a Solid-compatible AsyncBatcher instance for managing asynchronous batches of items, exposing Solid signals for all stateful properties.\n *\n * This is the async version of the createBatcher hook. Unlike the sync version, this async batcher:\n * - Handles promises and returns results from batch executions\n * - Provides error handling with configurable error behavior\n * - Tracks success, error, and settle counts separately\n * - Has state tracking for when batches are executing\n * - Returns the result of the batch function execution\n *\n * Features:\n * - Configurable batch size and wait time\n * - Custom batch processing logic via getShouldExecute\n * - Event callbacks for monitoring batch operations\n * - Error handling for failed batch operations\n * - Automatic or manual batch processing\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 (number of items per batch)\n * - Time-based batching (process after X milliseconds)\n * - Custom batch processing logic via getShouldExecute\n *\n * Error Handling:\n * - If an `onError` handler is provided, it will be called with the error and batcher instance\n * - If `throwOnError` is true (default when no onError handler is provided), the error will be thrown\n * - If `throwOnError` is false (default when onError handler is provided), the error will be swallowed\n * - Both onError and throwOnError can be used together; the handler will be called before any error is thrown\n * - The error state can be checked using the underlying AsyncBatcher instance\n *\n * ## State Management and Selector\n *\n * The hook uses TanStack Store for reactive state management. The `selector` parameter allows you\n * to specify which state changes will trigger a re-render, optimizing performance by preventing\n * unnecessary re-renders when irrelevant state changes occur.\n *\n * **By default, there will be no reactive state subscriptions** and you must opt-in to state\n * tracking by providing a selector function. This prevents unnecessary re-renders and gives you\n * full control over when your component updates. Only when you provide a selector will the\n * component re-render when the selected state values change.\n *\n * Available state properties:\n * - `errorCount`: Number of failed batch executions\n * - `executionCount`: Total number of batch execution attempts (successful + failed)\n * - `hasError`: Whether the last batch execution resulted in an error\n * - `isExecuting`: Whether a batch execution is currently in progress\n * - `items`: Array of items currently queued for batching\n * - `lastError`: The error from the most recent failed batch execution (if any)\n * - `lastResult`: The result from the most recent successful batch execution\n * - `settleCount`: Number of batch executions that have completed (successful or failed)\n * - `successCount`: Number of successful batch executions\n *\n * Example usage:\n * ```tsx\n * // Default behavior - no reactive state subscriptions\n * const asyncBatcher = createAsyncBatcher(\n * async (items) => {\n * const results = await Promise.all(items.map(item => processItem(item)));\n * return results;\n * },\n * {\n * maxSize: 10,\n * wait: 2000,\n * onSuccess: (result) => {\n * console.log('Batch processed successfully:', result);\n * },\n * onError: (error) => {\n * console.error('Batch processing failed:', error);\n * }\n * }\n * );\n *\n * // Opt-in to re-render when items or isExecuting changes (optimized for UI updates)\n * const asyncBatcher = createAsyncBatcher(\n * async (items) => {\n * const results = await Promise.all(items.map(item => processItem(item)));\n * return results;\n * },\n * { maxSize: 10, wait: 2000 },\n * (state) => ({ items: state.items, isExecuting: state.isExecuting })\n * );\n *\n * // Opt-in to re-render when error state changes (optimized for error handling)\n * const asyncBatcher = createAsyncBatcher(\n * async (items) => {\n * const results = await Promise.all(items.map(item => processItem(item)));\n * return results;\n * },\n * { maxSize: 10, wait: 2000 },\n * (state) => ({ hasError: state.hasError, lastError: state.lastError })\n * );\n *\n * // Add items to batch\n * asyncBatcher.addItem(newItem);\n *\n * // Manually execute batch\n * const result = await asyncBatcher.execute();\n *\n * // Access the selected state (will be empty object {} unless selector provided)\n * const { items, isExecuting } = asyncBatcher.state();\n * ```\n */\nexport function createAsyncBatcher<TValue, TSelected = {}>(\n fn: (items: Array<TValue>) => Promise<any>,\n initialOptions: AsyncBatcherOptions<TValue> = {},\n selector: (state: AsyncBatcherState<TValue>) => TSelected = () =>\n ({}) as TSelected,\n): SolidAsyncBatcher<TValue, TSelected> {\n const asyncBatcher = new AsyncBatcher<TValue>(fn, initialOptions)\n\n const state = useStore(asyncBatcher.store, selector)\n\n return {\n ...asyncBatcher,\n state,\n } as SolidAsyncBatcher<TValue, TSelected> // omit `store` in favor of `state`\n}\n"],"names":["asyncBatcher","AsyncBatcher","useStore"],"mappings":";;;;AA+HO,SAAS,mBACd,IACA,iBAA8C,CAAA,GAC9C,WAA4D,OACzD,CAAA,IACmC;AACtC,QAAMA,iBAAe,IAAIC,0BAAqB,IAAI,cAAc;AAEhE,QAAM,QAAQC,WAAAA,SAASF,eAAa,OAAO,QAAQ;AAEnD,SAAO;AAAA,IACL,GAAGA;AAAAA,IACH;AAAA,EAAA;AAEJ;;"}
|
|
@@ -1,12 +1,19 @@
|
|
|
1
1
|
import { AsyncBatcher, AsyncBatcherOptions, AsyncBatcherState } from '@tanstack/pacer/async-batcher';
|
|
2
|
+
import { Store } from '@tanstack/solid-store';
|
|
2
3
|
import { Accessor } from 'solid-js';
|
|
3
|
-
export interface SolidAsyncBatcher<TValue, TSelected =
|
|
4
|
+
export interface SolidAsyncBatcher<TValue, TSelected = {}> extends Omit<AsyncBatcher<TValue>, 'store'> {
|
|
4
5
|
/**
|
|
5
6
|
* Reactive state that will be updated when the batcher state changes
|
|
6
7
|
*
|
|
7
8
|
* Use this instead of `batcher.store.state`
|
|
8
9
|
*/
|
|
9
10
|
readonly state: Accessor<Readonly<TSelected>>;
|
|
11
|
+
/**
|
|
12
|
+
* @deprecated Use `batcher.state` instead of `batcher.store.state` if you want to read reactive state.
|
|
13
|
+
* The state on the store object is not reactive, as it has not been wrapped in a `useStore` hook internally.
|
|
14
|
+
* Although, you can make the state reactive by using the `useStore` in your own usage.
|
|
15
|
+
*/
|
|
16
|
+
readonly store: Store<Readonly<AsyncBatcherState<TValue>>>;
|
|
10
17
|
}
|
|
11
18
|
/**
|
|
12
19
|
* Creates a Solid-compatible AsyncBatcher instance for managing asynchronous batches of items, exposing Solid signals for all stateful properties.
|
|
@@ -38,9 +45,31 @@ export interface SolidAsyncBatcher<TValue, TSelected = AsyncBatcherState<TValue>
|
|
|
38
45
|
* - Both onError and throwOnError can be used together; the handler will be called before any error is thrown
|
|
39
46
|
* - The error state can be checked using the underlying AsyncBatcher instance
|
|
40
47
|
*
|
|
48
|
+
* ## State Management and Selector
|
|
49
|
+
*
|
|
50
|
+
* The hook uses TanStack Store for reactive state management. The `selector` parameter allows you
|
|
51
|
+
* to specify which state changes will trigger a re-render, optimizing performance by preventing
|
|
52
|
+
* unnecessary re-renders when irrelevant state changes occur.
|
|
53
|
+
*
|
|
54
|
+
* **By default, there will be no reactive state subscriptions** and you must opt-in to state
|
|
55
|
+
* tracking by providing a selector function. This prevents unnecessary re-renders and gives you
|
|
56
|
+
* full control over when your component updates. Only when you provide a selector will the
|
|
57
|
+
* component re-render when the selected state values change.
|
|
58
|
+
*
|
|
59
|
+
* Available state properties:
|
|
60
|
+
* - `errorCount`: Number of failed batch executions
|
|
61
|
+
* - `executionCount`: Total number of batch execution attempts (successful + failed)
|
|
62
|
+
* - `hasError`: Whether the last batch execution resulted in an error
|
|
63
|
+
* - `isExecuting`: Whether a batch execution is currently in progress
|
|
64
|
+
* - `items`: Array of items currently queued for batching
|
|
65
|
+
* - `lastError`: The error from the most recent failed batch execution (if any)
|
|
66
|
+
* - `lastResult`: The result from the most recent successful batch execution
|
|
67
|
+
* - `settleCount`: Number of batch executions that have completed (successful or failed)
|
|
68
|
+
* - `successCount`: Number of successful batch executions
|
|
69
|
+
*
|
|
41
70
|
* Example usage:
|
|
42
71
|
* ```tsx
|
|
43
|
-
* //
|
|
72
|
+
* // Default behavior - no reactive state subscriptions
|
|
44
73
|
* const asyncBatcher = createAsyncBatcher(
|
|
45
74
|
* async (items) => {
|
|
46
75
|
* const results = await Promise.all(items.map(item => processItem(item)));
|
|
@@ -58,15 +87,34 @@ export interface SolidAsyncBatcher<TValue, TSelected = AsyncBatcherState<TValue>
|
|
|
58
87
|
* }
|
|
59
88
|
* );
|
|
60
89
|
*
|
|
90
|
+
* // Opt-in to re-render when items or isExecuting changes (optimized for UI updates)
|
|
91
|
+
* const asyncBatcher = createAsyncBatcher(
|
|
92
|
+
* async (items) => {
|
|
93
|
+
* const results = await Promise.all(items.map(item => processItem(item)));
|
|
94
|
+
* return results;
|
|
95
|
+
* },
|
|
96
|
+
* { maxSize: 10, wait: 2000 },
|
|
97
|
+
* (state) => ({ items: state.items, isExecuting: state.isExecuting })
|
|
98
|
+
* );
|
|
99
|
+
*
|
|
100
|
+
* // Opt-in to re-render when error state changes (optimized for error handling)
|
|
101
|
+
* const asyncBatcher = createAsyncBatcher(
|
|
102
|
+
* async (items) => {
|
|
103
|
+
* const results = await Promise.all(items.map(item => processItem(item)));
|
|
104
|
+
* return results;
|
|
105
|
+
* },
|
|
106
|
+
* { maxSize: 10, wait: 2000 },
|
|
107
|
+
* (state) => ({ hasError: state.hasError, lastError: state.lastError })
|
|
108
|
+
* );
|
|
109
|
+
*
|
|
61
110
|
* // Add items to batch
|
|
62
111
|
* asyncBatcher.addItem(newItem);
|
|
63
112
|
*
|
|
64
113
|
* // Manually execute batch
|
|
65
114
|
* const result = await asyncBatcher.execute();
|
|
66
115
|
*
|
|
67
|
-
* //
|
|
68
|
-
* const items = asyncBatcher.state()
|
|
69
|
-
* const isExecuting = asyncBatcher.state().isExecuting;
|
|
116
|
+
* // Access the selected state (will be empty object {} unless selector provided)
|
|
117
|
+
* const { items, isExecuting } = asyncBatcher.state();
|
|
70
118
|
* ```
|
|
71
119
|
*/
|
|
72
|
-
export declare function createAsyncBatcher<TValue, TSelected =
|
|
120
|
+
export declare function createAsyncBatcher<TValue, TSelected = {}>(fn: (items: Array<TValue>) => Promise<any>, initialOptions?: AsyncBatcherOptions<TValue>, selector?: (state: AsyncBatcherState<TValue>) => TSelected): SolidAsyncBatcher<TValue, TSelected>;
|
|
@@ -3,7 +3,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
|
3
3
|
const asyncDebouncer = require("@tanstack/pacer/async-debouncer");
|
|
4
4
|
const solidStore = require("@tanstack/solid-store");
|
|
5
5
|
const solidJs = require("solid-js");
|
|
6
|
-
function createAsyncDebouncer(fn, initialOptions, selector) {
|
|
6
|
+
function createAsyncDebouncer(fn, initialOptions, selector = () => ({})) {
|
|
7
7
|
const asyncDebouncer$1 = new asyncDebouncer.AsyncDebouncer(fn, initialOptions);
|
|
8
8
|
const state = solidStore.useStore(asyncDebouncer$1.store, selector);
|
|
9
9
|
solidJs.createEffect(() => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createAsyncDebouncer.cjs","sources":["../../../src/async-debouncer/createAsyncDebouncer.ts"],"sourcesContent":["import { AsyncDebouncer } from '@tanstack/pacer/async-debouncer'\nimport { useStore } from '@tanstack/solid-store'\nimport { createEffect, onCleanup } from 'solid-js'\nimport type { Accessor } from 'solid-js'\nimport type {\n AsyncDebouncerOptions,\n AsyncDebouncerState,\n} from '@tanstack/pacer/async-debouncer'\nimport type { AnyAsyncFunction } from '@tanstack/pacer/types'\n\nexport interface SolidAsyncDebouncer<\n TFn extends AnyAsyncFunction,\n TSelected =
|
|
1
|
+
{"version":3,"file":"createAsyncDebouncer.cjs","sources":["../../../src/async-debouncer/createAsyncDebouncer.ts"],"sourcesContent":["import { AsyncDebouncer } from '@tanstack/pacer/async-debouncer'\nimport { useStore } from '@tanstack/solid-store'\nimport { createEffect, onCleanup } from 'solid-js'\nimport type { Store } from '@tanstack/solid-store'\nimport type { Accessor } from 'solid-js'\nimport type {\n AsyncDebouncerOptions,\n AsyncDebouncerState,\n} from '@tanstack/pacer/async-debouncer'\nimport type { AnyAsyncFunction } from '@tanstack/pacer/types'\n\nexport interface SolidAsyncDebouncer<\n TFn extends AnyAsyncFunction,\n TSelected = {},\n> extends Omit<AsyncDebouncer<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<AsyncDebouncerState<TFn>>>\n}\n\n/**\n * A low-level Solid hook that creates an `AsyncDebouncer` instance to delay execution of an async function.\n *\n * This hook is designed to be flexible and state-management agnostic - it simply returns a debouncer instance that\n * you can integrate with any state management solution (createSignal, etc).\n *\n * Async debouncing ensures that an async function only executes after a specified delay has passed since its last invocation.\n * Each new invocation resets the delay timer. This is useful for handling frequent events like window resizing\n * or input changes where you only want to execute the handler after the events have stopped occurring.\n *\n * Unlike throttling which allows execution at regular intervals, debouncing prevents any execution until\n * the function stops being called for the specified delay period.\n *\n * Unlike the non-async Debouncer, this async version supports returning values from the debounced function,\n * making it ideal for API calls and other async operations where you want the result of the `maybeExecute` call\n * instead of setting the result on a state variable from within the debounced function.\n *\n * Error Handling:\n * - If an `onError` handler is provided, it will be called with the error and debouncer instance\n * - If `throwOnError` is true (default when no onError handler is provided), the error will be thrown\n * - If `throwOnError` is false (default when onError handler is provided), the error will be swallowed\n * - Both onError and throwOnError can be used together - the handler will be called before any error is thrown\n * - The error state can be checked using the underlying AsyncDebouncer instance\n *\n * ## State Management and Selector\n *\n * The hook uses TanStack Store for reactive state management. The `selector` parameter allows you\n * to specify which state changes will trigger a re-render, optimizing performance by preventing\n * unnecessary re-renders when irrelevant state changes occur.\n *\n * **By default, there will be no reactive state subscriptions** and you must opt-in to state\n * tracking by providing a selector function. This prevents unnecessary re-renders and gives you\n * full control over when your component updates. Only when you provide a selector will the\n * component re-render when the selected state values change.\n *\n * Available state properties:\n * - `canLeadingExecute`: Whether the debouncer can execute on the leading edge\n * - `executionCount`: Number of function executions that have been completed\n * - `hasError`: Whether the last execution resulted in an error\n * - `isPending`: Whether the debouncer is waiting for the timeout to trigger execution\n * - `isExecuting`: Whether an async function execution is currently in progress\n * - `lastArgs`: The arguments from the most recent call to maybeExecute\n * - `lastError`: The error from the most recent failed execution (if any)\n * - `lastResult`: The result from the most recent successful execution\n * - `status`: Current execution status ('disabled' | 'idle' | 'pending' | 'executing')\n *\n * @example\n * ```tsx\n * // Default behavior - no reactive state subscriptions\n * const { maybeExecute } = createAsyncDebouncer(\n * async (query: string) => {\n * const results = await api.search(query);\n * return results;\n * },\n * { wait: 500 }\n * );\n *\n * // Opt-in to re-render when isPending or isExecuting changes (optimized for loading states)\n * const debouncer = createAsyncDebouncer(\n * async (query: string) => {\n * const results = await api.search(query);\n * return results;\n * },\n * { wait: 500 },\n * (state) => ({ isPending: state.isPending, isExecuting: state.isExecuting })\n * );\n *\n * // Opt-in to re-render when error state changes (optimized for error handling)\n * const debouncer = createAsyncDebouncer(\n * async (searchTerm) => {\n * const data = await searchAPI(searchTerm);\n * return data;\n * },\n * {\n * wait: 300,\n * leading: true, // Execute immediately on first call\n * trailing: false, // Skip trailing edge updates\n * onError: (error) => {\n * console.error('API call failed:', error);\n * }\n * },\n * (state) => ({ hasError: state.hasError, lastError: state.lastError })\n * );\n *\n * // Access the selected state (will be empty object {} unless selector provided)\n * const { isPending, isExecuting } = debouncer.state();\n * ```\n */\nexport function createAsyncDebouncer<\n TFn extends AnyAsyncFunction,\n TSelected = {},\n>(\n fn: TFn,\n initialOptions: AsyncDebouncerOptions<TFn>,\n selector: (state: AsyncDebouncerState<TFn>) => TSelected = () =>\n ({}) as TSelected,\n): SolidAsyncDebouncer<TFn, TSelected> {\n const asyncDebouncer = new AsyncDebouncer<TFn>(fn, initialOptions)\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 SolidAsyncDebouncer<TFn, TSelected> // omit `store` in favor of `state`\n}\n"],"names":["asyncDebouncer","AsyncDebouncer","useStore","createEffect","onCleanup"],"mappings":";;;;;AAqHO,SAAS,qBAId,IACA,gBACA,WAA2D,OACxD,CAAA,IACkC;AACrC,QAAMA,mBAAiB,IAAIC,8BAAoB,IAAI,cAAc;AAEjE,QAAM,QAAQC,WAAAA,SAASF,iBAAe,OAAO,QAAQ;AAErDG,UAAAA,aAAa,MAAM;AACjBC,YAAAA,UAAU,MAAM;AACdJ,uBAAe,OAAA;AAAA,IAAO,CACvB;AAAA,EAAA,CACF;AAED,SAAO;AAAA,IACL,GAAGA;AAAAA,IACH;AAAA,EAAA;AAEJ;;"}
|
|
@@ -1,13 +1,20 @@
|
|
|
1
1
|
import { AsyncDebouncer, AsyncDebouncerOptions, AsyncDebouncerState } from '@tanstack/pacer/async-debouncer';
|
|
2
|
+
import { Store } from '@tanstack/solid-store';
|
|
2
3
|
import { Accessor } from 'solid-js';
|
|
3
4
|
import { AnyAsyncFunction } from '@tanstack/pacer/types';
|
|
4
|
-
export interface SolidAsyncDebouncer<TFn extends AnyAsyncFunction, TSelected =
|
|
5
|
+
export interface SolidAsyncDebouncer<TFn extends AnyAsyncFunction, TSelected = {}> extends Omit<AsyncDebouncer<TFn>, 'store'> {
|
|
5
6
|
/**
|
|
6
7
|
* Reactive state that will be updated when the debouncer state changes
|
|
7
8
|
*
|
|
8
9
|
* Use this instead of `debouncer.store.state`
|
|
9
10
|
*/
|
|
10
11
|
readonly state: Accessor<Readonly<TSelected>>;
|
|
12
|
+
/**
|
|
13
|
+
* @deprecated Use `debouncer.state` instead of `debouncer.store.state` if you want to read reactive state.
|
|
14
|
+
* The state on the store object is not reactive, as it has not been wrapped in a `useStore` hook internally.
|
|
15
|
+
* Although, you can make the state reactive by using the `useStore` in your own usage.
|
|
16
|
+
*/
|
|
17
|
+
readonly store: Store<Readonly<AsyncDebouncerState<TFn>>>;
|
|
11
18
|
}
|
|
12
19
|
/**
|
|
13
20
|
* A low-level Solid hook that creates an `AsyncDebouncer` instance to delay execution of an async function.
|
|
@@ -33,9 +40,31 @@ export interface SolidAsyncDebouncer<TFn extends AnyAsyncFunction, TSelected = A
|
|
|
33
40
|
* - Both onError and throwOnError can be used together - the handler will be called before any error is thrown
|
|
34
41
|
* - The error state can be checked using the underlying AsyncDebouncer instance
|
|
35
42
|
*
|
|
43
|
+
* ## State Management and Selector
|
|
44
|
+
*
|
|
45
|
+
* The hook uses TanStack Store for reactive state management. The `selector` parameter allows you
|
|
46
|
+
* to specify which state changes will trigger a re-render, optimizing performance by preventing
|
|
47
|
+
* unnecessary re-renders when irrelevant state changes occur.
|
|
48
|
+
*
|
|
49
|
+
* **By default, there will be no reactive state subscriptions** and you must opt-in to state
|
|
50
|
+
* tracking by providing a selector function. This prevents unnecessary re-renders and gives you
|
|
51
|
+
* full control over when your component updates. Only when you provide a selector will the
|
|
52
|
+
* component re-render when the selected state values change.
|
|
53
|
+
*
|
|
54
|
+
* Available state properties:
|
|
55
|
+
* - `canLeadingExecute`: Whether the debouncer can execute on the leading edge
|
|
56
|
+
* - `executionCount`: Number of function executions that have been completed
|
|
57
|
+
* - `hasError`: Whether the last execution resulted in an error
|
|
58
|
+
* - `isPending`: Whether the debouncer is waiting for the timeout to trigger execution
|
|
59
|
+
* - `isExecuting`: Whether an async function execution is currently in progress
|
|
60
|
+
* - `lastArgs`: The arguments from the most recent call to maybeExecute
|
|
61
|
+
* - `lastError`: The error from the most recent failed execution (if any)
|
|
62
|
+
* - `lastResult`: The result from the most recent successful execution
|
|
63
|
+
* - `status`: Current execution status ('disabled' | 'idle' | 'pending' | 'executing')
|
|
64
|
+
*
|
|
36
65
|
* @example
|
|
37
66
|
* ```tsx
|
|
38
|
-
* //
|
|
67
|
+
* // Default behavior - no reactive state subscriptions
|
|
39
68
|
* const { maybeExecute } = createAsyncDebouncer(
|
|
40
69
|
* async (query: string) => {
|
|
41
70
|
* const results = await api.search(query);
|
|
@@ -44,12 +73,21 @@ export interface SolidAsyncDebouncer<TFn extends AnyAsyncFunction, TSelected = A
|
|
|
44
73
|
* { wait: 500 }
|
|
45
74
|
* );
|
|
46
75
|
*
|
|
47
|
-
* //
|
|
48
|
-
* const
|
|
49
|
-
*
|
|
76
|
+
* // Opt-in to re-render when isPending or isExecuting changes (optimized for loading states)
|
|
77
|
+
* const debouncer = createAsyncDebouncer(
|
|
78
|
+
* async (query: string) => {
|
|
79
|
+
* const results = await api.search(query);
|
|
80
|
+
* return results;
|
|
81
|
+
* },
|
|
82
|
+
* { wait: 500 },
|
|
83
|
+
* (state) => ({ isPending: state.isPending, isExecuting: state.isExecuting })
|
|
84
|
+
* );
|
|
85
|
+
*
|
|
86
|
+
* // Opt-in to re-render when error state changes (optimized for error handling)
|
|
87
|
+
* const debouncer = createAsyncDebouncer(
|
|
50
88
|
* async (searchTerm) => {
|
|
51
89
|
* const data = await searchAPI(searchTerm);
|
|
52
|
-
*
|
|
90
|
+
* return data;
|
|
53
91
|
* },
|
|
54
92
|
* {
|
|
55
93
|
* wait: 300,
|
|
@@ -58,8 +96,12 @@ export interface SolidAsyncDebouncer<TFn extends AnyAsyncFunction, TSelected = A
|
|
|
58
96
|
* onError: (error) => {
|
|
59
97
|
* console.error('API call failed:', error);
|
|
60
98
|
* }
|
|
61
|
-
* }
|
|
99
|
+
* },
|
|
100
|
+
* (state) => ({ hasError: state.hasError, lastError: state.lastError })
|
|
62
101
|
* );
|
|
102
|
+
*
|
|
103
|
+
* // Access the selected state (will be empty object {} unless selector provided)
|
|
104
|
+
* const { isPending, isExecuting } = debouncer.state();
|
|
63
105
|
* ```
|
|
64
106
|
*/
|
|
65
|
-
export declare function createAsyncDebouncer<TFn extends AnyAsyncFunction, TSelected =
|
|
107
|
+
export declare function createAsyncDebouncer<TFn extends AnyAsyncFunction, TSelected = {}>(fn: TFn, initialOptions: AsyncDebouncerOptions<TFn>, selector?: (state: AsyncDebouncerState<TFn>) => TSelected): SolidAsyncDebouncer<TFn, TSelected>;
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
3
|
const asyncQueuer = require("@tanstack/pacer/async-queuer");
|
|
4
4
|
const solidStore = require("@tanstack/solid-store");
|
|
5
|
-
function createAsyncQueuer(fn, initialOptions = {}, selector) {
|
|
5
|
+
function createAsyncQueuer(fn, initialOptions = {}, selector = () => ({})) {
|
|
6
6
|
const asyncQueuer$1 = new asyncQueuer.AsyncQueuer(fn, initialOptions);
|
|
7
7
|
const state = solidStore.useStore(asyncQueuer$1.store, selector);
|
|
8
8
|
return {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createAsyncQueuer.cjs","sources":["../../../src/async-queuer/createAsyncQueuer.ts"],"sourcesContent":["import { AsyncQueuer } from '@tanstack/pacer/async-queuer'\nimport { useStore } from '@tanstack/solid-store'\nimport type { Accessor } from 'solid-js'\nimport type {\n AsyncQueuerOptions,\n AsyncQueuerState,\n} from '@tanstack/pacer/async-queuer'\n\nexport interface SolidAsyncQueuer<TValue, TSelected =
|
|
1
|
+
{"version":3,"file":"createAsyncQueuer.cjs","sources":["../../../src/async-queuer/createAsyncQueuer.ts"],"sourcesContent":["import { AsyncQueuer } from '@tanstack/pacer/async-queuer'\nimport { useStore } from '@tanstack/solid-store'\nimport type { Store } from '@tanstack/solid-store'\nimport type { Accessor } from 'solid-js'\nimport type {\n AsyncQueuerOptions,\n AsyncQueuerState,\n} from '@tanstack/pacer/async-queuer'\n\nexport interface SolidAsyncQueuer<TValue, TSelected = {}>\n extends Omit<AsyncQueuer<TValue>, 'store'> {\n /**\n * Reactive state that will be updated when the queuer state changes\n *\n * Use this instead of `queuer.store.state`\n */\n readonly state: Accessor<Readonly<TSelected>>\n /**\n * @deprecated Use `queuer.state` instead of `queuer.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<AsyncQueuerState<TValue>>>\n}\n\n/**\n * Creates a Solid-compatible AsyncQueuer instance for managing an asynchronous queue of items, exposing Solid signals for all stateful properties.\n *\n * Features:\n * - Priority queueing via `getPriority` or item `priority` property\n * - Configurable concurrency limit\n * - FIFO (First In First Out) or LIFO (Last In First Out) queue behavior\n * - Pause/resume processing\n * - Task cancellation\n * - Item expiration\n * - Lifecycle callbacks for success, error, settled, items change, etc.\n * - All stateful properties (active items, pending items, counts, etc.) are exposed as Solid signals for reactivity\n *\n * Tasks are processed concurrently up to the configured concurrency limit. When a task completes,\n * the next pending task is processed if the concurrency limit allows.\n *\n * Error Handling:\n * - If an `onError` handler is provided, it will be called with the error and queuer instance\n * - If `throwOnError` is true (default when no onError handler is provided), the error will be thrown\n * - If `throwOnError` is false (default when onError handler is provided), the error will be swallowed\n * - Both onError and throwOnError can be used together; the handler will be called before any error is thrown\n * - The error state can be checked using the underlying AsyncQueuer instance\n *\n * ## State Management and Selector\n *\n * The hook uses TanStack Store for reactive state management. The `selector` parameter allows you\n * to specify which state changes will trigger a re-render, optimizing performance by preventing\n * unnecessary re-renders when irrelevant state changes occur.\n *\n * **By default, there will be no reactive state subscriptions** and you must opt-in to state\n * tracking by providing a selector function. This prevents unnecessary re-renders and gives you\n * full control over when your component updates. Only when you provide a selector will the\n * component re-render when the selected state values change.\n *\n * Available state properties:\n * - `activeItems`: Array of items currently being processed\n * - `errorCount`: Number of items that failed processing\n * - `isRunning`: Whether the queuer is currently running (not stopped)\n * - `pendingItems`: Array of items waiting to be processed\n * - `rejectionCount`: Number of items that were rejected (expired or failed validation)\n * - `settleCount`: Number of items that have completed processing (successful or failed)\n * - `successCount`: Number of items that were processed successfully\n *\n * Example usage:\n * ```tsx\n * // Default behavior - no reactive state subscriptions\n * const asyncQueuer = createAsyncQueuer(async (item) => {\n * // process item\n * return await fetchData(item);\n * }, {\n * initialItems: [],\n * concurrency: 2,\n * maxSize: 100,\n * started: false,\n * onSuccess: (result) => {\n * console.log('Item processed:', result);\n * },\n * onError: (error) => {\n * console.error('Processing failed:', error);\n * }\n * });\n *\n * // Opt-in to re-render when queue state changes (optimized for UI updates)\n * const asyncQueuer = createAsyncQueuer(\n * async (item) => await fetchData(item),\n * { concurrency: 2, started: true },\n * (state) => ({\n * pendingItems: state.pendingItems,\n * activeItems: state.activeItems,\n * isRunning: state.isRunning\n * })\n * );\n *\n * // Opt-in to re-render when processing metrics change (optimized for tracking progress)\n * const asyncQueuer = createAsyncQueuer(\n * async (item) => await fetchData(item),\n * { concurrency: 2, started: true },\n * (state) => ({\n * successCount: state.successCount,\n * errorCount: state.errorCount,\n * settleCount: state.settleCount\n * })\n * );\n *\n * // Add items to queue\n * asyncQueuer.addItem(newItem);\n *\n * // Start processing\n * asyncQueuer.start();\n *\n * // Access the selected state (will be empty object {} unless selector provided)\n * const { pendingItems, activeItems } = asyncQueuer.state();\n * ```\n */\nexport function createAsyncQueuer<TValue, TSelected = {}>(\n fn: (value: TValue) => Promise<any>,\n initialOptions: AsyncQueuerOptions<TValue> = {},\n selector: (state: AsyncQueuerState<TValue>) => TSelected = () =>\n ({}) as TSelected,\n): SolidAsyncQueuer<TValue, TSelected> {\n const asyncQueuer = new AsyncQueuer<TValue>(fn, initialOptions)\n\n const state = useStore(asyncQueuer.store, selector)\n\n return {\n ...asyncQueuer,\n state,\n } as SolidAsyncQueuer<TValue, TSelected> // omit `store` in favor of `state`\n}\n"],"names":["asyncQueuer","AsyncQueuer","useStore"],"mappings":";;;;AAuHO,SAAS,kBACd,IACA,iBAA6C,CAAA,GAC7C,WAA2D,OACxD,CAAA,IACkC;AACrC,QAAMA,gBAAc,IAAIC,wBAAoB,IAAI,cAAc;AAE9D,QAAM,QAAQC,WAAAA,SAASF,cAAY,OAAO,QAAQ;AAElD,SAAO;AAAA,IACL,GAAGA;AAAAA,IACH;AAAA,EAAA;AAEJ;;"}
|
|
@@ -1,12 +1,19 @@
|
|
|
1
1
|
import { AsyncQueuer, AsyncQueuerOptions, AsyncQueuerState } from '@tanstack/pacer/async-queuer';
|
|
2
|
+
import { Store } from '@tanstack/solid-store';
|
|
2
3
|
import { Accessor } from 'solid-js';
|
|
3
|
-
export interface SolidAsyncQueuer<TValue, TSelected =
|
|
4
|
+
export interface SolidAsyncQueuer<TValue, TSelected = {}> extends Omit<AsyncQueuer<TValue>, 'store'> {
|
|
4
5
|
/**
|
|
5
|
-
* Reactive state that will be updated
|
|
6
|
+
* Reactive state that will be updated when the queuer state changes
|
|
6
7
|
*
|
|
7
8
|
* Use this instead of `queuer.store.state`
|
|
8
9
|
*/
|
|
9
10
|
readonly state: Accessor<Readonly<TSelected>>;
|
|
11
|
+
/**
|
|
12
|
+
* @deprecated Use `queuer.state` instead of `queuer.store.state` if you want to read reactive state.
|
|
13
|
+
* The state on the store object is not reactive, as it has not been wrapped in a `useStore` hook internally.
|
|
14
|
+
* Although, you can make the state reactive by using the `useStore` in your own usage.
|
|
15
|
+
*/
|
|
16
|
+
readonly store: Store<Readonly<AsyncQueuerState<TValue>>>;
|
|
10
17
|
}
|
|
11
18
|
/**
|
|
12
19
|
* Creates a Solid-compatible AsyncQueuer instance for managing an asynchronous queue of items, exposing Solid signals for all stateful properties.
|
|
@@ -31,9 +38,29 @@ export interface SolidAsyncQueuer<TValue, TSelected = AsyncQueuerState<TValue>>
|
|
|
31
38
|
* - Both onError and throwOnError can be used together; the handler will be called before any error is thrown
|
|
32
39
|
* - The error state can be checked using the underlying AsyncQueuer instance
|
|
33
40
|
*
|
|
41
|
+
* ## State Management and Selector
|
|
42
|
+
*
|
|
43
|
+
* The hook uses TanStack Store for reactive state management. The `selector` parameter allows you
|
|
44
|
+
* to specify which state changes will trigger a re-render, optimizing performance by preventing
|
|
45
|
+
* unnecessary re-renders when irrelevant state changes occur.
|
|
46
|
+
*
|
|
47
|
+
* **By default, there will be no reactive state subscriptions** and you must opt-in to state
|
|
48
|
+
* tracking by providing a selector function. This prevents unnecessary re-renders and gives you
|
|
49
|
+
* full control over when your component updates. Only when you provide a selector will the
|
|
50
|
+
* component re-render when the selected state values change.
|
|
51
|
+
*
|
|
52
|
+
* Available state properties:
|
|
53
|
+
* - `activeItems`: Array of items currently being processed
|
|
54
|
+
* - `errorCount`: Number of items that failed processing
|
|
55
|
+
* - `isRunning`: Whether the queuer is currently running (not stopped)
|
|
56
|
+
* - `pendingItems`: Array of items waiting to be processed
|
|
57
|
+
* - `rejectionCount`: Number of items that were rejected (expired or failed validation)
|
|
58
|
+
* - `settleCount`: Number of items that have completed processing (successful or failed)
|
|
59
|
+
* - `successCount`: Number of items that were processed successfully
|
|
60
|
+
*
|
|
34
61
|
* Example usage:
|
|
35
62
|
* ```tsx
|
|
36
|
-
* //
|
|
63
|
+
* // Default behavior - no reactive state subscriptions
|
|
37
64
|
* const asyncQueuer = createAsyncQueuer(async (item) => {
|
|
38
65
|
* // process item
|
|
39
66
|
* return await fetchData(item);
|
|
@@ -50,14 +77,36 @@ export interface SolidAsyncQueuer<TValue, TSelected = AsyncQueuerState<TValue>>
|
|
|
50
77
|
* }
|
|
51
78
|
* });
|
|
52
79
|
*
|
|
80
|
+
* // Opt-in to re-render when queue state changes (optimized for UI updates)
|
|
81
|
+
* const asyncQueuer = createAsyncQueuer(
|
|
82
|
+
* async (item) => await fetchData(item),
|
|
83
|
+
* { concurrency: 2, started: true },
|
|
84
|
+
* (state) => ({
|
|
85
|
+
* pendingItems: state.pendingItems,
|
|
86
|
+
* activeItems: state.activeItems,
|
|
87
|
+
* isRunning: state.isRunning
|
|
88
|
+
* })
|
|
89
|
+
* );
|
|
90
|
+
*
|
|
91
|
+
* // Opt-in to re-render when processing metrics change (optimized for tracking progress)
|
|
92
|
+
* const asyncQueuer = createAsyncQueuer(
|
|
93
|
+
* async (item) => await fetchData(item),
|
|
94
|
+
* { concurrency: 2, started: true },
|
|
95
|
+
* (state) => ({
|
|
96
|
+
* successCount: state.successCount,
|
|
97
|
+
* errorCount: state.errorCount,
|
|
98
|
+
* settleCount: state.settleCount
|
|
99
|
+
* })
|
|
100
|
+
* );
|
|
101
|
+
*
|
|
53
102
|
* // Add items to queue
|
|
54
103
|
* asyncQueuer.addItem(newItem);
|
|
55
104
|
*
|
|
56
105
|
* // Start processing
|
|
57
106
|
* asyncQueuer.start();
|
|
58
107
|
*
|
|
59
|
-
* //
|
|
60
|
-
* const
|
|
108
|
+
* // Access the selected state (will be empty object {} unless selector provided)
|
|
109
|
+
* const { pendingItems, activeItems } = asyncQueuer.state();
|
|
61
110
|
* ```
|
|
62
111
|
*/
|
|
63
|
-
export declare function createAsyncQueuer<TValue, TSelected =
|
|
112
|
+
export declare function createAsyncQueuer<TValue, TSelected = {}>(fn: (value: TValue) => Promise<any>, initialOptions?: AsyncQueuerOptions<TValue>, selector?: (state: AsyncQueuerState<TValue>) => TSelected): SolidAsyncQueuer<TValue, TSelected>;
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
3
|
const asyncRateLimiter = require("@tanstack/pacer/async-rate-limiter");
|
|
4
4
|
const solidStore = require("@tanstack/solid-store");
|
|
5
|
-
function createAsyncRateLimiter(fn, initialOptions, selector) {
|
|
5
|
+
function createAsyncRateLimiter(fn, initialOptions, selector = () => ({})) {
|
|
6
6
|
const asyncRateLimiter$1 = new asyncRateLimiter.AsyncRateLimiter(fn, initialOptions);
|
|
7
7
|
const state = solidStore.useStore(asyncRateLimiter$1.store, selector);
|
|
8
8
|
return {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createAsyncRateLimiter.cjs","sources":["../../../src/async-rate-limiter/createAsyncRateLimiter.ts"],"sourcesContent":["import { AsyncRateLimiter } from '@tanstack/pacer/async-rate-limiter'\nimport { useStore } from '@tanstack/solid-store'\nimport type { Accessor } from 'solid-js'\nimport type { AnyAsyncFunction } from '@tanstack/pacer/types'\nimport type {\n AsyncRateLimiterOptions,\n AsyncRateLimiterState,\n} from '@tanstack/pacer/async-rate-limiter'\n\nexport interface SolidAsyncRateLimiter<\n TFn extends AnyAsyncFunction,\n TSelected =
|
|
1
|
+
{"version":3,"file":"createAsyncRateLimiter.cjs","sources":["../../../src/async-rate-limiter/createAsyncRateLimiter.ts"],"sourcesContent":["import { AsyncRateLimiter } from '@tanstack/pacer/async-rate-limiter'\nimport { useStore } from '@tanstack/solid-store'\nimport type { Store } from '@tanstack/solid-store'\nimport type { Accessor } from 'solid-js'\nimport type { AnyAsyncFunction } from '@tanstack/pacer/types'\nimport type {\n AsyncRateLimiterOptions,\n AsyncRateLimiterState,\n} from '@tanstack/pacer/async-rate-limiter'\n\nexport interface SolidAsyncRateLimiter<\n TFn extends AnyAsyncFunction,\n TSelected = {},\n> extends Omit<AsyncRateLimiter<TFn>, 'store'> {\n /**\n * Reactive state that will be updated when the rate limiter state changes\n *\n * Use this instead of `rateLimiter.store.state`\n */\n readonly state: Accessor<Readonly<TSelected>>\n /**\n * @deprecated Use `rateLimiter.state` instead of `rateLimiter.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<AsyncRateLimiterState<TFn>>>\n}\n\n/**\n * A low-level Solid hook that creates an `AsyncRateLimiter` instance to limit how many times an async function can execute within a time window.\n *\n * This hook is designed to be flexible and state-management agnostic - it simply returns a rate limiter instance that\n * you can integrate with any state management solution (createSignal, etc).\n *\n * Rate limiting is a simple approach that allows a function to execute up to a limit within a time window,\n * then blocks all subsequent calls until the window passes. This can lead to \"bursty\" behavior where\n * all executions happen immediately, followed by a complete block.\n *\n * The rate limiter supports two types of windows:\n * - 'fixed': A strict window that resets after the window period. All executions within the window count\n * towards the limit, and the window resets completely after the period.\n * - 'sliding': A rolling window that allows executions as old ones expire. This provides a more\n * consistent rate of execution over time.\n *\n * Unlike the non-async RateLimiter, this async version supports returning values from the rate-limited function,\n * making it ideal for API calls and other async operations where you want the result of the `maybeExecute` call\n * instead of setting the result on a state variable from within the rate-limited function.\n *\n * For smoother execution patterns, consider using:\n * - Throttling: Ensures consistent spacing between executions (e.g. max once per 200ms)\n * - Debouncing: Waits for a pause in calls before executing (e.g. after 500ms of no calls)\n *\n * Rate limiting is best used for hard API limits or resource constraints. For UI updates or\n * smoothing out frequent events, throttling or debouncing usually provide better user experience.\n *\n * Error Handling:\n * - If an `onError` handler is provided, it will be called with the error and rate limiter instance\n * - If `throwOnError` is true (default when no onError handler is provided), the error will be thrown\n * - If `throwOnError` is false (default when onError handler is provided), the error will be swallowed\n * - Both onError and throwOnError can be used together - the handler will be called before any error is thrown\n * - The error state can be checked using the underlying AsyncRateLimiter instance\n * - Rate limit rejections (when limit is exceeded) are handled separately from execution errors via the `onReject` handler\n *\n * ## State Management and Selector\n *\n * The hook uses TanStack Store for reactive state management. The `selector` parameter allows you\n * to specify which state changes will trigger a re-render, optimizing performance by preventing\n * unnecessary re-renders when irrelevant state changes occur.\n *\n * **By default, there will be no reactive state subscriptions** and you must opt-in to state\n * tracking by providing a selector function. This prevents unnecessary re-renders and gives you\n * full control over when your component updates. Only when you provide a selector will the\n * component re-render when the selected state values change.\n *\n * Available state properties:\n * - `currentWindowStart`: Timestamp when the current window started\n * - `executionCount`: Number of function executions that have been completed\n * - `hasError`: Whether the last execution resulted in an error\n * - `isExecuting`: Whether an async function execution is currently in progress\n * - `lastError`: The error from the most recent failed execution (if any)\n * - `lastResult`: The result from the most recent successful execution\n * - `nextWindowTime`: Timestamp when the next window begins\n * - `rejectionCount`: Number of function calls that were rejected due to rate limiting\n * - `remainingInWindow`: Number of executions remaining in the current window\n *\n * @example\n * ```tsx\n * // Default behavior - no reactive state subscriptions\n * const { maybeExecute } = createAsyncRateLimiter(\n * async (id: string) => {\n * const data = await api.fetchData(id);\n * return data; // Return value is preserved\n * },\n * { limit: 5, window: 1000 } // 5 calls per second\n * );\n *\n * // Opt-in to re-render when rate limit and execution state changes (optimized for UI feedback)\n * const rateLimiter = createAsyncRateLimiter(\n * async (query) => {\n * const result = await searchAPI(query);\n * return result;\n * },\n * { limit: 10, window: 60000 },\n * (state) => ({\n * remainingInWindow: state.remainingInWindow,\n * isExecuting: state.isExecuting,\n * rejectionCount: state.rejectionCount\n * })\n * );\n *\n * // Opt-in to re-render when error state changes (optimized for error handling)\n * const rateLimiter = createAsyncRateLimiter(\n * async (query) => {\n * const result = await searchAPI(query);\n * return result;\n * },\n * {\n * limit: 10,\n * window: 60000, // 10 calls per minute\n * onReject: (info) => console.log(`Rate limit exceeded: ${info.nextValidTime - Date.now()}ms until next window`)\n * },\n * (state) => ({ hasError: state.hasError, lastError: state.lastError })\n * );\n *\n * // Access the selected state (will be empty object {} unless selector provided)\n * const { remainingInWindow, isExecuting } = rateLimiter.state();\n * ```\n */\nexport function createAsyncRateLimiter<\n TFn extends AnyAsyncFunction,\n TSelected = {},\n>(\n fn: TFn,\n initialOptions: AsyncRateLimiterOptions<TFn>,\n selector: (state: AsyncRateLimiterState<TFn>) => TSelected = () =>\n ({}) as TSelected,\n): SolidAsyncRateLimiter<TFn, TSelected> {\n const asyncRateLimiter = new AsyncRateLimiter<TFn>(fn, initialOptions)\n\n const state = useStore(asyncRateLimiter.store, selector)\n\n return {\n ...asyncRateLimiter,\n state,\n } as SolidAsyncRateLimiter<TFn, TSelected>\n}\n"],"names":["asyncRateLimiter","AsyncRateLimiter","useStore"],"mappings":";;;;AAgIO,SAAS,uBAId,IACA,gBACA,WAA6D,OAC1D,CAAA,IACoC;AACvC,QAAMA,qBAAmB,IAAIC,kCAAsB,IAAI,cAAc;AAErE,QAAM,QAAQC,WAAAA,SAASF,mBAAiB,OAAO,QAAQ;AAEvD,SAAO;AAAA,IACL,GAAGA;AAAAA,IACH;AAAA,EAAA;AAEJ;;"}
|
|
@@ -1,8 +1,20 @@
|
|
|
1
1
|
import { AsyncRateLimiter, AsyncRateLimiterOptions, AsyncRateLimiterState } from '@tanstack/pacer/async-rate-limiter';
|
|
2
|
+
import { Store } from '@tanstack/solid-store';
|
|
2
3
|
import { Accessor } from 'solid-js';
|
|
3
4
|
import { AnyAsyncFunction } from '@tanstack/pacer/types';
|
|
4
|
-
export interface SolidAsyncRateLimiter<TFn extends AnyAsyncFunction, TSelected =
|
|
5
|
+
export interface SolidAsyncRateLimiter<TFn extends AnyAsyncFunction, TSelected = {}> extends Omit<AsyncRateLimiter<TFn>, 'store'> {
|
|
6
|
+
/**
|
|
7
|
+
* Reactive state that will be updated when the rate limiter state changes
|
|
8
|
+
*
|
|
9
|
+
* Use this instead of `rateLimiter.store.state`
|
|
10
|
+
*/
|
|
5
11
|
readonly state: Accessor<Readonly<TSelected>>;
|
|
12
|
+
/**
|
|
13
|
+
* @deprecated Use `rateLimiter.state` instead of `rateLimiter.store.state` if you want to read reactive state.
|
|
14
|
+
* The state on the store object is not reactive, as it has not been wrapped in a `useStore` hook internally.
|
|
15
|
+
* Although, you can make the state reactive by using the `useStore` in your own usage.
|
|
16
|
+
*/
|
|
17
|
+
readonly store: Store<Readonly<AsyncRateLimiterState<TFn>>>;
|
|
6
18
|
}
|
|
7
19
|
/**
|
|
8
20
|
* A low-level Solid hook that creates an `AsyncRateLimiter` instance to limit how many times an async function can execute within a time window.
|
|
@@ -39,9 +51,31 @@ export interface SolidAsyncRateLimiter<TFn extends AnyAsyncFunction, TSelected =
|
|
|
39
51
|
* - The error state can be checked using the underlying AsyncRateLimiter instance
|
|
40
52
|
* - Rate limit rejections (when limit is exceeded) are handled separately from execution errors via the `onReject` handler
|
|
41
53
|
*
|
|
54
|
+
* ## State Management and Selector
|
|
55
|
+
*
|
|
56
|
+
* The hook uses TanStack Store for reactive state management. The `selector` parameter allows you
|
|
57
|
+
* to specify which state changes will trigger a re-render, optimizing performance by preventing
|
|
58
|
+
* unnecessary re-renders when irrelevant state changes occur.
|
|
59
|
+
*
|
|
60
|
+
* **By default, there will be no reactive state subscriptions** and you must opt-in to state
|
|
61
|
+
* tracking by providing a selector function. This prevents unnecessary re-renders and gives you
|
|
62
|
+
* full control over when your component updates. Only when you provide a selector will the
|
|
63
|
+
* component re-render when the selected state values change.
|
|
64
|
+
*
|
|
65
|
+
* Available state properties:
|
|
66
|
+
* - `currentWindowStart`: Timestamp when the current window started
|
|
67
|
+
* - `executionCount`: Number of function executions that have been completed
|
|
68
|
+
* - `hasError`: Whether the last execution resulted in an error
|
|
69
|
+
* - `isExecuting`: Whether an async function execution is currently in progress
|
|
70
|
+
* - `lastError`: The error from the most recent failed execution (if any)
|
|
71
|
+
* - `lastResult`: The result from the most recent successful execution
|
|
72
|
+
* - `nextWindowTime`: Timestamp when the next window begins
|
|
73
|
+
* - `rejectionCount`: Number of function calls that were rejected due to rate limiting
|
|
74
|
+
* - `remainingInWindow`: Number of executions remaining in the current window
|
|
75
|
+
*
|
|
42
76
|
* @example
|
|
43
77
|
* ```tsx
|
|
44
|
-
* //
|
|
78
|
+
* // Default behavior - no reactive state subscriptions
|
|
45
79
|
* const { maybeExecute } = createAsyncRateLimiter(
|
|
46
80
|
* async (id: string) => {
|
|
47
81
|
* const data = await api.fetchData(id);
|
|
@@ -50,20 +84,36 @@ export interface SolidAsyncRateLimiter<TFn extends AnyAsyncFunction, TSelected =
|
|
|
50
84
|
* { limit: 5, window: 1000 } // 5 calls per second
|
|
51
85
|
* );
|
|
52
86
|
*
|
|
53
|
-
* //
|
|
54
|
-
* const
|
|
55
|
-
*
|
|
87
|
+
* // Opt-in to re-render when rate limit and execution state changes (optimized for UI feedback)
|
|
88
|
+
* const rateLimiter = createAsyncRateLimiter(
|
|
89
|
+
* async (query) => {
|
|
90
|
+
* const result = await searchAPI(query);
|
|
91
|
+
* return result;
|
|
92
|
+
* },
|
|
93
|
+
* { limit: 10, window: 60000 },
|
|
94
|
+
* (state) => ({
|
|
95
|
+
* remainingInWindow: state.remainingInWindow,
|
|
96
|
+
* isExecuting: state.isExecuting,
|
|
97
|
+
* rejectionCount: state.rejectionCount
|
|
98
|
+
* })
|
|
99
|
+
* );
|
|
100
|
+
*
|
|
101
|
+
* // Opt-in to re-render when error state changes (optimized for error handling)
|
|
102
|
+
* const rateLimiter = createAsyncRateLimiter(
|
|
56
103
|
* async (query) => {
|
|
57
104
|
* const result = await searchAPI(query);
|
|
58
|
-
*
|
|
59
|
-
* return result; // Return value can be used by the caller
|
|
105
|
+
* return result;
|
|
60
106
|
* },
|
|
61
107
|
* {
|
|
62
108
|
* limit: 10,
|
|
63
109
|
* window: 60000, // 10 calls per minute
|
|
64
110
|
* onReject: (info) => console.log(`Rate limit exceeded: ${info.nextValidTime - Date.now()}ms until next window`)
|
|
65
|
-
* }
|
|
111
|
+
* },
|
|
112
|
+
* (state) => ({ hasError: state.hasError, lastError: state.lastError })
|
|
66
113
|
* );
|
|
114
|
+
*
|
|
115
|
+
* // Access the selected state (will be empty object {} unless selector provided)
|
|
116
|
+
* const { remainingInWindow, isExecuting } = rateLimiter.state();
|
|
67
117
|
* ```
|
|
68
118
|
*/
|
|
69
|
-
export declare function createAsyncRateLimiter<TFn extends AnyAsyncFunction, TSelected =
|
|
119
|
+
export declare function createAsyncRateLimiter<TFn extends AnyAsyncFunction, TSelected = {}>(fn: TFn, initialOptions: AsyncRateLimiterOptions<TFn>, selector?: (state: AsyncRateLimiterState<TFn>) => TSelected): SolidAsyncRateLimiter<TFn, TSelected>;
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
3
|
const asyncThrottler = require("@tanstack/pacer/async-throttler");
|
|
4
4
|
const solidStore = require("@tanstack/solid-store");
|
|
5
|
-
function createAsyncThrottler(fn, initialOptions, selector) {
|
|
5
|
+
function createAsyncThrottler(fn, initialOptions, selector = () => ({})) {
|
|
6
6
|
const asyncThrottler$1 = new asyncThrottler.AsyncThrottler(fn, initialOptions);
|
|
7
7
|
const state = solidStore.useStore(asyncThrottler$1.store, selector);
|
|
8
8
|
return {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createAsyncThrottler.cjs","sources":["../../../src/async-throttler/createAsyncThrottler.ts"],"sourcesContent":["import { AsyncThrottler } from '@tanstack/pacer/async-throttler'\nimport { useStore } from '@tanstack/solid-store'\nimport type { Accessor } from 'solid-js'\nimport type { AnyAsyncFunction } from '@tanstack/pacer/types'\nimport type {\n AsyncThrottlerOptions,\n AsyncThrottlerState,\n} from '@tanstack/pacer/async-throttler'\n\nexport interface SolidAsyncThrottler<\n TFn extends AnyAsyncFunction,\n TSelected =
|
|
1
|
+
{"version":3,"file":"createAsyncThrottler.cjs","sources":["../../../src/async-throttler/createAsyncThrottler.ts"],"sourcesContent":["import { AsyncThrottler } from '@tanstack/pacer/async-throttler'\nimport { useStore } from '@tanstack/solid-store'\nimport type { Store } from '@tanstack/solid-store'\nimport type { Accessor } from 'solid-js'\nimport type { AnyAsyncFunction } from '@tanstack/pacer/types'\nimport type {\n AsyncThrottlerOptions,\n AsyncThrottlerState,\n} from '@tanstack/pacer/async-throttler'\n\nexport interface SolidAsyncThrottler<\n TFn extends AnyAsyncFunction,\n TSelected = {},\n> extends Omit<AsyncThrottler<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<AsyncThrottlerState<TFn>>>\n}\n\n/**\n * A low-level Solid hook that creates an `AsyncThrottler` instance to limit how often an async 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, etc).\n *\n * Async throttling ensures an async 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 expensive API calls,\n * database operations, or other async tasks.\n *\n * Unlike the non-async Throttler, this async version supports returning values from the throttled function,\n * making it ideal for API calls and other async operations where you want the result of the `maybeExecute` call\n * instead of setting the result on a state variable from within the throttled function.\n *\n * Error Handling:\n * - If an `onError` handler is provided, it will be called with the error and throttler instance\n * - If `throwOnError` is true (default when no onError handler is provided), the error will be thrown\n * - If `throwOnError` is false (default when onError handler is provided), the error will be swallowed\n * - Both onError and throwOnError can be used together - the handler will be called before any error is thrown\n * - The error state can be checked using the underlying AsyncThrottler instance\n *\n * ## State Management and Selector\n *\n * The hook uses TanStack Store for reactive state management. The `selector` parameter allows you\n * to specify which state changes will trigger a re-render, optimizing performance by preventing\n * unnecessary re-renders when irrelevant state changes occur.\n *\n * **By default, there will be no reactive state subscriptions** and you must opt-in to state\n * tracking by providing a selector function. This prevents unnecessary re-renders and gives you\n * full control over when your component updates. Only when you provide a selector will the\n * component re-render when the selected state values change.\n *\n * Available state properties:\n * - `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 * - `hasError`: Whether the last execution resulted in an error\n * - `isPending`: Whether the throttler is waiting for the timeout to trigger execution\n * - `isExecuting`: Whether an async function execution is currently in progress\n * - `lastArgs`: The arguments from the most recent call to maybeExecute\n * - `lastError`: The error from the most recent failed execution (if any)\n * - `lastExecutionTime`: Timestamp of the last execution\n * - `lastResult`: The result from the most recent successful execution\n * - `nextExecutionTime`: Timestamp of the next allowed execution\n * - `status`: Current execution status ('disabled' | 'idle' | 'pending' | 'executing')\n *\n * @example\n * ```tsx\n * // Default behavior - no reactive state subscriptions\n * const { maybeExecute } = createAsyncThrottler(\n * async (id: string) => {\n * const data = await api.fetchData(id);\n * return data;\n * },\n * { wait: 1000 }\n * );\n *\n * // Opt-in to re-render when isPending or isExecuting changes (optimized for loading states)\n * const throttler = createAsyncThrottler(\n * async (query) => {\n * const result = await searchAPI(query);\n * return result;\n * },\n * { wait: 2000 },\n * (state) => ({ isPending: state.isPending, isExecuting: state.isExecuting })\n * );\n *\n * // Opt-in to re-render when error state changes (optimized for error handling)\n * const throttler = createAsyncThrottler(\n * async (query) => {\n * const result = await searchAPI(query);\n * return result;\n * },\n * {\n * wait: 2000,\n * leading: true, // Execute immediately on first call\n * trailing: false, // Skip trailing edge updates\n * onError: (error) => {\n * console.error('API call failed:', error);\n * }\n * },\n * (state) => ({ hasError: state.hasError, lastError: state.lastError })\n * );\n *\n * // Access the selected state (will be empty object {} unless selector provided)\n * const { isPending, isExecuting } = throttler.state();\n * ```\n */\nexport function createAsyncThrottler<\n TFn extends AnyAsyncFunction,\n TSelected = {},\n>(\n fn: TFn,\n initialOptions: AsyncThrottlerOptions<TFn>,\n selector: (state: AsyncThrottlerState<TFn>) => TSelected = () =>\n ({}) as TSelected,\n): SolidAsyncThrottler<TFn, TSelected> {\n const asyncThrottler = new AsyncThrottler(fn, initialOptions)\n\n const state = useStore(asyncThrottler.store, selector)\n\n return {\n ...asyncThrottler,\n state,\n } as SolidAsyncThrottler<TFn, TSelected>\n}\n"],"names":["asyncThrottler","AsyncThrottler","useStore"],"mappings":";;;;AAoHO,SAAS,qBAId,IACA,gBACA,WAA2D,OACxD,CAAA,IACkC;AACrC,QAAMA,mBAAiB,IAAIC,8BAAe,IAAI,cAAc;AAE5D,QAAM,QAAQC,WAAAA,SAASF,iBAAe,OAAO,QAAQ;AAErD,SAAO;AAAA,IACL,GAAGA;AAAAA,IACH;AAAA,EAAA;AAEJ;;"}
|