@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
|
@@ -1,44 +1,17 @@
|
|
|
1
1
|
import { AsyncDebouncer } from "@tanstack/pacer/async-debouncer";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
function createAsyncDebouncer(fn, initialOptions) {
|
|
2
|
+
import { useStore } from "@tanstack/solid-store";
|
|
3
|
+
import { createEffect, onCleanup } from "solid-js";
|
|
4
|
+
function createAsyncDebouncer(fn, initialOptions, selector) {
|
|
5
5
|
const asyncDebouncer = new AsyncDebouncer(fn, initialOptions);
|
|
6
|
-
const
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
asyncDebouncer.getSettleCount()
|
|
11
|
-
);
|
|
12
|
-
const [successCount, setSuccessCount] = createSignal(
|
|
13
|
-
asyncDebouncer.getSuccessCount()
|
|
14
|
-
);
|
|
15
|
-
const [isPending, setIsPending] = createSignal(asyncDebouncer.getIsPending());
|
|
16
|
-
const [lastResult, setLastResult] = createSignal(
|
|
17
|
-
asyncDebouncer.getLastResult()
|
|
18
|
-
);
|
|
19
|
-
function setOptions(newOptions) {
|
|
20
|
-
asyncDebouncer.setOptions({
|
|
21
|
-
...newOptions,
|
|
22
|
-
onSettled: (asyncDebouncer2) => {
|
|
23
|
-
setSuccessCount(asyncDebouncer2.getSuccessCount());
|
|
24
|
-
setErrorCount(asyncDebouncer2.getErrorCount());
|
|
25
|
-
setSettleCount(asyncDebouncer2.getSettleCount());
|
|
26
|
-
setIsPending(asyncDebouncer2.getIsPending());
|
|
27
|
-
setLastResult(asyncDebouncer2.getLastResult());
|
|
28
|
-
const onSettled = newOptions.onSettled ?? initialOptions.onSettled;
|
|
29
|
-
onSettled == null ? void 0 : onSettled(asyncDebouncer2);
|
|
30
|
-
}
|
|
6
|
+
const state = useStore(asyncDebouncer.store, selector);
|
|
7
|
+
createEffect(() => {
|
|
8
|
+
onCleanup(() => {
|
|
9
|
+
asyncDebouncer.cancel();
|
|
31
10
|
});
|
|
32
|
-
}
|
|
33
|
-
setOptions(initialOptions);
|
|
11
|
+
});
|
|
34
12
|
return {
|
|
35
|
-
...
|
|
36
|
-
|
|
37
|
-
isPending,
|
|
38
|
-
lastResult,
|
|
39
|
-
settleCount,
|
|
40
|
-
successCount,
|
|
41
|
-
setOptions
|
|
13
|
+
...asyncDebouncer,
|
|
14
|
+
state
|
|
42
15
|
};
|
|
43
16
|
}
|
|
44
17
|
export {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createAsyncDebouncer.js","sources":["../../../src/async-debouncer/createAsyncDebouncer.ts"],"sourcesContent":["import { AsyncDebouncer } from '@tanstack/pacer/async-debouncer'\nimport {
|
|
1
|
+
{"version":3,"file":"createAsyncDebouncer.js","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":[],"mappings":";;;AA2EO,SAAS,qBAId,IACA,gBACA,UACqC;AACrC,QAAM,iBAAiB,IAAI,eAAoB,IAAI,cAAc;AAEjE,QAAM,QAAQ,SAAS,eAAe,OAAO,QAAQ;AAErD,eAAa,MAAM;AACjB,cAAU,MAAM;AACd,qBAAe,OAAA;AAAA,IAAO,CACvB;AAAA,EAAA,CACF;AAED,SAAO;AAAA,IACL,GAAG;AAAA,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,79 +1,11 @@
|
|
|
1
1
|
import { AsyncQueuer } from "@tanstack/pacer/async-queuer";
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
function createAsyncQueuer(fn, initialOptions = {}) {
|
|
2
|
+
import { useStore } from "@tanstack/solid-store";
|
|
3
|
+
function createAsyncQueuer(fn, initialOptions = {}, selector) {
|
|
5
4
|
const asyncQueuer = new AsyncQueuer(fn, initialOptions);
|
|
6
|
-
const
|
|
7
|
-
asyncQueuer.getSuccessCount()
|
|
8
|
-
);
|
|
9
|
-
const [errorCount, setErrorCount] = createSignal(asyncQueuer.getErrorCount());
|
|
10
|
-
const [settledCount, setSettledCount] = createSignal(
|
|
11
|
-
asyncQueuer.getSettledCount()
|
|
12
|
-
);
|
|
13
|
-
const [rejectionCount, setRejectionCount] = createSignal(
|
|
14
|
-
asyncQueuer.getRejectionCount()
|
|
15
|
-
);
|
|
16
|
-
const [isEmpty, setIsEmpty] = createSignal(asyncQueuer.getIsEmpty());
|
|
17
|
-
const [isFull, setIsFull] = createSignal(asyncQueuer.getIsFull());
|
|
18
|
-
const [isIdle, setIsIdle] = createSignal(asyncQueuer.getIsIdle());
|
|
19
|
-
const [isRunning, setIsRunning] = createSignal(asyncQueuer.getIsRunning());
|
|
20
|
-
const [allItems, setAllItems] = createSignal(
|
|
21
|
-
asyncQueuer.getAllItems()
|
|
22
|
-
);
|
|
23
|
-
const [activeItems, setActiveItems] = createSignal(
|
|
24
|
-
asyncQueuer.getActiveItems()
|
|
25
|
-
);
|
|
26
|
-
const [pendingItems, setPendingItems] = createSignal(
|
|
27
|
-
asyncQueuer.getPendingItems()
|
|
28
|
-
);
|
|
29
|
-
const [peek, setPeek] = createSignal(
|
|
30
|
-
asyncQueuer.getPeek()
|
|
31
|
-
);
|
|
32
|
-
const [size, setSize] = createSignal(asyncQueuer.getSize());
|
|
33
|
-
asyncQueuer.setOptions({
|
|
34
|
-
onItemsChange: (queuer) => {
|
|
35
|
-
var _a;
|
|
36
|
-
setActiveItems(queuer.getActiveItems());
|
|
37
|
-
setAllItems(queuer.getAllItems());
|
|
38
|
-
setErrorCount(queuer.getErrorCount());
|
|
39
|
-
setIsEmpty(queuer.getIsEmpty());
|
|
40
|
-
setIsFull(queuer.getIsFull());
|
|
41
|
-
setIsIdle(queuer.getIsIdle());
|
|
42
|
-
setPeek(() => queuer.getPeek());
|
|
43
|
-
setPendingItems(queuer.getPendingItems());
|
|
44
|
-
setRejectionCount(queuer.getRejectionCount());
|
|
45
|
-
setSettledCount(queuer.getSettledCount());
|
|
46
|
-
setSize(queuer.getSize());
|
|
47
|
-
setSuccessCount(queuer.getSuccessCount());
|
|
48
|
-
(_a = initialOptions.onItemsChange) == null ? void 0 : _a.call(initialOptions, queuer);
|
|
49
|
-
},
|
|
50
|
-
onIsRunningChange: (queuer) => {
|
|
51
|
-
var _a;
|
|
52
|
-
setIsRunning(queuer.getIsRunning());
|
|
53
|
-
setIsIdle(queuer.getIsIdle());
|
|
54
|
-
(_a = initialOptions.onIsRunningChange) == null ? void 0 : _a.call(initialOptions, queuer);
|
|
55
|
-
},
|
|
56
|
-
onReject: (item, queuer) => {
|
|
57
|
-
var _a;
|
|
58
|
-
setRejectionCount(queuer.getRejectionCount());
|
|
59
|
-
(_a = initialOptions.onReject) == null ? void 0 : _a.call(initialOptions, item, queuer);
|
|
60
|
-
}
|
|
61
|
-
});
|
|
5
|
+
const state = useStore(asyncQueuer.store, selector);
|
|
62
6
|
return {
|
|
63
|
-
...
|
|
64
|
-
|
|
65
|
-
allItems,
|
|
66
|
-
errorCount,
|
|
67
|
-
isEmpty,
|
|
68
|
-
isFull,
|
|
69
|
-
isIdle,
|
|
70
|
-
isRunning,
|
|
71
|
-
peek,
|
|
72
|
-
pendingItems,
|
|
73
|
-
rejectionCount,
|
|
74
|
-
settledCount,
|
|
75
|
-
size,
|
|
76
|
-
successCount
|
|
7
|
+
...asyncQueuer,
|
|
8
|
+
state
|
|
77
9
|
};
|
|
78
10
|
}
|
|
79
11
|
export {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createAsyncQueuer.js","sources":["../../../src/async-queuer/createAsyncQueuer.ts"],"sourcesContent":["import { AsyncQueuer } from '@tanstack/pacer/async-queuer'\nimport {
|
|
1
|
+
{"version":3,"file":"createAsyncQueuer.js","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":[],"mappings":";;AAsEO,SAAS,kBACd,IACA,iBAA6C,CAAA,GAC7C,UACqC;AACrC,QAAM,cAAc,IAAI,YAAoB,IAAI,cAAc;AAE9D,QAAM,QAAQ,SAAS,YAAY,OAAO,QAAQ;AAElD,SAAO;AAAA,IACL,GAAG;AAAA,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,58 +1,11 @@
|
|
|
1
1
|
import { AsyncRateLimiter } from "@tanstack/pacer/async-rate-limiter";
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
function createAsyncRateLimiter(fn, initialOptions) {
|
|
2
|
+
import { useStore } from "@tanstack/solid-store";
|
|
3
|
+
function createAsyncRateLimiter(fn, initialOptions, selector) {
|
|
5
4
|
const asyncRateLimiter = new AsyncRateLimiter(fn, initialOptions);
|
|
6
|
-
const
|
|
7
|
-
asyncRateLimiter.getSuccessCount()
|
|
8
|
-
);
|
|
9
|
-
const [rejectionCount, setRejectionCount] = createSignal(
|
|
10
|
-
asyncRateLimiter.getRejectionCount()
|
|
11
|
-
);
|
|
12
|
-
const [errorCount, setErrorCount] = createSignal(
|
|
13
|
-
asyncRateLimiter.getErrorCount()
|
|
14
|
-
);
|
|
15
|
-
const [settleCount, setSettleCount] = createSignal(
|
|
16
|
-
asyncRateLimiter.getSettleCount()
|
|
17
|
-
);
|
|
18
|
-
const [remainingInWindow, setRemainingInWindow] = createSignal(
|
|
19
|
-
asyncRateLimiter.getRemainingInWindow()
|
|
20
|
-
);
|
|
21
|
-
const [msUntilNextWindow, setMsUntilNextWindow] = createSignal(
|
|
22
|
-
asyncRateLimiter.getMsUntilNextWindow()
|
|
23
|
-
);
|
|
24
|
-
function setOptions(newOptions) {
|
|
25
|
-
asyncRateLimiter.setOptions({
|
|
26
|
-
...newOptions,
|
|
27
|
-
onSettled: (rateLimiter) => {
|
|
28
|
-
setSuccessCount(rateLimiter.getSuccessCount());
|
|
29
|
-
setSettleCount(rateLimiter.getSettleCount());
|
|
30
|
-
setErrorCount(rateLimiter.getErrorCount());
|
|
31
|
-
setRejectionCount(rateLimiter.getRejectionCount());
|
|
32
|
-
setRemainingInWindow(rateLimiter.getRemainingInWindow());
|
|
33
|
-
setMsUntilNextWindow(rateLimiter.getMsUntilNextWindow());
|
|
34
|
-
const onSettled = newOptions.onSettled ?? initialOptions.onSettled;
|
|
35
|
-
onSettled == null ? void 0 : onSettled(rateLimiter);
|
|
36
|
-
},
|
|
37
|
-
onReject: (rateLimiter) => {
|
|
38
|
-
setRejectionCount(rateLimiter.getRejectionCount());
|
|
39
|
-
setRemainingInWindow(rateLimiter.getRemainingInWindow());
|
|
40
|
-
setMsUntilNextWindow(rateLimiter.getMsUntilNextWindow());
|
|
41
|
-
const onReject = newOptions.onReject ?? initialOptions.onReject;
|
|
42
|
-
onReject == null ? void 0 : onReject(rateLimiter);
|
|
43
|
-
}
|
|
44
|
-
});
|
|
45
|
-
}
|
|
46
|
-
setOptions(initialOptions);
|
|
5
|
+
const state = useStore(asyncRateLimiter.store, selector);
|
|
47
6
|
return {
|
|
48
|
-
...
|
|
49
|
-
|
|
50
|
-
remainingInWindow,
|
|
51
|
-
msUntilNextWindow,
|
|
52
|
-
rejectionCount,
|
|
53
|
-
setOptions,
|
|
54
|
-
settleCount,
|
|
55
|
-
successCount
|
|
7
|
+
...asyncRateLimiter,
|
|
8
|
+
state
|
|
56
9
|
};
|
|
57
10
|
}
|
|
58
11
|
export {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createAsyncRateLimiter.js","sources":["../../../src/async-rate-limiter/createAsyncRateLimiter.ts"],"sourcesContent":["import { AsyncRateLimiter } from '@tanstack/pacer/async-rate-limiter'\nimport {
|
|
1
|
+
{"version":3,"file":"createAsyncRateLimiter.js","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":[],"mappings":";;AA8EO,SAAS,uBAId,IACA,gBACA,UACuC;AACvC,QAAM,mBAAmB,IAAI,iBAAsB,IAAI,cAAc;AAErE,QAAM,QAAQ,SAAS,iBAAiB,OAAO,QAAQ;AAEvD,SAAO;AAAA,IACL,GAAG;AAAA,IACH;AAAA,EAAA;AAEJ;"}
|
|
@@ -1,15 +1,13 @@
|
|
|
1
|
-
import { AsyncThrottler, AsyncThrottlerOptions } from '@tanstack/pacer/async-throttler';
|
|
1
|
+
import { AsyncThrottler, AsyncThrottlerOptions, AsyncThrottlerState } from '@tanstack/pacer/async-throttler';
|
|
2
2
|
import { Accessor } from 'solid-js';
|
|
3
3
|
import { AnyAsyncFunction } from '@tanstack/pacer/types';
|
|
4
|
-
export interface SolidAsyncThrottler<TFn extends AnyAsyncFunction
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
lastExecutionTime: Accessor<number>;
|
|
12
|
-
nextExecutionTime: Accessor<number>;
|
|
4
|
+
export interface SolidAsyncThrottler<TFn extends AnyAsyncFunction, TSelected = AsyncThrottlerState<TFn>> extends Omit<AsyncThrottler<TFn>, 'store'> {
|
|
5
|
+
/**
|
|
6
|
+
* Reactive state that will be updated and re-rendered when the throttler state changes
|
|
7
|
+
*
|
|
8
|
+
* Use this instead of `throttler.store.state`
|
|
9
|
+
*/
|
|
10
|
+
readonly state: Accessor<Readonly<TSelected>>;
|
|
13
11
|
}
|
|
14
12
|
/**
|
|
15
13
|
* A low-level Solid hook that creates an `AsyncThrottler` instance to limit how often an async function can execute.
|
|
@@ -61,4 +59,4 @@ export interface SolidAsyncThrottler<TFn extends AnyAsyncFunction> extends Omit<
|
|
|
61
59
|
* );
|
|
62
60
|
* ```
|
|
63
61
|
*/
|
|
64
|
-
export declare function createAsyncThrottler<TFn extends AnyAsyncFunction
|
|
62
|
+
export declare function createAsyncThrottler<TFn extends AnyAsyncFunction, TSelected = AsyncThrottlerState<TFn>>(fn: TFn, initialOptions: AsyncThrottlerOptions<TFn>, selector?: (state: AsyncThrottlerState<TFn>) => TSelected): SolidAsyncThrottler<TFn, TSelected>;
|
|
@@ -1,61 +1,11 @@
|
|
|
1
1
|
import { AsyncThrottler } from "@tanstack/pacer/async-throttler";
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const
|
|
6
|
-
new AsyncThrottler(fn, initialOptions)
|
|
7
|
-
);
|
|
8
|
-
const [successCount, setSuccessCount] = createSignal(
|
|
9
|
-
asyncThrottler.getSuccessCount()
|
|
10
|
-
);
|
|
11
|
-
const [settleCount, setSettleCount] = createSignal(
|
|
12
|
-
asyncThrottler.getSettleCount()
|
|
13
|
-
);
|
|
14
|
-
const [errorCount, setErrorCount] = createSignal(
|
|
15
|
-
asyncThrottler.getErrorCount()
|
|
16
|
-
);
|
|
17
|
-
const [isPending, setIsPending] = createSignal(asyncThrottler.getIsPending());
|
|
18
|
-
const [isExecuting, setIsExecuting] = createSignal(
|
|
19
|
-
asyncThrottler.getIsExecuting()
|
|
20
|
-
);
|
|
21
|
-
const [lastResult, setLastResult] = createSignal(
|
|
22
|
-
asyncThrottler.getLastResult()
|
|
23
|
-
);
|
|
24
|
-
const [lastExecutionTime, setLastExecutionTime] = createSignal(
|
|
25
|
-
asyncThrottler.getLastExecutionTime()
|
|
26
|
-
);
|
|
27
|
-
const [nextExecutionTime, setNextExecutionTime] = createSignal(
|
|
28
|
-
asyncThrottler.getNextExecutionTime()
|
|
29
|
-
);
|
|
30
|
-
function setOptions(newOptions) {
|
|
31
|
-
asyncThrottler.setOptions({
|
|
32
|
-
...newOptions,
|
|
33
|
-
onSettled: (throttler) => {
|
|
34
|
-
setSuccessCount(throttler.getSuccessCount());
|
|
35
|
-
setSettleCount(throttler.getSettleCount());
|
|
36
|
-
setErrorCount(throttler.getErrorCount());
|
|
37
|
-
setIsPending(throttler.getIsPending());
|
|
38
|
-
setIsExecuting(throttler.getIsExecuting());
|
|
39
|
-
setLastExecutionTime(throttler.getLastExecutionTime());
|
|
40
|
-
setNextExecutionTime(throttler.getNextExecutionTime());
|
|
41
|
-
setLastResult(throttler.getLastResult());
|
|
42
|
-
const onSettled = newOptions.onSettled ?? initialOptions.onSettled;
|
|
43
|
-
onSettled == null ? void 0 : onSettled(throttler);
|
|
44
|
-
}
|
|
45
|
-
});
|
|
46
|
-
}
|
|
47
|
-
setOptions(initialOptions);
|
|
2
|
+
import { useStore } from "@tanstack/solid-store";
|
|
3
|
+
function createAsyncThrottler(fn, initialOptions, selector) {
|
|
4
|
+
const asyncThrottler = new AsyncThrottler(fn, initialOptions);
|
|
5
|
+
const state = useStore(asyncThrottler.store, selector);
|
|
48
6
|
return {
|
|
49
7
|
...asyncThrottler,
|
|
50
|
-
|
|
51
|
-
isExecuting,
|
|
52
|
-
isPending,
|
|
53
|
-
lastExecutionTime,
|
|
54
|
-
lastResult,
|
|
55
|
-
nextExecutionTime,
|
|
56
|
-
setOptions,
|
|
57
|
-
settleCount,
|
|
58
|
-
successCount
|
|
8
|
+
state
|
|
59
9
|
};
|
|
60
10
|
}
|
|
61
11
|
export {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createAsyncThrottler.js","sources":["../../../src/async-throttler/createAsyncThrottler.ts"],"sourcesContent":["import { AsyncThrottler } from '@tanstack/pacer/async-throttler'\nimport {
|
|
1
|
+
{"version":3,"file":"createAsyncThrottler.js","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 = AsyncThrottlerState<TFn>,\n> extends Omit<AsyncThrottler<TFn>, 'store'> {\n /**\n * Reactive state that will be updated and re-rendered when the throttler state changes\n *\n * Use this instead of `throttler.store.state`\n */\n readonly state: Accessor<Readonly<TSelected>>\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 * @example\n * ```tsx\n * // Basic API call throttling\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 * // With state management\n * const [data, setData] = createSignal(null);\n * const { maybeExecute } = createAsyncThrottler(\n * async (query) => {\n * const result = await searchAPI(query);\n * setData(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 * );\n * ```\n */\nexport function createAsyncThrottler<\n TFn extends AnyAsyncFunction,\n TSelected = AsyncThrottlerState<TFn>,\n>(\n fn: TFn,\n initialOptions: AsyncThrottlerOptions<TFn>,\n selector?: (state: AsyncThrottlerState<TFn>) => 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":[],"mappings":";;AAuEO,SAAS,qBAId,IACA,gBACA,UACqC;AACrC,QAAM,iBAAiB,IAAI,eAAe,IAAI,cAAc;AAE5D,QAAM,QAAQ,SAAS,eAAe,OAAO,QAAQ;AAErD,SAAO;AAAA,IACL,GAAG;AAAA,IACH;AAAA,EAAA;AAEJ;"}
|
|
@@ -1,30 +1,12 @@
|
|
|
1
|
-
import { Batcher, BatcherOptions } from '@tanstack/pacer/batcher';
|
|
1
|
+
import { Batcher, BatcherOptions, BatcherState } from '@tanstack/pacer/batcher';
|
|
2
2
|
import { Accessor } from 'solid-js';
|
|
3
|
-
export interface SolidBatcher<TValue
|
|
3
|
+
export interface SolidBatcher<TValue, TSelected = BatcherState<TValue>> extends Omit<Batcher<TValue>, 'store'> {
|
|
4
4
|
/**
|
|
5
|
-
*
|
|
5
|
+
* Reactive state that will be updated when the batcher state changes
|
|
6
|
+
*
|
|
7
|
+
* Use this instead of `batcher.store.state`
|
|
6
8
|
*/
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Signal version of `getBatchExecutionCount`
|
|
10
|
-
*/
|
|
11
|
-
batchExecutionCount: Accessor<number>;
|
|
12
|
-
/**
|
|
13
|
-
* Signal version of `getIsEmpty`
|
|
14
|
-
*/
|
|
15
|
-
isEmpty: Accessor<boolean>;
|
|
16
|
-
/**
|
|
17
|
-
* Signal version of `getIsRunning`
|
|
18
|
-
*/
|
|
19
|
-
isRunning: Accessor<boolean>;
|
|
20
|
-
/**
|
|
21
|
-
* Signal version of `getItemExecutionCount`
|
|
22
|
-
*/
|
|
23
|
-
itemExecutionCount: Accessor<number>;
|
|
24
|
-
/**
|
|
25
|
-
* Signal version of `getSize`
|
|
26
|
-
*/
|
|
27
|
-
size: Accessor<number>;
|
|
9
|
+
readonly state: Accessor<Readonly<TSelected>>;
|
|
28
10
|
}
|
|
29
11
|
/**
|
|
30
12
|
* Creates a Solid-compatible Batcher instance for managing batches of items, exposing Solid signals for all stateful properties.
|
|
@@ -69,8 +51,8 @@ export interface SolidBatcher<TValue> extends Omit<Batcher<TValue>, 'getAllItems
|
|
|
69
51
|
* console.log('Size:', batcher.size());
|
|
70
52
|
* console.log('Is empty:', batcher.isEmpty());
|
|
71
53
|
* console.log('Is running:', batcher.isRunning());
|
|
72
|
-
* console.log('Batch count:', batcher.
|
|
73
|
-
* console.log('Item count:', batcher.
|
|
54
|
+
* console.log('Batch count:', batcher.executionCount());
|
|
55
|
+
* console.log('Item count:', batcher.totalItemsProcessed());
|
|
74
56
|
* ```
|
|
75
57
|
*/
|
|
76
|
-
export declare function createBatcher<TValue
|
|
58
|
+
export declare function createBatcher<TValue, TSelected = BatcherState<TValue>>(fn: (items: Array<TValue>) => void, initialOptions?: BatcherOptions<TValue>, selector?: (state: BatcherState<TValue>) => TSelected): SolidBatcher<TValue, TSelected>;
|
|
@@ -1,55 +1,11 @@
|
|
|
1
1
|
import { Batcher } from "@tanstack/pacer/batcher";
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const
|
|
6
|
-
const [allItems, setAllItems] = createSignal(
|
|
7
|
-
batcher.getAllItems()
|
|
8
|
-
);
|
|
9
|
-
const [batchExecutionCount, setBatchExecutionCount] = createSignal(
|
|
10
|
-
batcher.getBatchExecutionCount()
|
|
11
|
-
);
|
|
12
|
-
const [itemExecutionCount, setItemExecutionCount] = createSignal(
|
|
13
|
-
batcher.getItemExecutionCount()
|
|
14
|
-
);
|
|
15
|
-
const [isEmpty, setIsEmpty] = createSignal(batcher.getIsEmpty());
|
|
16
|
-
const [isRunning, setIsRunning] = createSignal(batcher.getIsRunning());
|
|
17
|
-
const [size, setSize] = createSignal(batcher.getSize());
|
|
18
|
-
function setOptions(newOptions) {
|
|
19
|
-
batcher.setOptions({
|
|
20
|
-
...newOptions,
|
|
21
|
-
onItemsChange: (batcher2) => {
|
|
22
|
-
setAllItems(batcher2.getAllItems());
|
|
23
|
-
setBatchExecutionCount(batcher2.getBatchExecutionCount());
|
|
24
|
-
setItemExecutionCount(batcher2.getItemExecutionCount());
|
|
25
|
-
setIsEmpty(batcher2.getIsEmpty());
|
|
26
|
-
setSize(batcher2.getSize());
|
|
27
|
-
const onItemsChange = newOptions.onItemsChange ?? initialOptions.onItemsChange;
|
|
28
|
-
onItemsChange == null ? void 0 : onItemsChange(batcher2);
|
|
29
|
-
},
|
|
30
|
-
onExecute: (batcher2) => {
|
|
31
|
-
setBatchExecutionCount(batcher2.getBatchExecutionCount());
|
|
32
|
-
setItemExecutionCount(batcher2.getItemExecutionCount());
|
|
33
|
-
const onExecute = newOptions.onExecute ?? initialOptions.onExecute;
|
|
34
|
-
onExecute == null ? void 0 : onExecute(batcher2);
|
|
35
|
-
},
|
|
36
|
-
onIsRunningChange: (batcher2) => {
|
|
37
|
-
setIsRunning(batcher2.getIsRunning());
|
|
38
|
-
const onIsRunningChange = newOptions.onIsRunningChange ?? initialOptions.onIsRunningChange;
|
|
39
|
-
onIsRunningChange == null ? void 0 : onIsRunningChange(batcher2);
|
|
40
|
-
}
|
|
41
|
-
});
|
|
42
|
-
}
|
|
43
|
-
setOptions(initialOptions);
|
|
2
|
+
import { useStore } from "@tanstack/solid-store";
|
|
3
|
+
function createBatcher(fn, initialOptions = {}, selector) {
|
|
4
|
+
const batcher = new Batcher(fn, initialOptions);
|
|
5
|
+
const state = useStore(batcher.store, selector);
|
|
44
6
|
return {
|
|
45
7
|
...batcher,
|
|
46
|
-
|
|
47
|
-
batchExecutionCount,
|
|
48
|
-
isEmpty,
|
|
49
|
-
isRunning,
|
|
50
|
-
itemExecutionCount,
|
|
51
|
-
size,
|
|
52
|
-
setOptions
|
|
8
|
+
state
|
|
53
9
|
};
|
|
54
10
|
}
|
|
55
11
|
export {
|