@tanstack/solid-pacer 0.2.0 → 0.4.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-debouncer/createAsyncDebouncer.cjs +22 -7
- package/dist/cjs/async-debouncer/createAsyncDebouncer.cjs.map +1 -1
- package/dist/cjs/async-debouncer/createAsyncDebouncer.d.cts +6 -3
- package/dist/cjs/async-queuer/createAsyncQueuer.cjs.map +1 -1
- package/dist/cjs/async-queuer/createAsyncQueuer.d.cts +5 -1
- package/dist/cjs/async-rate-limiter/createAsyncRateLimiter.cjs +20 -9
- package/dist/cjs/async-rate-limiter/createAsyncRateLimiter.cjs.map +1 -1
- package/dist/cjs/async-rate-limiter/createAsyncRateLimiter.d.cts +19 -6
- package/dist/cjs/async-throttler/createAsyncThrottler.cjs +32 -10
- package/dist/cjs/async-throttler/createAsyncThrottler.cjs.map +1 -1
- package/dist/cjs/async-throttler/createAsyncThrottler.d.cts +7 -3
- package/dist/cjs/debouncer/createDebouncedSignal.cjs.map +1 -1
- package/dist/cjs/debouncer/createDebouncedSignal.d.cts +1 -5
- package/dist/cjs/debouncer/createDebouncedValue.cjs +0 -3
- package/dist/cjs/debouncer/createDebouncedValue.cjs.map +1 -1
- package/dist/cjs/debouncer/createDebouncedValue.d.cts +5 -11
- package/dist/cjs/debouncer/createDebouncer.cjs +7 -2
- package/dist/cjs/debouncer/createDebouncer.cjs.map +1 -1
- package/dist/cjs/debouncer/createDebouncer.d.cts +2 -2
- package/dist/cjs/queuer/createQueuer.cjs +2 -2
- package/dist/cjs/queuer/createQueuer.cjs.map +1 -1
- package/dist/cjs/queuer/createQueuer.d.cts +4 -0
- package/dist/cjs/rate-limiter/createRateLimitedSignal.cjs.map +1 -1
- package/dist/cjs/rate-limiter/createRateLimitedSignal.d.cts +12 -8
- package/dist/cjs/rate-limiter/createRateLimitedValue.cjs.map +1 -1
- package/dist/cjs/rate-limiter/createRateLimitedValue.d.cts +18 -22
- package/dist/cjs/rate-limiter/createRateLimiter.cjs +4 -2
- package/dist/cjs/rate-limiter/createRateLimiter.cjs.map +1 -1
- package/dist/cjs/rate-limiter/createRateLimiter.d.cts +10 -3
- package/dist/cjs/throttler/createThrottledSignal.cjs.map +1 -1
- package/dist/cjs/throttler/createThrottledSignal.d.cts +1 -5
- package/dist/cjs/throttler/createThrottledValue.cjs +0 -3
- package/dist/cjs/throttler/createThrottledValue.cjs.map +1 -1
- package/dist/cjs/throttler/createThrottledValue.d.cts +10 -14
- package/dist/cjs/throttler/createThrottler.cjs +7 -2
- package/dist/cjs/throttler/createThrottler.cjs.map +1 -1
- package/dist/cjs/throttler/createThrottler.d.cts +2 -2
- package/dist/esm/async-debouncer/createAsyncDebouncer.d.ts +6 -3
- package/dist/esm/async-debouncer/createAsyncDebouncer.js +22 -7
- package/dist/esm/async-debouncer/createAsyncDebouncer.js.map +1 -1
- package/dist/esm/async-queuer/createAsyncQueuer.d.ts +5 -1
- package/dist/esm/async-queuer/createAsyncQueuer.js.map +1 -1
- package/dist/esm/async-rate-limiter/createAsyncRateLimiter.d.ts +19 -6
- package/dist/esm/async-rate-limiter/createAsyncRateLimiter.js +20 -9
- package/dist/esm/async-rate-limiter/createAsyncRateLimiter.js.map +1 -1
- package/dist/esm/async-throttler/createAsyncThrottler.d.ts +7 -3
- package/dist/esm/async-throttler/createAsyncThrottler.js +32 -10
- package/dist/esm/async-throttler/createAsyncThrottler.js.map +1 -1
- package/dist/esm/debouncer/createDebouncedSignal.d.ts +1 -5
- package/dist/esm/debouncer/createDebouncedSignal.js.map +1 -1
- package/dist/esm/debouncer/createDebouncedValue.d.ts +5 -11
- package/dist/esm/debouncer/createDebouncedValue.js +1 -4
- package/dist/esm/debouncer/createDebouncedValue.js.map +1 -1
- package/dist/esm/debouncer/createDebouncer.d.ts +2 -2
- package/dist/esm/debouncer/createDebouncer.js +8 -3
- package/dist/esm/debouncer/createDebouncer.js.map +1 -1
- package/dist/esm/queuer/createQueuer.d.ts +4 -0
- package/dist/esm/queuer/createQueuer.js +2 -2
- package/dist/esm/queuer/createQueuer.js.map +1 -1
- package/dist/esm/rate-limiter/createRateLimitedSignal.d.ts +12 -8
- package/dist/esm/rate-limiter/createRateLimitedSignal.js.map +1 -1
- package/dist/esm/rate-limiter/createRateLimitedValue.d.ts +18 -22
- package/dist/esm/rate-limiter/createRateLimitedValue.js.map +1 -1
- package/dist/esm/rate-limiter/createRateLimiter.d.ts +10 -3
- package/dist/esm/rate-limiter/createRateLimiter.js +4 -2
- package/dist/esm/rate-limiter/createRateLimiter.js.map +1 -1
- package/dist/esm/throttler/createThrottledSignal.d.ts +1 -5
- package/dist/esm/throttler/createThrottledSignal.js.map +1 -1
- package/dist/esm/throttler/createThrottledValue.d.ts +10 -14
- package/dist/esm/throttler/createThrottledValue.js +1 -4
- package/dist/esm/throttler/createThrottledValue.js.map +1 -1
- package/dist/esm/throttler/createThrottler.d.ts +2 -2
- package/dist/esm/throttler/createThrottler.js +8 -3
- package/dist/esm/throttler/createThrottler.js.map +1 -1
- package/package.json +3 -3
- package/src/async-debouncer/createAsyncDebouncer.ts +40 -24
- package/src/async-queuer/createAsyncQueuer.ts +6 -2
- package/src/async-rate-limiter/createAsyncRateLimiter.ts +48 -29
- package/src/async-throttler/createAsyncThrottler.ts +49 -24
- package/src/debouncer/createDebouncedSignal.ts +2 -6
- package/src/debouncer/createDebouncedValue.ts +7 -16
- package/src/debouncer/createDebouncer.ts +16 -15
- package/src/queuer/createQueuer.ts +7 -3
- package/src/rate-limiter/createRateLimitedSignal.ts +13 -9
- package/src/rate-limiter/createRateLimitedValue.ts +19 -23
- package/src/rate-limiter/createRateLimiter.ts +22 -16
- package/src/throttler/createThrottledSignal.ts +2 -6
- package/src/throttler/createThrottledValue.ts +12 -19
- package/src/throttler/createThrottler.ts +17 -16
|
@@ -9,43 +9,39 @@ import { RateLimiterOptions } from '@tanstack/pacer/rate-limiter';
|
|
|
9
9
|
* subsequent updates until the window resets. Unlike throttling or debouncing, it does not attempt to space out
|
|
10
10
|
* or intelligently collapse updates. This can lead to bursts of rapid updates followed by periods of no updates.
|
|
11
11
|
*
|
|
12
|
+
* The rate limiter supports two types of windows:
|
|
13
|
+
* - 'fixed': A strict window that resets after the window period. All updates within the window count
|
|
14
|
+
* towards the limit, and the window resets completely after the period.
|
|
15
|
+
* - 'sliding': A rolling window that allows updates as old ones expire. This provides a more
|
|
16
|
+
* consistent rate of updates over time.
|
|
17
|
+
*
|
|
12
18
|
* For smoother update patterns, consider:
|
|
13
19
|
* - createThrottledValue: When you want consistent spacing between updates (e.g. UI changes)
|
|
14
20
|
* - createDebouncedValue: When you want to collapse rapid updates into a single update (e.g. search input)
|
|
15
21
|
*
|
|
16
22
|
* Rate limiting should primarily be used when you need to enforce strict limits, like API rate limits.
|
|
17
23
|
*
|
|
18
|
-
* The hook returns
|
|
24
|
+
* The hook returns a tuple containing:
|
|
25
|
+
* - An accessor function that provides the rate-limited value
|
|
26
|
+
* - The rate limiter instance with control methods
|
|
19
27
|
*
|
|
20
28
|
* For more direct control over rate limiting behavior without Solid state management,
|
|
21
29
|
* consider using the lower-level createRateLimiter hook instead.
|
|
22
30
|
*
|
|
23
31
|
* @example
|
|
24
32
|
* ```tsx
|
|
25
|
-
* // Basic rate limiting - update at most 5 times per minute
|
|
26
|
-
* const [rateLimitedValue] = createRateLimitedValue(rawValue, {
|
|
33
|
+
* // Basic rate limiting - update at most 5 times per minute with a sliding window
|
|
34
|
+
* const [rateLimitedValue, rateLimiter] = createRateLimitedValue(rawValue, {
|
|
27
35
|
* limit: 5,
|
|
28
|
-
* window: 60000
|
|
36
|
+
* window: 60000,
|
|
37
|
+
* windowType: 'sliding'
|
|
29
38
|
* });
|
|
30
39
|
*
|
|
31
|
-
* //
|
|
32
|
-
*
|
|
33
|
-
* limit: 3,
|
|
34
|
-
* window: 5000,
|
|
35
|
-
* onReject: (rateLimiter) => {
|
|
36
|
-
* console.log(`Update rejected. Try again in ${rateLimiter.getMsUntilNextWindow()}ms`);
|
|
37
|
-
* }
|
|
38
|
-
* });
|
|
40
|
+
* // Use the rate-limited value
|
|
41
|
+
* console.log(rateLimitedValue()); // Access the current rate-limited value
|
|
39
42
|
*
|
|
40
|
-
* //
|
|
41
|
-
*
|
|
42
|
-
* const remaining = rateLimiter.remainingInWindow();
|
|
43
|
-
* if (remaining > 0) {
|
|
44
|
-
* console.log(`${remaining} updates remaining in this window`);
|
|
45
|
-
* } else {
|
|
46
|
-
* console.log('Rate limit reached for this window');
|
|
47
|
-
* }
|
|
48
|
-
* };
|
|
43
|
+
* // Control the rate limiter
|
|
44
|
+
* rateLimiter.reset(); // Reset the rate limit window
|
|
49
45
|
* ```
|
|
50
46
|
*/
|
|
51
|
-
export declare function createRateLimitedValue<TValue>(value: Accessor<TValue>, initialOptions: RateLimiterOptions<Setter<TValue
|
|
47
|
+
export declare function createRateLimitedValue<TValue>(value: Accessor<TValue>, initialOptions: RateLimiterOptions<Setter<TValue>>): [Accessor<TValue>, SolidRateLimiter<Setter<TValue>>];
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createRateLimitedValue.js","sources":["../../../src/rate-limiter/createRateLimitedValue.ts"],"sourcesContent":["import { createEffect } from 'solid-js'\nimport { createRateLimitedSignal } from './createRateLimitedSignal'\nimport type { SolidRateLimiter } from './createRateLimiter'\nimport type { Accessor, Setter } from 'solid-js'\nimport type { RateLimiterOptions } from '@tanstack/pacer/rate-limiter'\n\n/**\n * A high-level Solid hook that creates a rate-limited version of a value that updates at most a certain number of times within a time window.\n * This hook uses Solid's createSignal internally to manage the rate-limited state.\n *\n * Rate limiting is a simple \"hard limit\" approach - it allows all updates until the limit is reached, then blocks\n * subsequent updates until the window resets. Unlike throttling or debouncing, it does not attempt to space out\n * or intelligently collapse updates. This can lead to bursts of rapid updates followed by periods of no updates.\n *\n * For smoother update patterns, consider:\n * - createThrottledValue: When you want consistent spacing between updates (e.g. UI changes)\n * - createDebouncedValue: When you want to collapse rapid updates into a single update (e.g. search input)\n *\n * Rate limiting should primarily be used when you need to enforce strict limits, like API rate limits.\n *\n * The hook returns
|
|
1
|
+
{"version":3,"file":"createRateLimitedValue.js","sources":["../../../src/rate-limiter/createRateLimitedValue.ts"],"sourcesContent":["import { createEffect } from 'solid-js'\nimport { createRateLimitedSignal } from './createRateLimitedSignal'\nimport type { SolidRateLimiter } from './createRateLimiter'\nimport type { Accessor, Setter } from 'solid-js'\nimport type { RateLimiterOptions } from '@tanstack/pacer/rate-limiter'\n\n/**\n * A high-level Solid hook that creates a rate-limited version of a value that updates at most a certain number of times within a time window.\n * This hook uses Solid's createSignal internally to manage the rate-limited state.\n *\n * Rate limiting is a simple \"hard limit\" approach - it allows all updates until the limit is reached, then blocks\n * subsequent updates until the window resets. Unlike throttling or debouncing, it does not attempt to space out\n * or intelligently collapse updates. This can lead to bursts of rapid updates followed by periods of no updates.\n *\n * The rate limiter supports two types of windows:\n * - 'fixed': A strict window that resets after the window period. All updates within the window count\n * towards the limit, and the window resets completely after the period.\n * - 'sliding': A rolling window that allows updates as old ones expire. This provides a more\n * consistent rate of updates over time.\n *\n * For smoother update patterns, consider:\n * - createThrottledValue: When you want consistent spacing between updates (e.g. UI changes)\n * - createDebouncedValue: When you want to collapse rapid updates into a single update (e.g. search input)\n *\n * Rate limiting should primarily be used when you need to enforce strict limits, like API rate limits.\n *\n * The hook returns a tuple containing:\n * - An accessor function that provides the rate-limited value\n * - The rate limiter instance with control methods\n *\n * For more direct control over rate limiting behavior without Solid state management,\n * consider using the lower-level createRateLimiter hook instead.\n *\n * @example\n * ```tsx\n * // Basic rate limiting - update at most 5 times per minute with a sliding window\n * const [rateLimitedValue, rateLimiter] = createRateLimitedValue(rawValue, {\n * limit: 5,\n * window: 60000,\n * windowType: 'sliding'\n * });\n *\n * // Use the rate-limited value\n * console.log(rateLimitedValue()); // Access the current rate-limited value\n *\n * // Control the rate limiter\n * rateLimiter.reset(); // Reset the rate limit window\n * ```\n */\nexport function createRateLimitedValue<TValue>(\n value: Accessor<TValue>,\n initialOptions: RateLimiterOptions<Setter<TValue>>,\n): [Accessor<TValue>, SolidRateLimiter<Setter<TValue>>] {\n const [rateLimitedValue, setRateLimitedValue, rateLimiter] =\n createRateLimitedSignal(value(), initialOptions)\n\n createEffect(() => {\n setRateLimitedValue(value() as any)\n })\n\n return [rateLimitedValue, rateLimiter]\n}\n"],"names":[],"mappings":";;AAiDgB,SAAA,uBACd,OACA,gBACsD;AAChD,QAAA,CAAC,kBAAkB,qBAAqB,WAAW,IACvD,wBAAwB,SAAS,cAAc;AAEjD,eAAa,MAAM;AACjB,wBAAoB,OAAc;AAAA,EAAA,CACnC;AAEM,SAAA,CAAC,kBAAkB,WAAW;AACvC;"}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { RateLimiter, RateLimiterOptions } from '@tanstack/pacer/rate-limiter';
|
|
2
2
|
import { Accessor } from 'solid-js';
|
|
3
3
|
import { AnyFunction } from '@tanstack/pacer/types';
|
|
4
|
-
export interface SolidRateLimiter<TFn extends AnyFunction
|
|
4
|
+
export interface SolidRateLimiter<TFn extends AnyFunction> extends Omit<RateLimiter<TFn>, 'getExecutionCount' | 'getMsUntilNextWindow' | 'getRejectionCount' | 'getRemainingInWindow'> {
|
|
5
5
|
executionCount: Accessor<number>;
|
|
6
6
|
msUntilNextWindow: Accessor<number>;
|
|
7
7
|
rejectionCount: Accessor<number>;
|
|
@@ -17,6 +17,12 @@ export interface SolidRateLimiter<TFn extends AnyFunction, TArgs extends Paramet
|
|
|
17
17
|
* a time window, then blocks all subsequent calls until the window resets. Unlike throttling or debouncing,
|
|
18
18
|
* it does not attempt to space out or collapse executions intelligently.
|
|
19
19
|
*
|
|
20
|
+
* The rate limiter supports two types of windows:
|
|
21
|
+
* - 'fixed': A strict window that resets after the window period. All executions within the window count
|
|
22
|
+
* towards the limit, and the window resets completely after the period.
|
|
23
|
+
* - 'sliding': A rolling window that allows executions as old ones expire. This provides a more
|
|
24
|
+
* consistent rate of execution over time.
|
|
25
|
+
*
|
|
20
26
|
* For smoother execution patterns:
|
|
21
27
|
* - Use throttling when you want consistent spacing between executions (e.g. UI updates)
|
|
22
28
|
* - Use debouncing when you want to collapse rapid-fire events (e.g. search input)
|
|
@@ -24,10 +30,11 @@ export interface SolidRateLimiter<TFn extends AnyFunction, TArgs extends Paramet
|
|
|
24
30
|
*
|
|
25
31
|
* @example
|
|
26
32
|
* ```tsx
|
|
27
|
-
* // Basic rate limiting - max 5 calls per minute
|
|
33
|
+
* // Basic rate limiting - max 5 calls per minute with a sliding window
|
|
28
34
|
* const rateLimiter = createRateLimiter(apiCall, {
|
|
29
35
|
* limit: 5,
|
|
30
36
|
* window: 60000,
|
|
37
|
+
* windowType: 'sliding'
|
|
31
38
|
* });
|
|
32
39
|
*
|
|
33
40
|
* // Monitor rate limit status
|
|
@@ -46,4 +53,4 @@ export interface SolidRateLimiter<TFn extends AnyFunction, TArgs extends Paramet
|
|
|
46
53
|
* console.log('Next window in:', rateLimiter.msUntilNextWindow());
|
|
47
54
|
* ```
|
|
48
55
|
*/
|
|
49
|
-
export declare function createRateLimiter<TFn extends AnyFunction
|
|
56
|
+
export declare function createRateLimiter<TFn extends AnyFunction>(fn: TFn, initialOptions: RateLimiterOptions<TFn>): SolidRateLimiter<TFn>;
|
|
@@ -2,7 +2,9 @@ import { RateLimiter } from "@tanstack/pacer/rate-limiter";
|
|
|
2
2
|
import { createSignal } from "solid-js";
|
|
3
3
|
import { bindInstanceMethods } from "@tanstack/pacer/utils";
|
|
4
4
|
function createRateLimiter(fn, initialOptions) {
|
|
5
|
-
const rateLimiter =
|
|
5
|
+
const rateLimiter = bindInstanceMethods(
|
|
6
|
+
new RateLimiter(fn, initialOptions)
|
|
7
|
+
);
|
|
6
8
|
const [executionCount, setExecutionCount] = createSignal(
|
|
7
9
|
rateLimiter.getExecutionCount()
|
|
8
10
|
);
|
|
@@ -36,7 +38,7 @@ function createRateLimiter(fn, initialOptions) {
|
|
|
36
38
|
}
|
|
37
39
|
setOptions(initialOptions);
|
|
38
40
|
return {
|
|
39
|
-
...
|
|
41
|
+
...rateLimiter,
|
|
40
42
|
executionCount,
|
|
41
43
|
rejectionCount,
|
|
42
44
|
remainingInWindow,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createRateLimiter.js","sources":["../../../src/rate-limiter/createRateLimiter.ts"],"sourcesContent":["import { RateLimiter } from '@tanstack/pacer/rate-limiter'\nimport { createSignal } from 'solid-js'\nimport { bindInstanceMethods } from '@tanstack/pacer/utils'\nimport type { Accessor } from 'solid-js'\nimport type { AnyFunction } from '@tanstack/pacer/types'\nimport type { RateLimiterOptions } from '@tanstack/pacer/rate-limiter'\n\nexport interface SolidRateLimiter
|
|
1
|
+
{"version":3,"file":"createRateLimiter.js","sources":["../../../src/rate-limiter/createRateLimiter.ts"],"sourcesContent":["import { RateLimiter } from '@tanstack/pacer/rate-limiter'\nimport { createSignal } from 'solid-js'\nimport { bindInstanceMethods } from '@tanstack/pacer/utils'\nimport type { Accessor } from 'solid-js'\nimport type { AnyFunction } from '@tanstack/pacer/types'\nimport type { RateLimiterOptions } from '@tanstack/pacer/rate-limiter'\n\nexport interface SolidRateLimiter<TFn extends AnyFunction>\n extends Omit<\n RateLimiter<TFn>,\n | 'getExecutionCount'\n | 'getMsUntilNextWindow'\n | 'getRejectionCount'\n | 'getRemainingInWindow'\n > {\n executionCount: Accessor<number>\n msUntilNextWindow: Accessor<number>\n rejectionCount: Accessor<number>\n remainingInWindow: Accessor<number>\n}\n\n/**\n * A low-level Solid hook that creates a `RateLimiter` instance to enforce rate limits on function execution.\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 \"hard limit\" approach that allows executions until a maximum count is reached within\n * a time window, then blocks all subsequent calls until the window resets. Unlike throttling or debouncing,\n * it does not attempt to space out or collapse executions intelligently.\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 * For smoother execution patterns:\n * - Use throttling when you want consistent spacing between executions (e.g. UI updates)\n * - Use debouncing when you want to collapse rapid-fire events (e.g. search input)\n * - Use rate limiting only when you need to enforce hard limits (e.g. API rate limits)\n *\n * @example\n * ```tsx\n * // Basic rate limiting - max 5 calls per minute with a sliding window\n * const rateLimiter = createRateLimiter(apiCall, {\n * limit: 5,\n * window: 60000,\n * windowType: 'sliding'\n * });\n *\n * // Monitor rate limit status\n * const handleClick = () => {\n * if (rateLimiter.remainingInWindow() > 0) {\n * rateLimiter.maybeExecute(data);\n * } else {\n * showRateLimitWarning();\n * }\n * };\n *\n * // Access rate limiter state via signals\n * console.log('Executions:', rateLimiter.executionCount());\n * console.log('Rejections:', rateLimiter.rejectionCount());\n * console.log('Remaining:', rateLimiter.remainingInWindow());\n * console.log('Next window in:', rateLimiter.msUntilNextWindow());\n * ```\n */\nexport function createRateLimiter<TFn extends AnyFunction>(\n fn: TFn,\n initialOptions: RateLimiterOptions<TFn>,\n): SolidRateLimiter<TFn> {\n const rateLimiter = bindInstanceMethods(\n new RateLimiter<TFn>(fn, initialOptions),\n )\n\n const [executionCount, setExecutionCount] = createSignal(\n rateLimiter.getExecutionCount(),\n )\n const [rejectionCount, setRejectionCount] = createSignal(\n rateLimiter.getRejectionCount(),\n )\n const [remainingInWindow, setRemainingInWindow] = createSignal(\n rateLimiter.getRemainingInWindow(),\n )\n const [msUntilNextWindow, setMsUntilNextWindow] = createSignal(\n rateLimiter.getMsUntilNextWindow(),\n )\n\n function setOptions(newOptions: Partial<RateLimiterOptions<TFn>>) {\n rateLimiter.setOptions({\n ...newOptions,\n onExecute: (rateLimiter) => {\n setExecutionCount(rateLimiter.getExecutionCount())\n setRemainingInWindow(rateLimiter.getRemainingInWindow())\n setMsUntilNextWindow(rateLimiter.getMsUntilNextWindow())\n\n const onExecute = newOptions.onExecute ?? initialOptions.onExecute\n onExecute?.(rateLimiter)\n },\n onReject: (rateLimiter) => {\n setRejectionCount(rateLimiter.getRejectionCount())\n setRemainingInWindow(rateLimiter.getRemainingInWindow())\n setMsUntilNextWindow(rateLimiter.getMsUntilNextWindow())\n\n const onReject = newOptions.onReject ?? initialOptions.onReject\n onReject?.(rateLimiter)\n },\n })\n }\n\n setOptions(initialOptions)\n\n return {\n ...rateLimiter,\n executionCount,\n rejectionCount,\n remainingInWindow,\n msUntilNextWindow,\n setOptions,\n } as SolidRateLimiter<TFn>\n}\n"],"names":["rateLimiter"],"mappings":";;;AAmEgB,SAAA,kBACd,IACA,gBACuB;AACvB,QAAM,cAAc;AAAA,IAClB,IAAI,YAAiB,IAAI,cAAc;AAAA,EACzC;AAEM,QAAA,CAAC,gBAAgB,iBAAiB,IAAI;AAAA,IAC1C,YAAY,kBAAkB;AAAA,EAChC;AACM,QAAA,CAAC,gBAAgB,iBAAiB,IAAI;AAAA,IAC1C,YAAY,kBAAkB;AAAA,EAChC;AACM,QAAA,CAAC,mBAAmB,oBAAoB,IAAI;AAAA,IAChD,YAAY,qBAAqB;AAAA,EACnC;AACM,QAAA,CAAC,mBAAmB,oBAAoB,IAAI;AAAA,IAChD,YAAY,qBAAqB;AAAA,EACnC;AAEA,WAAS,WAAW,YAA8C;AAChE,gBAAY,WAAW;AAAA,MACrB,GAAG;AAAA,MACH,WAAW,CAACA,iBAAgB;AACRA,0BAAAA,aAAY,mBAAmB;AAC5BA,6BAAAA,aAAY,sBAAsB;AAClCA,6BAAAA,aAAY,sBAAsB;AAEjD,cAAA,YAAY,WAAW,aAAa,eAAe;AACzD,+CAAYA;AAAAA,MACd;AAAA,MACA,UAAU,CAACA,iBAAgB;AACPA,0BAAAA,aAAY,mBAAmB;AAC5BA,6BAAAA,aAAY,sBAAsB;AAClCA,6BAAAA,aAAY,sBAAsB;AAEjD,cAAA,WAAW,WAAW,YAAY,eAAe;AACvD,6CAAWA;AAAAA,MAAW;AAAA,IACxB,CACD;AAAA,EAAA;AAGH,aAAW,cAAc;AAElB,SAAA;AAAA,IACL,GAAG;AAAA,IACH;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACF;"}
|
|
@@ -35,8 +35,4 @@ import { ThrottlerOptions } from '@tanstack/pacer/throttler';
|
|
|
35
35
|
* console.log('Next execution:', throttler.nextExecutionTime());
|
|
36
36
|
* ```
|
|
37
37
|
*/
|
|
38
|
-
export declare function createThrottledSignal<TValue>(value: TValue, initialOptions: ThrottlerOptions<Setter<TValue
|
|
39
|
-
Accessor<TValue>,
|
|
40
|
-
Setter<TValue>,
|
|
41
|
-
SolidThrottler<Setter<TValue>, [Accessor<TValue>]>
|
|
42
|
-
];
|
|
38
|
+
export declare function createThrottledSignal<TValue>(value: TValue, initialOptions: ThrottlerOptions<Setter<TValue>>): [Accessor<TValue>, Setter<TValue>, SolidThrottler<Setter<TValue>>];
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createThrottledSignal.js","sources":["../../../src/throttler/createThrottledSignal.ts"],"sourcesContent":["import { createSignal } from 'solid-js'\nimport { createThrottler } from './createThrottler'\nimport type { SolidThrottler } from './createThrottler'\nimport type { Accessor, Setter } from 'solid-js'\nimport type { ThrottlerOptions } from '@tanstack/pacer/throttler'\n\n/**\n * A Solid hook that creates a throttled state value that updates at most once within a specified time window.\n * This hook combines Solid's createSignal with throttling functionality to provide controlled state updates.\n *\n * Throttling ensures state updates occur at a controlled rate regardless of how frequently the setter is called.\n * This is useful for rate-limiting expensive re-renders or operations that depend on rapidly changing state.\n *\n * The hook returns a tuple containing:\n * - The throttled state value accessor\n * - A throttled setter function that respects the configured wait time\n * - The throttler instance for additional control\n *\n * For more direct control over throttling without state management,\n * consider using the lower-level createThrottler hook instead.\n *\n * @example\n * ```tsx\n * // Basic throttling - update state at most once per second\n * const [value, setValue, throttler] = createThrottledSignal(0, { wait: 1000 });\n *\n * // With custom leading/trailing behavior\n * const [value, setValue] = createThrottledSignal(0, {\n * wait: 1000,\n * leading: true, // Update immediately on first change\n * trailing: false // Skip trailing edge updates\n * });\n *\n * // Access throttler state via signals\n * console.log('Executions:', throttler.executionCount());\n * console.log('Is pending:', throttler.isPending());\n * console.log('Last execution:', throttler.lastExecutionTime());\n * console.log('Next execution:', throttler.nextExecutionTime());\n * ```\n */\nexport function createThrottledSignal<TValue>(\n value: TValue,\n initialOptions: ThrottlerOptions<Setter<TValue
|
|
1
|
+
{"version":3,"file":"createThrottledSignal.js","sources":["../../../src/throttler/createThrottledSignal.ts"],"sourcesContent":["import { createSignal } from 'solid-js'\nimport { createThrottler } from './createThrottler'\nimport type { SolidThrottler } from './createThrottler'\nimport type { Accessor, Setter } from 'solid-js'\nimport type { ThrottlerOptions } from '@tanstack/pacer/throttler'\n\n/**\n * A Solid hook that creates a throttled state value that updates at most once within a specified time window.\n * This hook combines Solid's createSignal with throttling functionality to provide controlled state updates.\n *\n * Throttling ensures state updates occur at a controlled rate regardless of how frequently the setter is called.\n * This is useful for rate-limiting expensive re-renders or operations that depend on rapidly changing state.\n *\n * The hook returns a tuple containing:\n * - The throttled state value accessor\n * - A throttled setter function that respects the configured wait time\n * - The throttler instance for additional control\n *\n * For more direct control over throttling without state management,\n * consider using the lower-level createThrottler hook instead.\n *\n * @example\n * ```tsx\n * // Basic throttling - update state at most once per second\n * const [value, setValue, throttler] = createThrottledSignal(0, { wait: 1000 });\n *\n * // With custom leading/trailing behavior\n * const [value, setValue] = createThrottledSignal(0, {\n * wait: 1000,\n * leading: true, // Update immediately on first change\n * trailing: false // Skip trailing edge updates\n * });\n *\n * // Access throttler state via signals\n * console.log('Executions:', throttler.executionCount());\n * console.log('Is pending:', throttler.isPending());\n * console.log('Last execution:', throttler.lastExecutionTime());\n * console.log('Next execution:', throttler.nextExecutionTime());\n * ```\n */\nexport function createThrottledSignal<TValue>(\n value: TValue,\n initialOptions: ThrottlerOptions<Setter<TValue>>,\n): [Accessor<TValue>, Setter<TValue>, SolidThrottler<Setter<TValue>>] {\n const [throttledValue, setThrottledValue] = createSignal<TValue>(value)\n const throttler = createThrottler(setThrottledValue, initialOptions)\n return [throttledValue, throttler.maybeExecute as Setter<TValue>, throttler]\n}\n"],"names":[],"mappings":";;AAwCgB,SAAA,sBACd,OACA,gBACoE;AACpE,QAAM,CAAC,gBAAgB,iBAAiB,IAAI,aAAqB,KAAK;AAChE,QAAA,YAAY,gBAAgB,mBAAmB,cAAc;AACnE,SAAO,CAAC,gBAAgB,UAAU,cAAgC,SAAS;AAC7E;"}
|
|
@@ -8,7 +8,10 @@ import { ThrottlerOptions } from '@tanstack/pacer/throttler';
|
|
|
8
8
|
* Throttling ensures the value updates occur at a controlled rate regardless of how frequently the input value changes.
|
|
9
9
|
* This is useful for rate-limiting expensive re-renders or API calls that depend on rapidly changing values.
|
|
10
10
|
*
|
|
11
|
-
* The hook returns
|
|
11
|
+
* The hook returns a tuple containing:
|
|
12
|
+
* - An accessor function that provides the throttled value
|
|
13
|
+
* - The throttler instance with control methods
|
|
14
|
+
*
|
|
12
15
|
* The throttled value will update according to the leading/trailing edge behavior specified in the options.
|
|
13
16
|
*
|
|
14
17
|
* For more direct control over throttling behavior without Solid state management,
|
|
@@ -17,20 +20,13 @@ import { ThrottlerOptions } from '@tanstack/pacer/throttler';
|
|
|
17
20
|
* @example
|
|
18
21
|
* ```tsx
|
|
19
22
|
* // Basic throttling - update at most once per second
|
|
20
|
-
* const [throttledValue] = createThrottledValue(rawValue, { wait: 1000 });
|
|
23
|
+
* const [throttledValue, throttler] = createThrottledValue(rawValue, { wait: 1000 });
|
|
21
24
|
*
|
|
22
|
-
* //
|
|
23
|
-
*
|
|
24
|
-
* wait: 1000,
|
|
25
|
-
* leading: true, // Update immediately on first change
|
|
26
|
-
* trailing: false // Skip trailing edge updates
|
|
27
|
-
* });
|
|
25
|
+
* // Use the throttled value
|
|
26
|
+
* console.log(throttledValue()); // Access the current throttled value
|
|
28
27
|
*
|
|
29
|
-
* //
|
|
30
|
-
*
|
|
31
|
-
* console.log('Is pending:', throttler.isPending());
|
|
32
|
-
* console.log('Last execution:', throttler.lastExecutionTime());
|
|
33
|
-
* console.log('Next execution:', throttler.nextExecutionTime());
|
|
28
|
+
* // Control the throttler
|
|
29
|
+
* throttler.cancel(); // Cancel any pending updates
|
|
34
30
|
* ```
|
|
35
31
|
*/
|
|
36
|
-
export declare function createThrottledValue<TValue>(value: Accessor<TValue>, initialOptions: ThrottlerOptions<Setter<TValue
|
|
32
|
+
export declare function createThrottledValue<TValue>(value: Accessor<TValue>, initialOptions: ThrottlerOptions<Setter<TValue>>): [Accessor<TValue>, SolidThrottler<Setter<TValue>>];
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { createEffect
|
|
1
|
+
import { createEffect } from "solid-js";
|
|
2
2
|
import { createThrottledSignal } from "./createThrottledSignal.js";
|
|
3
3
|
function createThrottledValue(value, initialOptions) {
|
|
4
4
|
const [throttledValue, setThrottledValue, throttler] = createThrottledSignal(
|
|
@@ -7,9 +7,6 @@ function createThrottledValue(value, initialOptions) {
|
|
|
7
7
|
);
|
|
8
8
|
createEffect(() => {
|
|
9
9
|
setThrottledValue(value());
|
|
10
|
-
onCleanup(() => {
|
|
11
|
-
throttler.cancel();
|
|
12
|
-
});
|
|
13
10
|
});
|
|
14
11
|
return [throttledValue, throttler];
|
|
15
12
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createThrottledValue.js","sources":["../../../src/throttler/createThrottledValue.ts"],"sourcesContent":["import { createEffect
|
|
1
|
+
{"version":3,"file":"createThrottledValue.js","sources":["../../../src/throttler/createThrottledValue.ts"],"sourcesContent":["import { createEffect } from 'solid-js'\nimport { createThrottledSignal } from './createThrottledSignal'\nimport type { SolidThrottler } from './createThrottler'\nimport type { Accessor, Setter } from 'solid-js'\nimport type { ThrottlerOptions } from '@tanstack/pacer/throttler'\n\n/**\n * A high-level Solid hook that creates a throttled version of a value that updates at most once within a specified time window.\n * This hook uses Solid's createSignal internally to manage the throttled state.\n *\n * Throttling ensures the value updates occur at a controlled rate regardless of how frequently the input value changes.\n * This is useful for rate-limiting expensive re-renders or API calls that depend on rapidly changing values.\n *\n * The hook returns a tuple containing:\n * - An accessor function that provides the throttled value\n * - The throttler instance with control methods\n *\n * The throttled value will update according to the leading/trailing edge behavior specified in the options.\n *\n * For more direct control over throttling behavior without Solid state management,\n * consider using the lower-level createThrottler hook instead.\n *\n * @example\n * ```tsx\n * // Basic throttling - update at most once per second\n * const [throttledValue, throttler] = createThrottledValue(rawValue, { wait: 1000 });\n *\n * // Use the throttled value\n * console.log(throttledValue()); // Access the current throttled value\n *\n * // Control the throttler\n * throttler.cancel(); // Cancel any pending updates\n * ```\n */\nexport function createThrottledValue<TValue>(\n value: Accessor<TValue>,\n initialOptions: ThrottlerOptions<Setter<TValue>>,\n): [Accessor<TValue>, SolidThrottler<Setter<TValue>>] {\n const [throttledValue, setThrottledValue, throttler] = createThrottledSignal(\n value(),\n initialOptions,\n )\n\n createEffect(() => {\n setThrottledValue(value() as any)\n })\n\n return [throttledValue, throttler]\n}\n"],"names":[],"mappings":";;AAkCgB,SAAA,qBACd,OACA,gBACoD;AACpD,QAAM,CAAC,gBAAgB,mBAAmB,SAAS,IAAI;AAAA,IACrD,MAAM;AAAA,IACN;AAAA,EACF;AAEA,eAAa,MAAM;AACjB,sBAAkB,OAAc;AAAA,EAAA,CACjC;AAEM,SAAA,CAAC,gBAAgB,SAAS;AACnC;"}
|
|
@@ -4,7 +4,7 @@ import { AnyFunction } from '@tanstack/pacer/types';
|
|
|
4
4
|
/**
|
|
5
5
|
* An extension of the Throttler class that adds Solid signals to access the internal state of the throttler
|
|
6
6
|
*/
|
|
7
|
-
export interface SolidThrottler<TFn extends AnyFunction
|
|
7
|
+
export interface SolidThrottler<TFn extends AnyFunction> extends Omit<Throttler<TFn>, 'getExecutionCount' | 'getIsPending' | 'getLastExecutionTime' | 'getNextExecutionTime'> {
|
|
8
8
|
executionCount: Accessor<number>;
|
|
9
9
|
isPending: Accessor<boolean>;
|
|
10
10
|
lastExecutionTime: Accessor<number>;
|
|
@@ -44,4 +44,4 @@ export interface SolidThrottler<TFn extends AnyFunction, TArgs extends Parameter
|
|
|
44
44
|
* console.log(throttler.nextExecutionTime()); // timestamp of next allowed execution
|
|
45
45
|
* ```
|
|
46
46
|
*/
|
|
47
|
-
export declare function createThrottler<TFn extends AnyFunction
|
|
47
|
+
export declare function createThrottler<TFn extends AnyFunction>(fn: TFn, initialOptions: ThrottlerOptions<TFn>): SolidThrottler<TFn>;
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { Throttler } from "@tanstack/pacer/throttler";
|
|
2
|
-
import { createSignal } from "solid-js";
|
|
2
|
+
import { createSignal, createEffect, onCleanup } from "solid-js";
|
|
3
3
|
import { bindInstanceMethods } from "@tanstack/pacer/utils";
|
|
4
4
|
function createThrottler(fn, initialOptions) {
|
|
5
|
-
const throttler = new Throttler(fn, initialOptions);
|
|
5
|
+
const throttler = bindInstanceMethods(new Throttler(fn, initialOptions));
|
|
6
6
|
const [executionCount, setExecutionCount] = createSignal(
|
|
7
7
|
throttler.getExecutionCount()
|
|
8
8
|
);
|
|
@@ -27,8 +27,13 @@ function createThrottler(fn, initialOptions) {
|
|
|
27
27
|
});
|
|
28
28
|
}
|
|
29
29
|
setOptions(initialOptions);
|
|
30
|
+
createEffect(() => {
|
|
31
|
+
onCleanup(() => {
|
|
32
|
+
throttler.cancel();
|
|
33
|
+
});
|
|
34
|
+
});
|
|
30
35
|
return {
|
|
31
|
-
...
|
|
36
|
+
...throttler,
|
|
32
37
|
executionCount,
|
|
33
38
|
isPending,
|
|
34
39
|
lastExecutionTime,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createThrottler.js","sources":["../../../src/throttler/createThrottler.ts"],"sourcesContent":["import { Throttler } from '@tanstack/pacer/throttler'\nimport { createSignal } from 'solid-js'\nimport { bindInstanceMethods } from '@tanstack/pacer/utils'\nimport type { Accessor } from 'solid-js'\nimport type { AnyFunction } from '@tanstack/pacer/types'\nimport type { ThrottlerOptions } from '@tanstack/pacer/throttler'\n\n/**\n * An extension of the Throttler class that adds Solid signals to access the internal state of the 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, createSignal, onCleanup } from 'solid-js'\nimport { bindInstanceMethods } from '@tanstack/pacer/utils'\nimport type { Accessor } from 'solid-js'\nimport type { AnyFunction } from '@tanstack/pacer/types'\nimport type { ThrottlerOptions } from '@tanstack/pacer/throttler'\n\n/**\n * An extension of the Throttler class that adds Solid signals to access the internal state of the throttler\n */\nexport interface SolidThrottler<TFn extends AnyFunction>\n extends Omit<\n Throttler<TFn>,\n | 'getExecutionCount'\n | 'getIsPending'\n | 'getLastExecutionTime'\n | 'getNextExecutionTime'\n > {\n executionCount: Accessor<number>\n isPending: Accessor<boolean>\n lastExecutionTime: Accessor<number>\n nextExecutionTime: Accessor<number>\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 * @example\n * ```tsx\n * // Basic throttling with custom state\n * const [value, setValue] = createSignal(0);\n * const throttler = createThrottler(setValue, { wait: 1000 });\n *\n * // With any state manager\n * const throttler = createThrottler(\n * (value) => stateManager.setState(value),\n * {\n * wait: 2000,\n * leading: true, // Execute immediately on first call\n * trailing: false // Skip trailing edge updates\n * }\n * );\n *\n * // Access throttler state via signals\n * console.log(throttler.executionCount()); // number of times executed\n * console.log(throttler.isPending()); // whether throttled function is pending\n * console.log(throttler.lastExecutionTime()); // timestamp of last execution\n * console.log(throttler.nextExecutionTime()); // timestamp of next allowed execution\n * ```\n */\nexport function createThrottler<TFn extends AnyFunction>(\n fn: TFn,\n initialOptions: ThrottlerOptions<TFn>,\n): SolidThrottler<TFn> {\n const throttler = bindInstanceMethods(new Throttler<TFn>(fn, initialOptions))\n\n const [executionCount, setExecutionCount] = createSignal(\n throttler.getExecutionCount(),\n )\n const [isPending, setIsPending] = createSignal(throttler.getIsPending())\n const [lastExecutionTime, setLastExecutionTime] = createSignal(\n throttler.getLastExecutionTime(),\n )\n const [nextExecutionTime, setNextExecutionTime] = createSignal(\n throttler.getNextExecutionTime(),\n )\n\n function setOptions(newOptions: Partial<ThrottlerOptions<TFn>>) {\n throttler.setOptions({\n ...newOptions,\n onExecute: (throttler) => {\n setExecutionCount(throttler.getExecutionCount())\n setIsPending(throttler.getIsPending())\n setLastExecutionTime(throttler.getLastExecutionTime())\n setNextExecutionTime(throttler.getNextExecutionTime())\n\n const onExecute = newOptions.onExecute ?? initialOptions.onExecute\n onExecute?.(throttler)\n },\n })\n }\n\n setOptions(initialOptions)\n\n createEffect(() => {\n onCleanup(() => {\n throttler.cancel()\n })\n })\n\n return {\n ...throttler,\n executionCount,\n isPending,\n lastExecutionTime,\n nextExecutionTime,\n setOptions,\n } as SolidThrottler<TFn>\n}\n"],"names":["throttler"],"mappings":";;;AA0DgB,SAAA,gBACd,IACA,gBACqB;AACrB,QAAM,YAAY,oBAAoB,IAAI,UAAe,IAAI,cAAc,CAAC;AAEtE,QAAA,CAAC,gBAAgB,iBAAiB,IAAI;AAAA,IAC1C,UAAU,kBAAkB;AAAA,EAC9B;AACA,QAAM,CAAC,WAAW,YAAY,IAAI,aAAa,UAAU,cAAc;AACjE,QAAA,CAAC,mBAAmB,oBAAoB,IAAI;AAAA,IAChD,UAAU,qBAAqB;AAAA,EACjC;AACM,QAAA,CAAC,mBAAmB,oBAAoB,IAAI;AAAA,IAChD,UAAU,qBAAqB;AAAA,EACjC;AAEA,WAAS,WAAW,YAA4C;AAC9D,cAAU,WAAW;AAAA,MACnB,GAAG;AAAA,MACH,WAAW,CAACA,eAAc;AACNA,0BAAAA,WAAU,mBAAmB;AAClCA,qBAAAA,WAAU,cAAc;AAChBA,6BAAAA,WAAU,sBAAsB;AAChCA,6BAAAA,WAAU,sBAAsB;AAE/C,cAAA,YAAY,WAAW,aAAa,eAAe;AACzD,+CAAYA;AAAAA,MAAS;AAAA,IACvB,CACD;AAAA,EAAA;AAGH,aAAW,cAAc;AAEzB,eAAa,MAAM;AACjB,cAAU,MAAM;AACd,gBAAU,OAAO;AAAA,IAAA,CAClB;AAAA,EAAA,CACF;AAEM,SAAA;AAAA,IACL,GAAG;AAAA,IACH;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACF;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/solid-pacer",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "Utilities for debouncing and throttling functions in Solid.",
|
|
5
5
|
"author": "Tanner Linsley",
|
|
6
6
|
"license": "MIT",
|
|
@@ -142,10 +142,10 @@
|
|
|
142
142
|
"src"
|
|
143
143
|
],
|
|
144
144
|
"dependencies": {
|
|
145
|
-
"@tanstack/pacer": "0.
|
|
145
|
+
"@tanstack/pacer": "0.4.0"
|
|
146
146
|
},
|
|
147
147
|
"devDependencies": {
|
|
148
|
-
"solid-js": "^1.9.
|
|
148
|
+
"solid-js": "^1.9.6",
|
|
149
149
|
"vite-plugin-solid": "^2.11.6"
|
|
150
150
|
},
|
|
151
151
|
"peerDependencies": {
|
|
@@ -5,15 +5,20 @@ import type { AsyncDebouncerOptions } from '@tanstack/pacer/async-debouncer'
|
|
|
5
5
|
import type { AnyAsyncFunction } from '@tanstack/pacer/types'
|
|
6
6
|
import type { Accessor } from 'solid-js'
|
|
7
7
|
|
|
8
|
-
export interface SolidAsyncDebouncer<
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
8
|
+
export interface SolidAsyncDebouncer<TFn extends AnyAsyncFunction>
|
|
9
|
+
extends Omit<
|
|
10
|
+
AsyncDebouncer<TFn>,
|
|
11
|
+
| 'getErrorCount'
|
|
12
|
+
| 'getIsPending'
|
|
13
|
+
| 'getLastResult'
|
|
14
|
+
| 'getSettleCount'
|
|
15
|
+
| 'getSuccessCount'
|
|
14
16
|
> {
|
|
15
|
-
|
|
17
|
+
errorCount: Accessor<number>
|
|
16
18
|
isPending: Accessor<boolean>
|
|
19
|
+
lastResult: Accessor<ReturnType<TFn> | undefined>
|
|
20
|
+
settleCount: Accessor<number>
|
|
21
|
+
successCount: Accessor<number>
|
|
17
22
|
}
|
|
18
23
|
|
|
19
24
|
/**
|
|
@@ -51,29 +56,37 @@ export interface SolidAsyncDebouncer<
|
|
|
51
56
|
* ```
|
|
52
57
|
*/
|
|
53
58
|
|
|
54
|
-
export function createAsyncDebouncer<
|
|
55
|
-
TFn extends AnyAsyncFunction,
|
|
56
|
-
TArgs extends Parameters<TFn>,
|
|
57
|
-
>(
|
|
59
|
+
export function createAsyncDebouncer<TFn extends AnyAsyncFunction>(
|
|
58
60
|
fn: TFn,
|
|
59
|
-
initialOptions: AsyncDebouncerOptions<TFn
|
|
60
|
-
): SolidAsyncDebouncer<TFn
|
|
61
|
-
const asyncDebouncer = new AsyncDebouncer<TFn
|
|
61
|
+
initialOptions: AsyncDebouncerOptions<TFn>,
|
|
62
|
+
): SolidAsyncDebouncer<TFn> {
|
|
63
|
+
const asyncDebouncer = new AsyncDebouncer<TFn>(fn, initialOptions)
|
|
62
64
|
|
|
63
|
-
const [
|
|
64
|
-
asyncDebouncer.
|
|
65
|
+
const [errorCount, setErrorCount] = createSignal(
|
|
66
|
+
asyncDebouncer.getErrorCount(),
|
|
67
|
+
)
|
|
68
|
+
const [settleCount, setSettleCount] = createSignal(
|
|
69
|
+
asyncDebouncer.getSettleCount(),
|
|
70
|
+
)
|
|
71
|
+
const [successCount, setSuccessCount] = createSignal(
|
|
72
|
+
asyncDebouncer.getSuccessCount(),
|
|
65
73
|
)
|
|
66
74
|
const [isPending, setIsPending] = createSignal(asyncDebouncer.getIsPending())
|
|
75
|
+
const [lastResult, setLastResult] = createSignal(
|
|
76
|
+
asyncDebouncer.getLastResult(),
|
|
77
|
+
)
|
|
67
78
|
|
|
68
|
-
function setOptions(newOptions: Partial<AsyncDebouncerOptions<TFn
|
|
79
|
+
function setOptions(newOptions: Partial<AsyncDebouncerOptions<TFn>>) {
|
|
69
80
|
asyncDebouncer.setOptions({
|
|
70
81
|
...newOptions,
|
|
71
|
-
|
|
72
|
-
|
|
82
|
+
onSettled: (asyncDebouncer) => {
|
|
83
|
+
setSuccessCount(asyncDebouncer.getSuccessCount())
|
|
84
|
+
setErrorCount(asyncDebouncer.getErrorCount())
|
|
85
|
+
setSettleCount(asyncDebouncer.getSettleCount())
|
|
73
86
|
setIsPending(asyncDebouncer.getIsPending())
|
|
74
|
-
|
|
75
|
-
const
|
|
76
|
-
|
|
87
|
+
setLastResult(asyncDebouncer.getLastResult())
|
|
88
|
+
const onSettled = newOptions.onSettled ?? initialOptions.onSettled
|
|
89
|
+
onSettled?.(asyncDebouncer)
|
|
77
90
|
},
|
|
78
91
|
})
|
|
79
92
|
}
|
|
@@ -82,8 +95,11 @@ export function createAsyncDebouncer<
|
|
|
82
95
|
|
|
83
96
|
return {
|
|
84
97
|
...bindInstanceMethods(asyncDebouncer),
|
|
85
|
-
|
|
98
|
+
errorCount,
|
|
86
99
|
isPending,
|
|
100
|
+
lastResult,
|
|
101
|
+
settleCount,
|
|
102
|
+
successCount,
|
|
87
103
|
setOptions,
|
|
88
|
-
}
|
|
104
|
+
} as SolidAsyncDebouncer<TFn>
|
|
89
105
|
}
|
|
@@ -49,11 +49,15 @@ export interface SolidAsyncQueuer<TValue>
|
|
|
49
49
|
/**
|
|
50
50
|
* Signal version of `getPeek`
|
|
51
51
|
*/
|
|
52
|
-
peek: Accessor<TValue | undefined>
|
|
52
|
+
peek: Accessor<(() => Promise<TValue>) | undefined>
|
|
53
53
|
/**
|
|
54
54
|
* Signal version of `getPendingItems`
|
|
55
55
|
*/
|
|
56
56
|
pendingItems: Accessor<Array<() => Promise<TValue>>>
|
|
57
|
+
/**
|
|
58
|
+
* Signal version of `getRejectionCount`
|
|
59
|
+
*/
|
|
60
|
+
rejectionCount: Accessor<number>
|
|
57
61
|
/**
|
|
58
62
|
* Signal version of `getSize`
|
|
59
63
|
*/
|
|
@@ -171,5 +175,5 @@ export function createAsyncQueuer<TValue>(
|
|
|
171
175
|
pendingItems,
|
|
172
176
|
rejectionCount,
|
|
173
177
|
size,
|
|
174
|
-
}
|
|
178
|
+
} as SolidAsyncQueuer<TValue>
|
|
175
179
|
}
|
|
@@ -5,17 +5,19 @@ import type { Accessor } from 'solid-js'
|
|
|
5
5
|
import type { AnyAsyncFunction } from '@tanstack/pacer/types'
|
|
6
6
|
import type { AsyncRateLimiterOptions } from '@tanstack/pacer/async-rate-limiter'
|
|
7
7
|
|
|
8
|
-
export interface SolidAsyncRateLimiter<
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
| '
|
|
8
|
+
export interface SolidAsyncRateLimiter<TFn extends AnyAsyncFunction>
|
|
9
|
+
extends Omit<
|
|
10
|
+
AsyncRateLimiter<TFn>,
|
|
11
|
+
| 'getSuccessCount'
|
|
12
|
+
| 'getSettleCount'
|
|
13
|
+
| 'getErrorCount'
|
|
14
14
|
| 'getRejectionCount'
|
|
15
15
|
| 'getRemainingInWindow'
|
|
16
16
|
| 'getMsUntilNextWindow'
|
|
17
17
|
> {
|
|
18
|
-
|
|
18
|
+
successCount: Accessor<number>
|
|
19
|
+
settleCount: Accessor<number>
|
|
20
|
+
errorCount: Accessor<number>
|
|
19
21
|
rejectionCount: Accessor<number>
|
|
20
22
|
remainingInWindow: Accessor<number>
|
|
21
23
|
msUntilNextWindow: Accessor<number>
|
|
@@ -31,23 +33,34 @@ export interface SolidAsyncRateLimiter<
|
|
|
31
33
|
* then blocks subsequent calls until the window passes. This is useful for respecting API rate limits,
|
|
32
34
|
* managing resource constraints, or controlling bursts of async operations.
|
|
33
35
|
*
|
|
36
|
+
* Unlike the non-async RateLimiter, this async version supports returning values from the rate-limited function,
|
|
37
|
+
* making it ideal for API calls and other async operations where you want the result of the `maybeExecute` call
|
|
38
|
+
* instead of setting the result on a state variable from within the rate-limited function.
|
|
39
|
+
*
|
|
40
|
+
* The rate limiter supports two types of windows:
|
|
41
|
+
* - 'fixed': A strict window that resets after the window period. All executions within the window count
|
|
42
|
+
* towards the limit, and the window resets completely after the period.
|
|
43
|
+
* - 'sliding': A rolling window that allows executions as old ones expire. This provides a more
|
|
44
|
+
* consistent rate of execution over time.
|
|
45
|
+
*
|
|
34
46
|
* @example
|
|
35
47
|
* ```tsx
|
|
36
|
-
* // Basic API call rate limiting
|
|
48
|
+
* // Basic API call rate limiting with return value
|
|
37
49
|
* const { maybeExecute } = createAsyncRateLimiter(
|
|
38
50
|
* async (id: string) => {
|
|
39
51
|
* const data = await api.fetchData(id);
|
|
40
|
-
* return data;
|
|
52
|
+
* return data; // Return value is preserved
|
|
41
53
|
* },
|
|
42
54
|
* { limit: 5, window: 1000 } // 5 calls per second
|
|
43
55
|
* );
|
|
44
56
|
*
|
|
45
|
-
* // With state management
|
|
57
|
+
* // With state management and return value
|
|
46
58
|
* const [data, setData] = createSignal(null);
|
|
47
59
|
* const { maybeExecute } = createAsyncRateLimiter(
|
|
48
60
|
* async (query) => {
|
|
49
61
|
* const result = await searchAPI(query);
|
|
50
62
|
* setData(result);
|
|
63
|
+
* return result; // Return value can be used by the caller
|
|
51
64
|
* },
|
|
52
65
|
* {
|
|
53
66
|
* limit: 10,
|
|
@@ -57,21 +70,24 @@ export interface SolidAsyncRateLimiter<
|
|
|
57
70
|
* );
|
|
58
71
|
* ```
|
|
59
72
|
*/
|
|
60
|
-
export function createAsyncRateLimiter<
|
|
61
|
-
TFn extends AnyAsyncFunction,
|
|
62
|
-
TArgs extends Parameters<TFn>,
|
|
63
|
-
>(
|
|
73
|
+
export function createAsyncRateLimiter<TFn extends AnyAsyncFunction>(
|
|
64
74
|
fn: TFn,
|
|
65
|
-
initialOptions: AsyncRateLimiterOptions<TFn
|
|
66
|
-
): SolidAsyncRateLimiter<TFn
|
|
67
|
-
const asyncRateLimiter = new AsyncRateLimiter<TFn
|
|
75
|
+
initialOptions: AsyncRateLimiterOptions<TFn>,
|
|
76
|
+
): SolidAsyncRateLimiter<TFn> {
|
|
77
|
+
const asyncRateLimiter = new AsyncRateLimiter<TFn>(fn, initialOptions)
|
|
68
78
|
|
|
69
|
-
const [
|
|
70
|
-
asyncRateLimiter.
|
|
79
|
+
const [successCount, setSuccessCount] = createSignal(
|
|
80
|
+
asyncRateLimiter.getSuccessCount(),
|
|
71
81
|
)
|
|
72
82
|
const [rejectionCount, setRejectionCount] = createSignal(
|
|
73
83
|
asyncRateLimiter.getRejectionCount(),
|
|
74
84
|
)
|
|
85
|
+
const [errorCount, setErrorCount] = createSignal(
|
|
86
|
+
asyncRateLimiter.getErrorCount(),
|
|
87
|
+
)
|
|
88
|
+
const [settleCount, setSettleCount] = createSignal(
|
|
89
|
+
asyncRateLimiter.getSettleCount(),
|
|
90
|
+
)
|
|
75
91
|
const [remainingInWindow, setRemainingInWindow] = createSignal(
|
|
76
92
|
asyncRateLimiter.getRemainingInWindow(),
|
|
77
93
|
)
|
|
@@ -79,18 +95,19 @@ export function createAsyncRateLimiter<
|
|
|
79
95
|
asyncRateLimiter.getMsUntilNextWindow(),
|
|
80
96
|
)
|
|
81
97
|
|
|
82
|
-
function setOptions(
|
|
83
|
-
newOptions: Partial<AsyncRateLimiterOptions<TFn, TArgs>>,
|
|
84
|
-
) {
|
|
98
|
+
function setOptions(newOptions: Partial<AsyncRateLimiterOptions<TFn>>) {
|
|
85
99
|
asyncRateLimiter.setOptions({
|
|
86
100
|
...newOptions,
|
|
87
|
-
|
|
88
|
-
|
|
101
|
+
onSettled: (rateLimiter) => {
|
|
102
|
+
setSuccessCount(rateLimiter.getSuccessCount())
|
|
103
|
+
setSettleCount(rateLimiter.getSettleCount())
|
|
104
|
+
setErrorCount(rateLimiter.getErrorCount())
|
|
105
|
+
setRejectionCount(rateLimiter.getRejectionCount())
|
|
89
106
|
setRemainingInWindow(rateLimiter.getRemainingInWindow())
|
|
90
107
|
setMsUntilNextWindow(rateLimiter.getMsUntilNextWindow())
|
|
91
108
|
|
|
92
|
-
const
|
|
93
|
-
|
|
109
|
+
const onSettled = newOptions.onSettled ?? initialOptions.onSettled
|
|
110
|
+
onSettled?.(rateLimiter)
|
|
94
111
|
},
|
|
95
112
|
onReject: (rateLimiter) => {
|
|
96
113
|
setRejectionCount(rateLimiter.getRejectionCount())
|
|
@@ -107,10 +124,12 @@ export function createAsyncRateLimiter<
|
|
|
107
124
|
|
|
108
125
|
return {
|
|
109
126
|
...bindInstanceMethods(asyncRateLimiter),
|
|
110
|
-
|
|
111
|
-
rejectionCount,
|
|
127
|
+
errorCount,
|
|
112
128
|
remainingInWindow,
|
|
113
129
|
msUntilNextWindow,
|
|
130
|
+
rejectionCount,
|
|
114
131
|
setOptions,
|
|
115
|
-
|
|
132
|
+
settleCount,
|
|
133
|
+
successCount,
|
|
134
|
+
} as SolidAsyncRateLimiter<TFn>
|
|
116
135
|
}
|