@tanstack/solid-pacer 0.7.0 → 0.9.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 +14 -0
- package/dist/cjs/async-batcher/createAsyncBatcher.cjs.map +1 -0
- package/dist/cjs/async-batcher/createAsyncBatcher.d.cts +72 -0
- package/dist/cjs/async-batcher/index.cjs +12 -0
- package/dist/cjs/{compare → async-batcher}/index.cjs.map +1 -1
- package/dist/cjs/async-batcher/index.d.cts +2 -0
- package/dist/cjs/async-debouncer/createAsyncDebouncer.cjs +9 -36
- package/dist/cjs/async-debouncer/createAsyncDebouncer.cjs.map +1 -1
- package/dist/cjs/async-debouncer/createAsyncDebouncer.d.cts +10 -9
- package/dist/cjs/async-queuer/createAsyncQueuer.cjs +5 -73
- package/dist/cjs/async-queuer/createAsyncQueuer.cjs.map +1 -1
- package/dist/cjs/async-queuer/createAsyncQueuer.d.cts +7 -53
- package/dist/cjs/async-rate-limiter/createAsyncRateLimiter.cjs +5 -52
- package/dist/cjs/async-rate-limiter/createAsyncRateLimiter.cjs.map +1 -1
- package/dist/cjs/async-rate-limiter/createAsyncRateLimiter.d.cts +4 -9
- package/dist/cjs/async-throttler/createAsyncThrottler.cjs +5 -55
- package/dist/cjs/async-throttler/createAsyncThrottler.cjs.map +1 -1
- package/dist/cjs/async-throttler/createAsyncThrottler.d.cts +9 -11
- package/dist/cjs/batcher/createBatcher.cjs +5 -49
- package/dist/cjs/batcher/createBatcher.cjs.map +1 -1
- package/dist/cjs/batcher/createBatcher.d.cts +9 -27
- package/dist/cjs/debouncer/createDebouncedSignal.cjs +2 -2
- package/dist/cjs/debouncer/createDebouncedSignal.cjs.map +1 -1
- package/dist/cjs/debouncer/createDebouncedSignal.d.cts +6 -2
- package/dist/cjs/debouncer/createDebouncedValue.cjs +3 -2
- package/dist/cjs/debouncer/createDebouncedValue.cjs.map +1 -1
- package/dist/cjs/debouncer/createDebouncedValue.d.cts +2 -2
- package/dist/cjs/debouncer/createDebouncer.cjs +7 -24
- package/dist/cjs/debouncer/createDebouncer.cjs.map +1 -1
- package/dist/cjs/debouncer/createDebouncer.d.cts +9 -8
- package/dist/cjs/index.cjs +2 -0
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.cts +1 -0
- package/dist/cjs/queuer/createQueuer.cjs +5 -57
- package/dist/cjs/queuer/createQueuer.cjs.map +1 -1
- package/dist/cjs/queuer/createQueuer.d.cts +9 -39
- package/dist/cjs/rate-limiter/createRateLimitedSignal.cjs +7 -3
- package/dist/cjs/rate-limiter/createRateLimitedSignal.cjs.map +1 -1
- package/dist/cjs/rate-limiter/createRateLimitedSignal.d.cts +6 -2
- package/dist/cjs/rate-limiter/createRateLimitedValue.cjs +2 -2
- package/dist/cjs/rate-limiter/createRateLimitedValue.cjs.map +1 -1
- package/dist/cjs/rate-limiter/createRateLimitedValue.d.cts +2 -2
- package/dist/cjs/rate-limiter/createRateLimiter.cjs +5 -43
- package/dist/cjs/rate-limiter/createRateLimiter.cjs.map +1 -1
- package/dist/cjs/rate-limiter/createRateLimiter.d.cts +9 -7
- package/dist/cjs/throttler/createThrottledSignal.cjs +2 -2
- package/dist/cjs/throttler/createThrottledSignal.cjs.map +1 -1
- package/dist/cjs/throttler/createThrottledSignal.d.cts +6 -2
- package/dist/cjs/throttler/createThrottledValue.cjs +3 -2
- package/dist/cjs/throttler/createThrottledValue.cjs.map +1 -1
- package/dist/cjs/throttler/createThrottledValue.d.cts +2 -2
- package/dist/cjs/throttler/createThrottler.cjs +7 -34
- package/dist/cjs/throttler/createThrottler.cjs.map +1 -1
- package/dist/cjs/throttler/createThrottler.d.cts +9 -10
- package/dist/esm/async-batcher/createAsyncBatcher.d.ts +72 -0
- package/dist/esm/async-batcher/createAsyncBatcher.js +14 -0
- package/dist/esm/async-batcher/createAsyncBatcher.js.map +1 -0
- package/dist/esm/async-batcher/index.d.ts +2 -0
- package/dist/esm/async-batcher/index.js +6 -0
- package/dist/esm/{compare → async-batcher}/index.js.map +1 -1
- package/dist/esm/async-debouncer/createAsyncDebouncer.d.ts +10 -9
- package/dist/esm/async-debouncer/createAsyncDebouncer.js +10 -37
- package/dist/esm/async-debouncer/createAsyncDebouncer.js.map +1 -1
- package/dist/esm/async-queuer/createAsyncQueuer.d.ts +7 -53
- package/dist/esm/async-queuer/createAsyncQueuer.js +5 -73
- package/dist/esm/async-queuer/createAsyncQueuer.js.map +1 -1
- package/dist/esm/async-rate-limiter/createAsyncRateLimiter.d.ts +4 -9
- package/dist/esm/async-rate-limiter/createAsyncRateLimiter.js +5 -52
- package/dist/esm/async-rate-limiter/createAsyncRateLimiter.js.map +1 -1
- package/dist/esm/async-throttler/createAsyncThrottler.d.ts +9 -11
- package/dist/esm/async-throttler/createAsyncThrottler.js +5 -55
- package/dist/esm/async-throttler/createAsyncThrottler.js.map +1 -1
- package/dist/esm/batcher/createBatcher.d.ts +9 -27
- package/dist/esm/batcher/createBatcher.js +5 -49
- package/dist/esm/batcher/createBatcher.js.map +1 -1
- package/dist/esm/debouncer/createDebouncedSignal.d.ts +6 -2
- package/dist/esm/debouncer/createDebouncedSignal.js +2 -2
- package/dist/esm/debouncer/createDebouncedSignal.js.map +1 -1
- package/dist/esm/debouncer/createDebouncedValue.d.ts +2 -2
- package/dist/esm/debouncer/createDebouncedValue.js +3 -2
- package/dist/esm/debouncer/createDebouncedValue.js.map +1 -1
- package/dist/esm/debouncer/createDebouncer.d.ts +9 -8
- package/dist/esm/debouncer/createDebouncer.js +8 -25
- package/dist/esm/debouncer/createDebouncer.js.map +1 -1
- package/dist/esm/index.d.ts +1 -0
- package/dist/esm/index.js +2 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/queuer/createQueuer.d.ts +9 -39
- package/dist/esm/queuer/createQueuer.js +5 -57
- package/dist/esm/queuer/createQueuer.js.map +1 -1
- package/dist/esm/rate-limiter/createRateLimitedSignal.d.ts +6 -2
- package/dist/esm/rate-limiter/createRateLimitedSignal.js +7 -3
- package/dist/esm/rate-limiter/createRateLimitedSignal.js.map +1 -1
- package/dist/esm/rate-limiter/createRateLimitedValue.d.ts +2 -2
- package/dist/esm/rate-limiter/createRateLimitedValue.js +2 -2
- package/dist/esm/rate-limiter/createRateLimitedValue.js.map +1 -1
- package/dist/esm/rate-limiter/createRateLimiter.d.ts +9 -7
- package/dist/esm/rate-limiter/createRateLimiter.js +5 -43
- package/dist/esm/rate-limiter/createRateLimiter.js.map +1 -1
- package/dist/esm/throttler/createThrottledSignal.d.ts +6 -2
- package/dist/esm/throttler/createThrottledSignal.js +2 -2
- package/dist/esm/throttler/createThrottledSignal.js.map +1 -1
- package/dist/esm/throttler/createThrottledValue.d.ts +2 -2
- package/dist/esm/throttler/createThrottledValue.js +3 -2
- package/dist/esm/throttler/createThrottledValue.js.map +1 -1
- package/dist/esm/throttler/createThrottler.d.ts +9 -10
- package/dist/esm/throttler/createThrottler.js +8 -35
- package/dist/esm/throttler/createThrottler.js.map +1 -1
- package/package.json +15 -4
- package/src/async-batcher/createAsyncBatcher.ts +98 -0
- package/src/async-batcher/index.ts +3 -0
- package/src/async-debouncer/createAsyncDebouncer.ts +31 -56
- package/src/async-queuer/createAsyncQueuer.ts +18 -141
- package/src/async-rate-limiter/createAsyncRateLimiter.ts +20 -75
- package/src/async-throttler/createAsyncThrottler.ts +25 -81
- package/src/batcher/createBatcher.ts +17 -94
- package/src/debouncer/createDebouncedSignal.ts +15 -4
- package/src/debouncer/createDebouncedValue.ts +11 -3
- package/src/debouncer/createDebouncer.ts +28 -38
- package/src/index.ts +3 -0
- package/src/queuer/createQueuer.ts +17 -115
- package/src/rate-limiter/createRateLimitedSignal.ts +17 -5
- package/src/rate-limiter/createRateLimitedValue.ts +8 -4
- package/src/rate-limiter/createRateLimiter.ts +25 -62
- package/src/throttler/createThrottledSignal.ts +15 -4
- package/src/throttler/createThrottledValue.ts +11 -3
- package/src/throttler/createThrottler.ts +28 -56
- package/dist/cjs/compare/index.cjs +0 -10
- package/dist/cjs/compare/index.d.cts +0 -1
- package/dist/esm/compare/index.d.ts +0 -1
- package/dist/esm/compare/index.js +0 -2
- package/src/compare/index.ts +0 -1
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const asyncBatcher = require("@tanstack/pacer/async-batcher");
|
|
4
|
+
const solidStore = require("@tanstack/solid-store");
|
|
5
|
+
function createAsyncBatcher(fn, initialOptions = {}, selector) {
|
|
6
|
+
const asyncBatcher$1 = new asyncBatcher.AsyncBatcher(fn, initialOptions);
|
|
7
|
+
const state = solidStore.useStore(asyncBatcher$1.store, selector);
|
|
8
|
+
return {
|
|
9
|
+
...asyncBatcher$1,
|
|
10
|
+
state
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
exports.createAsyncBatcher = createAsyncBatcher;
|
|
14
|
+
//# sourceMappingURL=createAsyncBatcher.cjs.map
|
|
@@ -0,0 +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<\n TValue,\n TSelected = AsyncBatcherState<TValue>,\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\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 * Example usage:\n * ```tsx\n * // Basic async batcher for API requests\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 * // Add items to batch\n * asyncBatcher.addItem(newItem);\n *\n * // Manually execute batch\n * const result = await asyncBatcher.execute();\n *\n * // Use Solid signals in your UI\n * const items = asyncBatcher.state().items;\n * const isExecuting = asyncBatcher.state().isExecuting;\n * ```\n */\nexport function createAsyncBatcher<\n TValue,\n TSelected = AsyncBatcherState<TValue>,\n>(\n fn: (items: Array<TValue>) => Promise<any>,\n initialOptions: AsyncBatcherOptions<TValue> = {},\n selector?: (state: AsyncBatcherState<TValue>) => 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 unknown as SolidAsyncBatcher<TValue, TSelected> // omit `store` in favor of `state`\n}\n"],"names":["asyncBatcher","AsyncBatcher","useStore"],"mappings":";;;;AAiFO,SAAS,mBAId,IACA,iBAA8C,CAAA,GAC9C,UACsC;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;;"}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { AsyncBatcher, AsyncBatcherOptions, AsyncBatcherState } from '@tanstack/pacer/async-batcher';
|
|
2
|
+
import { Accessor } from 'solid-js';
|
|
3
|
+
export interface SolidAsyncBatcher<TValue, TSelected = AsyncBatcherState<TValue>> extends Omit<AsyncBatcher<TValue>, 'store'> {
|
|
4
|
+
/**
|
|
5
|
+
* Reactive state that will be updated when the batcher state changes
|
|
6
|
+
*
|
|
7
|
+
* Use this instead of `batcher.store.state`
|
|
8
|
+
*/
|
|
9
|
+
readonly state: Accessor<Readonly<TSelected>>;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Creates a Solid-compatible AsyncBatcher instance for managing asynchronous batches of items, exposing Solid signals for all stateful properties.
|
|
13
|
+
*
|
|
14
|
+
* This is the async version of the createBatcher hook. Unlike the sync version, this async batcher:
|
|
15
|
+
* - Handles promises and returns results from batch executions
|
|
16
|
+
* - Provides error handling with configurable error behavior
|
|
17
|
+
* - Tracks success, error, and settle counts separately
|
|
18
|
+
* - Has state tracking for when batches are executing
|
|
19
|
+
* - Returns the result of the batch function execution
|
|
20
|
+
*
|
|
21
|
+
* Features:
|
|
22
|
+
* - Configurable batch size and wait time
|
|
23
|
+
* - Custom batch processing logic via getShouldExecute
|
|
24
|
+
* - Event callbacks for monitoring batch operations
|
|
25
|
+
* - Error handling for failed batch operations
|
|
26
|
+
* - Automatic or manual batch processing
|
|
27
|
+
* - All stateful properties (items, counts, etc.) are exposed as Solid signals for reactivity
|
|
28
|
+
*
|
|
29
|
+
* The batcher collects items and processes them in batches based on:
|
|
30
|
+
* - Maximum batch size (number of items per batch)
|
|
31
|
+
* - Time-based batching (process after X milliseconds)
|
|
32
|
+
* - Custom batch processing logic via getShouldExecute
|
|
33
|
+
*
|
|
34
|
+
* Error Handling:
|
|
35
|
+
* - If an `onError` handler is provided, it will be called with the error and batcher instance
|
|
36
|
+
* - If `throwOnError` is true (default when no onError handler is provided), the error will be thrown
|
|
37
|
+
* - If `throwOnError` is false (default when onError handler is provided), the error will be swallowed
|
|
38
|
+
* - Both onError and throwOnError can be used together; the handler will be called before any error is thrown
|
|
39
|
+
* - The error state can be checked using the underlying AsyncBatcher instance
|
|
40
|
+
*
|
|
41
|
+
* Example usage:
|
|
42
|
+
* ```tsx
|
|
43
|
+
* // Basic async batcher for API requests
|
|
44
|
+
* const asyncBatcher = createAsyncBatcher(
|
|
45
|
+
* async (items) => {
|
|
46
|
+
* const results = await Promise.all(items.map(item => processItem(item)));
|
|
47
|
+
* return results;
|
|
48
|
+
* },
|
|
49
|
+
* {
|
|
50
|
+
* maxSize: 10,
|
|
51
|
+
* wait: 2000,
|
|
52
|
+
* onSuccess: (result) => {
|
|
53
|
+
* console.log('Batch processed successfully:', result);
|
|
54
|
+
* },
|
|
55
|
+
* onError: (error) => {
|
|
56
|
+
* console.error('Batch processing failed:', error);
|
|
57
|
+
* }
|
|
58
|
+
* }
|
|
59
|
+
* );
|
|
60
|
+
*
|
|
61
|
+
* // Add items to batch
|
|
62
|
+
* asyncBatcher.addItem(newItem);
|
|
63
|
+
*
|
|
64
|
+
* // Manually execute batch
|
|
65
|
+
* const result = await asyncBatcher.execute();
|
|
66
|
+
*
|
|
67
|
+
* // Use Solid signals in your UI
|
|
68
|
+
* const items = asyncBatcher.state().items;
|
|
69
|
+
* const isExecuting = asyncBatcher.state().isExecuting;
|
|
70
|
+
* ```
|
|
71
|
+
*/
|
|
72
|
+
export declare function createAsyncBatcher<TValue, TSelected = AsyncBatcherState<TValue>>(fn: (items: Array<TValue>) => Promise<any>, initialOptions?: AsyncBatcherOptions<TValue>, selector?: (state: AsyncBatcherState<TValue>) => TSelected): SolidAsyncBatcher<TValue, TSelected>;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const asyncBatcher = require("@tanstack/pacer/async-batcher");
|
|
4
|
+
const createAsyncBatcher = require("./createAsyncBatcher.cjs");
|
|
5
|
+
exports.createAsyncBatcher = createAsyncBatcher.createAsyncBatcher;
|
|
6
|
+
Object.keys(asyncBatcher).forEach((k) => {
|
|
7
|
+
if (k !== "default" && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
|
|
8
|
+
enumerable: true,
|
|
9
|
+
get: () => asyncBatcher[k]
|
|
10
|
+
});
|
|
11
|
+
});
|
|
12
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;"}
|
|
@@ -1,46 +1,19 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
3
|
const asyncDebouncer = require("@tanstack/pacer/async-debouncer");
|
|
4
|
+
const solidStore = require("@tanstack/solid-store");
|
|
4
5
|
const solidJs = require("solid-js");
|
|
5
|
-
|
|
6
|
-
function createAsyncDebouncer(fn, initialOptions) {
|
|
6
|
+
function createAsyncDebouncer(fn, initialOptions, selector) {
|
|
7
7
|
const asyncDebouncer$1 = new asyncDebouncer.AsyncDebouncer(fn, initialOptions);
|
|
8
|
-
const
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
asyncDebouncer$1.getSettleCount()
|
|
13
|
-
);
|
|
14
|
-
const [successCount, setSuccessCount] = solidJs.createSignal(
|
|
15
|
-
asyncDebouncer$1.getSuccessCount()
|
|
16
|
-
);
|
|
17
|
-
const [isPending, setIsPending] = solidJs.createSignal(asyncDebouncer$1.getIsPending());
|
|
18
|
-
const [lastResult, setLastResult] = solidJs.createSignal(
|
|
19
|
-
asyncDebouncer$1.getLastResult()
|
|
20
|
-
);
|
|
21
|
-
function setOptions(newOptions) {
|
|
22
|
-
asyncDebouncer$1.setOptions({
|
|
23
|
-
...newOptions,
|
|
24
|
-
onSettled: (asyncDebouncer2) => {
|
|
25
|
-
setSuccessCount(asyncDebouncer2.getSuccessCount());
|
|
26
|
-
setErrorCount(asyncDebouncer2.getErrorCount());
|
|
27
|
-
setSettleCount(asyncDebouncer2.getSettleCount());
|
|
28
|
-
setIsPending(asyncDebouncer2.getIsPending());
|
|
29
|
-
setLastResult(asyncDebouncer2.getLastResult());
|
|
30
|
-
const onSettled = newOptions.onSettled ?? initialOptions.onSettled;
|
|
31
|
-
onSettled == null ? void 0 : onSettled(asyncDebouncer2);
|
|
32
|
-
}
|
|
8
|
+
const state = solidStore.useStore(asyncDebouncer$1.store, selector);
|
|
9
|
+
solidJs.createEffect(() => {
|
|
10
|
+
solidJs.onCleanup(() => {
|
|
11
|
+
asyncDebouncer$1.cancel();
|
|
33
12
|
});
|
|
34
|
-
}
|
|
35
|
-
setOptions(initialOptions);
|
|
13
|
+
});
|
|
36
14
|
return {
|
|
37
|
-
...
|
|
38
|
-
|
|
39
|
-
isPending,
|
|
40
|
-
lastResult,
|
|
41
|
-
settleCount,
|
|
42
|
-
successCount,
|
|
43
|
-
setOptions
|
|
15
|
+
...asyncDebouncer$1,
|
|
16
|
+
state
|
|
44
17
|
};
|
|
45
18
|
}
|
|
46
19
|
exports.createAsyncDebouncer = createAsyncDebouncer;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createAsyncDebouncer.cjs","sources":["../../../src/async-debouncer/createAsyncDebouncer.ts"],"sourcesContent":["import { AsyncDebouncer } from '@tanstack/pacer/async-debouncer'\nimport {
|
|
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 = AsyncDebouncerState<TFn>,\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\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 * @example\n * ```tsx\n * // Basic API call debouncing\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 * // With state management\n * const [results, setResults] = createSignal([]);\n * const { maybeExecute } = createAsyncDebouncer(\n * async (searchTerm) => {\n * const data = await searchAPI(searchTerm);\n * setResults(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 * );\n * ```\n */\nexport function createAsyncDebouncer<\n TFn extends AnyAsyncFunction,\n TSelected = AsyncDebouncerState<TFn>,\n>(\n fn: TFn,\n initialOptions: AsyncDebouncerOptions<TFn>,\n selector?: (state: AsyncDebouncerState<TFn>) => 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 unknown as SolidAsyncDebouncer<TFn, TSelected> // omit `store` in favor of `state`\n}\n"],"names":["asyncDebouncer","AsyncDebouncer","useStore","createEffect","onCleanup"],"mappings":";;;;;AA2EO,SAAS,qBAId,IACA,gBACA,UACqC;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,12 +1,13 @@
|
|
|
1
|
-
import { AsyncDebouncer, AsyncDebouncerOptions } from '@tanstack/pacer/async-debouncer';
|
|
2
|
-
import { AnyAsyncFunction } from '@tanstack/pacer/types';
|
|
1
|
+
import { AsyncDebouncer, AsyncDebouncerOptions, AsyncDebouncerState } from '@tanstack/pacer/async-debouncer';
|
|
3
2
|
import { Accessor } from 'solid-js';
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
3
|
+
import { AnyAsyncFunction } from '@tanstack/pacer/types';
|
|
4
|
+
export interface SolidAsyncDebouncer<TFn extends AnyAsyncFunction, TSelected = AsyncDebouncerState<TFn>> extends Omit<AsyncDebouncer<TFn>, 'store'> {
|
|
5
|
+
/**
|
|
6
|
+
* Reactive state that will be updated when the debouncer state changes
|
|
7
|
+
*
|
|
8
|
+
* Use this instead of `debouncer.store.state`
|
|
9
|
+
*/
|
|
10
|
+
readonly state: Accessor<Readonly<TSelected>>;
|
|
10
11
|
}
|
|
11
12
|
/**
|
|
12
13
|
* A low-level Solid hook that creates an `AsyncDebouncer` instance to delay execution of an async function.
|
|
@@ -61,4 +62,4 @@ export interface SolidAsyncDebouncer<TFn extends AnyAsyncFunction> extends Omit<
|
|
|
61
62
|
* );
|
|
62
63
|
* ```
|
|
63
64
|
*/
|
|
64
|
-
export declare function createAsyncDebouncer<TFn extends AnyAsyncFunction
|
|
65
|
+
export declare function createAsyncDebouncer<TFn extends AnyAsyncFunction, TSelected = AsyncDebouncerState<TFn>>(fn: TFn, initialOptions: AsyncDebouncerOptions<TFn>, selector?: (state: AsyncDebouncerState<TFn>) => TSelected): SolidAsyncDebouncer<TFn, TSelected>;
|
|
@@ -1,81 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
3
|
const asyncQueuer = require("@tanstack/pacer/async-queuer");
|
|
4
|
-
const
|
|
5
|
-
|
|
6
|
-
function createAsyncQueuer(fn, initialOptions = {}) {
|
|
4
|
+
const solidStore = require("@tanstack/solid-store");
|
|
5
|
+
function createAsyncQueuer(fn, initialOptions = {}, selector) {
|
|
7
6
|
const asyncQueuer$1 = new asyncQueuer.AsyncQueuer(fn, initialOptions);
|
|
8
|
-
const
|
|
9
|
-
asyncQueuer$1.getSuccessCount()
|
|
10
|
-
);
|
|
11
|
-
const [errorCount, setErrorCount] = solidJs.createSignal(asyncQueuer$1.getErrorCount());
|
|
12
|
-
const [settledCount, setSettledCount] = solidJs.createSignal(
|
|
13
|
-
asyncQueuer$1.getSettledCount()
|
|
14
|
-
);
|
|
15
|
-
const [rejectionCount, setRejectionCount] = solidJs.createSignal(
|
|
16
|
-
asyncQueuer$1.getRejectionCount()
|
|
17
|
-
);
|
|
18
|
-
const [isEmpty, setIsEmpty] = solidJs.createSignal(asyncQueuer$1.getIsEmpty());
|
|
19
|
-
const [isFull, setIsFull] = solidJs.createSignal(asyncQueuer$1.getIsFull());
|
|
20
|
-
const [isIdle, setIsIdle] = solidJs.createSignal(asyncQueuer$1.getIsIdle());
|
|
21
|
-
const [isRunning, setIsRunning] = solidJs.createSignal(asyncQueuer$1.getIsRunning());
|
|
22
|
-
const [allItems, setAllItems] = solidJs.createSignal(
|
|
23
|
-
asyncQueuer$1.getAllItems()
|
|
24
|
-
);
|
|
25
|
-
const [activeItems, setActiveItems] = solidJs.createSignal(
|
|
26
|
-
asyncQueuer$1.getActiveItems()
|
|
27
|
-
);
|
|
28
|
-
const [pendingItems, setPendingItems] = solidJs.createSignal(
|
|
29
|
-
asyncQueuer$1.getPendingItems()
|
|
30
|
-
);
|
|
31
|
-
const [peek, setPeek] = solidJs.createSignal(
|
|
32
|
-
asyncQueuer$1.getPeek()
|
|
33
|
-
);
|
|
34
|
-
const [size, setSize] = solidJs.createSignal(asyncQueuer$1.getSize());
|
|
35
|
-
asyncQueuer$1.setOptions({
|
|
36
|
-
onItemsChange: (queuer) => {
|
|
37
|
-
var _a;
|
|
38
|
-
setActiveItems(queuer.getActiveItems());
|
|
39
|
-
setAllItems(queuer.getAllItems());
|
|
40
|
-
setErrorCount(queuer.getErrorCount());
|
|
41
|
-
setIsEmpty(queuer.getIsEmpty());
|
|
42
|
-
setIsFull(queuer.getIsFull());
|
|
43
|
-
setIsIdle(queuer.getIsIdle());
|
|
44
|
-
setPeek(() => queuer.getPeek());
|
|
45
|
-
setPendingItems(queuer.getPendingItems());
|
|
46
|
-
setRejectionCount(queuer.getRejectionCount());
|
|
47
|
-
setSettledCount(queuer.getSettledCount());
|
|
48
|
-
setSize(queuer.getSize());
|
|
49
|
-
setSuccessCount(queuer.getSuccessCount());
|
|
50
|
-
(_a = initialOptions.onItemsChange) == null ? void 0 : _a.call(initialOptions, queuer);
|
|
51
|
-
},
|
|
52
|
-
onIsRunningChange: (queuer) => {
|
|
53
|
-
var _a;
|
|
54
|
-
setIsRunning(queuer.getIsRunning());
|
|
55
|
-
setIsIdle(queuer.getIsIdle());
|
|
56
|
-
(_a = initialOptions.onIsRunningChange) == null ? void 0 : _a.call(initialOptions, queuer);
|
|
57
|
-
},
|
|
58
|
-
onReject: (item, queuer) => {
|
|
59
|
-
var _a;
|
|
60
|
-
setRejectionCount(queuer.getRejectionCount());
|
|
61
|
-
(_a = initialOptions.onReject) == null ? void 0 : _a.call(initialOptions, item, queuer);
|
|
62
|
-
}
|
|
63
|
-
});
|
|
7
|
+
const state = solidStore.useStore(asyncQueuer$1.store, selector);
|
|
64
8
|
return {
|
|
65
|
-
...
|
|
66
|
-
|
|
67
|
-
allItems,
|
|
68
|
-
errorCount,
|
|
69
|
-
isEmpty,
|
|
70
|
-
isFull,
|
|
71
|
-
isIdle,
|
|
72
|
-
isRunning,
|
|
73
|
-
peek,
|
|
74
|
-
pendingItems,
|
|
75
|
-
rejectionCount,
|
|
76
|
-
settledCount,
|
|
77
|
-
size,
|
|
78
|
-
successCount
|
|
9
|
+
...asyncQueuer$1,
|
|
10
|
+
state
|
|
79
11
|
};
|
|
80
12
|
}
|
|
81
13
|
exports.createAsyncQueuer = createAsyncQueuer;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createAsyncQueuer.cjs","sources":["../../../src/async-queuer/createAsyncQueuer.ts"],"sourcesContent":["import { AsyncQueuer } from '@tanstack/pacer/async-queuer'\nimport {
|
|
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 = AsyncQueuerState<TValue>>\n extends Omit<AsyncQueuer<TValue>, 'store'> {\n /**\n * Reactive state that will be updated and re-rendered when the queuer state changes\n *\n * Use this instead of `queuer.store.state`\n */\n readonly state: Accessor<Readonly<TSelected>>\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 * Example usage:\n * ```tsx\n * // Basic async queuer for API requests\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 * // Add items to queue\n * asyncQueuer.addItem(newItem);\n *\n * // Start processing\n * asyncQueuer.start();\n *\n * // Use Solid signals in your UI\n * const pending = asyncQueuer.pendingItems();\n * ```\n */\nexport function createAsyncQueuer<TValue, TSelected = AsyncQueuerState<TValue>>(\n fn: (value: TValue) => Promise<any>,\n initialOptions: AsyncQueuerOptions<TValue> = {},\n selector?: (state: AsyncQueuerState<TValue>) => 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 unknown as SolidAsyncQueuer<TValue, TSelected> // omit `store` in favor of `state`\n}\n"],"names":["asyncQueuer","AsyncQueuer","useStore"],"mappings":";;;;AAsEO,SAAS,kBACd,IACA,iBAA6C,CAAA,GAC7C,UACqC;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,58 +1,12 @@
|
|
|
1
|
-
import { AsyncQueuer, AsyncQueuerOptions } from '@tanstack/pacer/async-queuer';
|
|
1
|
+
import { AsyncQueuer, AsyncQueuerOptions, AsyncQueuerState } from '@tanstack/pacer/async-queuer';
|
|
2
2
|
import { Accessor } from 'solid-js';
|
|
3
|
-
export interface SolidAsyncQueuer<TValue
|
|
3
|
+
export interface SolidAsyncQueuer<TValue, TSelected = AsyncQueuerState<TValue>> extends Omit<AsyncQueuer<TValue>, 'store'> {
|
|
4
4
|
/**
|
|
5
|
-
*
|
|
5
|
+
* Reactive state that will be updated and re-rendered when the queuer state changes
|
|
6
|
+
*
|
|
7
|
+
* Use this instead of `queuer.store.state`
|
|
6
8
|
*/
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Signal version of `getAllItems`
|
|
10
|
-
*/
|
|
11
|
-
allItems: Accessor<Array<TValue>>;
|
|
12
|
-
/**
|
|
13
|
-
* Signal version of `getErrorCount`
|
|
14
|
-
*/
|
|
15
|
-
errorCount: Accessor<number>;
|
|
16
|
-
/**
|
|
17
|
-
* Signal version of `getIsEmpty`
|
|
18
|
-
*/
|
|
19
|
-
isEmpty: Accessor<boolean>;
|
|
20
|
-
/**
|
|
21
|
-
* Signal version of `getIsFull`
|
|
22
|
-
*/
|
|
23
|
-
isFull: Accessor<boolean>;
|
|
24
|
-
/**
|
|
25
|
-
* Signal version of `getIsIdle`
|
|
26
|
-
*/
|
|
27
|
-
isIdle: Accessor<boolean>;
|
|
28
|
-
/**
|
|
29
|
-
* Signal version of `getIsRunning`
|
|
30
|
-
*/
|
|
31
|
-
isRunning: Accessor<boolean>;
|
|
32
|
-
/**
|
|
33
|
-
* Signal version of `getPeek`
|
|
34
|
-
*/
|
|
35
|
-
peek: Accessor<TValue | undefined>;
|
|
36
|
-
/**
|
|
37
|
-
* Signal version of `getPendingItems`
|
|
38
|
-
*/
|
|
39
|
-
pendingItems: Accessor<Array<TValue>>;
|
|
40
|
-
/**
|
|
41
|
-
* Signal version of `getRejectionCount`
|
|
42
|
-
*/
|
|
43
|
-
rejectionCount: Accessor<number>;
|
|
44
|
-
/**
|
|
45
|
-
* Signal version of `getSettledCount`
|
|
46
|
-
*/
|
|
47
|
-
settledCount: Accessor<number>;
|
|
48
|
-
/**
|
|
49
|
-
* Signal version of `getSize`
|
|
50
|
-
*/
|
|
51
|
-
size: Accessor<number>;
|
|
52
|
-
/**
|
|
53
|
-
* Signal version of `getSuccessCount`
|
|
54
|
-
*/
|
|
55
|
-
successCount: Accessor<number>;
|
|
9
|
+
readonly state: Accessor<Readonly<TSelected>>;
|
|
56
10
|
}
|
|
57
11
|
/**
|
|
58
12
|
* Creates a Solid-compatible AsyncQueuer instance for managing an asynchronous queue of items, exposing Solid signals for all stateful properties.
|
|
@@ -106,4 +60,4 @@ export interface SolidAsyncQueuer<TValue> extends Omit<AsyncQueuer<TValue>, 'get
|
|
|
106
60
|
* const pending = asyncQueuer.pendingItems();
|
|
107
61
|
* ```
|
|
108
62
|
*/
|
|
109
|
-
export declare function createAsyncQueuer<TValue
|
|
63
|
+
export declare function createAsyncQueuer<TValue, TSelected = AsyncQueuerState<TValue>>(fn: (value: TValue) => Promise<any>, initialOptions?: AsyncQueuerOptions<TValue>, selector?: (state: AsyncQueuerState<TValue>) => TSelected): SolidAsyncQueuer<TValue, TSelected>;
|
|
@@ -1,60 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
3
|
const asyncRateLimiter = require("@tanstack/pacer/async-rate-limiter");
|
|
4
|
-
const
|
|
5
|
-
|
|
6
|
-
function createAsyncRateLimiter(fn, initialOptions) {
|
|
4
|
+
const solidStore = require("@tanstack/solid-store");
|
|
5
|
+
function createAsyncRateLimiter(fn, initialOptions, selector) {
|
|
7
6
|
const asyncRateLimiter$1 = new asyncRateLimiter.AsyncRateLimiter(fn, initialOptions);
|
|
8
|
-
const
|
|
9
|
-
asyncRateLimiter$1.getSuccessCount()
|
|
10
|
-
);
|
|
11
|
-
const [rejectionCount, setRejectionCount] = solidJs.createSignal(
|
|
12
|
-
asyncRateLimiter$1.getRejectionCount()
|
|
13
|
-
);
|
|
14
|
-
const [errorCount, setErrorCount] = solidJs.createSignal(
|
|
15
|
-
asyncRateLimiter$1.getErrorCount()
|
|
16
|
-
);
|
|
17
|
-
const [settleCount, setSettleCount] = solidJs.createSignal(
|
|
18
|
-
asyncRateLimiter$1.getSettleCount()
|
|
19
|
-
);
|
|
20
|
-
const [remainingInWindow, setRemainingInWindow] = solidJs.createSignal(
|
|
21
|
-
asyncRateLimiter$1.getRemainingInWindow()
|
|
22
|
-
);
|
|
23
|
-
const [msUntilNextWindow, setMsUntilNextWindow] = solidJs.createSignal(
|
|
24
|
-
asyncRateLimiter$1.getMsUntilNextWindow()
|
|
25
|
-
);
|
|
26
|
-
function setOptions(newOptions) {
|
|
27
|
-
asyncRateLimiter$1.setOptions({
|
|
28
|
-
...newOptions,
|
|
29
|
-
onSettled: (rateLimiter) => {
|
|
30
|
-
setSuccessCount(rateLimiter.getSuccessCount());
|
|
31
|
-
setSettleCount(rateLimiter.getSettleCount());
|
|
32
|
-
setErrorCount(rateLimiter.getErrorCount());
|
|
33
|
-
setRejectionCount(rateLimiter.getRejectionCount());
|
|
34
|
-
setRemainingInWindow(rateLimiter.getRemainingInWindow());
|
|
35
|
-
setMsUntilNextWindow(rateLimiter.getMsUntilNextWindow());
|
|
36
|
-
const onSettled = newOptions.onSettled ?? initialOptions.onSettled;
|
|
37
|
-
onSettled == null ? void 0 : onSettled(rateLimiter);
|
|
38
|
-
},
|
|
39
|
-
onReject: (rateLimiter) => {
|
|
40
|
-
setRejectionCount(rateLimiter.getRejectionCount());
|
|
41
|
-
setRemainingInWindow(rateLimiter.getRemainingInWindow());
|
|
42
|
-
setMsUntilNextWindow(rateLimiter.getMsUntilNextWindow());
|
|
43
|
-
const onReject = newOptions.onReject ?? initialOptions.onReject;
|
|
44
|
-
onReject == null ? void 0 : onReject(rateLimiter);
|
|
45
|
-
}
|
|
46
|
-
});
|
|
47
|
-
}
|
|
48
|
-
setOptions(initialOptions);
|
|
7
|
+
const state = solidStore.useStore(asyncRateLimiter$1.store, selector);
|
|
49
8
|
return {
|
|
50
|
-
...
|
|
51
|
-
|
|
52
|
-
remainingInWindow,
|
|
53
|
-
msUntilNextWindow,
|
|
54
|
-
rejectionCount,
|
|
55
|
-
setOptions,
|
|
56
|
-
settleCount,
|
|
57
|
-
successCount
|
|
9
|
+
...asyncRateLimiter$1,
|
|
10
|
+
state
|
|
58
11
|
};
|
|
59
12
|
}
|
|
60
13
|
exports.createAsyncRateLimiter = createAsyncRateLimiter;
|
|
@@ -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 {
|
|
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 = AsyncRateLimiterState<TFn>,\n> extends Omit<AsyncRateLimiter<TFn>, 'store'> {\n readonly state: Accessor<Readonly<TSelected>>\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 * @example\n * ```tsx\n * // Basic API call rate limiting with return value\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 * // With state management and return value\n * const [data, setData] = createSignal(null);\n * const { maybeExecute } = createAsyncRateLimiter(\n * async (query) => {\n * const result = await searchAPI(query);\n * setData(result);\n * return result; // Return value can be used by the caller\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 * );\n * ```\n */\nexport function createAsyncRateLimiter<\n TFn extends AnyAsyncFunction,\n TSelected = AsyncRateLimiterState<TFn>,\n>(\n fn: TFn,\n initialOptions: AsyncRateLimiterOptions<TFn>,\n selector?: (state: AsyncRateLimiterState<TFn>) => 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":";;;;AA8EO,SAAS,uBAId,IACA,gBACA,UACuC;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,13 +1,8 @@
|
|
|
1
|
-
import { AsyncRateLimiter, AsyncRateLimiterOptions } from '@tanstack/pacer/async-rate-limiter';
|
|
1
|
+
import { AsyncRateLimiter, AsyncRateLimiterOptions, AsyncRateLimiterState } from '@tanstack/pacer/async-rate-limiter';
|
|
2
2
|
import { Accessor } from 'solid-js';
|
|
3
3
|
import { AnyAsyncFunction } from '@tanstack/pacer/types';
|
|
4
|
-
export interface SolidAsyncRateLimiter<TFn extends AnyAsyncFunction
|
|
5
|
-
|
|
6
|
-
settleCount: Accessor<number>;
|
|
7
|
-
errorCount: Accessor<number>;
|
|
8
|
-
rejectionCount: Accessor<number>;
|
|
9
|
-
remainingInWindow: Accessor<number>;
|
|
10
|
-
msUntilNextWindow: Accessor<number>;
|
|
4
|
+
export interface SolidAsyncRateLimiter<TFn extends AnyAsyncFunction, TSelected = AsyncRateLimiterState<TFn>> extends Omit<AsyncRateLimiter<TFn>, 'store'> {
|
|
5
|
+
readonly state: Accessor<Readonly<TSelected>>;
|
|
11
6
|
}
|
|
12
7
|
/**
|
|
13
8
|
* A low-level Solid hook that creates an `AsyncRateLimiter` instance to limit how many times an async function can execute within a time window.
|
|
@@ -71,4 +66,4 @@ export interface SolidAsyncRateLimiter<TFn extends AnyAsyncFunction> extends Omi
|
|
|
71
66
|
* );
|
|
72
67
|
* ```
|
|
73
68
|
*/
|
|
74
|
-
export declare function createAsyncRateLimiter<TFn extends AnyAsyncFunction
|
|
69
|
+
export declare function createAsyncRateLimiter<TFn extends AnyAsyncFunction, TSelected = AsyncRateLimiterState<TFn>>(fn: TFn, initialOptions: AsyncRateLimiterOptions<TFn>, selector?: (state: AsyncRateLimiterState<TFn>) => TSelected): SolidAsyncRateLimiter<TFn, TSelected>;
|
|
@@ -1,63 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
3
|
const asyncThrottler = require("@tanstack/pacer/async-throttler");
|
|
4
|
-
const
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
const
|
|
8
|
-
new asyncThrottler.AsyncThrottler(fn, initialOptions)
|
|
9
|
-
);
|
|
10
|
-
const [successCount, setSuccessCount] = solidJs.createSignal(
|
|
11
|
-
asyncThrottler$1.getSuccessCount()
|
|
12
|
-
);
|
|
13
|
-
const [settleCount, setSettleCount] = solidJs.createSignal(
|
|
14
|
-
asyncThrottler$1.getSettleCount()
|
|
15
|
-
);
|
|
16
|
-
const [errorCount, setErrorCount] = solidJs.createSignal(
|
|
17
|
-
asyncThrottler$1.getErrorCount()
|
|
18
|
-
);
|
|
19
|
-
const [isPending, setIsPending] = solidJs.createSignal(asyncThrottler$1.getIsPending());
|
|
20
|
-
const [isExecuting, setIsExecuting] = solidJs.createSignal(
|
|
21
|
-
asyncThrottler$1.getIsExecuting()
|
|
22
|
-
);
|
|
23
|
-
const [lastResult, setLastResult] = solidJs.createSignal(
|
|
24
|
-
asyncThrottler$1.getLastResult()
|
|
25
|
-
);
|
|
26
|
-
const [lastExecutionTime, setLastExecutionTime] = solidJs.createSignal(
|
|
27
|
-
asyncThrottler$1.getLastExecutionTime()
|
|
28
|
-
);
|
|
29
|
-
const [nextExecutionTime, setNextExecutionTime] = solidJs.createSignal(
|
|
30
|
-
asyncThrottler$1.getNextExecutionTime()
|
|
31
|
-
);
|
|
32
|
-
function setOptions(newOptions) {
|
|
33
|
-
asyncThrottler$1.setOptions({
|
|
34
|
-
...newOptions,
|
|
35
|
-
onSettled: (throttler) => {
|
|
36
|
-
setSuccessCount(throttler.getSuccessCount());
|
|
37
|
-
setSettleCount(throttler.getSettleCount());
|
|
38
|
-
setErrorCount(throttler.getErrorCount());
|
|
39
|
-
setIsPending(throttler.getIsPending());
|
|
40
|
-
setIsExecuting(throttler.getIsExecuting());
|
|
41
|
-
setLastExecutionTime(throttler.getLastExecutionTime());
|
|
42
|
-
setNextExecutionTime(throttler.getNextExecutionTime());
|
|
43
|
-
setLastResult(throttler.getLastResult());
|
|
44
|
-
const onSettled = newOptions.onSettled ?? initialOptions.onSettled;
|
|
45
|
-
onSettled == null ? void 0 : onSettled(throttler);
|
|
46
|
-
}
|
|
47
|
-
});
|
|
48
|
-
}
|
|
49
|
-
setOptions(initialOptions);
|
|
4
|
+
const solidStore = require("@tanstack/solid-store");
|
|
5
|
+
function createAsyncThrottler(fn, initialOptions, selector) {
|
|
6
|
+
const asyncThrottler$1 = new asyncThrottler.AsyncThrottler(fn, initialOptions);
|
|
7
|
+
const state = solidStore.useStore(asyncThrottler$1.store, selector);
|
|
50
8
|
return {
|
|
51
9
|
...asyncThrottler$1,
|
|
52
|
-
|
|
53
|
-
isExecuting,
|
|
54
|
-
isPending,
|
|
55
|
-
lastExecutionTime,
|
|
56
|
-
lastResult,
|
|
57
|
-
nextExecutionTime,
|
|
58
|
-
setOptions,
|
|
59
|
-
settleCount,
|
|
60
|
-
successCount
|
|
10
|
+
state
|
|
61
11
|
};
|
|
62
12
|
}
|
|
63
13
|
exports.createAsyncThrottler = createAsyncThrottler;
|