@tanstack/solid-pacer 0.17.0 → 0.18.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/async-batcher/createAsyncBatcher.cjs +26 -12
- package/dist/async-batcher/createAsyncBatcher.cjs.map +1 -1
- package/dist/async-batcher/createAsyncBatcher.d.cts +36 -9
- package/dist/async-batcher/createAsyncBatcher.d.ts +36 -9
- package/dist/async-batcher/createAsyncBatcher.js +22 -8
- package/dist/async-batcher/createAsyncBatcher.js.map +1 -1
- package/dist/async-batcher/index.cjs +3 -3
- package/dist/async-debouncer/createAsyncDebouncer.cjs +26 -12
- package/dist/async-debouncer/createAsyncDebouncer.cjs.map +1 -1
- package/dist/async-debouncer/createAsyncDebouncer.d.cts +36 -9
- package/dist/async-debouncer/createAsyncDebouncer.d.ts +36 -9
- package/dist/async-debouncer/createAsyncDebouncer.js +22 -8
- package/dist/async-debouncer/createAsyncDebouncer.js.map +1 -1
- package/dist/async-debouncer/index.cjs +3 -3
- package/dist/async-queuer/createAsyncQueuer.cjs +26 -12
- package/dist/async-queuer/createAsyncQueuer.cjs.map +1 -1
- package/dist/async-queuer/createAsyncQueuer.d.cts +36 -9
- package/dist/async-queuer/createAsyncQueuer.d.ts +36 -9
- package/dist/async-queuer/createAsyncQueuer.js +22 -8
- package/dist/async-queuer/createAsyncQueuer.js.map +1 -1
- package/dist/async-queuer/index.cjs +3 -3
- package/dist/async-rate-limiter/createAsyncRateLimiter.cjs +89 -30
- package/dist/async-rate-limiter/createAsyncRateLimiter.cjs.map +1 -1
- package/dist/async-rate-limiter/createAsyncRateLimiter.d.cts +99 -27
- package/dist/async-rate-limiter/createAsyncRateLimiter.d.ts +99 -27
- package/dist/async-rate-limiter/createAsyncRateLimiter.js +85 -26
- package/dist/async-rate-limiter/createAsyncRateLimiter.js.map +1 -1
- package/dist/async-rate-limiter/index.cjs +3 -3
- package/dist/async-throttler/createAsyncThrottler.cjs +26 -12
- package/dist/async-throttler/createAsyncThrottler.cjs.map +1 -1
- package/dist/async-throttler/createAsyncThrottler.d.cts +36 -9
- package/dist/async-throttler/createAsyncThrottler.d.ts +36 -9
- package/dist/async-throttler/createAsyncThrottler.js +22 -8
- package/dist/async-throttler/createAsyncThrottler.js.map +1 -1
- package/dist/async-throttler/index.cjs +3 -3
- package/dist/batcher/createBatcher.cjs +26 -12
- package/dist/batcher/createBatcher.cjs.map +1 -1
- package/dist/batcher/createBatcher.d.cts +36 -9
- package/dist/batcher/createBatcher.d.ts +36 -9
- package/dist/batcher/createBatcher.js +22 -8
- package/dist/batcher/createBatcher.js.map +1 -1
- package/dist/batcher/index.cjs +3 -3
- package/dist/debouncer/createDebouncer.cjs +27 -13
- package/dist/debouncer/createDebouncer.cjs.map +1 -1
- package/dist/debouncer/createDebouncer.d.cts +37 -10
- package/dist/debouncer/createDebouncer.d.ts +37 -10
- package/dist/debouncer/createDebouncer.js +23 -9
- package/dist/debouncer/createDebouncer.js.map +1 -1
- package/dist/debouncer/index.cjs +3 -3
- package/dist/index.cjs +3 -3
- package/dist/queuer/createQueuedSignal.cjs +8 -8
- package/dist/queuer/createQueuedSignal.cjs.map +1 -1
- package/dist/queuer/createQueuedSignal.d.cts +8 -8
- package/dist/queuer/createQueuedSignal.d.ts +8 -8
- package/dist/queuer/createQueuedSignal.js +8 -8
- package/dist/queuer/createQueuedSignal.js.map +1 -1
- package/dist/queuer/createQueuer.cjs +26 -12
- package/dist/queuer/createQueuer.cjs.map +1 -1
- package/dist/queuer/createQueuer.d.cts +36 -9
- package/dist/queuer/createQueuer.d.ts +36 -9
- package/dist/queuer/createQueuer.js +22 -8
- package/dist/queuer/createQueuer.js.map +1 -1
- package/dist/queuer/index.cjs +3 -3
- package/dist/rate-limiter/createRateLimiter.cjs +78 -23
- package/dist/rate-limiter/createRateLimiter.cjs.map +1 -1
- package/dist/rate-limiter/createRateLimiter.d.cts +88 -20
- package/dist/rate-limiter/createRateLimiter.d.ts +88 -20
- package/dist/rate-limiter/createRateLimiter.js +74 -19
- package/dist/rate-limiter/createRateLimiter.js.map +1 -1
- package/dist/rate-limiter/index.cjs +3 -3
- package/dist/throttler/createThrottledSignal.cjs +1 -1
- package/dist/throttler/createThrottledSignal.cjs.map +1 -1
- package/dist/throttler/createThrottledSignal.d.cts +1 -1
- package/dist/throttler/createThrottledSignal.d.ts +1 -1
- package/dist/throttler/createThrottledSignal.js +1 -1
- package/dist/throttler/createThrottledSignal.js.map +1 -1
- package/dist/throttler/createThrottledValue.cjs +1 -1
- package/dist/throttler/createThrottledValue.cjs.map +1 -1
- package/dist/throttler/createThrottledValue.d.cts +1 -1
- package/dist/throttler/createThrottledValue.d.ts +1 -1
- package/dist/throttler/createThrottledValue.js +1 -1
- package/dist/throttler/createThrottledValue.js.map +1 -1
- package/dist/throttler/createThrottler.cjs +34 -13
- package/dist/throttler/createThrottler.cjs.map +1 -1
- package/dist/throttler/createThrottler.d.cts +44 -10
- package/dist/throttler/createThrottler.d.ts +44 -10
- package/dist/throttler/createThrottler.js +30 -9
- package/dist/throttler/createThrottler.js.map +1 -1
- package/dist/throttler/index.cjs +3 -3
- package/dist/types/index.cjs +3 -3
- package/dist/utils/index.cjs +3 -3
- package/package.json +1 -1
- package/src/async-batcher/createAsyncBatcher.ts +51 -10
- package/src/async-debouncer/createAsyncDebouncer.ts +51 -10
- package/src/async-queuer/createAsyncQueuer.ts +51 -10
- package/src/async-rate-limiter/createAsyncRateLimiter.ts +114 -28
- package/src/async-throttler/createAsyncThrottler.ts +51 -10
- package/src/batcher/createBatcher.ts +51 -10
- package/src/debouncer/createDebouncer.ts +52 -11
- package/src/queuer/createQueuedSignal.ts +8 -8
- package/src/queuer/createQueuer.ts +51 -10
- package/src/rate-limiter/createRateLimiter.ts +103 -21
- package/src/throttler/createThrottledSignal.ts +1 -1
- package/src/throttler/createThrottledValue.ts +1 -1
- package/src/throttler/createThrottler.ts +59 -11
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
const require_PacerProvider = require('../provider/PacerProvider.cjs');
|
|
2
|
-
let
|
|
3
|
-
let
|
|
2
|
+
let _tanstack_solid_store = require("@tanstack/solid-store");
|
|
3
|
+
let _tanstack_pacer_async_rate_limiter = require("@tanstack/pacer/async-rate-limiter");
|
|
4
4
|
|
|
5
5
|
//#region src/async-rate-limiter/createAsyncRateLimiter.ts
|
|
6
6
|
/**
|
|
@@ -40,14 +40,24 @@ let __tanstack_pacer_async_rate_limiter = require("@tanstack/pacer/async-rate-li
|
|
|
40
40
|
*
|
|
41
41
|
* ## State Management and Selector
|
|
42
42
|
*
|
|
43
|
-
* The hook uses TanStack Store for reactive state management.
|
|
44
|
-
*
|
|
45
|
-
*
|
|
43
|
+
* The hook uses TanStack Store for reactive state management. You can subscribe to state changes
|
|
44
|
+
* in two ways:
|
|
45
|
+
*
|
|
46
|
+
* **1. Using `rateLimiter.Subscribe` component (Recommended for component tree subscriptions)**
|
|
47
|
+
*
|
|
48
|
+
* Use the `Subscribe` component to subscribe to state changes deep in your component tree without
|
|
49
|
+
* needing to pass a selector to the hook. This is ideal when you want to subscribe to state
|
|
50
|
+
* in child components.
|
|
51
|
+
*
|
|
52
|
+
* **2. Using the `selector` parameter (For hook-level subscriptions)**
|
|
53
|
+
*
|
|
54
|
+
* The `selector` parameter allows you to specify which state changes will trigger reactive updates
|
|
55
|
+
* at the hook level, optimizing performance by preventing unnecessary updates when irrelevant
|
|
56
|
+
* state changes occur.
|
|
46
57
|
*
|
|
47
58
|
* **By default, there will be no reactive state subscriptions** and you must opt-in to state
|
|
48
|
-
* tracking by providing a selector function. This prevents unnecessary
|
|
49
|
-
* full control over when your component
|
|
50
|
-
* component re-render when the selected state values change.
|
|
59
|
+
* tracking by providing a selector function or using the `Subscribe` component. This prevents unnecessary
|
|
60
|
+
* updates and gives you full control over when your component tracks state changes.
|
|
51
61
|
*
|
|
52
62
|
* Available state properties:
|
|
53
63
|
* - `currentWindowStart`: Timestamp when the current window started
|
|
@@ -63,7 +73,7 @@ let __tanstack_pacer_async_rate_limiter = require("@tanstack/pacer/async-rate-li
|
|
|
63
73
|
* @example
|
|
64
74
|
* ```tsx
|
|
65
75
|
* // Default behavior - no reactive state subscriptions
|
|
66
|
-
* const
|
|
76
|
+
* const asyncRateLimiter = createAsyncRateLimiter(
|
|
67
77
|
* async (id: string) => {
|
|
68
78
|
* const data = await api.fetchData(id);
|
|
69
79
|
* return data; // Return value is preserved
|
|
@@ -71,44 +81,93 @@ let __tanstack_pacer_async_rate_limiter = require("@tanstack/pacer/async-rate-li
|
|
|
71
81
|
* { limit: 5, window: 1000 } // 5 calls per second
|
|
72
82
|
* );
|
|
73
83
|
*
|
|
74
|
-
* //
|
|
75
|
-
*
|
|
76
|
-
*
|
|
77
|
-
*
|
|
78
|
-
*
|
|
84
|
+
* // Subscribe to state changes deep in component tree using Subscribe component
|
|
85
|
+
* <asyncRateLimiter.Subscribe selector={(state) => ({ rejectionCount: state.rejectionCount, isExecuting: state.isExecuting })}>
|
|
86
|
+
* {(state) => (
|
|
87
|
+
* <div>Rejected: {state().rejectionCount}, {state().isExecuting ? 'Executing' : 'Idle'}</div>
|
|
88
|
+
* )}
|
|
89
|
+
* </asyncRateLimiter.Subscribe>
|
|
90
|
+
*
|
|
91
|
+
* // Opt-in to track execution state changes at hook level (optimized for loading indicators)
|
|
92
|
+
* const asyncRateLimiter = createAsyncRateLimiter(
|
|
93
|
+
* async (id: string) => {
|
|
94
|
+
* const data = await api.fetchData(id);
|
|
95
|
+
* return data;
|
|
96
|
+
* },
|
|
97
|
+
* { limit: 5, window: 1000 },
|
|
98
|
+
* (state) => ({ isExecuting: state.isExecuting })
|
|
99
|
+
* );
|
|
100
|
+
*
|
|
101
|
+
* // Opt-in to track results when available (optimized for data display)
|
|
102
|
+
* const asyncRateLimiter = createAsyncRateLimiter(
|
|
103
|
+
* async (id: string) => {
|
|
104
|
+
* const data = await api.fetchData(id);
|
|
105
|
+
* return data;
|
|
79
106
|
* },
|
|
80
|
-
* { limit:
|
|
107
|
+
* { limit: 5, window: 1000 },
|
|
81
108
|
* (state) => ({
|
|
82
|
-
*
|
|
83
|
-
*
|
|
84
|
-
* rejectionCount: state.rejectionCount
|
|
109
|
+
* lastResult: state.lastResult,
|
|
110
|
+
* successCount: state.successCount
|
|
85
111
|
* })
|
|
86
112
|
* );
|
|
87
113
|
*
|
|
88
|
-
* // Opt-in to
|
|
89
|
-
* const
|
|
90
|
-
* async (
|
|
91
|
-
* const
|
|
92
|
-
* return
|
|
114
|
+
* // Opt-in to track error/rejection state changes (optimized for error handling)
|
|
115
|
+
* const asyncRateLimiter = createAsyncRateLimiter(
|
|
116
|
+
* async (id: string) => {
|
|
117
|
+
* const data = await api.fetchData(id);
|
|
118
|
+
* return data;
|
|
93
119
|
* },
|
|
94
120
|
* {
|
|
95
|
-
* limit:
|
|
96
|
-
* window:
|
|
97
|
-
*
|
|
121
|
+
* limit: 5,
|
|
122
|
+
* window: 1000,
|
|
123
|
+
* onError: (error) => console.error('API call failed:', error),
|
|
124
|
+
* onReject: (rateLimiter) => console.log('Rate limit exceeded')
|
|
125
|
+
* },
|
|
126
|
+
* (state) => ({
|
|
127
|
+
* errorCount: state.errorCount,
|
|
128
|
+
* rejectionCount: state.rejectionCount
|
|
129
|
+
* })
|
|
130
|
+
* );
|
|
131
|
+
*
|
|
132
|
+
* // Opt-in to track execution metrics changes (optimized for stats display)
|
|
133
|
+
* const asyncRateLimiter = createAsyncRateLimiter(
|
|
134
|
+
* async (id: string) => {
|
|
135
|
+
* const data = await api.fetchData(id);
|
|
136
|
+
* return data;
|
|
137
|
+
* },
|
|
138
|
+
* { limit: 5, window: 1000 },
|
|
139
|
+
* (state) => ({
|
|
140
|
+
* successCount: state.successCount,
|
|
141
|
+
* errorCount: state.errorCount,
|
|
142
|
+
* settleCount: state.settleCount,
|
|
143
|
+
* rejectionCount: state.rejectionCount
|
|
144
|
+
* })
|
|
145
|
+
* );
|
|
146
|
+
*
|
|
147
|
+
* // Opt-in to track execution times changes (optimized for window calculations)
|
|
148
|
+
* const asyncRateLimiter = createAsyncRateLimiter(
|
|
149
|
+
* async (id: string) => {
|
|
150
|
+
* const data = await api.fetchData(id);
|
|
151
|
+
* return data;
|
|
98
152
|
* },
|
|
99
|
-
*
|
|
153
|
+
* { limit: 5, window: 1000 },
|
|
154
|
+
* (state) => ({ executionTimes: state.executionTimes })
|
|
100
155
|
* );
|
|
101
156
|
*
|
|
102
157
|
* // Access the selected state (will be empty object {} unless selector provided)
|
|
103
|
-
* const {
|
|
158
|
+
* const { isExecuting, lastResult, rejectionCount } = asyncRateLimiter.state();
|
|
104
159
|
* ```
|
|
105
160
|
*/
|
|
106
161
|
function createAsyncRateLimiter(fn, options, selector = () => ({})) {
|
|
107
|
-
const asyncRateLimiter = new
|
|
162
|
+
const asyncRateLimiter = new _tanstack_pacer_async_rate_limiter.AsyncRateLimiter(fn, {
|
|
108
163
|
...require_PacerProvider.useDefaultPacerOptions().asyncRateLimiter,
|
|
109
164
|
...options
|
|
110
165
|
});
|
|
111
|
-
|
|
166
|
+
asyncRateLimiter.Subscribe = function Subscribe(props) {
|
|
167
|
+
const selected = (0, _tanstack_solid_store.useStore)(asyncRateLimiter.store, props.selector);
|
|
168
|
+
return typeof props.children === "function" ? props.children(selected) : props.children;
|
|
169
|
+
};
|
|
170
|
+
const state = (0, _tanstack_solid_store.useStore)(asyncRateLimiter.store, selector);
|
|
112
171
|
return {
|
|
113
172
|
...asyncRateLimiter,
|
|
114
173
|
state
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createAsyncRateLimiter.cjs","names":["AsyncRateLimiter","useDefaultPacerOptions"],"sources":["../../src/async-rate-limiter/createAsyncRateLimiter.ts"],"sourcesContent":["import { AsyncRateLimiter } from '@tanstack/pacer/async-rate-limiter'\nimport { useStore } from '@tanstack/solid-store'\nimport { useDefaultPacerOptions } from '../provider/PacerProvider'\nimport type { Store } from '@tanstack/solid-store'\nimport type { Accessor } from 'solid-js'\nimport type { AnyAsyncFunction } from '@tanstack/pacer/types'\nimport type {\n AsyncRateLimiterOptions,\n AsyncRateLimiterState,\n} from '@tanstack/pacer/async-rate-limiter'\n\nexport interface SolidAsyncRateLimiter<\n TFn extends AnyAsyncFunction,\n TSelected = {},\n> extends Omit<AsyncRateLimiter<TFn>, 'store'> {\n /**\n * Reactive state that will be updated when the rate limiter state changes\n *\n * Use this instead of `rateLimiter.store.state`\n */\n readonly state: Accessor<Readonly<TSelected>>\n /**\n * @deprecated Use `rateLimiter.state` instead of `rateLimiter.store.state` if you want to read reactive state.\n * The state on the store object is not reactive, as it has not been wrapped in a `useStore` hook internally.\n * Although, you can make the state reactive by using the `useStore` in your own usage.\n */\n readonly store: Store<Readonly<AsyncRateLimiterState<TFn>>>\n}\n\n/**\n * A low-level Solid hook that creates an `AsyncRateLimiter` instance to limit how many times an async function can execute within a time window.\n *\n * This hook is designed to be flexible and state-management agnostic - it simply returns a rate limiter instance that\n * you can integrate with any state management solution (createSignal, etc).\n *\n * Rate limiting is a simple approach that allows a function to execute up to a limit within a time window,\n * then blocks all subsequent calls until the window passes. This can lead to \"bursty\" behavior where\n * all executions happen immediately, followed by a complete block.\n *\n * The rate limiter supports two types of windows:\n * - 'fixed': A strict window that resets after the window period. All executions within the window count\n * towards the limit, and the window resets completely after the period.\n * - 'sliding': A rolling window that allows executions as old ones expire. This provides a more\n * consistent rate of execution over time.\n *\n * Unlike the non-async RateLimiter, this async version supports returning values from the rate-limited function,\n * making it ideal for API calls and other async operations where you want the result of the `maybeExecute` call\n * instead of setting the result on a state variable from within the rate-limited function.\n *\n * For smoother execution patterns, consider using:\n * - Throttling: Ensures consistent spacing between executions (e.g. max once per 200ms)\n * - Debouncing: Waits for a pause in calls before executing (e.g. after 500ms of no calls)\n *\n * Rate limiting is best used for hard API limits or resource constraints. For UI updates or\n * smoothing out frequent events, throttling or debouncing usually provide better user experience.\n *\n * Error Handling:\n * - If an `onError` handler is provided, it will be called with the error and rate limiter instance\n * - If `throwOnError` is true (default when no onError handler is provided), the error will be thrown\n * - If `throwOnError` is false (default when onError handler is provided), the error will be swallowed\n * - Both onError and throwOnError can be used together - the handler will be called before any error is thrown\n * - The error state can be checked using the underlying AsyncRateLimiter instance\n * - Rate limit rejections (when limit is exceeded) are handled separately from execution errors via the `onReject` handler\n *\n * ## State Management and Selector\n *\n * The hook uses TanStack Store for reactive state management. The `selector` parameter allows you\n * to specify which state changes will trigger a re-render, optimizing performance by preventing\n * unnecessary re-renders when irrelevant state changes occur.\n *\n * **By default, there will be no reactive state subscriptions** and you must opt-in to state\n * tracking by providing a selector function. This prevents unnecessary re-renders and gives you\n * full control over when your component updates. Only when you provide a selector will the\n * component re-render when the selected state values change.\n *\n * Available state properties:\n * - `currentWindowStart`: Timestamp when the current window started\n * - `executionCount`: Number of function executions that have been completed\n * - `hasError`: Whether the last execution resulted in an error\n * - `isExecuting`: Whether an async function execution is currently in progress\n * - `lastError`: The error from the most recent failed execution (if any)\n * - `lastResult`: The result from the most recent successful execution\n * - `nextWindowTime`: Timestamp when the next window begins\n * - `rejectionCount`: Number of function calls that were rejected due to rate limiting\n * - `remainingInWindow`: Number of executions remaining in the current window\n *\n * @example\n * ```tsx\n * // Default behavior - no reactive state subscriptions\n * const { maybeExecute } = createAsyncRateLimiter(\n * async (id: string) => {\n * const data = await api.fetchData(id);\n * return data; // Return value is preserved\n * },\n * { limit: 5, window: 1000 } // 5 calls per second\n * );\n *\n * // Opt-in to re-render when rate limit and execution state changes (optimized for UI feedback)\n * const rateLimiter = createAsyncRateLimiter(\n * async (query) => {\n * const result = await searchAPI(query);\n * return result;\n * },\n * { limit: 10, window: 60000 },\n * (state) => ({\n * remainingInWindow: state.remainingInWindow,\n * isExecuting: state.isExecuting,\n * rejectionCount: state.rejectionCount\n * })\n * );\n *\n * // Opt-in to re-render when error state changes (optimized for error handling)\n * const rateLimiter = createAsyncRateLimiter(\n * async (query) => {\n * const result = await searchAPI(query);\n * return result;\n * },\n * {\n * limit: 10,\n * window: 60000, // 10 calls per minute\n * onReject: (info) => console.log(`Rate limit exceeded: ${info.nextValidTime - Date.now()}ms until next window`)\n * },\n * (state) => ({ hasError: state.hasError, lastError: state.lastError })\n * );\n *\n * // Access the selected state (will be empty object {} unless selector provided)\n * const { remainingInWindow, isExecuting } = rateLimiter.state();\n * ```\n */\nexport function createAsyncRateLimiter<\n TFn extends AnyAsyncFunction,\n TSelected = {},\n>(\n fn: TFn,\n options: AsyncRateLimiterOptions<TFn>,\n selector: (state: AsyncRateLimiterState<TFn>) => TSelected = () =>\n ({}) as TSelected,\n): SolidAsyncRateLimiter<TFn, TSelected> {\n const mergedOptions = {\n ...useDefaultPacerOptions().asyncRateLimiter,\n ...options,\n } as AsyncRateLimiterOptions<TFn>\n\n const asyncRateLimiter = new AsyncRateLimiter<TFn>(fn, mergedOptions)\n\n const state = useStore(asyncRateLimiter.store, selector)\n\n return {\n ...asyncRateLimiter,\n state,\n } as SolidAsyncRateLimiter<TFn, TSelected>\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAiIA,SAAgB,uBAId,IACA,SACA,kBACG,EAAE,GACkC;CAMvC,MAAM,mBAAmB,IAAIA,qDAAsB,IAL7B;EACpB,GAAGC,8CAAwB,CAAC;EAC5B,GAAG;EACJ,CAEoE;CAErE,MAAM,6CAAiB,iBAAiB,OAAO,SAAS;AAExD,QAAO;EACL,GAAG;EACH;EACD"}
|
|
1
|
+
{"version":3,"file":"createAsyncRateLimiter.cjs","names":["AsyncRateLimiter","useDefaultPacerOptions"],"sources":["../../src/async-rate-limiter/createAsyncRateLimiter.ts"],"sourcesContent":["import { AsyncRateLimiter } from '@tanstack/pacer/async-rate-limiter'\nimport { useStore } from '@tanstack/solid-store'\nimport { useDefaultPacerOptions } from '../provider/PacerProvider'\nimport type { Store } from '@tanstack/solid-store'\nimport type { Accessor, JSX } from 'solid-js'\nimport type { AnyAsyncFunction } from '@tanstack/pacer/types'\nimport type {\n AsyncRateLimiterOptions,\n AsyncRateLimiterState,\n} from '@tanstack/pacer/async-rate-limiter'\n\nexport interface SolidAsyncRateLimiter<\n TFn extends AnyAsyncFunction,\n TSelected = {},\n> extends Omit<AsyncRateLimiter<TFn>, 'store'> {\n /**\n * A Solid component that allows you to subscribe to the rate limiter state.\n *\n * This is useful for tracking specific parts of the rate limiter state\n * deep in your component tree without needing to pass a selector to the hook.\n *\n * @example\n * <rateLimiter.Subscribe selector={(state) => ({ rejectionCount: state.rejectionCount, isExecuting: state.isExecuting })}>\n * {(state) => (\n * <div>Rejected: {state().rejectionCount}, {state().isExecuting ? 'Executing' : 'Idle'}</div>\n * )}\n * </rateLimiter.Subscribe>\n */\n Subscribe: <TSelected>(props: {\n selector: (state: AsyncRateLimiterState<TFn>) => TSelected\n children: ((state: Accessor<TSelected>) => JSX.Element) | JSX.Element\n }) => JSX.Element\n /**\n * Reactive state that will be updated when the rate limiter state changes\n *\n * Use this instead of `rateLimiter.store.state`\n */\n readonly state: Accessor<Readonly<TSelected>>\n /**\n * @deprecated Use `rateLimiter.state` instead of `rateLimiter.store.state` if you want to read reactive state.\n * The state on the store object is not reactive, as it has not been wrapped in a `useStore` hook internally.\n * Although, you can make the state reactive by using the `useStore` in your own usage.\n */\n readonly store: Store<Readonly<AsyncRateLimiterState<TFn>>>\n}\n\n/**\n * A low-level Solid hook that creates an `AsyncRateLimiter` instance to limit how many times an async function can execute within a time window.\n *\n * This hook is designed to be flexible and state-management agnostic - it simply returns a rate limiter instance that\n * you can integrate with any state management solution (createSignal, etc).\n *\n * Rate limiting is a simple approach that allows a function to execute up to a limit within a time window,\n * then blocks all subsequent calls until the window passes. This can lead to \"bursty\" behavior where\n * all executions happen immediately, followed by a complete block.\n *\n * The rate limiter supports two types of windows:\n * - 'fixed': A strict window that resets after the window period. All executions within the window count\n * towards the limit, and the window resets completely after the period.\n * - 'sliding': A rolling window that allows executions as old ones expire. This provides a more\n * consistent rate of execution over time.\n *\n * Unlike the non-async RateLimiter, this async version supports returning values from the rate-limited function,\n * making it ideal for API calls and other async operations where you want the result of the `maybeExecute` call\n * instead of setting the result on a state variable from within the rate-limited function.\n *\n * For smoother execution patterns, consider using:\n * - Throttling: Ensures consistent spacing between executions (e.g. max once per 200ms)\n * - Debouncing: Waits for a pause in calls before executing (e.g. after 500ms of no calls)\n *\n * Rate limiting is best used for hard API limits or resource constraints. For UI updates or\n * smoothing out frequent events, throttling or debouncing usually provide better user experience.\n *\n * Error Handling:\n * - If an `onError` handler is provided, it will be called with the error and rate limiter instance\n * - If `throwOnError` is true (default when no onError handler is provided), the error will be thrown\n * - If `throwOnError` is false (default when onError handler is provided), the error will be swallowed\n * - Both onError and throwOnError can be used together - the handler will be called before any error is thrown\n * - The error state can be checked using the underlying AsyncRateLimiter instance\n * - Rate limit rejections (when limit is exceeded) are handled separately from execution errors via the `onReject` handler\n *\n * ## State Management and Selector\n *\n * The hook uses TanStack Store for reactive state management. You can subscribe to state changes\n * in two ways:\n *\n * **1. Using `rateLimiter.Subscribe` component (Recommended for component tree subscriptions)**\n *\n * Use the `Subscribe` component to subscribe to state changes deep in your component tree without\n * needing to pass a selector to the hook. This is ideal when you want to subscribe to state\n * in child components.\n *\n * **2. Using the `selector` parameter (For hook-level subscriptions)**\n *\n * The `selector` parameter allows you to specify which state changes will trigger reactive updates\n * at the hook level, optimizing performance by preventing unnecessary updates when irrelevant\n * state changes occur.\n *\n * **By default, there will be no reactive state subscriptions** and you must opt-in to state\n * tracking by providing a selector function or using the `Subscribe` component. This prevents unnecessary\n * updates and gives you full control over when your component tracks state changes.\n *\n * Available state properties:\n * - `currentWindowStart`: Timestamp when the current window started\n * - `executionCount`: Number of function executions that have been completed\n * - `hasError`: Whether the last execution resulted in an error\n * - `isExecuting`: Whether an async function execution is currently in progress\n * - `lastError`: The error from the most recent failed execution (if any)\n * - `lastResult`: The result from the most recent successful execution\n * - `nextWindowTime`: Timestamp when the next window begins\n * - `rejectionCount`: Number of function calls that were rejected due to rate limiting\n * - `remainingInWindow`: Number of executions remaining in the current window\n *\n * @example\n * ```tsx\n * // Default behavior - no reactive state subscriptions\n * const asyncRateLimiter = 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 * // Subscribe to state changes deep in component tree using Subscribe component\n * <asyncRateLimiter.Subscribe selector={(state) => ({ rejectionCount: state.rejectionCount, isExecuting: state.isExecuting })}>\n * {(state) => (\n * <div>Rejected: {state().rejectionCount}, {state().isExecuting ? 'Executing' : 'Idle'}</div>\n * )}\n * </asyncRateLimiter.Subscribe>\n *\n * // Opt-in to track execution state changes at hook level (optimized for loading indicators)\n * const asyncRateLimiter = createAsyncRateLimiter(\n * async (id: string) => {\n * const data = await api.fetchData(id);\n * return data;\n * },\n * { limit: 5, window: 1000 },\n * (state) => ({ isExecuting: state.isExecuting })\n * );\n *\n * // Opt-in to track results when available (optimized for data display)\n * const asyncRateLimiter = createAsyncRateLimiter(\n * async (id: string) => {\n * const data = await api.fetchData(id);\n * return data;\n * },\n * { limit: 5, window: 1000 },\n * (state) => ({\n * lastResult: state.lastResult,\n * successCount: state.successCount\n * })\n * );\n *\n * // Opt-in to track error/rejection state changes (optimized for error handling)\n * const asyncRateLimiter = createAsyncRateLimiter(\n * async (id: string) => {\n * const data = await api.fetchData(id);\n * return data;\n * },\n * {\n * limit: 5,\n * window: 1000,\n * onError: (error) => console.error('API call failed:', error),\n * onReject: (rateLimiter) => console.log('Rate limit exceeded')\n * },\n * (state) => ({\n * errorCount: state.errorCount,\n * rejectionCount: state.rejectionCount\n * })\n * );\n *\n * // Opt-in to track execution metrics changes (optimized for stats display)\n * const asyncRateLimiter = createAsyncRateLimiter(\n * async (id: string) => {\n * const data = await api.fetchData(id);\n * return data;\n * },\n * { limit: 5, window: 1000 },\n * (state) => ({\n * successCount: state.successCount,\n * errorCount: state.errorCount,\n * settleCount: state.settleCount,\n * rejectionCount: state.rejectionCount\n * })\n * );\n *\n * // Opt-in to track execution times changes (optimized for window calculations)\n * const asyncRateLimiter = createAsyncRateLimiter(\n * async (id: string) => {\n * const data = await api.fetchData(id);\n * return data;\n * },\n * { limit: 5, window: 1000 },\n * (state) => ({ executionTimes: state.executionTimes })\n * );\n *\n * // Access the selected state (will be empty object {} unless selector provided)\n * const { isExecuting, lastResult, rejectionCount } = asyncRateLimiter.state();\n * ```\n */\nexport function createAsyncRateLimiter<\n TFn extends AnyAsyncFunction,\n TSelected = {},\n>(\n fn: TFn,\n options: AsyncRateLimiterOptions<TFn>,\n selector: (state: AsyncRateLimiterState<TFn>) => TSelected = () =>\n ({}) as TSelected,\n): SolidAsyncRateLimiter<TFn, TSelected> {\n const mergedOptions = {\n ...useDefaultPacerOptions().asyncRateLimiter,\n ...options,\n } as AsyncRateLimiterOptions<TFn>\n\n const asyncRateLimiter = new AsyncRateLimiter<TFn>(\n fn,\n mergedOptions,\n ) as unknown as SolidAsyncRateLimiter<TFn, TSelected>\n\n asyncRateLimiter.Subscribe = function Subscribe<TSelected>(props: {\n selector: (state: AsyncRateLimiterState<TFn>) => TSelected\n children: ((state: Accessor<TSelected>) => JSX.Element) | JSX.Element\n }) {\n const selected = useStore(asyncRateLimiter.store, props.selector)\n\n return typeof props.children === 'function'\n ? props.children(selected)\n : props.children\n }\n\n const state = useStore(asyncRateLimiter.store, selector)\n\n return {\n ...asyncRateLimiter,\n state,\n } as SolidAsyncRateLimiter<TFn, TSelected>\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAyMA,SAAgB,uBAId,IACA,SACA,kBACG,EAAE,GACkC;CAMvC,MAAM,mBAAmB,IAAIA,oDAC3B,IANoB;EACpB,GAAGC,8CAAwB,CAAC;EAC5B,GAAG;EACJ,CAKA;AAED,kBAAiB,YAAY,SAAS,UAAqB,OAGxD;EACD,MAAM,+CAAoB,iBAAiB,OAAO,MAAM,SAAS;AAEjE,SAAO,OAAO,MAAM,aAAa,aAC7B,MAAM,SAAS,SAAS,GACxB,MAAM;;CAGZ,MAAM,4CAAiB,iBAAiB,OAAO,SAAS;AAExD,QAAO;EACL,GAAG;EACH;EACD"}
|
|
@@ -1,10 +1,27 @@
|
|
|
1
1
|
import { Store } from "@tanstack/solid-store";
|
|
2
|
-
import { Accessor } from "solid-js";
|
|
2
|
+
import { Accessor, JSX } from "solid-js";
|
|
3
3
|
import { AnyAsyncFunction } from "@tanstack/pacer/types";
|
|
4
4
|
import { AsyncRateLimiter, AsyncRateLimiterOptions, AsyncRateLimiterState } from "@tanstack/pacer/async-rate-limiter";
|
|
5
5
|
|
|
6
6
|
//#region src/async-rate-limiter/createAsyncRateLimiter.d.ts
|
|
7
7
|
interface SolidAsyncRateLimiter<TFn extends AnyAsyncFunction, TSelected = {}> extends Omit<AsyncRateLimiter<TFn>, 'store'> {
|
|
8
|
+
/**
|
|
9
|
+
* A Solid component that allows you to subscribe to the rate limiter state.
|
|
10
|
+
*
|
|
11
|
+
* This is useful for tracking specific parts of the rate limiter state
|
|
12
|
+
* deep in your component tree without needing to pass a selector to the hook.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* <rateLimiter.Subscribe selector={(state) => ({ rejectionCount: state.rejectionCount, isExecuting: state.isExecuting })}>
|
|
16
|
+
* {(state) => (
|
|
17
|
+
* <div>Rejected: {state().rejectionCount}, {state().isExecuting ? 'Executing' : 'Idle'}</div>
|
|
18
|
+
* )}
|
|
19
|
+
* </rateLimiter.Subscribe>
|
|
20
|
+
*/
|
|
21
|
+
Subscribe: <TSelected>(props: {
|
|
22
|
+
selector: (state: AsyncRateLimiterState<TFn>) => TSelected;
|
|
23
|
+
children: ((state: Accessor<TSelected>) => JSX.Element) | JSX.Element;
|
|
24
|
+
}) => JSX.Element;
|
|
8
25
|
/**
|
|
9
26
|
* Reactive state that will be updated when the rate limiter state changes
|
|
10
27
|
*
|
|
@@ -55,14 +72,24 @@ interface SolidAsyncRateLimiter<TFn extends AnyAsyncFunction, TSelected = {}> ex
|
|
|
55
72
|
*
|
|
56
73
|
* ## State Management and Selector
|
|
57
74
|
*
|
|
58
|
-
* The hook uses TanStack Store for reactive state management.
|
|
59
|
-
*
|
|
60
|
-
*
|
|
75
|
+
* The hook uses TanStack Store for reactive state management. You can subscribe to state changes
|
|
76
|
+
* in two ways:
|
|
77
|
+
*
|
|
78
|
+
* **1. Using `rateLimiter.Subscribe` component (Recommended for component tree subscriptions)**
|
|
79
|
+
*
|
|
80
|
+
* Use the `Subscribe` component to subscribe to state changes deep in your component tree without
|
|
81
|
+
* needing to pass a selector to the hook. This is ideal when you want to subscribe to state
|
|
82
|
+
* in child components.
|
|
83
|
+
*
|
|
84
|
+
* **2. Using the `selector` parameter (For hook-level subscriptions)**
|
|
85
|
+
*
|
|
86
|
+
* The `selector` parameter allows you to specify which state changes will trigger reactive updates
|
|
87
|
+
* at the hook level, optimizing performance by preventing unnecessary updates when irrelevant
|
|
88
|
+
* state changes occur.
|
|
61
89
|
*
|
|
62
90
|
* **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
|
|
64
|
-
* full control over when your component
|
|
65
|
-
* component re-render when the selected state values change.
|
|
91
|
+
* tracking by providing a selector function or using the `Subscribe` component. This prevents unnecessary
|
|
92
|
+
* updates and gives you full control over when your component tracks state changes.
|
|
66
93
|
*
|
|
67
94
|
* Available state properties:
|
|
68
95
|
* - `currentWindowStart`: Timestamp when the current window started
|
|
@@ -78,7 +105,7 @@ interface SolidAsyncRateLimiter<TFn extends AnyAsyncFunction, TSelected = {}> ex
|
|
|
78
105
|
* @example
|
|
79
106
|
* ```tsx
|
|
80
107
|
* // Default behavior - no reactive state subscriptions
|
|
81
|
-
* const
|
|
108
|
+
* const asyncRateLimiter = createAsyncRateLimiter(
|
|
82
109
|
* async (id: string) => {
|
|
83
110
|
* const data = await api.fetchData(id);
|
|
84
111
|
* return data; // Return value is preserved
|
|
@@ -86,36 +113,81 @@ interface SolidAsyncRateLimiter<TFn extends AnyAsyncFunction, TSelected = {}> ex
|
|
|
86
113
|
* { limit: 5, window: 1000 } // 5 calls per second
|
|
87
114
|
* );
|
|
88
115
|
*
|
|
89
|
-
* //
|
|
90
|
-
*
|
|
91
|
-
*
|
|
92
|
-
*
|
|
93
|
-
*
|
|
116
|
+
* // Subscribe to state changes deep in component tree using Subscribe component
|
|
117
|
+
* <asyncRateLimiter.Subscribe selector={(state) => ({ rejectionCount: state.rejectionCount, isExecuting: state.isExecuting })}>
|
|
118
|
+
* {(state) => (
|
|
119
|
+
* <div>Rejected: {state().rejectionCount}, {state().isExecuting ? 'Executing' : 'Idle'}</div>
|
|
120
|
+
* )}
|
|
121
|
+
* </asyncRateLimiter.Subscribe>
|
|
122
|
+
*
|
|
123
|
+
* // Opt-in to track execution state changes at hook level (optimized for loading indicators)
|
|
124
|
+
* const asyncRateLimiter = createAsyncRateLimiter(
|
|
125
|
+
* async (id: string) => {
|
|
126
|
+
* const data = await api.fetchData(id);
|
|
127
|
+
* return data;
|
|
128
|
+
* },
|
|
129
|
+
* { limit: 5, window: 1000 },
|
|
130
|
+
* (state) => ({ isExecuting: state.isExecuting })
|
|
131
|
+
* );
|
|
132
|
+
*
|
|
133
|
+
* // Opt-in to track results when available (optimized for data display)
|
|
134
|
+
* const asyncRateLimiter = createAsyncRateLimiter(
|
|
135
|
+
* async (id: string) => {
|
|
136
|
+
* const data = await api.fetchData(id);
|
|
137
|
+
* return data;
|
|
94
138
|
* },
|
|
95
|
-
* { limit:
|
|
139
|
+
* { limit: 5, window: 1000 },
|
|
96
140
|
* (state) => ({
|
|
97
|
-
*
|
|
98
|
-
*
|
|
99
|
-
* rejectionCount: state.rejectionCount
|
|
141
|
+
* lastResult: state.lastResult,
|
|
142
|
+
* successCount: state.successCount
|
|
100
143
|
* })
|
|
101
144
|
* );
|
|
102
145
|
*
|
|
103
|
-
* // Opt-in to
|
|
104
|
-
* const
|
|
105
|
-
* async (
|
|
106
|
-
* const
|
|
107
|
-
* return
|
|
146
|
+
* // Opt-in to track error/rejection state changes (optimized for error handling)
|
|
147
|
+
* const asyncRateLimiter = createAsyncRateLimiter(
|
|
148
|
+
* async (id: string) => {
|
|
149
|
+
* const data = await api.fetchData(id);
|
|
150
|
+
* return data;
|
|
108
151
|
* },
|
|
109
152
|
* {
|
|
110
|
-
* limit:
|
|
111
|
-
* window:
|
|
112
|
-
*
|
|
153
|
+
* limit: 5,
|
|
154
|
+
* window: 1000,
|
|
155
|
+
* onError: (error) => console.error('API call failed:', error),
|
|
156
|
+
* onReject: (rateLimiter) => console.log('Rate limit exceeded')
|
|
157
|
+
* },
|
|
158
|
+
* (state) => ({
|
|
159
|
+
* errorCount: state.errorCount,
|
|
160
|
+
* rejectionCount: state.rejectionCount
|
|
161
|
+
* })
|
|
162
|
+
* );
|
|
163
|
+
*
|
|
164
|
+
* // Opt-in to track execution metrics changes (optimized for stats display)
|
|
165
|
+
* const asyncRateLimiter = createAsyncRateLimiter(
|
|
166
|
+
* async (id: string) => {
|
|
167
|
+
* const data = await api.fetchData(id);
|
|
168
|
+
* return data;
|
|
169
|
+
* },
|
|
170
|
+
* { limit: 5, window: 1000 },
|
|
171
|
+
* (state) => ({
|
|
172
|
+
* successCount: state.successCount,
|
|
173
|
+
* errorCount: state.errorCount,
|
|
174
|
+
* settleCount: state.settleCount,
|
|
175
|
+
* rejectionCount: state.rejectionCount
|
|
176
|
+
* })
|
|
177
|
+
* );
|
|
178
|
+
*
|
|
179
|
+
* // Opt-in to track execution times changes (optimized for window calculations)
|
|
180
|
+
* const asyncRateLimiter = createAsyncRateLimiter(
|
|
181
|
+
* async (id: string) => {
|
|
182
|
+
* const data = await api.fetchData(id);
|
|
183
|
+
* return data;
|
|
113
184
|
* },
|
|
114
|
-
*
|
|
185
|
+
* { limit: 5, window: 1000 },
|
|
186
|
+
* (state) => ({ executionTimes: state.executionTimes })
|
|
115
187
|
* );
|
|
116
188
|
*
|
|
117
189
|
* // Access the selected state (will be empty object {} unless selector provided)
|
|
118
|
-
* const {
|
|
190
|
+
* const { isExecuting, lastResult, rejectionCount } = asyncRateLimiter.state();
|
|
119
191
|
* ```
|
|
120
192
|
*/
|
|
121
193
|
declare function createAsyncRateLimiter<TFn extends AnyAsyncFunction, TSelected = {}>(fn: TFn, options: AsyncRateLimiterOptions<TFn>, selector?: (state: AsyncRateLimiterState<TFn>) => TSelected): SolidAsyncRateLimiter<TFn, TSelected>;
|
|
@@ -1,10 +1,27 @@
|
|
|
1
|
-
import { Accessor } from "solid-js";
|
|
1
|
+
import { Accessor, JSX } from "solid-js";
|
|
2
2
|
import { Store } from "@tanstack/solid-store";
|
|
3
3
|
import { AsyncRateLimiter, AsyncRateLimiterOptions, AsyncRateLimiterState } from "@tanstack/pacer/async-rate-limiter";
|
|
4
4
|
import { AnyAsyncFunction } from "@tanstack/pacer/types";
|
|
5
5
|
|
|
6
6
|
//#region src/async-rate-limiter/createAsyncRateLimiter.d.ts
|
|
7
7
|
interface SolidAsyncRateLimiter<TFn extends AnyAsyncFunction, TSelected = {}> extends Omit<AsyncRateLimiter<TFn>, 'store'> {
|
|
8
|
+
/**
|
|
9
|
+
* A Solid component that allows you to subscribe to the rate limiter state.
|
|
10
|
+
*
|
|
11
|
+
* This is useful for tracking specific parts of the rate limiter state
|
|
12
|
+
* deep in your component tree without needing to pass a selector to the hook.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* <rateLimiter.Subscribe selector={(state) => ({ rejectionCount: state.rejectionCount, isExecuting: state.isExecuting })}>
|
|
16
|
+
* {(state) => (
|
|
17
|
+
* <div>Rejected: {state().rejectionCount}, {state().isExecuting ? 'Executing' : 'Idle'}</div>
|
|
18
|
+
* )}
|
|
19
|
+
* </rateLimiter.Subscribe>
|
|
20
|
+
*/
|
|
21
|
+
Subscribe: <TSelected>(props: {
|
|
22
|
+
selector: (state: AsyncRateLimiterState<TFn>) => TSelected;
|
|
23
|
+
children: ((state: Accessor<TSelected>) => JSX.Element) | JSX.Element;
|
|
24
|
+
}) => JSX.Element;
|
|
8
25
|
/**
|
|
9
26
|
* Reactive state that will be updated when the rate limiter state changes
|
|
10
27
|
*
|
|
@@ -55,14 +72,24 @@ interface SolidAsyncRateLimiter<TFn extends AnyAsyncFunction, TSelected = {}> ex
|
|
|
55
72
|
*
|
|
56
73
|
* ## State Management and Selector
|
|
57
74
|
*
|
|
58
|
-
* The hook uses TanStack Store for reactive state management.
|
|
59
|
-
*
|
|
60
|
-
*
|
|
75
|
+
* The hook uses TanStack Store for reactive state management. You can subscribe to state changes
|
|
76
|
+
* in two ways:
|
|
77
|
+
*
|
|
78
|
+
* **1. Using `rateLimiter.Subscribe` component (Recommended for component tree subscriptions)**
|
|
79
|
+
*
|
|
80
|
+
* Use the `Subscribe` component to subscribe to state changes deep in your component tree without
|
|
81
|
+
* needing to pass a selector to the hook. This is ideal when you want to subscribe to state
|
|
82
|
+
* in child components.
|
|
83
|
+
*
|
|
84
|
+
* **2. Using the `selector` parameter (For hook-level subscriptions)**
|
|
85
|
+
*
|
|
86
|
+
* The `selector` parameter allows you to specify which state changes will trigger reactive updates
|
|
87
|
+
* at the hook level, optimizing performance by preventing unnecessary updates when irrelevant
|
|
88
|
+
* state changes occur.
|
|
61
89
|
*
|
|
62
90
|
* **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
|
|
64
|
-
* full control over when your component
|
|
65
|
-
* component re-render when the selected state values change.
|
|
91
|
+
* tracking by providing a selector function or using the `Subscribe` component. This prevents unnecessary
|
|
92
|
+
* updates and gives you full control over when your component tracks state changes.
|
|
66
93
|
*
|
|
67
94
|
* Available state properties:
|
|
68
95
|
* - `currentWindowStart`: Timestamp when the current window started
|
|
@@ -78,7 +105,7 @@ interface SolidAsyncRateLimiter<TFn extends AnyAsyncFunction, TSelected = {}> ex
|
|
|
78
105
|
* @example
|
|
79
106
|
* ```tsx
|
|
80
107
|
* // Default behavior - no reactive state subscriptions
|
|
81
|
-
* const
|
|
108
|
+
* const asyncRateLimiter = createAsyncRateLimiter(
|
|
82
109
|
* async (id: string) => {
|
|
83
110
|
* const data = await api.fetchData(id);
|
|
84
111
|
* return data; // Return value is preserved
|
|
@@ -86,36 +113,81 @@ interface SolidAsyncRateLimiter<TFn extends AnyAsyncFunction, TSelected = {}> ex
|
|
|
86
113
|
* { limit: 5, window: 1000 } // 5 calls per second
|
|
87
114
|
* );
|
|
88
115
|
*
|
|
89
|
-
* //
|
|
90
|
-
*
|
|
91
|
-
*
|
|
92
|
-
*
|
|
93
|
-
*
|
|
116
|
+
* // Subscribe to state changes deep in component tree using Subscribe component
|
|
117
|
+
* <asyncRateLimiter.Subscribe selector={(state) => ({ rejectionCount: state.rejectionCount, isExecuting: state.isExecuting })}>
|
|
118
|
+
* {(state) => (
|
|
119
|
+
* <div>Rejected: {state().rejectionCount}, {state().isExecuting ? 'Executing' : 'Idle'}</div>
|
|
120
|
+
* )}
|
|
121
|
+
* </asyncRateLimiter.Subscribe>
|
|
122
|
+
*
|
|
123
|
+
* // Opt-in to track execution state changes at hook level (optimized for loading indicators)
|
|
124
|
+
* const asyncRateLimiter = createAsyncRateLimiter(
|
|
125
|
+
* async (id: string) => {
|
|
126
|
+
* const data = await api.fetchData(id);
|
|
127
|
+
* return data;
|
|
128
|
+
* },
|
|
129
|
+
* { limit: 5, window: 1000 },
|
|
130
|
+
* (state) => ({ isExecuting: state.isExecuting })
|
|
131
|
+
* );
|
|
132
|
+
*
|
|
133
|
+
* // Opt-in to track results when available (optimized for data display)
|
|
134
|
+
* const asyncRateLimiter = createAsyncRateLimiter(
|
|
135
|
+
* async (id: string) => {
|
|
136
|
+
* const data = await api.fetchData(id);
|
|
137
|
+
* return data;
|
|
94
138
|
* },
|
|
95
|
-
* { limit:
|
|
139
|
+
* { limit: 5, window: 1000 },
|
|
96
140
|
* (state) => ({
|
|
97
|
-
*
|
|
98
|
-
*
|
|
99
|
-
* rejectionCount: state.rejectionCount
|
|
141
|
+
* lastResult: state.lastResult,
|
|
142
|
+
* successCount: state.successCount
|
|
100
143
|
* })
|
|
101
144
|
* );
|
|
102
145
|
*
|
|
103
|
-
* // Opt-in to
|
|
104
|
-
* const
|
|
105
|
-
* async (
|
|
106
|
-
* const
|
|
107
|
-
* return
|
|
146
|
+
* // Opt-in to track error/rejection state changes (optimized for error handling)
|
|
147
|
+
* const asyncRateLimiter = createAsyncRateLimiter(
|
|
148
|
+
* async (id: string) => {
|
|
149
|
+
* const data = await api.fetchData(id);
|
|
150
|
+
* return data;
|
|
108
151
|
* },
|
|
109
152
|
* {
|
|
110
|
-
* limit:
|
|
111
|
-
* window:
|
|
112
|
-
*
|
|
153
|
+
* limit: 5,
|
|
154
|
+
* window: 1000,
|
|
155
|
+
* onError: (error) => console.error('API call failed:', error),
|
|
156
|
+
* onReject: (rateLimiter) => console.log('Rate limit exceeded')
|
|
157
|
+
* },
|
|
158
|
+
* (state) => ({
|
|
159
|
+
* errorCount: state.errorCount,
|
|
160
|
+
* rejectionCount: state.rejectionCount
|
|
161
|
+
* })
|
|
162
|
+
* );
|
|
163
|
+
*
|
|
164
|
+
* // Opt-in to track execution metrics changes (optimized for stats display)
|
|
165
|
+
* const asyncRateLimiter = createAsyncRateLimiter(
|
|
166
|
+
* async (id: string) => {
|
|
167
|
+
* const data = await api.fetchData(id);
|
|
168
|
+
* return data;
|
|
169
|
+
* },
|
|
170
|
+
* { limit: 5, window: 1000 },
|
|
171
|
+
* (state) => ({
|
|
172
|
+
* successCount: state.successCount,
|
|
173
|
+
* errorCount: state.errorCount,
|
|
174
|
+
* settleCount: state.settleCount,
|
|
175
|
+
* rejectionCount: state.rejectionCount
|
|
176
|
+
* })
|
|
177
|
+
* );
|
|
178
|
+
*
|
|
179
|
+
* // Opt-in to track execution times changes (optimized for window calculations)
|
|
180
|
+
* const asyncRateLimiter = createAsyncRateLimiter(
|
|
181
|
+
* async (id: string) => {
|
|
182
|
+
* const data = await api.fetchData(id);
|
|
183
|
+
* return data;
|
|
113
184
|
* },
|
|
114
|
-
*
|
|
185
|
+
* { limit: 5, window: 1000 },
|
|
186
|
+
* (state) => ({ executionTimes: state.executionTimes })
|
|
115
187
|
* );
|
|
116
188
|
*
|
|
117
189
|
* // Access the selected state (will be empty object {} unless selector provided)
|
|
118
|
-
* const {
|
|
190
|
+
* const { isExecuting, lastResult, rejectionCount } = asyncRateLimiter.state();
|
|
119
191
|
* ```
|
|
120
192
|
*/
|
|
121
193
|
declare function createAsyncRateLimiter<TFn extends AnyAsyncFunction, TSelected = {}>(fn: TFn, options: AsyncRateLimiterOptions<TFn>, selector?: (state: AsyncRateLimiterState<TFn>) => TSelected): SolidAsyncRateLimiter<TFn, TSelected>;
|