@tanstack/solid-pacer 0.9.1 → 0.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/async-batcher/createAsyncBatcher.cjs +1 -1
- package/dist/cjs/async-batcher/createAsyncBatcher.cjs.map +1 -1
- package/dist/cjs/async-batcher/createAsyncBatcher.d.cts +54 -6
- package/dist/cjs/async-debouncer/createAsyncDebouncer.cjs +1 -1
- package/dist/cjs/async-debouncer/createAsyncDebouncer.cjs.map +1 -1
- package/dist/cjs/async-debouncer/createAsyncDebouncer.d.cts +50 -8
- package/dist/cjs/async-queuer/createAsyncQueuer.cjs +1 -1
- package/dist/cjs/async-queuer/createAsyncQueuer.cjs.map +1 -1
- package/dist/cjs/async-queuer/createAsyncQueuer.d.cts +55 -6
- package/dist/cjs/async-rate-limiter/createAsyncRateLimiter.cjs +1 -1
- package/dist/cjs/async-rate-limiter/createAsyncRateLimiter.cjs.map +1 -1
- package/dist/cjs/async-rate-limiter/createAsyncRateLimiter.d.cts +59 -9
- package/dist/cjs/async-throttler/createAsyncThrottler.cjs +1 -1
- package/dist/cjs/async-throttler/createAsyncThrottler.cjs.map +1 -1
- package/dist/cjs/async-throttler/createAsyncThrottler.d.cts +54 -9
- package/dist/cjs/batcher/createBatcher.cjs +1 -1
- package/dist/cjs/batcher/createBatcher.cjs.map +1 -1
- package/dist/cjs/batcher/createBatcher.d.cts +46 -9
- package/dist/cjs/debouncer/createDebouncedSignal.cjs.map +1 -1
- package/dist/cjs/debouncer/createDebouncedSignal.d.cts +36 -4
- package/dist/cjs/debouncer/createDebouncedValue.cjs.map +1 -1
- package/dist/cjs/debouncer/createDebouncedValue.d.cts +30 -2
- package/dist/cjs/debouncer/createDebouncer.cjs +1 -1
- package/dist/cjs/debouncer/createDebouncer.cjs.map +1 -1
- package/dist/cjs/debouncer/createDebouncer.d.cts +56 -10
- package/dist/cjs/queuer/createQueuer.cjs +1 -1
- package/dist/cjs/queuer/createQueuer.cjs.map +1 -1
- package/dist/cjs/queuer/createQueuer.d.cts +46 -12
- package/dist/cjs/rate-limiter/createRateLimitedSignal.cjs.map +1 -1
- package/dist/cjs/rate-limiter/createRateLimitedSignal.d.cts +30 -3
- package/dist/cjs/rate-limiter/createRateLimitedValue.cjs.map +1 -1
- package/dist/cjs/rate-limiter/createRateLimitedValue.d.cts +32 -2
- package/dist/cjs/rate-limiter/createRateLimiter.cjs +1 -1
- package/dist/cjs/rate-limiter/createRateLimiter.cjs.map +1 -1
- package/dist/cjs/rate-limiter/createRateLimiter.d.cts +50 -8
- package/dist/cjs/throttler/createThrottledSignal.cjs.map +1 -1
- package/dist/cjs/throttler/createThrottledSignal.d.cts +34 -6
- package/dist/cjs/throttler/createThrottledValue.cjs.map +1 -1
- package/dist/cjs/throttler/createThrottledValue.d.cts +33 -2
- package/dist/cjs/throttler/createThrottler.cjs +1 -1
- package/dist/cjs/throttler/createThrottler.cjs.map +1 -1
- package/dist/cjs/throttler/createThrottler.d.cts +56 -12
- package/dist/esm/async-batcher/createAsyncBatcher.d.ts +54 -6
- package/dist/esm/async-batcher/createAsyncBatcher.js +1 -1
- package/dist/esm/async-batcher/createAsyncBatcher.js.map +1 -1
- package/dist/esm/async-debouncer/createAsyncDebouncer.d.ts +50 -8
- package/dist/esm/async-debouncer/createAsyncDebouncer.js +1 -1
- package/dist/esm/async-debouncer/createAsyncDebouncer.js.map +1 -1
- package/dist/esm/async-queuer/createAsyncQueuer.d.ts +55 -6
- package/dist/esm/async-queuer/createAsyncQueuer.js +1 -1
- package/dist/esm/async-queuer/createAsyncQueuer.js.map +1 -1
- package/dist/esm/async-rate-limiter/createAsyncRateLimiter.d.ts +59 -9
- package/dist/esm/async-rate-limiter/createAsyncRateLimiter.js +1 -1
- package/dist/esm/async-rate-limiter/createAsyncRateLimiter.js.map +1 -1
- package/dist/esm/async-throttler/createAsyncThrottler.d.ts +54 -9
- package/dist/esm/async-throttler/createAsyncThrottler.js +1 -1
- package/dist/esm/async-throttler/createAsyncThrottler.js.map +1 -1
- package/dist/esm/batcher/createBatcher.d.ts +46 -9
- package/dist/esm/batcher/createBatcher.js +1 -1
- package/dist/esm/batcher/createBatcher.js.map +1 -1
- package/dist/esm/debouncer/createDebouncedSignal.d.ts +36 -4
- package/dist/esm/debouncer/createDebouncedSignal.js.map +1 -1
- package/dist/esm/debouncer/createDebouncedValue.d.ts +30 -2
- package/dist/esm/debouncer/createDebouncedValue.js.map +1 -1
- package/dist/esm/debouncer/createDebouncer.d.ts +56 -10
- package/dist/esm/debouncer/createDebouncer.js +1 -1
- package/dist/esm/debouncer/createDebouncer.js.map +1 -1
- package/dist/esm/queuer/createQueuer.d.ts +46 -12
- package/dist/esm/queuer/createQueuer.js +1 -1
- package/dist/esm/queuer/createQueuer.js.map +1 -1
- package/dist/esm/rate-limiter/createRateLimitedSignal.d.ts +30 -3
- package/dist/esm/rate-limiter/createRateLimitedSignal.js.map +1 -1
- package/dist/esm/rate-limiter/createRateLimitedValue.d.ts +32 -2
- package/dist/esm/rate-limiter/createRateLimitedValue.js.map +1 -1
- package/dist/esm/rate-limiter/createRateLimiter.d.ts +50 -8
- package/dist/esm/rate-limiter/createRateLimiter.js +1 -1
- package/dist/esm/rate-limiter/createRateLimiter.js.map +1 -1
- package/dist/esm/throttler/createThrottledSignal.d.ts +34 -6
- package/dist/esm/throttler/createThrottledSignal.js.map +1 -1
- package/dist/esm/throttler/createThrottledValue.d.ts +33 -2
- package/dist/esm/throttler/createThrottledValue.js.map +1 -1
- package/dist/esm/throttler/createThrottler.d.ts +56 -12
- package/dist/esm/throttler/createThrottler.js +1 -1
- package/dist/esm/throttler/createThrottler.js.map +1 -1
- package/package.json +2 -2
- package/src/async-batcher/createAsyncBatcher.ts +58 -14
- package/src/async-debouncer/createAsyncDebouncer.ts +53 -10
- package/src/async-queuer/createAsyncQueuer.ts +58 -8
- package/src/async-rate-limiter/createAsyncRateLimiter.ts +61 -10
- package/src/async-throttler/createAsyncThrottler.ts +56 -10
- package/src/batcher/createBatcher.ts +48 -10
- package/src/debouncer/createDebouncedSignal.ts +36 -7
- package/src/debouncer/createDebouncedValue.ts +30 -5
- package/src/debouncer/createDebouncer.ts +58 -17
- package/src/queuer/createQueuer.ts +48 -14
- package/src/rate-limiter/createRateLimitedSignal.ts +30 -3
- package/src/rate-limiter/createRateLimitedValue.ts +32 -2
- package/src/rate-limiter/createRateLimiter.ts +53 -16
- package/src/throttler/createThrottledSignal.ts +34 -9
- package/src/throttler/createThrottledValue.ts +33 -5
- package/src/throttler/createThrottler.ts +58 -19
|
@@ -1,13 +1,20 @@
|
|
|
1
1
|
import { Throttler, ThrottlerOptions, ThrottlerState } from '@tanstack/pacer/throttler';
|
|
2
|
+
import { Store } from '@tanstack/solid-store';
|
|
2
3
|
import { Accessor } from 'solid-js';
|
|
3
4
|
import { AnyFunction } from '@tanstack/pacer/types';
|
|
4
|
-
export interface SolidThrottler<TFn extends AnyFunction, TSelected =
|
|
5
|
+
export interface SolidThrottler<TFn extends AnyFunction, TSelected = {}> extends Omit<Throttler<TFn>, 'store'> {
|
|
5
6
|
/**
|
|
6
7
|
* Reactive state that will be updated when the throttler state changes
|
|
7
8
|
*
|
|
8
9
|
* Use this instead of `throttler.store.state`
|
|
9
10
|
*/
|
|
10
11
|
readonly state: Accessor<Readonly<TSelected>>;
|
|
12
|
+
/**
|
|
13
|
+
* @deprecated Use `throttler.state` instead of `throttler.store.state` if you want to read reactive state.
|
|
14
|
+
* The state on the store object is not reactive, as it has not been wrapped in a `useStore` hook internally.
|
|
15
|
+
* Although, you can make the state reactive by using the `useStore` in your own usage.
|
|
16
|
+
*/
|
|
17
|
+
readonly store: Store<Readonly<ThrottlerState<TFn>>>;
|
|
11
18
|
}
|
|
12
19
|
/**
|
|
13
20
|
* A low-level Solid hook that creates a `Throttler` instance that limits how often the provided function can execute.
|
|
@@ -20,27 +27,64 @@ export interface SolidThrottler<TFn extends AnyFunction, TSelected = ThrottlerSt
|
|
|
20
27
|
* regardless of how many times it is called. This is useful for rate-limiting
|
|
21
28
|
* expensive operations or UI updates.
|
|
22
29
|
*
|
|
30
|
+
* ## State Management and Selector
|
|
31
|
+
*
|
|
32
|
+
* The hook uses TanStack Store for reactive state management. The `selector` parameter allows you
|
|
33
|
+
* to specify which state changes will trigger a re-render, optimizing performance by preventing
|
|
34
|
+
* unnecessary re-renders when irrelevant state changes occur.
|
|
35
|
+
*
|
|
36
|
+
* **By default, there will be no reactive state subscriptions** and you must opt-in to state
|
|
37
|
+
* tracking by providing a selector function. This prevents unnecessary re-renders and gives you
|
|
38
|
+
* full control over when your component updates. Only when you provide a selector will the
|
|
39
|
+
* component re-render when the selected state values change.
|
|
40
|
+
*
|
|
41
|
+
* Available state properties:
|
|
42
|
+
* - `canLeadingExecute`: Whether the throttler can execute on the leading edge
|
|
43
|
+
* - `canTrailingExecute`: Whether the throttler can execute on the trailing edge
|
|
44
|
+
* - `executionCount`: Number of function executions that have been completed
|
|
45
|
+
* - `isPending`: Whether the throttler is waiting for the timeout to trigger execution
|
|
46
|
+
* - `lastArgs`: The arguments from the most recent call to maybeExecute
|
|
47
|
+
* - `lastExecutionTime`: Timestamp of the last execution
|
|
48
|
+
* - `nextExecutionTime`: Timestamp of the next allowed execution
|
|
49
|
+
* - `status`: Current execution status ('disabled' | 'idle' | 'pending')
|
|
50
|
+
*
|
|
23
51
|
* @example
|
|
24
52
|
* ```tsx
|
|
25
|
-
* //
|
|
26
|
-
* const [value, setValue] = createSignal(0);
|
|
53
|
+
* // Default behavior - no reactive state subscriptions
|
|
27
54
|
* const throttler = createThrottler(setValue, { wait: 1000 });
|
|
28
55
|
*
|
|
29
|
-
* //
|
|
56
|
+
* // Opt-in to re-render when isPending changes (optimized for loading states)
|
|
57
|
+
* const throttler = createThrottler(
|
|
58
|
+
* setValue,
|
|
59
|
+
* { wait: 1000 },
|
|
60
|
+
* (state) => ({ isPending: state.isPending })
|
|
61
|
+
* );
|
|
62
|
+
*
|
|
63
|
+
* // Opt-in to re-render when executionCount changes (optimized for tracking execution)
|
|
64
|
+
* const throttler = createThrottler(
|
|
65
|
+
* setValue,
|
|
66
|
+
* { wait: 1000 },
|
|
67
|
+
* (state) => ({ executionCount: state.executionCount })
|
|
68
|
+
* );
|
|
69
|
+
*
|
|
70
|
+
* // Multiple state properties - re-render when any of these change
|
|
30
71
|
* const throttler = createThrottler(
|
|
31
|
-
*
|
|
72
|
+
* setValue,
|
|
32
73
|
* {
|
|
33
74
|
* wait: 2000,
|
|
34
75
|
* leading: true, // Execute immediately on first call
|
|
35
76
|
* trailing: false // Skip trailing edge updates
|
|
36
|
-
* }
|
|
77
|
+
* },
|
|
78
|
+
* (state) => ({
|
|
79
|
+
* isPending: state.isPending,
|
|
80
|
+
* executionCount: state.executionCount,
|
|
81
|
+
* lastExecutionTime: state.lastExecutionTime,
|
|
82
|
+
* nextExecutionTime: state.nextExecutionTime
|
|
83
|
+
* })
|
|
37
84
|
* );
|
|
38
85
|
*
|
|
39
|
-
* // Access
|
|
40
|
-
*
|
|
41
|
-
* console.log(throttler.isPending()); // whether throttled function is pending
|
|
42
|
-
* console.log(throttler.lastExecutionTime()); // timestamp of last execution
|
|
43
|
-
* console.log(throttler.nextExecutionTime()); // timestamp of next allowed execution
|
|
86
|
+
* // Access the selected state (will be empty object {} unless selector provided)
|
|
87
|
+
* const { isPending, executionCount } = throttler.state();
|
|
44
88
|
* ```
|
|
45
89
|
*/
|
|
46
|
-
export declare function createThrottler<TFn extends AnyFunction, TSelected =
|
|
90
|
+
export declare function createThrottler<TFn extends AnyFunction, TSelected = {}>(fn: TFn, initialOptions: ThrottlerOptions<TFn>, selector?: (state: ThrottlerState<TFn>) => TSelected): SolidThrottler<TFn, TSelected>;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Throttler } from "@tanstack/pacer/throttler";
|
|
2
2
|
import { createEffect, onCleanup } from "solid-js";
|
|
3
3
|
import { useStore } from "@tanstack/solid-store";
|
|
4
|
-
function createThrottler(fn, initialOptions, selector) {
|
|
4
|
+
function createThrottler(fn, initialOptions, selector = () => ({})) {
|
|
5
5
|
const asyncThrottler = new Throttler(fn, initialOptions);
|
|
6
6
|
const state = useStore(asyncThrottler.store, selector);
|
|
7
7
|
createEffect(() => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createThrottler.js","sources":["../../../src/throttler/createThrottler.ts"],"sourcesContent":["import { Throttler } from '@tanstack/pacer/throttler'\nimport { createEffect, onCleanup } from 'solid-js'\nimport { useStore } from '@tanstack/solid-store'\nimport type { Accessor } from 'solid-js'\nimport type { AnyFunction } from '@tanstack/pacer/types'\nimport type {\n ThrottlerOptions,\n ThrottlerState,\n} from '@tanstack/pacer/throttler'\n\nexport interface SolidThrottler
|
|
1
|
+
{"version":3,"file":"createThrottler.js","sources":["../../../src/throttler/createThrottler.ts"],"sourcesContent":["import { Throttler } from '@tanstack/pacer/throttler'\nimport { createEffect, onCleanup } from 'solid-js'\nimport { useStore } from '@tanstack/solid-store'\nimport type { Store } from '@tanstack/solid-store'\nimport type { Accessor } from 'solid-js'\nimport type { AnyFunction } from '@tanstack/pacer/types'\nimport type {\n ThrottlerOptions,\n ThrottlerState,\n} from '@tanstack/pacer/throttler'\n\nexport interface SolidThrottler<TFn extends AnyFunction, TSelected = {}>\n extends Omit<Throttler<TFn>, 'store'> {\n /**\n * Reactive state that will be updated when the throttler state changes\n *\n * Use this instead of `throttler.store.state`\n */\n readonly state: Accessor<Readonly<TSelected>>\n /**\n * @deprecated Use `throttler.state` instead of `throttler.store.state` if you want to read reactive state.\n * The state on the store object is not reactive, as it has not been wrapped in a `useStore` hook internally.\n * Although, you can make the state reactive by using the `useStore` in your own usage.\n */\n readonly store: Store<Readonly<ThrottlerState<TFn>>>\n}\n\n/**\n * A low-level Solid hook that creates a `Throttler` instance that limits how often the provided function can execute.\n *\n * This hook is designed to be flexible and state-management agnostic - it simply returns a throttler instance that\n * you can integrate with any state management solution (createSignal, Redux, Zustand, Jotai, etc). For a simpler and higher-level hook that\n * integrates directly with Solid's createSignal, see createThrottledSignal.\n *\n * Throttling ensures a function executes at most once within a specified time window,\n * regardless of how many times it is called. This is useful for rate-limiting\n * expensive operations or UI updates.\n *\n * ## State Management and Selector\n *\n * The hook uses TanStack Store for reactive state management. The `selector` parameter allows you\n * to specify which state changes will trigger a re-render, optimizing performance by preventing\n * unnecessary re-renders when irrelevant state changes occur.\n *\n * **By default, there will be no reactive state subscriptions** and you must opt-in to state\n * tracking by providing a selector function. This prevents unnecessary re-renders and gives you\n * full control over when your component updates. Only when you provide a selector will the\n * component re-render when the selected state values change.\n *\n * Available state properties:\n * - `canLeadingExecute`: Whether the throttler can execute on the leading edge\n * - `canTrailingExecute`: Whether the throttler can execute on the trailing edge\n * - `executionCount`: Number of function executions that have been completed\n * - `isPending`: Whether the throttler is waiting for the timeout to trigger execution\n * - `lastArgs`: The arguments from the most recent call to maybeExecute\n * - `lastExecutionTime`: Timestamp of the last execution\n * - `nextExecutionTime`: Timestamp of the next allowed execution\n * - `status`: Current execution status ('disabled' | 'idle' | 'pending')\n *\n * @example\n * ```tsx\n * // Default behavior - no reactive state subscriptions\n * const throttler = createThrottler(setValue, { wait: 1000 });\n *\n * // Opt-in to re-render when isPending changes (optimized for loading states)\n * const throttler = createThrottler(\n * setValue,\n * { wait: 1000 },\n * (state) => ({ isPending: state.isPending })\n * );\n *\n * // Opt-in to re-render when executionCount changes (optimized for tracking execution)\n * const throttler = createThrottler(\n * setValue,\n * { wait: 1000 },\n * (state) => ({ executionCount: state.executionCount })\n * );\n *\n * // Multiple state properties - re-render when any of these change\n * const throttler = createThrottler(\n * setValue,\n * {\n * wait: 2000,\n * leading: true, // Execute immediately on first call\n * trailing: false // Skip trailing edge updates\n * },\n * (state) => ({\n * isPending: state.isPending,\n * executionCount: state.executionCount,\n * lastExecutionTime: state.lastExecutionTime,\n * nextExecutionTime: state.nextExecutionTime\n * })\n * );\n *\n * // Access the selected state (will be empty object {} unless selector provided)\n * const { isPending, executionCount } = throttler.state();\n * ```\n */\nexport function createThrottler<TFn extends AnyFunction, TSelected = {}>(\n fn: TFn,\n initialOptions: ThrottlerOptions<TFn>,\n selector: (state: ThrottlerState<TFn>) => TSelected = () => ({}) as TSelected,\n): SolidThrottler<TFn, TSelected> {\n const asyncThrottler = new Throttler<TFn>(fn, initialOptions)\n\n const state = useStore(asyncThrottler.store, selector)\n\n createEffect(() => {\n onCleanup(() => {\n asyncThrottler.cancel()\n })\n })\n\n return {\n ...asyncThrottler,\n state,\n } as SolidThrottler<TFn, TSelected> // omit `store` in favor of `state`\n}\n"],"names":[],"mappings":";;;AAkGO,SAAS,gBACd,IACA,gBACA,WAAsD,OAAO,CAAA,IAC7B;AAChC,QAAM,iBAAiB,IAAI,UAAe,IAAI,cAAc;AAE5D,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;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/solid-pacer",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.11.0",
|
|
4
4
|
"description": "Utilities for debouncing and throttling functions in Solid.",
|
|
5
5
|
"author": "Tanner Linsley",
|
|
6
6
|
"license": "MIT",
|
|
@@ -163,7 +163,7 @@
|
|
|
163
163
|
],
|
|
164
164
|
"dependencies": {
|
|
165
165
|
"@tanstack/solid-store": "^0.7.3",
|
|
166
|
-
"@tanstack/pacer": "0.
|
|
166
|
+
"@tanstack/pacer": "0.11.0"
|
|
167
167
|
},
|
|
168
168
|
"devDependencies": {
|
|
169
169
|
"solid-js": "^1.9.7",
|
|
@@ -1,21 +1,26 @@
|
|
|
1
1
|
import { AsyncBatcher } from '@tanstack/pacer/async-batcher'
|
|
2
2
|
import { useStore } from '@tanstack/solid-store'
|
|
3
|
+
import type { Store } from '@tanstack/solid-store'
|
|
3
4
|
import type { Accessor } from 'solid-js'
|
|
4
5
|
import type {
|
|
5
6
|
AsyncBatcherOptions,
|
|
6
7
|
AsyncBatcherState,
|
|
7
8
|
} from '@tanstack/pacer/async-batcher'
|
|
8
9
|
|
|
9
|
-
export interface SolidAsyncBatcher<
|
|
10
|
-
TValue
|
|
11
|
-
TSelected = AsyncBatcherState<TValue>,
|
|
12
|
-
> extends Omit<AsyncBatcher<TValue>, 'store'> {
|
|
10
|
+
export interface SolidAsyncBatcher<TValue, TSelected = {}>
|
|
11
|
+
extends Omit<AsyncBatcher<TValue>, 'store'> {
|
|
13
12
|
/**
|
|
14
13
|
* Reactive state that will be updated when the batcher state changes
|
|
15
14
|
*
|
|
16
15
|
* Use this instead of `batcher.store.state`
|
|
17
16
|
*/
|
|
18
17
|
readonly state: Accessor<Readonly<TSelected>>
|
|
18
|
+
/**
|
|
19
|
+
* @deprecated Use `batcher.state` instead of `batcher.store.state` if you want to read reactive state.
|
|
20
|
+
* The state on the store object is not reactive, as it has not been wrapped in a `useStore` hook internally.
|
|
21
|
+
* Although, you can make the state reactive by using the `useStore` in your own usage.
|
|
22
|
+
*/
|
|
23
|
+
readonly store: Store<Readonly<AsyncBatcherState<TValue>>>
|
|
19
24
|
}
|
|
20
25
|
|
|
21
26
|
/**
|
|
@@ -48,9 +53,31 @@ export interface SolidAsyncBatcher<
|
|
|
48
53
|
* - Both onError and throwOnError can be used together; the handler will be called before any error is thrown
|
|
49
54
|
* - The error state can be checked using the underlying AsyncBatcher instance
|
|
50
55
|
*
|
|
56
|
+
* ## State Management and Selector
|
|
57
|
+
*
|
|
58
|
+
* The hook uses TanStack Store for reactive state management. The `selector` parameter allows you
|
|
59
|
+
* to specify which state changes will trigger a re-render, optimizing performance by preventing
|
|
60
|
+
* unnecessary re-renders when irrelevant state changes occur.
|
|
61
|
+
*
|
|
62
|
+
* **By default, there will be no reactive state subscriptions** and you must opt-in to state
|
|
63
|
+
* tracking by providing a selector function. This prevents unnecessary re-renders and gives you
|
|
64
|
+
* full control over when your component updates. Only when you provide a selector will the
|
|
65
|
+
* component re-render when the selected state values change.
|
|
66
|
+
*
|
|
67
|
+
* Available state properties:
|
|
68
|
+
* - `errorCount`: Number of failed batch executions
|
|
69
|
+
* - `executionCount`: Total number of batch execution attempts (successful + failed)
|
|
70
|
+
* - `hasError`: Whether the last batch execution resulted in an error
|
|
71
|
+
* - `isExecuting`: Whether a batch execution is currently in progress
|
|
72
|
+
* - `items`: Array of items currently queued for batching
|
|
73
|
+
* - `lastError`: The error from the most recent failed batch execution (if any)
|
|
74
|
+
* - `lastResult`: The result from the most recent successful batch execution
|
|
75
|
+
* - `settleCount`: Number of batch executions that have completed (successful or failed)
|
|
76
|
+
* - `successCount`: Number of successful batch executions
|
|
77
|
+
*
|
|
51
78
|
* Example usage:
|
|
52
79
|
* ```tsx
|
|
53
|
-
* //
|
|
80
|
+
* // Default behavior - no reactive state subscriptions
|
|
54
81
|
* const asyncBatcher = createAsyncBatcher(
|
|
55
82
|
* async (items) => {
|
|
56
83
|
* const results = await Promise.all(items.map(item => processItem(item)));
|
|
@@ -68,24 +95,41 @@ export interface SolidAsyncBatcher<
|
|
|
68
95
|
* }
|
|
69
96
|
* );
|
|
70
97
|
*
|
|
98
|
+
* // Opt-in to re-render when items or isExecuting changes (optimized for UI updates)
|
|
99
|
+
* const asyncBatcher = createAsyncBatcher(
|
|
100
|
+
* async (items) => {
|
|
101
|
+
* const results = await Promise.all(items.map(item => processItem(item)));
|
|
102
|
+
* return results;
|
|
103
|
+
* },
|
|
104
|
+
* { maxSize: 10, wait: 2000 },
|
|
105
|
+
* (state) => ({ items: state.items, isExecuting: state.isExecuting })
|
|
106
|
+
* );
|
|
107
|
+
*
|
|
108
|
+
* // Opt-in to re-render when error state changes (optimized for error handling)
|
|
109
|
+
* const asyncBatcher = createAsyncBatcher(
|
|
110
|
+
* async (items) => {
|
|
111
|
+
* const results = await Promise.all(items.map(item => processItem(item)));
|
|
112
|
+
* return results;
|
|
113
|
+
* },
|
|
114
|
+
* { maxSize: 10, wait: 2000 },
|
|
115
|
+
* (state) => ({ hasError: state.hasError, lastError: state.lastError })
|
|
116
|
+
* );
|
|
117
|
+
*
|
|
71
118
|
* // Add items to batch
|
|
72
119
|
* asyncBatcher.addItem(newItem);
|
|
73
120
|
*
|
|
74
121
|
* // Manually execute batch
|
|
75
122
|
* const result = await asyncBatcher.execute();
|
|
76
123
|
*
|
|
77
|
-
* //
|
|
78
|
-
* const items = asyncBatcher.state()
|
|
79
|
-
* const isExecuting = asyncBatcher.state().isExecuting;
|
|
124
|
+
* // Access the selected state (will be empty object {} unless selector provided)
|
|
125
|
+
* const { items, isExecuting } = asyncBatcher.state();
|
|
80
126
|
* ```
|
|
81
127
|
*/
|
|
82
|
-
export function createAsyncBatcher<
|
|
83
|
-
TValue,
|
|
84
|
-
TSelected = AsyncBatcherState<TValue>,
|
|
85
|
-
>(
|
|
128
|
+
export function createAsyncBatcher<TValue, TSelected = {}>(
|
|
86
129
|
fn: (items: Array<TValue>) => Promise<any>,
|
|
87
130
|
initialOptions: AsyncBatcherOptions<TValue> = {},
|
|
88
|
-
selector
|
|
131
|
+
selector: (state: AsyncBatcherState<TValue>) => TSelected = () =>
|
|
132
|
+
({}) as TSelected,
|
|
89
133
|
): SolidAsyncBatcher<TValue, TSelected> {
|
|
90
134
|
const asyncBatcher = new AsyncBatcher<TValue>(fn, initialOptions)
|
|
91
135
|
|
|
@@ -94,5 +138,5 @@ export function createAsyncBatcher<
|
|
|
94
138
|
return {
|
|
95
139
|
...asyncBatcher,
|
|
96
140
|
state,
|
|
97
|
-
} as
|
|
141
|
+
} as SolidAsyncBatcher<TValue, TSelected> // omit `store` in favor of `state`
|
|
98
142
|
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { AsyncDebouncer } from '@tanstack/pacer/async-debouncer'
|
|
2
2
|
import { useStore } from '@tanstack/solid-store'
|
|
3
3
|
import { createEffect, onCleanup } from 'solid-js'
|
|
4
|
+
import type { Store } from '@tanstack/solid-store'
|
|
4
5
|
import type { Accessor } from 'solid-js'
|
|
5
6
|
import type {
|
|
6
7
|
AsyncDebouncerOptions,
|
|
@@ -10,7 +11,7 @@ import type { AnyAsyncFunction } from '@tanstack/pacer/types'
|
|
|
10
11
|
|
|
11
12
|
export interface SolidAsyncDebouncer<
|
|
12
13
|
TFn extends AnyAsyncFunction,
|
|
13
|
-
TSelected =
|
|
14
|
+
TSelected = {},
|
|
14
15
|
> extends Omit<AsyncDebouncer<TFn>, 'store'> {
|
|
15
16
|
/**
|
|
16
17
|
* Reactive state that will be updated when the debouncer state changes
|
|
@@ -18,6 +19,12 @@ export interface SolidAsyncDebouncer<
|
|
|
18
19
|
* Use this instead of `debouncer.store.state`
|
|
19
20
|
*/
|
|
20
21
|
readonly state: Accessor<Readonly<TSelected>>
|
|
22
|
+
/**
|
|
23
|
+
* @deprecated Use `debouncer.state` instead of `debouncer.store.state` if you want to read reactive state.
|
|
24
|
+
* The state on the store object is not reactive, as it has not been wrapped in a `useStore` hook internally.
|
|
25
|
+
* Although, you can make the state reactive by using the `useStore` in your own usage.
|
|
26
|
+
*/
|
|
27
|
+
readonly store: Store<Readonly<AsyncDebouncerState<TFn>>>
|
|
21
28
|
}
|
|
22
29
|
|
|
23
30
|
/**
|
|
@@ -44,9 +51,31 @@ export interface SolidAsyncDebouncer<
|
|
|
44
51
|
* - Both onError and throwOnError can be used together - the handler will be called before any error is thrown
|
|
45
52
|
* - The error state can be checked using the underlying AsyncDebouncer instance
|
|
46
53
|
*
|
|
54
|
+
* ## State Management and Selector
|
|
55
|
+
*
|
|
56
|
+
* The hook uses TanStack Store for reactive state management. The `selector` parameter allows you
|
|
57
|
+
* to specify which state changes will trigger a re-render, optimizing performance by preventing
|
|
58
|
+
* unnecessary re-renders when irrelevant state changes occur.
|
|
59
|
+
*
|
|
60
|
+
* **By default, there will be no reactive state subscriptions** and you must opt-in to state
|
|
61
|
+
* tracking by providing a selector function. This prevents unnecessary re-renders and gives you
|
|
62
|
+
* full control over when your component updates. Only when you provide a selector will the
|
|
63
|
+
* component re-render when the selected state values change.
|
|
64
|
+
*
|
|
65
|
+
* Available state properties:
|
|
66
|
+
* - `canLeadingExecute`: Whether the debouncer can execute on the leading edge
|
|
67
|
+
* - `executionCount`: Number of function executions that have been completed
|
|
68
|
+
* - `hasError`: Whether the last execution resulted in an error
|
|
69
|
+
* - `isPending`: Whether the debouncer is waiting for the timeout to trigger execution
|
|
70
|
+
* - `isExecuting`: Whether an async function execution is currently in progress
|
|
71
|
+
* - `lastArgs`: The arguments from the most recent call to maybeExecute
|
|
72
|
+
* - `lastError`: The error from the most recent failed execution (if any)
|
|
73
|
+
* - `lastResult`: The result from the most recent successful execution
|
|
74
|
+
* - `status`: Current execution status ('disabled' | 'idle' | 'pending' | 'executing')
|
|
75
|
+
*
|
|
47
76
|
* @example
|
|
48
77
|
* ```tsx
|
|
49
|
-
* //
|
|
78
|
+
* // Default behavior - no reactive state subscriptions
|
|
50
79
|
* const { maybeExecute } = createAsyncDebouncer(
|
|
51
80
|
* async (query: string) => {
|
|
52
81
|
* const results = await api.search(query);
|
|
@@ -55,12 +84,21 @@ export interface SolidAsyncDebouncer<
|
|
|
55
84
|
* { wait: 500 }
|
|
56
85
|
* );
|
|
57
86
|
*
|
|
58
|
-
* //
|
|
59
|
-
* const
|
|
60
|
-
*
|
|
87
|
+
* // Opt-in to re-render when isPending or isExecuting changes (optimized for loading states)
|
|
88
|
+
* const debouncer = createAsyncDebouncer(
|
|
89
|
+
* async (query: string) => {
|
|
90
|
+
* const results = await api.search(query);
|
|
91
|
+
* return results;
|
|
92
|
+
* },
|
|
93
|
+
* { wait: 500 },
|
|
94
|
+
* (state) => ({ isPending: state.isPending, isExecuting: state.isExecuting })
|
|
95
|
+
* );
|
|
96
|
+
*
|
|
97
|
+
* // Opt-in to re-render when error state changes (optimized for error handling)
|
|
98
|
+
* const debouncer = createAsyncDebouncer(
|
|
61
99
|
* async (searchTerm) => {
|
|
62
100
|
* const data = await searchAPI(searchTerm);
|
|
63
|
-
*
|
|
101
|
+
* return data;
|
|
64
102
|
* },
|
|
65
103
|
* {
|
|
66
104
|
* wait: 300,
|
|
@@ -69,17 +107,22 @@ export interface SolidAsyncDebouncer<
|
|
|
69
107
|
* onError: (error) => {
|
|
70
108
|
* console.error('API call failed:', error);
|
|
71
109
|
* }
|
|
72
|
-
* }
|
|
110
|
+
* },
|
|
111
|
+
* (state) => ({ hasError: state.hasError, lastError: state.lastError })
|
|
73
112
|
* );
|
|
113
|
+
*
|
|
114
|
+
* // Access the selected state (will be empty object {} unless selector provided)
|
|
115
|
+
* const { isPending, isExecuting } = debouncer.state();
|
|
74
116
|
* ```
|
|
75
117
|
*/
|
|
76
118
|
export function createAsyncDebouncer<
|
|
77
119
|
TFn extends AnyAsyncFunction,
|
|
78
|
-
TSelected =
|
|
120
|
+
TSelected = {},
|
|
79
121
|
>(
|
|
80
122
|
fn: TFn,
|
|
81
123
|
initialOptions: AsyncDebouncerOptions<TFn>,
|
|
82
|
-
selector
|
|
124
|
+
selector: (state: AsyncDebouncerState<TFn>) => TSelected = () =>
|
|
125
|
+
({}) as TSelected,
|
|
83
126
|
): SolidAsyncDebouncer<TFn, TSelected> {
|
|
84
127
|
const asyncDebouncer = new AsyncDebouncer<TFn>(fn, initialOptions)
|
|
85
128
|
|
|
@@ -94,5 +137,5 @@ export function createAsyncDebouncer<
|
|
|
94
137
|
return {
|
|
95
138
|
...asyncDebouncer,
|
|
96
139
|
state,
|
|
97
|
-
} as
|
|
140
|
+
} as SolidAsyncDebouncer<TFn, TSelected> // omit `store` in favor of `state`
|
|
98
141
|
}
|
|
@@ -1,19 +1,26 @@
|
|
|
1
1
|
import { AsyncQueuer } from '@tanstack/pacer/async-queuer'
|
|
2
2
|
import { useStore } from '@tanstack/solid-store'
|
|
3
|
+
import type { Store } from '@tanstack/solid-store'
|
|
3
4
|
import type { Accessor } from 'solid-js'
|
|
4
5
|
import type {
|
|
5
6
|
AsyncQueuerOptions,
|
|
6
7
|
AsyncQueuerState,
|
|
7
8
|
} from '@tanstack/pacer/async-queuer'
|
|
8
9
|
|
|
9
|
-
export interface SolidAsyncQueuer<TValue, TSelected =
|
|
10
|
+
export interface SolidAsyncQueuer<TValue, TSelected = {}>
|
|
10
11
|
extends Omit<AsyncQueuer<TValue>, 'store'> {
|
|
11
12
|
/**
|
|
12
|
-
* Reactive state that will be updated
|
|
13
|
+
* Reactive state that will be updated when the queuer state changes
|
|
13
14
|
*
|
|
14
15
|
* Use this instead of `queuer.store.state`
|
|
15
16
|
*/
|
|
16
17
|
readonly state: Accessor<Readonly<TSelected>>
|
|
18
|
+
/**
|
|
19
|
+
* @deprecated Use `queuer.state` instead of `queuer.store.state` if you want to read reactive state.
|
|
20
|
+
* The state on the store object is not reactive, as it has not been wrapped in a `useStore` hook internally.
|
|
21
|
+
* Although, you can make the state reactive by using the `useStore` in your own usage.
|
|
22
|
+
*/
|
|
23
|
+
readonly store: Store<Readonly<AsyncQueuerState<TValue>>>
|
|
17
24
|
}
|
|
18
25
|
|
|
19
26
|
/**
|
|
@@ -39,9 +46,29 @@ export interface SolidAsyncQueuer<TValue, TSelected = AsyncQueuerState<TValue>>
|
|
|
39
46
|
* - Both onError and throwOnError can be used together; the handler will be called before any error is thrown
|
|
40
47
|
* - The error state can be checked using the underlying AsyncQueuer instance
|
|
41
48
|
*
|
|
49
|
+
* ## State Management and Selector
|
|
50
|
+
*
|
|
51
|
+
* The hook uses TanStack Store for reactive state management. The `selector` parameter allows you
|
|
52
|
+
* to specify which state changes will trigger a re-render, optimizing performance by preventing
|
|
53
|
+
* unnecessary re-renders when irrelevant state changes occur.
|
|
54
|
+
*
|
|
55
|
+
* **By default, there will be no reactive state subscriptions** and you must opt-in to state
|
|
56
|
+
* tracking by providing a selector function. This prevents unnecessary re-renders and gives you
|
|
57
|
+
* full control over when your component updates. Only when you provide a selector will the
|
|
58
|
+
* component re-render when the selected state values change.
|
|
59
|
+
*
|
|
60
|
+
* Available state properties:
|
|
61
|
+
* - `activeItems`: Array of items currently being processed
|
|
62
|
+
* - `errorCount`: Number of items that failed processing
|
|
63
|
+
* - `isRunning`: Whether the queuer is currently running (not stopped)
|
|
64
|
+
* - `pendingItems`: Array of items waiting to be processed
|
|
65
|
+
* - `rejectionCount`: Number of items that were rejected (expired or failed validation)
|
|
66
|
+
* - `settleCount`: Number of items that have completed processing (successful or failed)
|
|
67
|
+
* - `successCount`: Number of items that were processed successfully
|
|
68
|
+
*
|
|
42
69
|
* Example usage:
|
|
43
70
|
* ```tsx
|
|
44
|
-
* //
|
|
71
|
+
* // Default behavior - no reactive state subscriptions
|
|
45
72
|
* const asyncQueuer = createAsyncQueuer(async (item) => {
|
|
46
73
|
* // process item
|
|
47
74
|
* return await fetchData(item);
|
|
@@ -58,20 +85,43 @@ export interface SolidAsyncQueuer<TValue, TSelected = AsyncQueuerState<TValue>>
|
|
|
58
85
|
* }
|
|
59
86
|
* });
|
|
60
87
|
*
|
|
88
|
+
* // Opt-in to re-render when queue state changes (optimized for UI updates)
|
|
89
|
+
* const asyncQueuer = createAsyncQueuer(
|
|
90
|
+
* async (item) => await fetchData(item),
|
|
91
|
+
* { concurrency: 2, started: true },
|
|
92
|
+
* (state) => ({
|
|
93
|
+
* pendingItems: state.pendingItems,
|
|
94
|
+
* activeItems: state.activeItems,
|
|
95
|
+
* isRunning: state.isRunning
|
|
96
|
+
* })
|
|
97
|
+
* );
|
|
98
|
+
*
|
|
99
|
+
* // Opt-in to re-render when processing metrics change (optimized for tracking progress)
|
|
100
|
+
* const asyncQueuer = createAsyncQueuer(
|
|
101
|
+
* async (item) => await fetchData(item),
|
|
102
|
+
* { concurrency: 2, started: true },
|
|
103
|
+
* (state) => ({
|
|
104
|
+
* successCount: state.successCount,
|
|
105
|
+
* errorCount: state.errorCount,
|
|
106
|
+
* settleCount: state.settleCount
|
|
107
|
+
* })
|
|
108
|
+
* );
|
|
109
|
+
*
|
|
61
110
|
* // Add items to queue
|
|
62
111
|
* asyncQueuer.addItem(newItem);
|
|
63
112
|
*
|
|
64
113
|
* // Start processing
|
|
65
114
|
* asyncQueuer.start();
|
|
66
115
|
*
|
|
67
|
-
* //
|
|
68
|
-
* const
|
|
116
|
+
* // Access the selected state (will be empty object {} unless selector provided)
|
|
117
|
+
* const { pendingItems, activeItems } = asyncQueuer.state();
|
|
69
118
|
* ```
|
|
70
119
|
*/
|
|
71
|
-
export function createAsyncQueuer<TValue, TSelected =
|
|
120
|
+
export function createAsyncQueuer<TValue, TSelected = {}>(
|
|
72
121
|
fn: (value: TValue) => Promise<any>,
|
|
73
122
|
initialOptions: AsyncQueuerOptions<TValue> = {},
|
|
74
|
-
selector
|
|
123
|
+
selector: (state: AsyncQueuerState<TValue>) => TSelected = () =>
|
|
124
|
+
({}) as TSelected,
|
|
75
125
|
): SolidAsyncQueuer<TValue, TSelected> {
|
|
76
126
|
const asyncQueuer = new AsyncQueuer<TValue>(fn, initialOptions)
|
|
77
127
|
|
|
@@ -80,5 +130,5 @@ export function createAsyncQueuer<TValue, TSelected = AsyncQueuerState<TValue>>(
|
|
|
80
130
|
return {
|
|
81
131
|
...asyncQueuer,
|
|
82
132
|
state,
|
|
83
|
-
} as
|
|
133
|
+
} as SolidAsyncQueuer<TValue, TSelected> // omit `store` in favor of `state`
|
|
84
134
|
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { AsyncRateLimiter } from '@tanstack/pacer/async-rate-limiter'
|
|
2
2
|
import { useStore } from '@tanstack/solid-store'
|
|
3
|
+
import type { Store } from '@tanstack/solid-store'
|
|
3
4
|
import type { Accessor } from 'solid-js'
|
|
4
5
|
import type { AnyAsyncFunction } from '@tanstack/pacer/types'
|
|
5
6
|
import type {
|
|
@@ -9,9 +10,20 @@ import type {
|
|
|
9
10
|
|
|
10
11
|
export interface SolidAsyncRateLimiter<
|
|
11
12
|
TFn extends AnyAsyncFunction,
|
|
12
|
-
TSelected =
|
|
13
|
+
TSelected = {},
|
|
13
14
|
> extends Omit<AsyncRateLimiter<TFn>, 'store'> {
|
|
15
|
+
/**
|
|
16
|
+
* Reactive state that will be updated when the rate limiter state changes
|
|
17
|
+
*
|
|
18
|
+
* Use this instead of `rateLimiter.store.state`
|
|
19
|
+
*/
|
|
14
20
|
readonly state: Accessor<Readonly<TSelected>>
|
|
21
|
+
/**
|
|
22
|
+
* @deprecated Use `rateLimiter.state` instead of `rateLimiter.store.state` if you want to read reactive state.
|
|
23
|
+
* The state on the store object is not reactive, as it has not been wrapped in a `useStore` hook internally.
|
|
24
|
+
* Although, you can make the state reactive by using the `useStore` in your own usage.
|
|
25
|
+
*/
|
|
26
|
+
readonly store: Store<Readonly<AsyncRateLimiterState<TFn>>>
|
|
15
27
|
}
|
|
16
28
|
|
|
17
29
|
/**
|
|
@@ -49,9 +61,31 @@ export interface SolidAsyncRateLimiter<
|
|
|
49
61
|
* - The error state can be checked using the underlying AsyncRateLimiter instance
|
|
50
62
|
* - Rate limit rejections (when limit is exceeded) are handled separately from execution errors via the `onReject` handler
|
|
51
63
|
*
|
|
64
|
+
* ## State Management and Selector
|
|
65
|
+
*
|
|
66
|
+
* The hook uses TanStack Store for reactive state management. The `selector` parameter allows you
|
|
67
|
+
* to specify which state changes will trigger a re-render, optimizing performance by preventing
|
|
68
|
+
* unnecessary re-renders when irrelevant state changes occur.
|
|
69
|
+
*
|
|
70
|
+
* **By default, there will be no reactive state subscriptions** and you must opt-in to state
|
|
71
|
+
* tracking by providing a selector function. This prevents unnecessary re-renders and gives you
|
|
72
|
+
* full control over when your component updates. Only when you provide a selector will the
|
|
73
|
+
* component re-render when the selected state values change.
|
|
74
|
+
*
|
|
75
|
+
* Available state properties:
|
|
76
|
+
* - `currentWindowStart`: Timestamp when the current window started
|
|
77
|
+
* - `executionCount`: Number of function executions that have been completed
|
|
78
|
+
* - `hasError`: Whether the last execution resulted in an error
|
|
79
|
+
* - `isExecuting`: Whether an async function execution is currently in progress
|
|
80
|
+
* - `lastError`: The error from the most recent failed execution (if any)
|
|
81
|
+
* - `lastResult`: The result from the most recent successful execution
|
|
82
|
+
* - `nextWindowTime`: Timestamp when the next window begins
|
|
83
|
+
* - `rejectionCount`: Number of function calls that were rejected due to rate limiting
|
|
84
|
+
* - `remainingInWindow`: Number of executions remaining in the current window
|
|
85
|
+
*
|
|
52
86
|
* @example
|
|
53
87
|
* ```tsx
|
|
54
|
-
* //
|
|
88
|
+
* // Default behavior - no reactive state subscriptions
|
|
55
89
|
* const { maybeExecute } = createAsyncRateLimiter(
|
|
56
90
|
* async (id: string) => {
|
|
57
91
|
* const data = await api.fetchData(id);
|
|
@@ -60,29 +94,46 @@ export interface SolidAsyncRateLimiter<
|
|
|
60
94
|
* { limit: 5, window: 1000 } // 5 calls per second
|
|
61
95
|
* );
|
|
62
96
|
*
|
|
63
|
-
* //
|
|
64
|
-
* const
|
|
65
|
-
*
|
|
97
|
+
* // Opt-in to re-render when rate limit and execution state changes (optimized for UI feedback)
|
|
98
|
+
* const rateLimiter = createAsyncRateLimiter(
|
|
99
|
+
* async (query) => {
|
|
100
|
+
* const result = await searchAPI(query);
|
|
101
|
+
* return result;
|
|
102
|
+
* },
|
|
103
|
+
* { limit: 10, window: 60000 },
|
|
104
|
+
* (state) => ({
|
|
105
|
+
* remainingInWindow: state.remainingInWindow,
|
|
106
|
+
* isExecuting: state.isExecuting,
|
|
107
|
+
* rejectionCount: state.rejectionCount
|
|
108
|
+
* })
|
|
109
|
+
* );
|
|
110
|
+
*
|
|
111
|
+
* // Opt-in to re-render when error state changes (optimized for error handling)
|
|
112
|
+
* const rateLimiter = createAsyncRateLimiter(
|
|
66
113
|
* async (query) => {
|
|
67
114
|
* const result = await searchAPI(query);
|
|
68
|
-
*
|
|
69
|
-
* return result; // Return value can be used by the caller
|
|
115
|
+
* return result;
|
|
70
116
|
* },
|
|
71
117
|
* {
|
|
72
118
|
* limit: 10,
|
|
73
119
|
* window: 60000, // 10 calls per minute
|
|
74
120
|
* onReject: (info) => console.log(`Rate limit exceeded: ${info.nextValidTime - Date.now()}ms until next window`)
|
|
75
|
-
* }
|
|
121
|
+
* },
|
|
122
|
+
* (state) => ({ hasError: state.hasError, lastError: state.lastError })
|
|
76
123
|
* );
|
|
124
|
+
*
|
|
125
|
+
* // Access the selected state (will be empty object {} unless selector provided)
|
|
126
|
+
* const { remainingInWindow, isExecuting } = rateLimiter.state();
|
|
77
127
|
* ```
|
|
78
128
|
*/
|
|
79
129
|
export function createAsyncRateLimiter<
|
|
80
130
|
TFn extends AnyAsyncFunction,
|
|
81
|
-
TSelected =
|
|
131
|
+
TSelected = {},
|
|
82
132
|
>(
|
|
83
133
|
fn: TFn,
|
|
84
134
|
initialOptions: AsyncRateLimiterOptions<TFn>,
|
|
85
|
-
selector
|
|
135
|
+
selector: (state: AsyncRateLimiterState<TFn>) => TSelected = () =>
|
|
136
|
+
({}) as TSelected,
|
|
86
137
|
): SolidAsyncRateLimiter<TFn, TSelected> {
|
|
87
138
|
const asyncRateLimiter = new AsyncRateLimiter<TFn>(fn, initialOptions)
|
|
88
139
|
|