@tanstack/solid-pacer 0.2.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/LICENSE +21 -0
- package/dist/cjs/async-debouncer/createAsyncDebouncer.cjs +32 -0
- package/dist/cjs/async-debouncer/createAsyncDebouncer.cjs.map +1 -0
- package/dist/cjs/async-debouncer/createAsyncDebouncer.d.cts +42 -0
- package/dist/cjs/async-debouncer/index.cjs +12 -0
- package/dist/cjs/async-debouncer/index.cjs.map +1 -0
- package/dist/cjs/async-debouncer/index.d.cts +2 -0
- package/dist/cjs/async-queuer/createAsyncQueuer.cjs +69 -0
- package/dist/cjs/async-queuer/createAsyncQueuer.cjs.map +1 -0
- package/dist/cjs/async-queuer/createAsyncQueuer.d.cts +90 -0
- package/dist/cjs/async-queuer/index.cjs +12 -0
- package/dist/cjs/async-queuer/index.cjs.map +1 -0
- package/dist/cjs/async-queuer/index.d.cts +2 -0
- package/dist/cjs/async-rate-limiter/createAsyncRateLimiter.cjs +50 -0
- package/dist/cjs/async-rate-limiter/createAsyncRateLimiter.cjs.map +1 -0
- package/dist/cjs/async-rate-limiter/createAsyncRateLimiter.d.cts +46 -0
- package/dist/cjs/async-rate-limiter/index.cjs +12 -0
- package/dist/cjs/async-rate-limiter/index.cjs.map +1 -0
- package/dist/cjs/async-rate-limiter/index.d.cts +2 -0
- package/dist/cjs/async-throttler/createAsyncThrottler.cjs +42 -0
- package/dist/cjs/async-throttler/createAsyncThrottler.cjs.map +1 -0
- package/dist/cjs/async-throttler/createAsyncThrottler.d.cts +46 -0
- package/dist/cjs/async-throttler/index.cjs +12 -0
- package/dist/cjs/async-throttler/index.cjs.map +1 -0
- package/dist/cjs/async-throttler/index.d.cts +2 -0
- package/dist/cjs/compare/index.cjs +10 -0
- package/dist/cjs/compare/index.cjs.map +1 -0
- package/dist/cjs/compare/index.d.cts +1 -0
- package/dist/cjs/debouncer/createDebouncedSignal.cjs +11 -0
- package/dist/cjs/debouncer/createDebouncedSignal.cjs.map +1 -0
- package/dist/cjs/debouncer/createDebouncedSignal.d.cts +47 -0
- package/dist/cjs/debouncer/createDebouncedValue.cjs +19 -0
- package/dist/cjs/debouncer/createDebouncedValue.cjs.map +1 -0
- package/dist/cjs/debouncer/createDebouncedValue.d.cts +44 -0
- package/dist/cjs/debouncer/createDebouncer.cjs +32 -0
- package/dist/cjs/debouncer/createDebouncer.cjs.map +1 -0
- package/dist/cjs/debouncer/createDebouncer.d.cts +47 -0
- package/dist/cjs/debouncer/index.cjs +16 -0
- package/dist/cjs/debouncer/index.cjs.map +1 -0
- package/dist/cjs/debouncer/index.d.cts +4 -0
- package/dist/cjs/index.cjs +38 -0
- package/dist/cjs/index.cjs.map +1 -0
- package/dist/cjs/index.d.cts +18 -0
- package/dist/cjs/queuer/createQueuer.cjs +66 -0
- package/dist/cjs/queuer/createQueuer.cjs.map +1 -0
- package/dist/cjs/queuer/createQueuer.d.cts +82 -0
- package/dist/cjs/queuer/index.cjs +12 -0
- package/dist/cjs/queuer/index.cjs.map +1 -0
- package/dist/cjs/queuer/index.d.cts +2 -0
- package/dist/cjs/rate-limiter/createRateLimitedSignal.cjs +15 -0
- package/dist/cjs/rate-limiter/createRateLimitedSignal.cjs.map +1 -0
- package/dist/cjs/rate-limiter/createRateLimitedSignal.d.cts +58 -0
- package/dist/cjs/rate-limiter/createRateLimitedValue.cjs +13 -0
- package/dist/cjs/rate-limiter/createRateLimitedValue.cjs.map +1 -0
- package/dist/cjs/rate-limiter/createRateLimitedValue.d.cts +51 -0
- package/dist/cjs/rate-limiter/createRateLimiter.cjs +50 -0
- package/dist/cjs/rate-limiter/createRateLimiter.cjs.map +1 -0
- package/dist/cjs/rate-limiter/createRateLimiter.d.cts +49 -0
- package/dist/cjs/rate-limiter/index.cjs +16 -0
- package/dist/cjs/rate-limiter/index.cjs.map +1 -0
- package/dist/cjs/rate-limiter/index.d.cts +4 -0
- package/dist/cjs/throttler/createThrottledSignal.cjs +11 -0
- package/dist/cjs/throttler/createThrottledSignal.cjs.map +1 -0
- package/dist/cjs/throttler/createThrottledSignal.d.cts +42 -0
- package/dist/cjs/throttler/createThrottledValue.cjs +19 -0
- package/dist/cjs/throttler/createThrottledValue.cjs.map +1 -0
- package/dist/cjs/throttler/createThrottledValue.d.cts +36 -0
- package/dist/cjs/throttler/createThrottler.cjs +42 -0
- package/dist/cjs/throttler/createThrottler.cjs.map +1 -0
- package/dist/cjs/throttler/createThrottler.d.cts +47 -0
- package/dist/cjs/throttler/index.cjs +16 -0
- package/dist/cjs/throttler/index.cjs.map +1 -0
- package/dist/cjs/throttler/index.d.cts +4 -0
- package/dist/cjs/types/index.cjs +10 -0
- package/dist/cjs/types/index.cjs.map +1 -0
- package/dist/cjs/types/index.d.cts +1 -0
- package/dist/cjs/utils/index.cjs +10 -0
- package/dist/cjs/utils/index.cjs.map +1 -0
- package/dist/cjs/utils/index.d.cts +1 -0
- package/dist/esm/async-debouncer/createAsyncDebouncer.d.ts +42 -0
- package/dist/esm/async-debouncer/createAsyncDebouncer.js +32 -0
- package/dist/esm/async-debouncer/createAsyncDebouncer.js.map +1 -0
- package/dist/esm/async-debouncer/index.d.ts +2 -0
- package/dist/esm/async-debouncer/index.js +6 -0
- package/dist/esm/async-debouncer/index.js.map +1 -0
- package/dist/esm/async-queuer/createAsyncQueuer.d.ts +90 -0
- package/dist/esm/async-queuer/createAsyncQueuer.js +69 -0
- package/dist/esm/async-queuer/createAsyncQueuer.js.map +1 -0
- package/dist/esm/async-queuer/index.d.ts +2 -0
- package/dist/esm/async-queuer/index.js +6 -0
- package/dist/esm/async-queuer/index.js.map +1 -0
- package/dist/esm/async-rate-limiter/createAsyncRateLimiter.d.ts +46 -0
- package/dist/esm/async-rate-limiter/createAsyncRateLimiter.js +50 -0
- package/dist/esm/async-rate-limiter/createAsyncRateLimiter.js.map +1 -0
- package/dist/esm/async-rate-limiter/index.d.ts +2 -0
- package/dist/esm/async-rate-limiter/index.js +6 -0
- package/dist/esm/async-rate-limiter/index.js.map +1 -0
- package/dist/esm/async-throttler/createAsyncThrottler.d.ts +46 -0
- package/dist/esm/async-throttler/createAsyncThrottler.js +42 -0
- package/dist/esm/async-throttler/createAsyncThrottler.js.map +1 -0
- package/dist/esm/async-throttler/index.d.ts +2 -0
- package/dist/esm/async-throttler/index.js +6 -0
- package/dist/esm/async-throttler/index.js.map +1 -0
- package/dist/esm/compare/index.d.ts +1 -0
- package/dist/esm/compare/index.js +2 -0
- package/dist/esm/compare/index.js.map +1 -0
- package/dist/esm/debouncer/createDebouncedSignal.d.ts +47 -0
- package/dist/esm/debouncer/createDebouncedSignal.js +11 -0
- package/dist/esm/debouncer/createDebouncedSignal.js.map +1 -0
- package/dist/esm/debouncer/createDebouncedValue.d.ts +44 -0
- package/dist/esm/debouncer/createDebouncedValue.js +19 -0
- package/dist/esm/debouncer/createDebouncedValue.js.map +1 -0
- package/dist/esm/debouncer/createDebouncer.d.ts +47 -0
- package/dist/esm/debouncer/createDebouncer.js +32 -0
- package/dist/esm/debouncer/createDebouncer.js.map +1 -0
- package/dist/esm/debouncer/index.d.ts +4 -0
- package/dist/esm/debouncer/index.js +10 -0
- package/dist/esm/debouncer/index.js.map +1 -0
- package/dist/esm/index.d.ts +18 -0
- package/dist/esm/index.js +32 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/queuer/createQueuer.d.ts +82 -0
- package/dist/esm/queuer/createQueuer.js +66 -0
- package/dist/esm/queuer/createQueuer.js.map +1 -0
- package/dist/esm/queuer/index.d.ts +2 -0
- package/dist/esm/queuer/index.js +6 -0
- package/dist/esm/queuer/index.js.map +1 -0
- package/dist/esm/rate-limiter/createRateLimitedSignal.d.ts +58 -0
- package/dist/esm/rate-limiter/createRateLimitedSignal.js +15 -0
- package/dist/esm/rate-limiter/createRateLimitedSignal.js.map +1 -0
- package/dist/esm/rate-limiter/createRateLimitedValue.d.ts +51 -0
- package/dist/esm/rate-limiter/createRateLimitedValue.js +13 -0
- package/dist/esm/rate-limiter/createRateLimitedValue.js.map +1 -0
- package/dist/esm/rate-limiter/createRateLimiter.d.ts +49 -0
- package/dist/esm/rate-limiter/createRateLimiter.js +50 -0
- package/dist/esm/rate-limiter/createRateLimiter.js.map +1 -0
- package/dist/esm/rate-limiter/index.d.ts +4 -0
- package/dist/esm/rate-limiter/index.js +10 -0
- package/dist/esm/rate-limiter/index.js.map +1 -0
- package/dist/esm/throttler/createThrottledSignal.d.ts +42 -0
- package/dist/esm/throttler/createThrottledSignal.js +11 -0
- package/dist/esm/throttler/createThrottledSignal.js.map +1 -0
- package/dist/esm/throttler/createThrottledValue.d.ts +36 -0
- package/dist/esm/throttler/createThrottledValue.js +19 -0
- package/dist/esm/throttler/createThrottledValue.js.map +1 -0
- package/dist/esm/throttler/createThrottler.d.ts +47 -0
- package/dist/esm/throttler/createThrottler.js +42 -0
- package/dist/esm/throttler/createThrottler.js.map +1 -0
- package/dist/esm/throttler/index.d.ts +4 -0
- package/dist/esm/throttler/index.js +10 -0
- package/dist/esm/throttler/index.js.map +1 -0
- package/dist/esm/types/index.d.ts +1 -0
- package/dist/esm/types/index.js +2 -0
- package/dist/esm/types/index.js.map +1 -0
- package/dist/esm/utils/index.d.ts +1 -0
- package/dist/esm/utils/index.js +2 -0
- package/dist/esm/utils/index.js.map +1 -0
- package/package.json +163 -0
- package/src/async-debouncer/createAsyncDebouncer.ts +89 -0
- package/src/async-debouncer/index.ts +4 -0
- package/src/async-queuer/createAsyncQueuer.ts +175 -0
- package/src/async-queuer/index.ts +3 -0
- package/src/async-rate-limiter/createAsyncRateLimiter.ts +116 -0
- package/src/async-rate-limiter/index.ts +4 -0
- package/src/async-throttler/createAsyncThrottler.ts +106 -0
- package/src/async-throttler/index.ts +4 -0
- package/src/compare/index.ts +1 -0
- package/src/debouncer/createDebouncedSignal.ts +59 -0
- package/src/debouncer/createDebouncedValue.ts +64 -0
- package/src/debouncer/createDebouncer.ts +90 -0
- package/src/debouncer/index.ts +6 -0
- package/src/index.ts +36 -0
- package/src/queuer/createQueuer.ts +167 -0
- package/src/queuer/index.ts +3 -0
- package/src/rate-limiter/createRateLimitedSignal.ts +74 -0
- package/src/rate-limiter/createRateLimitedValue.ts +66 -0
- package/src/rate-limiter/createRateLimiter.ts +115 -0
- package/src/rate-limiter/index.ts +5 -0
- package/src/throttler/createThrottledSignal.ts +52 -0
- package/src/throttler/createThrottledValue.ts +56 -0
- package/src/throttler/createThrottler.ts +106 -0
- package/src/throttler/index.ts +6 -0
- package/src/types/index.ts +1 -0
- package/src/utils/index.ts +1 -0
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { createSignal } from 'solid-js'
|
|
2
|
+
import { createRateLimiter } from './createRateLimiter'
|
|
3
|
+
import type { SolidRateLimiter } from './createRateLimiter'
|
|
4
|
+
import type { Accessor, Setter } from 'solid-js'
|
|
5
|
+
import type { RateLimiterOptions } from '@tanstack/pacer/rate-limiter'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* A Solid hook that creates a rate-limited state value that enforces a hard limit on state updates within a time window.
|
|
9
|
+
* This hook combines Solid's createSignal with rate limiting functionality to provide controlled state updates.
|
|
10
|
+
*
|
|
11
|
+
* Rate limiting is a simple "hard limit" approach - it allows all updates until the limit is reached, then blocks
|
|
12
|
+
* subsequent updates until the window resets. Unlike throttling or debouncing, it does not attempt to space out
|
|
13
|
+
* or intelligently collapse updates. This can lead to bursts of rapid updates followed by periods of no updates.
|
|
14
|
+
*
|
|
15
|
+
* For smoother update patterns, consider:
|
|
16
|
+
* - createThrottledSignal: When you want consistent spacing between updates (e.g. UI changes)
|
|
17
|
+
* - createDebouncedSignal: When you want to collapse rapid updates into a single update (e.g. search input)
|
|
18
|
+
*
|
|
19
|
+
* Rate limiting should primarily be used when you need to enforce strict limits, like API rate limits.
|
|
20
|
+
*
|
|
21
|
+
* The hook returns a tuple containing:
|
|
22
|
+
* - The rate-limited state value accessor
|
|
23
|
+
* - A rate-limited setter function that respects the configured limits
|
|
24
|
+
* - The rateLimiter instance for additional control
|
|
25
|
+
*
|
|
26
|
+
* For more direct control over rate limiting without state management,
|
|
27
|
+
* consider using the lower-level createRateLimiter hook instead.
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```tsx
|
|
31
|
+
* // Basic rate limiting - update state at most 5 times per minute
|
|
32
|
+
* const [value, setValue, rateLimiter] = createRateLimitedSignal(0, {
|
|
33
|
+
* limit: 5,
|
|
34
|
+
* window: 60000
|
|
35
|
+
* });
|
|
36
|
+
*
|
|
37
|
+
* // With rejection callback
|
|
38
|
+
* const [value, setValue] = createRateLimitedSignal(0, {
|
|
39
|
+
* limit: 3,
|
|
40
|
+
* window: 5000,
|
|
41
|
+
* onReject: (rateLimiter) => {
|
|
42
|
+
* alert(`Rate limit reached. Try again in ${rateLimiter.getMsUntilNextWindow()}ms`);
|
|
43
|
+
* }
|
|
44
|
+
* });
|
|
45
|
+
*
|
|
46
|
+
* // Access rateLimiter state via signals
|
|
47
|
+
* const handleSubmit = () => {
|
|
48
|
+
* const remaining = rateLimiter.remainingInWindow();
|
|
49
|
+
* if (remaining > 0) {
|
|
50
|
+
* setValue(newValue);
|
|
51
|
+
* } else {
|
|
52
|
+
* showRateLimitWarning();
|
|
53
|
+
* }
|
|
54
|
+
* };
|
|
55
|
+
* ```
|
|
56
|
+
*/
|
|
57
|
+
export function createRateLimitedSignal<TValue>(
|
|
58
|
+
value: TValue,
|
|
59
|
+
initialOptions: RateLimiterOptions<Setter<TValue>, [Accessor<TValue>]>,
|
|
60
|
+
): [
|
|
61
|
+
Accessor<TValue>,
|
|
62
|
+
Setter<TValue>,
|
|
63
|
+
SolidRateLimiter<Setter<TValue>, [Accessor<TValue>]>,
|
|
64
|
+
] {
|
|
65
|
+
const [rateLimitedValue, setRateLimitedValue] = createSignal<TValue>(value)
|
|
66
|
+
|
|
67
|
+
const rateLimiter = createRateLimiter(setRateLimitedValue, initialOptions)
|
|
68
|
+
|
|
69
|
+
return [
|
|
70
|
+
rateLimitedValue,
|
|
71
|
+
rateLimiter.maybeExecute.bind(rateLimiter) as Setter<TValue>,
|
|
72
|
+
rateLimiter,
|
|
73
|
+
]
|
|
74
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { createEffect } from 'solid-js'
|
|
2
|
+
import { createRateLimitedSignal } from './createRateLimitedSignal'
|
|
3
|
+
import type { SolidRateLimiter } from './createRateLimiter'
|
|
4
|
+
import type { Accessor, Setter } from 'solid-js'
|
|
5
|
+
import type { RateLimiterOptions } from '@tanstack/pacer/rate-limiter'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* 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.
|
|
9
|
+
* This hook uses Solid's createSignal internally to manage the rate-limited state.
|
|
10
|
+
*
|
|
11
|
+
* Rate limiting is a simple "hard limit" approach - it allows all updates until the limit is reached, then blocks
|
|
12
|
+
* subsequent updates until the window resets. Unlike throttling or debouncing, it does not attempt to space out
|
|
13
|
+
* or intelligently collapse updates. This can lead to bursts of rapid updates followed by periods of no updates.
|
|
14
|
+
*
|
|
15
|
+
* For smoother update patterns, consider:
|
|
16
|
+
* - createThrottledValue: When you want consistent spacing between updates (e.g. UI changes)
|
|
17
|
+
* - createDebouncedValue: When you want to collapse rapid updates into a single update (e.g. search input)
|
|
18
|
+
*
|
|
19
|
+
* Rate limiting should primarily be used when you need to enforce strict limits, like API rate limits.
|
|
20
|
+
*
|
|
21
|
+
* The hook returns both the rate-limited value and the underlying rateLimiter instance for additional control.
|
|
22
|
+
*
|
|
23
|
+
* For more direct control over rate limiting behavior without Solid state management,
|
|
24
|
+
* consider using the lower-level createRateLimiter hook instead.
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```tsx
|
|
28
|
+
* // Basic rate limiting - update at most 5 times per minute
|
|
29
|
+
* const [rateLimitedValue] = createRateLimitedValue(rawValue, {
|
|
30
|
+
* limit: 5,
|
|
31
|
+
* window: 60000
|
|
32
|
+
* });
|
|
33
|
+
*
|
|
34
|
+
* // With rejection callback
|
|
35
|
+
* const [rateLimitedValue, rateLimiter] = createRateLimitedValue(rawValue, {
|
|
36
|
+
* limit: 3,
|
|
37
|
+
* window: 5000,
|
|
38
|
+
* onReject: (rateLimiter) => {
|
|
39
|
+
* console.log(`Update rejected. Try again in ${rateLimiter.getMsUntilNextWindow()}ms`);
|
|
40
|
+
* }
|
|
41
|
+
* });
|
|
42
|
+
*
|
|
43
|
+
* // Optionally access rateLimiter state via signals
|
|
44
|
+
* const handleSubmit = () => {
|
|
45
|
+
* const remaining = rateLimiter.remainingInWindow();
|
|
46
|
+
* if (remaining > 0) {
|
|
47
|
+
* console.log(`${remaining} updates remaining in this window`);
|
|
48
|
+
* } else {
|
|
49
|
+
* console.log('Rate limit reached for this window');
|
|
50
|
+
* }
|
|
51
|
+
* };
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
export function createRateLimitedValue<TValue>(
|
|
55
|
+
value: Accessor<TValue>,
|
|
56
|
+
initialOptions: RateLimiterOptions<Setter<TValue>, [Accessor<TValue>]>,
|
|
57
|
+
): [Accessor<TValue>, SolidRateLimiter<Setter<TValue>, [Accessor<TValue>]>] {
|
|
58
|
+
const [rateLimitedValue, setRateLimitedValue, rateLimiter] =
|
|
59
|
+
createRateLimitedSignal(value(), initialOptions)
|
|
60
|
+
|
|
61
|
+
createEffect(() => {
|
|
62
|
+
setRateLimitedValue(value() as any)
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
return [rateLimitedValue, rateLimiter]
|
|
66
|
+
}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { RateLimiter } from '@tanstack/pacer/rate-limiter'
|
|
2
|
+
import { createSignal } from 'solid-js'
|
|
3
|
+
import { bindInstanceMethods } from '@tanstack/pacer/utils'
|
|
4
|
+
import type { Accessor } from 'solid-js'
|
|
5
|
+
import type { AnyFunction } from '@tanstack/pacer/types'
|
|
6
|
+
import type { RateLimiterOptions } from '@tanstack/pacer/rate-limiter'
|
|
7
|
+
|
|
8
|
+
export interface SolidRateLimiter<
|
|
9
|
+
TFn extends AnyFunction,
|
|
10
|
+
TArgs extends Parameters<TFn>,
|
|
11
|
+
> extends Omit<
|
|
12
|
+
RateLimiter<TFn, TArgs>,
|
|
13
|
+
| 'getExecutionCount'
|
|
14
|
+
| 'getMsUntilNextWindow'
|
|
15
|
+
| 'getRejectionCount'
|
|
16
|
+
| 'getRemainingInWindow'
|
|
17
|
+
> {
|
|
18
|
+
executionCount: Accessor<number>
|
|
19
|
+
msUntilNextWindow: Accessor<number>
|
|
20
|
+
rejectionCount: Accessor<number>
|
|
21
|
+
remainingInWindow: Accessor<number>
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* A low-level Solid hook that creates a `RateLimiter` instance to enforce rate limits on function execution.
|
|
26
|
+
*
|
|
27
|
+
* This hook is designed to be flexible and state-management agnostic - it simply returns a rate limiter instance that
|
|
28
|
+
* you can integrate with any state management solution (createSignal, etc).
|
|
29
|
+
*
|
|
30
|
+
* Rate limiting is a simple "hard limit" approach that allows executions until a maximum count is reached within
|
|
31
|
+
* a time window, then blocks all subsequent calls until the window resets. Unlike throttling or debouncing,
|
|
32
|
+
* it does not attempt to space out or collapse executions intelligently.
|
|
33
|
+
*
|
|
34
|
+
* For smoother execution patterns:
|
|
35
|
+
* - Use throttling when you want consistent spacing between executions (e.g. UI updates)
|
|
36
|
+
* - Use debouncing when you want to collapse rapid-fire events (e.g. search input)
|
|
37
|
+
* - Use rate limiting only when you need to enforce hard limits (e.g. API rate limits)
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* ```tsx
|
|
41
|
+
* // Basic rate limiting - max 5 calls per minute
|
|
42
|
+
* const rateLimiter = createRateLimiter(apiCall, {
|
|
43
|
+
* limit: 5,
|
|
44
|
+
* window: 60000,
|
|
45
|
+
* });
|
|
46
|
+
*
|
|
47
|
+
* // Monitor rate limit status
|
|
48
|
+
* const handleClick = () => {
|
|
49
|
+
* if (rateLimiter.remainingInWindow() > 0) {
|
|
50
|
+
* rateLimiter.maybeExecute(data);
|
|
51
|
+
* } else {
|
|
52
|
+
* showRateLimitWarning();
|
|
53
|
+
* }
|
|
54
|
+
* };
|
|
55
|
+
*
|
|
56
|
+
* // Access rate limiter state via signals
|
|
57
|
+
* console.log('Executions:', rateLimiter.executionCount());
|
|
58
|
+
* console.log('Rejections:', rateLimiter.rejectionCount());
|
|
59
|
+
* console.log('Remaining:', rateLimiter.remainingInWindow());
|
|
60
|
+
* console.log('Next window in:', rateLimiter.msUntilNextWindow());
|
|
61
|
+
* ```
|
|
62
|
+
*/
|
|
63
|
+
export function createRateLimiter<
|
|
64
|
+
TFn extends AnyFunction,
|
|
65
|
+
TArgs extends Parameters<TFn>,
|
|
66
|
+
>(
|
|
67
|
+
fn: TFn,
|
|
68
|
+
initialOptions: RateLimiterOptions<TFn, TArgs>,
|
|
69
|
+
): SolidRateLimiter<TFn, TArgs> {
|
|
70
|
+
const rateLimiter = new RateLimiter<TFn, TArgs>(fn, initialOptions)
|
|
71
|
+
|
|
72
|
+
const [executionCount, setExecutionCount] = createSignal(
|
|
73
|
+
rateLimiter.getExecutionCount(),
|
|
74
|
+
)
|
|
75
|
+
const [rejectionCount, setRejectionCount] = createSignal(
|
|
76
|
+
rateLimiter.getRejectionCount(),
|
|
77
|
+
)
|
|
78
|
+
const [remainingInWindow, setRemainingInWindow] = createSignal(
|
|
79
|
+
rateLimiter.getRemainingInWindow(),
|
|
80
|
+
)
|
|
81
|
+
const [msUntilNextWindow, setMsUntilNextWindow] = createSignal(
|
|
82
|
+
rateLimiter.getMsUntilNextWindow(),
|
|
83
|
+
)
|
|
84
|
+
|
|
85
|
+
function setOptions(newOptions: Partial<RateLimiterOptions<TFn, TArgs>>) {
|
|
86
|
+
rateLimiter.setOptions({
|
|
87
|
+
...newOptions,
|
|
88
|
+
onExecute: (rateLimiter) => {
|
|
89
|
+
setExecutionCount(rateLimiter.getExecutionCount())
|
|
90
|
+
setRemainingInWindow(rateLimiter.getRemainingInWindow())
|
|
91
|
+
setMsUntilNextWindow(rateLimiter.getMsUntilNextWindow())
|
|
92
|
+
const onExecute = newOptions.onExecute ?? initialOptions.onExecute
|
|
93
|
+
onExecute?.(rateLimiter)
|
|
94
|
+
},
|
|
95
|
+
onReject: (rateLimiter) => {
|
|
96
|
+
setRejectionCount(rateLimiter.getRejectionCount())
|
|
97
|
+
setRemainingInWindow(rateLimiter.getRemainingInWindow())
|
|
98
|
+
setMsUntilNextWindow(rateLimiter.getMsUntilNextWindow())
|
|
99
|
+
const onReject = newOptions.onReject ?? initialOptions.onReject
|
|
100
|
+
onReject?.(rateLimiter)
|
|
101
|
+
},
|
|
102
|
+
})
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
setOptions(initialOptions)
|
|
106
|
+
|
|
107
|
+
return {
|
|
108
|
+
...bindInstanceMethods(rateLimiter),
|
|
109
|
+
executionCount,
|
|
110
|
+
rejectionCount,
|
|
111
|
+
remainingInWindow,
|
|
112
|
+
msUntilNextWindow,
|
|
113
|
+
setOptions,
|
|
114
|
+
}
|
|
115
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { createSignal } from 'solid-js'
|
|
2
|
+
import { createThrottler } from './createThrottler'
|
|
3
|
+
import type { SolidThrottler } from './createThrottler'
|
|
4
|
+
import type { Accessor, Setter } from 'solid-js'
|
|
5
|
+
import type { ThrottlerOptions } from '@tanstack/pacer/throttler'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* A Solid hook that creates a throttled state value that updates at most once within a specified time window.
|
|
9
|
+
* This hook combines Solid's createSignal with throttling functionality to provide controlled state updates.
|
|
10
|
+
*
|
|
11
|
+
* Throttling ensures state updates occur at a controlled rate regardless of how frequently the setter is called.
|
|
12
|
+
* This is useful for rate-limiting expensive re-renders or operations that depend on rapidly changing state.
|
|
13
|
+
*
|
|
14
|
+
* The hook returns a tuple containing:
|
|
15
|
+
* - The throttled state value accessor
|
|
16
|
+
* - A throttled setter function that respects the configured wait time
|
|
17
|
+
* - The throttler instance for additional control
|
|
18
|
+
*
|
|
19
|
+
* For more direct control over throttling without state management,
|
|
20
|
+
* consider using the lower-level createThrottler hook instead.
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```tsx
|
|
24
|
+
* // Basic throttling - update state at most once per second
|
|
25
|
+
* const [value, setValue, throttler] = createThrottledSignal(0, { wait: 1000 });
|
|
26
|
+
*
|
|
27
|
+
* // With custom leading/trailing behavior
|
|
28
|
+
* const [value, setValue] = createThrottledSignal(0, {
|
|
29
|
+
* wait: 1000,
|
|
30
|
+
* leading: true, // Update immediately on first change
|
|
31
|
+
* trailing: false // Skip trailing edge updates
|
|
32
|
+
* });
|
|
33
|
+
*
|
|
34
|
+
* // Access throttler state via signals
|
|
35
|
+
* console.log('Executions:', throttler.executionCount());
|
|
36
|
+
* console.log('Is pending:', throttler.isPending());
|
|
37
|
+
* console.log('Last execution:', throttler.lastExecutionTime());
|
|
38
|
+
* console.log('Next execution:', throttler.nextExecutionTime());
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
41
|
+
export function createThrottledSignal<TValue>(
|
|
42
|
+
value: TValue,
|
|
43
|
+
initialOptions: ThrottlerOptions<Setter<TValue>, [Accessor<TValue>]>,
|
|
44
|
+
): [
|
|
45
|
+
Accessor<TValue>,
|
|
46
|
+
Setter<TValue>,
|
|
47
|
+
SolidThrottler<Setter<TValue>, [Accessor<TValue>]>,
|
|
48
|
+
] {
|
|
49
|
+
const [throttledValue, setThrottledValue] = createSignal<TValue>(value)
|
|
50
|
+
const throttler = createThrottler(setThrottledValue, initialOptions)
|
|
51
|
+
return [throttledValue, throttler.maybeExecute as Setter<TValue>, throttler]
|
|
52
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { createEffect, onCleanup } from 'solid-js'
|
|
2
|
+
import { createThrottledSignal } from './createThrottledSignal'
|
|
3
|
+
import type { SolidThrottler } from './createThrottler'
|
|
4
|
+
import type { Accessor, Setter } from 'solid-js'
|
|
5
|
+
import type { ThrottlerOptions } from '@tanstack/pacer/throttler'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* A high-level Solid hook that creates a throttled version of a value that updates at most once within a specified time window.
|
|
9
|
+
* This hook uses Solid's createSignal internally to manage the throttled state.
|
|
10
|
+
*
|
|
11
|
+
* Throttling ensures the value updates occur at a controlled rate regardless of how frequently the input value changes.
|
|
12
|
+
* This is useful for rate-limiting expensive re-renders or API calls that depend on rapidly changing values.
|
|
13
|
+
*
|
|
14
|
+
* The hook returns both the throttled value and the underlying throttler instance for additional control.
|
|
15
|
+
* The throttled value will update according to the leading/trailing edge behavior specified in the options.
|
|
16
|
+
*
|
|
17
|
+
* For more direct control over throttling behavior without Solid state management,
|
|
18
|
+
* consider using the lower-level createThrottler hook instead.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```tsx
|
|
22
|
+
* // Basic throttling - update at most once per second
|
|
23
|
+
* const [throttledValue] = createThrottledValue(rawValue, { wait: 1000 });
|
|
24
|
+
*
|
|
25
|
+
* // With custom leading/trailing behavior
|
|
26
|
+
* const [throttledValue, throttler] = createThrottledValue(rawValue, {
|
|
27
|
+
* wait: 1000,
|
|
28
|
+
* leading: true, // Update immediately on first change
|
|
29
|
+
* trailing: false // Skip trailing edge updates
|
|
30
|
+
* });
|
|
31
|
+
*
|
|
32
|
+
* // Access throttler state via signals
|
|
33
|
+
* console.log('Executions:', throttler.executionCount());
|
|
34
|
+
* console.log('Is pending:', throttler.isPending());
|
|
35
|
+
* console.log('Last execution:', throttler.lastExecutionTime());
|
|
36
|
+
* console.log('Next execution:', throttler.nextExecutionTime());
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
export function createThrottledValue<TValue>(
|
|
40
|
+
value: Accessor<TValue>,
|
|
41
|
+
initialOptions: ThrottlerOptions<Setter<TValue>, [Accessor<TValue>]>,
|
|
42
|
+
): [Accessor<TValue>, SolidThrottler<Setter<TValue>, [Accessor<TValue>]>] {
|
|
43
|
+
const [throttledValue, setThrottledValue, throttler] = createThrottledSignal(
|
|
44
|
+
value(),
|
|
45
|
+
initialOptions,
|
|
46
|
+
)
|
|
47
|
+
|
|
48
|
+
createEffect(() => {
|
|
49
|
+
setThrottledValue(value() as any)
|
|
50
|
+
onCleanup(() => {
|
|
51
|
+
throttler.cancel()
|
|
52
|
+
})
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
return [throttledValue, throttler]
|
|
56
|
+
}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { Throttler } from '@tanstack/pacer/throttler'
|
|
2
|
+
import { createSignal } from 'solid-js'
|
|
3
|
+
import { bindInstanceMethods } from '@tanstack/pacer/utils'
|
|
4
|
+
import type { Accessor } from 'solid-js'
|
|
5
|
+
import type { AnyFunction } from '@tanstack/pacer/types'
|
|
6
|
+
import type { ThrottlerOptions } from '@tanstack/pacer/throttler'
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* An extension of the Throttler class that adds Solid signals to access the internal state of the throttler
|
|
10
|
+
*/
|
|
11
|
+
export interface SolidThrottler<
|
|
12
|
+
TFn extends AnyFunction,
|
|
13
|
+
TArgs extends Parameters<TFn>,
|
|
14
|
+
> extends Omit<
|
|
15
|
+
Throttler<TFn, TArgs>,
|
|
16
|
+
| 'getExecutionCount'
|
|
17
|
+
| 'getIsPending'
|
|
18
|
+
| 'getLastExecutionTime'
|
|
19
|
+
| 'getNextExecutionTime'
|
|
20
|
+
> {
|
|
21
|
+
executionCount: Accessor<number>
|
|
22
|
+
isPending: Accessor<boolean>
|
|
23
|
+
lastExecutionTime: Accessor<number>
|
|
24
|
+
nextExecutionTime: Accessor<number>
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* A low-level Solid hook that creates a `Throttler` instance that limits how often the provided function can execute.
|
|
29
|
+
*
|
|
30
|
+
* This hook is designed to be flexible and state-management agnostic - it simply returns a throttler instance that
|
|
31
|
+
* you can integrate with any state management solution (createSignal, Redux, Zustand, Jotai, etc). For a simpler and higher-level hook that
|
|
32
|
+
* integrates directly with Solid's createSignal, see createThrottledSignal.
|
|
33
|
+
*
|
|
34
|
+
* Throttling ensures a function executes at most once within a specified time window,
|
|
35
|
+
* regardless of how many times it is called. This is useful for rate-limiting
|
|
36
|
+
* expensive operations or UI updates.
|
|
37
|
+
*
|
|
38
|
+
* @example
|
|
39
|
+
* ```tsx
|
|
40
|
+
* // Basic throttling with custom state
|
|
41
|
+
* const [value, setValue] = createSignal(0);
|
|
42
|
+
* const throttler = createThrottler(setValue, { wait: 1000 });
|
|
43
|
+
*
|
|
44
|
+
* // With any state manager
|
|
45
|
+
* const throttler = createThrottler(
|
|
46
|
+
* (value) => stateManager.setState(value),
|
|
47
|
+
* {
|
|
48
|
+
* wait: 2000,
|
|
49
|
+
* leading: true, // Execute immediately on first call
|
|
50
|
+
* trailing: false // Skip trailing edge updates
|
|
51
|
+
* }
|
|
52
|
+
* );
|
|
53
|
+
*
|
|
54
|
+
* // Access throttler state via signals
|
|
55
|
+
* console.log(throttler.executionCount()); // number of times executed
|
|
56
|
+
* console.log(throttler.isPending()); // whether throttled function is pending
|
|
57
|
+
* console.log(throttler.lastExecutionTime()); // timestamp of last execution
|
|
58
|
+
* console.log(throttler.nextExecutionTime()); // timestamp of next allowed execution
|
|
59
|
+
* ```
|
|
60
|
+
*/
|
|
61
|
+
export function createThrottler<
|
|
62
|
+
TFn extends AnyFunction,
|
|
63
|
+
TArgs extends Parameters<TFn>,
|
|
64
|
+
>(
|
|
65
|
+
fn: TFn,
|
|
66
|
+
initialOptions: ThrottlerOptions<TFn, TArgs>,
|
|
67
|
+
): SolidThrottler<TFn, TArgs> {
|
|
68
|
+
const throttler = new Throttler<TFn, TArgs>(fn, initialOptions)
|
|
69
|
+
|
|
70
|
+
const [executionCount, setExecutionCount] = createSignal(
|
|
71
|
+
throttler.getExecutionCount(),
|
|
72
|
+
)
|
|
73
|
+
const [isPending, setIsPending] = createSignal(throttler.getIsPending())
|
|
74
|
+
const [lastExecutionTime, setLastExecutionTime] = createSignal(
|
|
75
|
+
throttler.getLastExecutionTime(),
|
|
76
|
+
)
|
|
77
|
+
const [nextExecutionTime, setNextExecutionTime] = createSignal(
|
|
78
|
+
throttler.getNextExecutionTime(),
|
|
79
|
+
)
|
|
80
|
+
|
|
81
|
+
function setOptions(newOptions: Partial<ThrottlerOptions<TFn, TArgs>>) {
|
|
82
|
+
throttler.setOptions({
|
|
83
|
+
...newOptions,
|
|
84
|
+
onExecute: (throttler) => {
|
|
85
|
+
setExecutionCount(throttler.getExecutionCount())
|
|
86
|
+
setIsPending(throttler.getIsPending())
|
|
87
|
+
setLastExecutionTime(throttler.getLastExecutionTime())
|
|
88
|
+
setNextExecutionTime(throttler.getNextExecutionTime())
|
|
89
|
+
|
|
90
|
+
const onExecute = newOptions.onExecute ?? initialOptions.onExecute
|
|
91
|
+
onExecute?.(throttler)
|
|
92
|
+
},
|
|
93
|
+
})
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
setOptions(initialOptions)
|
|
97
|
+
|
|
98
|
+
return {
|
|
99
|
+
...bindInstanceMethods(throttler),
|
|
100
|
+
executionCount,
|
|
101
|
+
isPending,
|
|
102
|
+
lastExecutionTime,
|
|
103
|
+
nextExecutionTime,
|
|
104
|
+
setOptions,
|
|
105
|
+
}
|
|
106
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '@tanstack/pacer/types'
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '@tanstack/pacer/utils'
|