@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.
Files changed (184) hide show
  1. package/LICENSE +21 -0
  2. package/dist/cjs/async-debouncer/createAsyncDebouncer.cjs +32 -0
  3. package/dist/cjs/async-debouncer/createAsyncDebouncer.cjs.map +1 -0
  4. package/dist/cjs/async-debouncer/createAsyncDebouncer.d.cts +42 -0
  5. package/dist/cjs/async-debouncer/index.cjs +12 -0
  6. package/dist/cjs/async-debouncer/index.cjs.map +1 -0
  7. package/dist/cjs/async-debouncer/index.d.cts +2 -0
  8. package/dist/cjs/async-queuer/createAsyncQueuer.cjs +69 -0
  9. package/dist/cjs/async-queuer/createAsyncQueuer.cjs.map +1 -0
  10. package/dist/cjs/async-queuer/createAsyncQueuer.d.cts +90 -0
  11. package/dist/cjs/async-queuer/index.cjs +12 -0
  12. package/dist/cjs/async-queuer/index.cjs.map +1 -0
  13. package/dist/cjs/async-queuer/index.d.cts +2 -0
  14. package/dist/cjs/async-rate-limiter/createAsyncRateLimiter.cjs +50 -0
  15. package/dist/cjs/async-rate-limiter/createAsyncRateLimiter.cjs.map +1 -0
  16. package/dist/cjs/async-rate-limiter/createAsyncRateLimiter.d.cts +46 -0
  17. package/dist/cjs/async-rate-limiter/index.cjs +12 -0
  18. package/dist/cjs/async-rate-limiter/index.cjs.map +1 -0
  19. package/dist/cjs/async-rate-limiter/index.d.cts +2 -0
  20. package/dist/cjs/async-throttler/createAsyncThrottler.cjs +42 -0
  21. package/dist/cjs/async-throttler/createAsyncThrottler.cjs.map +1 -0
  22. package/dist/cjs/async-throttler/createAsyncThrottler.d.cts +46 -0
  23. package/dist/cjs/async-throttler/index.cjs +12 -0
  24. package/dist/cjs/async-throttler/index.cjs.map +1 -0
  25. package/dist/cjs/async-throttler/index.d.cts +2 -0
  26. package/dist/cjs/compare/index.cjs +10 -0
  27. package/dist/cjs/compare/index.cjs.map +1 -0
  28. package/dist/cjs/compare/index.d.cts +1 -0
  29. package/dist/cjs/debouncer/createDebouncedSignal.cjs +11 -0
  30. package/dist/cjs/debouncer/createDebouncedSignal.cjs.map +1 -0
  31. package/dist/cjs/debouncer/createDebouncedSignal.d.cts +47 -0
  32. package/dist/cjs/debouncer/createDebouncedValue.cjs +19 -0
  33. package/dist/cjs/debouncer/createDebouncedValue.cjs.map +1 -0
  34. package/dist/cjs/debouncer/createDebouncedValue.d.cts +44 -0
  35. package/dist/cjs/debouncer/createDebouncer.cjs +32 -0
  36. package/dist/cjs/debouncer/createDebouncer.cjs.map +1 -0
  37. package/dist/cjs/debouncer/createDebouncer.d.cts +47 -0
  38. package/dist/cjs/debouncer/index.cjs +16 -0
  39. package/dist/cjs/debouncer/index.cjs.map +1 -0
  40. package/dist/cjs/debouncer/index.d.cts +4 -0
  41. package/dist/cjs/index.cjs +38 -0
  42. package/dist/cjs/index.cjs.map +1 -0
  43. package/dist/cjs/index.d.cts +18 -0
  44. package/dist/cjs/queuer/createQueuer.cjs +66 -0
  45. package/dist/cjs/queuer/createQueuer.cjs.map +1 -0
  46. package/dist/cjs/queuer/createQueuer.d.cts +82 -0
  47. package/dist/cjs/queuer/index.cjs +12 -0
  48. package/dist/cjs/queuer/index.cjs.map +1 -0
  49. package/dist/cjs/queuer/index.d.cts +2 -0
  50. package/dist/cjs/rate-limiter/createRateLimitedSignal.cjs +15 -0
  51. package/dist/cjs/rate-limiter/createRateLimitedSignal.cjs.map +1 -0
  52. package/dist/cjs/rate-limiter/createRateLimitedSignal.d.cts +58 -0
  53. package/dist/cjs/rate-limiter/createRateLimitedValue.cjs +13 -0
  54. package/dist/cjs/rate-limiter/createRateLimitedValue.cjs.map +1 -0
  55. package/dist/cjs/rate-limiter/createRateLimitedValue.d.cts +51 -0
  56. package/dist/cjs/rate-limiter/createRateLimiter.cjs +50 -0
  57. package/dist/cjs/rate-limiter/createRateLimiter.cjs.map +1 -0
  58. package/dist/cjs/rate-limiter/createRateLimiter.d.cts +49 -0
  59. package/dist/cjs/rate-limiter/index.cjs +16 -0
  60. package/dist/cjs/rate-limiter/index.cjs.map +1 -0
  61. package/dist/cjs/rate-limiter/index.d.cts +4 -0
  62. package/dist/cjs/throttler/createThrottledSignal.cjs +11 -0
  63. package/dist/cjs/throttler/createThrottledSignal.cjs.map +1 -0
  64. package/dist/cjs/throttler/createThrottledSignal.d.cts +42 -0
  65. package/dist/cjs/throttler/createThrottledValue.cjs +19 -0
  66. package/dist/cjs/throttler/createThrottledValue.cjs.map +1 -0
  67. package/dist/cjs/throttler/createThrottledValue.d.cts +36 -0
  68. package/dist/cjs/throttler/createThrottler.cjs +42 -0
  69. package/dist/cjs/throttler/createThrottler.cjs.map +1 -0
  70. package/dist/cjs/throttler/createThrottler.d.cts +47 -0
  71. package/dist/cjs/throttler/index.cjs +16 -0
  72. package/dist/cjs/throttler/index.cjs.map +1 -0
  73. package/dist/cjs/throttler/index.d.cts +4 -0
  74. package/dist/cjs/types/index.cjs +10 -0
  75. package/dist/cjs/types/index.cjs.map +1 -0
  76. package/dist/cjs/types/index.d.cts +1 -0
  77. package/dist/cjs/utils/index.cjs +10 -0
  78. package/dist/cjs/utils/index.cjs.map +1 -0
  79. package/dist/cjs/utils/index.d.cts +1 -0
  80. package/dist/esm/async-debouncer/createAsyncDebouncer.d.ts +42 -0
  81. package/dist/esm/async-debouncer/createAsyncDebouncer.js +32 -0
  82. package/dist/esm/async-debouncer/createAsyncDebouncer.js.map +1 -0
  83. package/dist/esm/async-debouncer/index.d.ts +2 -0
  84. package/dist/esm/async-debouncer/index.js +6 -0
  85. package/dist/esm/async-debouncer/index.js.map +1 -0
  86. package/dist/esm/async-queuer/createAsyncQueuer.d.ts +90 -0
  87. package/dist/esm/async-queuer/createAsyncQueuer.js +69 -0
  88. package/dist/esm/async-queuer/createAsyncQueuer.js.map +1 -0
  89. package/dist/esm/async-queuer/index.d.ts +2 -0
  90. package/dist/esm/async-queuer/index.js +6 -0
  91. package/dist/esm/async-queuer/index.js.map +1 -0
  92. package/dist/esm/async-rate-limiter/createAsyncRateLimiter.d.ts +46 -0
  93. package/dist/esm/async-rate-limiter/createAsyncRateLimiter.js +50 -0
  94. package/dist/esm/async-rate-limiter/createAsyncRateLimiter.js.map +1 -0
  95. package/dist/esm/async-rate-limiter/index.d.ts +2 -0
  96. package/dist/esm/async-rate-limiter/index.js +6 -0
  97. package/dist/esm/async-rate-limiter/index.js.map +1 -0
  98. package/dist/esm/async-throttler/createAsyncThrottler.d.ts +46 -0
  99. package/dist/esm/async-throttler/createAsyncThrottler.js +42 -0
  100. package/dist/esm/async-throttler/createAsyncThrottler.js.map +1 -0
  101. package/dist/esm/async-throttler/index.d.ts +2 -0
  102. package/dist/esm/async-throttler/index.js +6 -0
  103. package/dist/esm/async-throttler/index.js.map +1 -0
  104. package/dist/esm/compare/index.d.ts +1 -0
  105. package/dist/esm/compare/index.js +2 -0
  106. package/dist/esm/compare/index.js.map +1 -0
  107. package/dist/esm/debouncer/createDebouncedSignal.d.ts +47 -0
  108. package/dist/esm/debouncer/createDebouncedSignal.js +11 -0
  109. package/dist/esm/debouncer/createDebouncedSignal.js.map +1 -0
  110. package/dist/esm/debouncer/createDebouncedValue.d.ts +44 -0
  111. package/dist/esm/debouncer/createDebouncedValue.js +19 -0
  112. package/dist/esm/debouncer/createDebouncedValue.js.map +1 -0
  113. package/dist/esm/debouncer/createDebouncer.d.ts +47 -0
  114. package/dist/esm/debouncer/createDebouncer.js +32 -0
  115. package/dist/esm/debouncer/createDebouncer.js.map +1 -0
  116. package/dist/esm/debouncer/index.d.ts +4 -0
  117. package/dist/esm/debouncer/index.js +10 -0
  118. package/dist/esm/debouncer/index.js.map +1 -0
  119. package/dist/esm/index.d.ts +18 -0
  120. package/dist/esm/index.js +32 -0
  121. package/dist/esm/index.js.map +1 -0
  122. package/dist/esm/queuer/createQueuer.d.ts +82 -0
  123. package/dist/esm/queuer/createQueuer.js +66 -0
  124. package/dist/esm/queuer/createQueuer.js.map +1 -0
  125. package/dist/esm/queuer/index.d.ts +2 -0
  126. package/dist/esm/queuer/index.js +6 -0
  127. package/dist/esm/queuer/index.js.map +1 -0
  128. package/dist/esm/rate-limiter/createRateLimitedSignal.d.ts +58 -0
  129. package/dist/esm/rate-limiter/createRateLimitedSignal.js +15 -0
  130. package/dist/esm/rate-limiter/createRateLimitedSignal.js.map +1 -0
  131. package/dist/esm/rate-limiter/createRateLimitedValue.d.ts +51 -0
  132. package/dist/esm/rate-limiter/createRateLimitedValue.js +13 -0
  133. package/dist/esm/rate-limiter/createRateLimitedValue.js.map +1 -0
  134. package/dist/esm/rate-limiter/createRateLimiter.d.ts +49 -0
  135. package/dist/esm/rate-limiter/createRateLimiter.js +50 -0
  136. package/dist/esm/rate-limiter/createRateLimiter.js.map +1 -0
  137. package/dist/esm/rate-limiter/index.d.ts +4 -0
  138. package/dist/esm/rate-limiter/index.js +10 -0
  139. package/dist/esm/rate-limiter/index.js.map +1 -0
  140. package/dist/esm/throttler/createThrottledSignal.d.ts +42 -0
  141. package/dist/esm/throttler/createThrottledSignal.js +11 -0
  142. package/dist/esm/throttler/createThrottledSignal.js.map +1 -0
  143. package/dist/esm/throttler/createThrottledValue.d.ts +36 -0
  144. package/dist/esm/throttler/createThrottledValue.js +19 -0
  145. package/dist/esm/throttler/createThrottledValue.js.map +1 -0
  146. package/dist/esm/throttler/createThrottler.d.ts +47 -0
  147. package/dist/esm/throttler/createThrottler.js +42 -0
  148. package/dist/esm/throttler/createThrottler.js.map +1 -0
  149. package/dist/esm/throttler/index.d.ts +4 -0
  150. package/dist/esm/throttler/index.js +10 -0
  151. package/dist/esm/throttler/index.js.map +1 -0
  152. package/dist/esm/types/index.d.ts +1 -0
  153. package/dist/esm/types/index.js +2 -0
  154. package/dist/esm/types/index.js.map +1 -0
  155. package/dist/esm/utils/index.d.ts +1 -0
  156. package/dist/esm/utils/index.js +2 -0
  157. package/dist/esm/utils/index.js.map +1 -0
  158. package/package.json +163 -0
  159. package/src/async-debouncer/createAsyncDebouncer.ts +89 -0
  160. package/src/async-debouncer/index.ts +4 -0
  161. package/src/async-queuer/createAsyncQueuer.ts +175 -0
  162. package/src/async-queuer/index.ts +3 -0
  163. package/src/async-rate-limiter/createAsyncRateLimiter.ts +116 -0
  164. package/src/async-rate-limiter/index.ts +4 -0
  165. package/src/async-throttler/createAsyncThrottler.ts +106 -0
  166. package/src/async-throttler/index.ts +4 -0
  167. package/src/compare/index.ts +1 -0
  168. package/src/debouncer/createDebouncedSignal.ts +59 -0
  169. package/src/debouncer/createDebouncedValue.ts +64 -0
  170. package/src/debouncer/createDebouncer.ts +90 -0
  171. package/src/debouncer/index.ts +6 -0
  172. package/src/index.ts +36 -0
  173. package/src/queuer/createQueuer.ts +167 -0
  174. package/src/queuer/index.ts +3 -0
  175. package/src/rate-limiter/createRateLimitedSignal.ts +74 -0
  176. package/src/rate-limiter/createRateLimitedValue.ts +66 -0
  177. package/src/rate-limiter/createRateLimiter.ts +115 -0
  178. package/src/rate-limiter/index.ts +5 -0
  179. package/src/throttler/createThrottledSignal.ts +52 -0
  180. package/src/throttler/createThrottledValue.ts +56 -0
  181. package/src/throttler/createThrottler.ts +106 -0
  182. package/src/throttler/index.ts +6 -0
  183. package/src/types/index.ts +1 -0
  184. 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,5 @@
1
+ export * from '@tanstack/pacer/rate-limiter'
2
+
3
+ export * from './createRateLimiter'
4
+ export * from './createRateLimitedSignal'
5
+ export * from './createRateLimitedValue'
@@ -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,6 @@
1
+ // re-export everything from the core pacer package, BUT ONLY from the throttler module
2
+ export * from '@tanstack/pacer/throttler'
3
+
4
+ export * from './createThrottledSignal'
5
+ export * from './createThrottledValue'
6
+ export * from './createThrottler'
@@ -0,0 +1 @@
1
+ export * from '@tanstack/pacer/types'
@@ -0,0 +1 @@
1
+ export * from '@tanstack/pacer/utils'