@tanstack/solid-pacer 0.10.0 → 0.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/async-batcher/createAsyncBatcher.cjs +1 -1
- package/dist/cjs/async-batcher/createAsyncBatcher.cjs.map +1 -1
- package/dist/cjs/async-batcher/createAsyncBatcher.d.cts +54 -6
- package/dist/cjs/async-debouncer/createAsyncDebouncer.cjs +1 -1
- package/dist/cjs/async-debouncer/createAsyncDebouncer.cjs.map +1 -1
- package/dist/cjs/async-debouncer/createAsyncDebouncer.d.cts +50 -8
- package/dist/cjs/async-queuer/createAsyncQueuer.cjs +1 -1
- package/dist/cjs/async-queuer/createAsyncQueuer.cjs.map +1 -1
- package/dist/cjs/async-queuer/createAsyncQueuer.d.cts +55 -6
- package/dist/cjs/async-rate-limiter/createAsyncRateLimiter.cjs +1 -1
- package/dist/cjs/async-rate-limiter/createAsyncRateLimiter.cjs.map +1 -1
- package/dist/cjs/async-rate-limiter/createAsyncRateLimiter.d.cts +59 -9
- package/dist/cjs/async-throttler/createAsyncThrottler.cjs +1 -1
- package/dist/cjs/async-throttler/createAsyncThrottler.cjs.map +1 -1
- package/dist/cjs/async-throttler/createAsyncThrottler.d.cts +54 -9
- package/dist/cjs/batcher/createBatcher.cjs +1 -1
- package/dist/cjs/batcher/createBatcher.cjs.map +1 -1
- package/dist/cjs/batcher/createBatcher.d.cts +46 -9
- package/dist/cjs/debouncer/createDebouncedSignal.cjs.map +1 -1
- package/dist/cjs/debouncer/createDebouncedSignal.d.cts +36 -4
- package/dist/cjs/debouncer/createDebouncedValue.cjs.map +1 -1
- package/dist/cjs/debouncer/createDebouncedValue.d.cts +30 -2
- package/dist/cjs/debouncer/createDebouncer.cjs +1 -1
- package/dist/cjs/debouncer/createDebouncer.cjs.map +1 -1
- package/dist/cjs/debouncer/createDebouncer.d.cts +56 -10
- package/dist/cjs/queuer/createQueuer.cjs +1 -1
- package/dist/cjs/queuer/createQueuer.cjs.map +1 -1
- package/dist/cjs/queuer/createQueuer.d.cts +46 -12
- package/dist/cjs/rate-limiter/createRateLimitedSignal.cjs.map +1 -1
- package/dist/cjs/rate-limiter/createRateLimitedSignal.d.cts +30 -3
- package/dist/cjs/rate-limiter/createRateLimitedValue.cjs.map +1 -1
- package/dist/cjs/rate-limiter/createRateLimitedValue.d.cts +32 -2
- package/dist/cjs/rate-limiter/createRateLimiter.cjs +1 -1
- package/dist/cjs/rate-limiter/createRateLimiter.cjs.map +1 -1
- package/dist/cjs/rate-limiter/createRateLimiter.d.cts +50 -8
- package/dist/cjs/throttler/createThrottledSignal.cjs.map +1 -1
- package/dist/cjs/throttler/createThrottledSignal.d.cts +34 -6
- package/dist/cjs/throttler/createThrottledValue.cjs.map +1 -1
- package/dist/cjs/throttler/createThrottledValue.d.cts +33 -2
- package/dist/cjs/throttler/createThrottler.cjs +1 -1
- package/dist/cjs/throttler/createThrottler.cjs.map +1 -1
- package/dist/cjs/throttler/createThrottler.d.cts +56 -12
- package/dist/esm/async-batcher/createAsyncBatcher.d.ts +54 -6
- package/dist/esm/async-batcher/createAsyncBatcher.js +1 -1
- package/dist/esm/async-batcher/createAsyncBatcher.js.map +1 -1
- package/dist/esm/async-debouncer/createAsyncDebouncer.d.ts +50 -8
- package/dist/esm/async-debouncer/createAsyncDebouncer.js +1 -1
- package/dist/esm/async-debouncer/createAsyncDebouncer.js.map +1 -1
- package/dist/esm/async-queuer/createAsyncQueuer.d.ts +55 -6
- package/dist/esm/async-queuer/createAsyncQueuer.js +1 -1
- package/dist/esm/async-queuer/createAsyncQueuer.js.map +1 -1
- package/dist/esm/async-rate-limiter/createAsyncRateLimiter.d.ts +59 -9
- package/dist/esm/async-rate-limiter/createAsyncRateLimiter.js +1 -1
- package/dist/esm/async-rate-limiter/createAsyncRateLimiter.js.map +1 -1
- package/dist/esm/async-throttler/createAsyncThrottler.d.ts +54 -9
- package/dist/esm/async-throttler/createAsyncThrottler.js +1 -1
- package/dist/esm/async-throttler/createAsyncThrottler.js.map +1 -1
- package/dist/esm/batcher/createBatcher.d.ts +46 -9
- package/dist/esm/batcher/createBatcher.js +1 -1
- package/dist/esm/batcher/createBatcher.js.map +1 -1
- package/dist/esm/debouncer/createDebouncedSignal.d.ts +36 -4
- package/dist/esm/debouncer/createDebouncedSignal.js.map +1 -1
- package/dist/esm/debouncer/createDebouncedValue.d.ts +30 -2
- package/dist/esm/debouncer/createDebouncedValue.js.map +1 -1
- package/dist/esm/debouncer/createDebouncer.d.ts +56 -10
- package/dist/esm/debouncer/createDebouncer.js +1 -1
- package/dist/esm/debouncer/createDebouncer.js.map +1 -1
- package/dist/esm/queuer/createQueuer.d.ts +46 -12
- package/dist/esm/queuer/createQueuer.js +1 -1
- package/dist/esm/queuer/createQueuer.js.map +1 -1
- package/dist/esm/rate-limiter/createRateLimitedSignal.d.ts +30 -3
- package/dist/esm/rate-limiter/createRateLimitedSignal.js.map +1 -1
- package/dist/esm/rate-limiter/createRateLimitedValue.d.ts +32 -2
- package/dist/esm/rate-limiter/createRateLimitedValue.js.map +1 -1
- package/dist/esm/rate-limiter/createRateLimiter.d.ts +50 -8
- package/dist/esm/rate-limiter/createRateLimiter.js +1 -1
- package/dist/esm/rate-limiter/createRateLimiter.js.map +1 -1
- package/dist/esm/throttler/createThrottledSignal.d.ts +34 -6
- package/dist/esm/throttler/createThrottledSignal.js.map +1 -1
- package/dist/esm/throttler/createThrottledValue.d.ts +33 -2
- package/dist/esm/throttler/createThrottledValue.js.map +1 -1
- package/dist/esm/throttler/createThrottler.d.ts +56 -12
- package/dist/esm/throttler/createThrottler.js +1 -1
- package/dist/esm/throttler/createThrottler.js.map +1 -1
- package/package.json +2 -2
- package/src/async-batcher/createAsyncBatcher.ts +58 -14
- package/src/async-debouncer/createAsyncDebouncer.ts +53 -10
- package/src/async-queuer/createAsyncQueuer.ts +58 -8
- package/src/async-rate-limiter/createAsyncRateLimiter.ts +61 -10
- package/src/async-throttler/createAsyncThrottler.ts +56 -10
- package/src/batcher/createBatcher.ts +48 -10
- package/src/debouncer/createDebouncedSignal.ts +36 -7
- package/src/debouncer/createDebouncedValue.ts +30 -5
- package/src/debouncer/createDebouncer.ts +58 -17
- package/src/queuer/createQueuer.ts +48 -14
- package/src/rate-limiter/createRateLimitedSignal.ts +30 -3
- package/src/rate-limiter/createRateLimitedValue.ts +32 -2
- package/src/rate-limiter/createRateLimiter.ts +53 -16
- package/src/throttler/createThrottledSignal.ts +34 -9
- package/src/throttler/createThrottledValue.ts +33 -5
- package/src/throttler/createThrottler.ts +58 -19
|
@@ -1,12 +1,19 @@
|
|
|
1
1
|
import { Queuer, QueuerOptions, QueuerState } from '@tanstack/pacer/queuer';
|
|
2
|
+
import { Store } from '@tanstack/solid-store';
|
|
2
3
|
import { Accessor } from 'solid-js';
|
|
3
|
-
export interface SolidQueuer<TValue, TSelected =
|
|
4
|
+
export interface SolidQueuer<TValue, TSelected = {}> extends Omit<Queuer<TValue>, 'store'> {
|
|
4
5
|
/**
|
|
5
6
|
* Reactive state that will be updated when the queuer state changes
|
|
6
7
|
*
|
|
7
8
|
* Use this instead of `queuer.store.state`
|
|
8
9
|
*/
|
|
9
10
|
readonly state: Accessor<Readonly<TSelected>>;
|
|
11
|
+
/**
|
|
12
|
+
* @deprecated Use `queuer.state` instead of `queuer.store.state` if you want to read reactive state.
|
|
13
|
+
* The state on the store object is not reactive, as it has not been wrapped in a `useStore` hook internally.
|
|
14
|
+
* Although, you can make the state reactive by using the `useStore` in your own usage.
|
|
15
|
+
*/
|
|
16
|
+
readonly store: Store<Readonly<QueuerState<TValue>>>;
|
|
10
17
|
}
|
|
11
18
|
/**
|
|
12
19
|
* Creates a Solid-compatible Queuer instance for managing a synchronous queue of items, exposing Solid signals for all stateful properties.
|
|
@@ -25,11 +32,26 @@ export interface SolidQueuer<TValue, TSelected = QueuerState<TValue>> extends Om
|
|
|
25
32
|
*
|
|
26
33
|
* By default, the queue uses FIFO behavior, but you can configure LIFO or double-ended queueing by specifying the position when adding or removing items.
|
|
27
34
|
*
|
|
35
|
+
* ## State Management and Selector
|
|
36
|
+
*
|
|
37
|
+
* The hook uses TanStack Store for reactive state management. The `selector` parameter allows you
|
|
38
|
+
* to specify which state changes will trigger a re-render, optimizing performance by preventing
|
|
39
|
+
* unnecessary re-renders when irrelevant state changes occur.
|
|
40
|
+
*
|
|
41
|
+
* **By default, there will be no reactive state subscriptions** and you must opt-in to state
|
|
42
|
+
* tracking by providing a selector function. This prevents unnecessary re-renders and gives you
|
|
43
|
+
* full control over when your component updates. Only when you provide a selector will the
|
|
44
|
+
* component re-render when the selected state values change.
|
|
45
|
+
*
|
|
46
|
+
* Available state properties:
|
|
47
|
+
* - `executionCount`: Number of items that have been processed
|
|
48
|
+
* - `isRunning`: Whether the queuer is currently running (not stopped)
|
|
49
|
+
* - `items`: Array of items currently queued for processing
|
|
50
|
+
* - `rejectionCount`: Number of items that were rejected (expired or failed validation)
|
|
51
|
+
*
|
|
28
52
|
* Example usage:
|
|
29
53
|
* ```tsx
|
|
30
|
-
* //
|
|
31
|
-
* const [items, setItems] = createSignal([]);
|
|
32
|
-
*
|
|
54
|
+
* // Default behavior - no reactive state subscriptions
|
|
33
55
|
* const queue = createQueuer(
|
|
34
56
|
* (item) => {
|
|
35
57
|
* // process item synchronously
|
|
@@ -38,11 +60,27 @@ export interface SolidQueuer<TValue, TSelected = QueuerState<TValue>> extends Om
|
|
|
38
60
|
* {
|
|
39
61
|
* started: true, // Start processing immediately
|
|
40
62
|
* wait: 1000, // Process one item every second
|
|
41
|
-
* onItemsChange: (queue) => setItems(queue.peekAllItems()),
|
|
42
63
|
* getPriority: (item) => item.priority // Process higher priority items first
|
|
43
64
|
* }
|
|
44
65
|
* );
|
|
45
66
|
*
|
|
67
|
+
* // Opt-in to re-render when items or isRunning changes (optimized for UI updates)
|
|
68
|
+
* const queue = createQueuer(
|
|
69
|
+
* (item) => console.log('Processing', item),
|
|
70
|
+
* { started: true, wait: 1000 },
|
|
71
|
+
* (state) => ({ items: state.items, isRunning: state.isRunning })
|
|
72
|
+
* );
|
|
73
|
+
*
|
|
74
|
+
* // Opt-in to re-render when execution metrics change (optimized for tracking progress)
|
|
75
|
+
* const queue = createQueuer(
|
|
76
|
+
* (item) => console.log('Processing', item),
|
|
77
|
+
* { started: true, wait: 1000 },
|
|
78
|
+
* (state) => ({
|
|
79
|
+
* executionCount: state.executionCount,
|
|
80
|
+
* rejectionCount: state.rejectionCount
|
|
81
|
+
* })
|
|
82
|
+
* );
|
|
83
|
+
*
|
|
46
84
|
* // Add items to process - they'll be handled automatically
|
|
47
85
|
* queue.addItem('task1');
|
|
48
86
|
* queue.addItem('task2');
|
|
@@ -51,12 +89,8 @@ export interface SolidQueuer<TValue, TSelected = QueuerState<TValue>> extends Om
|
|
|
51
89
|
* queue.stop(); // Pause processing
|
|
52
90
|
* queue.start(); // Resume processing
|
|
53
91
|
*
|
|
54
|
-
* // Access
|
|
55
|
-
*
|
|
56
|
-
* console.log('Size:', queue.size());
|
|
57
|
-
* console.log('Is empty:', queue.isEmpty());
|
|
58
|
-
* console.log('Is running:', queue.isRunning());
|
|
59
|
-
* console.log('Next item:', queue.nextItem());
|
|
92
|
+
* // Access the selected state (will be empty object {} unless selector provided)
|
|
93
|
+
* const { items, isRunning } = queue.state();
|
|
60
94
|
* ```
|
|
61
95
|
*/
|
|
62
|
-
export declare function createQueuer<TValue, TSelected =
|
|
96
|
+
export declare function createQueuer<TValue, TSelected = {}>(fn: (item: TValue) => void, initialOptions?: QueuerOptions<TValue>, selector?: (state: QueuerState<TValue>) => TSelected): SolidQueuer<TValue, TSelected>;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Queuer } from "@tanstack/pacer/queuer";
|
|
2
2
|
import { useStore } from "@tanstack/solid-store";
|
|
3
|
-
function createQueuer(fn, initialOptions = {}, selector) {
|
|
3
|
+
function createQueuer(fn, initialOptions = {}, selector = () => ({})) {
|
|
4
4
|
const queuer = new Queuer(fn, initialOptions);
|
|
5
5
|
const state = useStore(queuer.store, selector);
|
|
6
6
|
return {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createQueuer.js","sources":["../../../src/queuer/createQueuer.ts"],"sourcesContent":["import { Queuer } from '@tanstack/pacer/queuer'\nimport { useStore } from '@tanstack/solid-store'\nimport type { Accessor } from 'solid-js'\nimport type { QueuerOptions, QueuerState } from '@tanstack/pacer/queuer'\n\nexport interface SolidQueuer<TValue, TSelected =
|
|
1
|
+
{"version":3,"file":"createQueuer.js","sources":["../../../src/queuer/createQueuer.ts"],"sourcesContent":["import { Queuer } from '@tanstack/pacer/queuer'\nimport { useStore } from '@tanstack/solid-store'\nimport type { Store } from '@tanstack/solid-store'\nimport type { Accessor } from 'solid-js'\nimport type { QueuerOptions, QueuerState } from '@tanstack/pacer/queuer'\n\nexport interface SolidQueuer<TValue, TSelected = {}>\n extends Omit<Queuer<TValue>, 'store'> {\n /**\n * Reactive state that will be updated when the queuer state changes\n *\n * Use this instead of `queuer.store.state`\n */\n readonly state: Accessor<Readonly<TSelected>>\n /**\n * @deprecated Use `queuer.state` instead of `queuer.store.state` if you want to read reactive state.\n * The state on the store object is not reactive, as it has not been wrapped in a `useStore` hook internally.\n * Although, you can make the state reactive by using the `useStore` in your own usage.\n */\n readonly store: Store<Readonly<QueuerState<TValue>>>\n}\n\n/**\n * Creates a Solid-compatible Queuer instance for managing a synchronous queue of items, exposing Solid signals for all stateful properties.\n *\n * Features:\n * - Synchronous processing of items using the provided `fn` function\n * - FIFO (First In First Out) or LIFO (Last In First Out) queue behavior\n * - Priority queueing via `getPriority` or item `priority` property\n * - Item expiration and removal of stale items\n * - Configurable wait time between processing items\n * - Pause/resume processing\n * - Callbacks for queue state changes, execution, rejection, and expiration\n * - All stateful properties (items, counts, etc.) are exposed as Solid signals for reactivity\n *\n * The queue processes items synchronously in order, with optional delays between each item. When started, it will process one item per tick, with an optional wait time between ticks. You can pause and resume processing with `stop()` and `start()`.\n *\n * By default, the queue uses FIFO behavior, but you can configure LIFO or double-ended queueing by specifying the position when adding or removing items.\n *\n * ## State Management and Selector\n *\n * The hook uses TanStack Store for reactive state management. The `selector` parameter allows you\n * to specify which state changes will trigger a re-render, optimizing performance by preventing\n * unnecessary re-renders when irrelevant state changes occur.\n *\n * **By default, there will be no reactive state subscriptions** and you must opt-in to state\n * tracking by providing a selector function. This prevents unnecessary re-renders and gives you\n * full control over when your component updates. Only when you provide a selector will the\n * component re-render when the selected state values change.\n *\n * Available state properties:\n * - `executionCount`: Number of items that have been processed\n * - `isRunning`: Whether the queuer is currently running (not stopped)\n * - `items`: Array of items currently queued for processing\n * - `rejectionCount`: Number of items that were rejected (expired or failed validation)\n *\n * Example usage:\n * ```tsx\n * // Default behavior - no reactive state subscriptions\n * const queue = createQueuer(\n * (item) => {\n * // process item synchronously\n * console.log('Processing', item);\n * },\n * {\n * started: true, // Start processing immediately\n * wait: 1000, // Process one item every second\n * getPriority: (item) => item.priority // Process higher priority items first\n * }\n * );\n *\n * // Opt-in to re-render when items or isRunning changes (optimized for UI updates)\n * const queue = createQueuer(\n * (item) => console.log('Processing', item),\n * { started: true, wait: 1000 },\n * (state) => ({ items: state.items, isRunning: state.isRunning })\n * );\n *\n * // Opt-in to re-render when execution metrics change (optimized for tracking progress)\n * const queue = createQueuer(\n * (item) => console.log('Processing', item),\n * { started: true, wait: 1000 },\n * (state) => ({\n * executionCount: state.executionCount,\n * rejectionCount: state.rejectionCount\n * })\n * );\n *\n * // Add items to process - they'll be handled automatically\n * queue.addItem('task1');\n * queue.addItem('task2');\n *\n * // Control the scheduler\n * queue.stop(); // Pause processing\n * queue.start(); // Resume processing\n *\n * // Access the selected state (will be empty object {} unless selector provided)\n * const { items, isRunning } = queue.state();\n * ```\n */\nexport function createQueuer<TValue, TSelected = {}>(\n fn: (item: TValue) => void,\n initialOptions: QueuerOptions<TValue> = {},\n selector: (state: QueuerState<TValue>) => TSelected = () => ({}) as TSelected,\n): SolidQueuer<TValue, TSelected> {\n const queuer = new Queuer(fn, initialOptions)\n\n const state = useStore(queuer.store, selector)\n\n return {\n ...queuer,\n state,\n } as SolidQueuer<TValue, TSelected> // omit `store` in favor of `state`\n}\n"],"names":[],"mappings":";;AAoGO,SAAS,aACd,IACA,iBAAwC,CAAA,GACxC,WAAsD,OAAO,CAAA,IAC7B;AAChC,QAAM,SAAS,IAAI,OAAO,IAAI,cAAc;AAE5C,QAAM,QAAQ,SAAS,OAAO,OAAO,QAAQ;AAE7C,SAAO;AAAA,IACL,GAAG;AAAA,IACH;AAAA,EAAA;AAEJ;"}
|
|
@@ -29,15 +29,42 @@ import { RateLimiterOptions, RateLimiterState } from '@tanstack/pacer/rate-limit
|
|
|
29
29
|
* For more direct control over rate limiting without state management,
|
|
30
30
|
* consider using the lower-level createRateLimiter hook instead.
|
|
31
31
|
*
|
|
32
|
+
* ## State Management and Selector
|
|
33
|
+
*
|
|
34
|
+
* The hook uses TanStack Store for reactive state management via the underlying rate limiter instance.
|
|
35
|
+
* The `selector` parameter allows you to specify which rate limiter state changes will trigger reactive updates,
|
|
36
|
+
* optimizing performance by preventing unnecessary subscriptions when irrelevant state changes occur.
|
|
37
|
+
*
|
|
38
|
+
* **By default, there will be no reactive state subscriptions** and you must opt-in to state
|
|
39
|
+
* tracking by providing a selector function. This prevents unnecessary reactive updates and gives you
|
|
40
|
+
* full control over when your component subscribes to state changes. Only when you provide a selector will
|
|
41
|
+
* the reactive system track the selected state values.
|
|
42
|
+
*
|
|
43
|
+
* Available rate limiter state properties:
|
|
44
|
+
* - `callsInWindow`: Number of calls made in the current window
|
|
45
|
+
* - `remainingInWindow`: Number of calls remaining in the current window
|
|
46
|
+
* - `windowStart`: Unix timestamp when the current window started
|
|
47
|
+
* - `nextWindowStart`: Unix timestamp when the next window will start
|
|
48
|
+
* - `msUntilNextWindow`: Milliseconds until the next window starts
|
|
49
|
+
* - `isAtLimit`: Whether the call limit for the current window has been reached
|
|
50
|
+
* - `status`: Current status ('disabled' | 'idle' | 'at-limit')
|
|
51
|
+
*
|
|
32
52
|
* @example
|
|
33
53
|
* ```tsx
|
|
34
|
-
* //
|
|
54
|
+
* // Default behavior - no reactive state subscriptions
|
|
35
55
|
* const [value, setValue, rateLimiter] = createRateLimitedSignal(0, {
|
|
36
56
|
* limit: 5,
|
|
37
57
|
* window: 60000,
|
|
38
58
|
* windowType: 'sliding'
|
|
39
59
|
* });
|
|
40
60
|
*
|
|
61
|
+
* // Opt-in to reactive updates when limit state changes (optimized for UI feedback)
|
|
62
|
+
* const [value, setValue, rateLimiter] = createRateLimitedSignal(
|
|
63
|
+
* 0,
|
|
64
|
+
* { limit: 5, window: 60000 },
|
|
65
|
+
* (state) => ({ isAtLimit: state.isAtLimit, remainingInWindow: state.remainingInWindow })
|
|
66
|
+
* );
|
|
67
|
+
*
|
|
41
68
|
* // With rejection callback and fixed window
|
|
42
69
|
* const [value, setValue] = createRateLimitedSignal(0, {
|
|
43
70
|
* limit: 3,
|
|
@@ -50,7 +77,7 @@ import { RateLimiterOptions, RateLimiterState } from '@tanstack/pacer/rate-limit
|
|
|
50
77
|
*
|
|
51
78
|
* // Access rateLimiter state via signals
|
|
52
79
|
* const handleSubmit = () => {
|
|
53
|
-
* const remaining = rateLimiter.
|
|
80
|
+
* const remaining = rateLimiter.state().remainingInWindow;
|
|
54
81
|
* if (remaining > 0) {
|
|
55
82
|
* setValue(newValue);
|
|
56
83
|
* } else {
|
|
@@ -59,7 +86,7 @@ import { RateLimiterOptions, RateLimiterState } from '@tanstack/pacer/rate-limit
|
|
|
59
86
|
* };
|
|
60
87
|
* ```
|
|
61
88
|
*/
|
|
62
|
-
export declare function createRateLimitedSignal<TValue, TSelected =
|
|
89
|
+
export declare function createRateLimitedSignal<TValue, TSelected = {}>(value: TValue, initialOptions: RateLimiterOptions<Setter<TValue>>, selector?: (state: RateLimiterState) => TSelected): [
|
|
63
90
|
Accessor<TValue>,
|
|
64
91
|
Setter<TValue>,
|
|
65
92
|
SolidRateLimiter<Setter<TValue>, TSelected>
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createRateLimitedSignal.js","sources":["../../../src/rate-limiter/createRateLimitedSignal.ts"],"sourcesContent":["import { createSignal } from 'solid-js'\nimport { createRateLimiter } from './createRateLimiter'\nimport type { SolidRateLimiter } from './createRateLimiter'\nimport type { Accessor, Setter } from 'solid-js'\nimport type {\n RateLimiterOptions,\n RateLimiterState,\n} from '@tanstack/pacer/rate-limiter'\n\n/**\n * A Solid hook that creates a rate-limited state value that enforces a hard limit on state updates within a time window.\n * This hook combines Solid's createSignal with rate limiting functionality to provide controlled state updates.\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 * - createThrottledSignal: When you want consistent spacing between updates (e.g. UI changes)\n * - createDebouncedSignal: 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 * - The rate-limited state value accessor\n * - A rate-limited setter function that respects the configured limits\n * - The rateLimiter instance for additional control\n *\n * For more direct control over rate limiting without state management,\n * consider using the lower-level createRateLimiter hook instead.\n *\n *
|
|
1
|
+
{"version":3,"file":"createRateLimitedSignal.js","sources":["../../../src/rate-limiter/createRateLimitedSignal.ts"],"sourcesContent":["import { createSignal } from 'solid-js'\nimport { createRateLimiter } from './createRateLimiter'\nimport type { SolidRateLimiter } from './createRateLimiter'\nimport type { Accessor, Setter } from 'solid-js'\nimport type {\n RateLimiterOptions,\n RateLimiterState,\n} from '@tanstack/pacer/rate-limiter'\n\n/**\n * A Solid hook that creates a rate-limited state value that enforces a hard limit on state updates within a time window.\n * This hook combines Solid's createSignal with rate limiting functionality to provide controlled state updates.\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 * - createThrottledSignal: When you want consistent spacing between updates (e.g. UI changes)\n * - createDebouncedSignal: 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 * - The rate-limited state value accessor\n * - A rate-limited setter function that respects the configured limits\n * - The rateLimiter instance for additional control\n *\n * For more direct control over rate limiting without state management,\n * consider using the lower-level createRateLimiter hook instead.\n *\n * ## State Management and Selector\n *\n * The hook uses TanStack Store for reactive state management via the underlying rate limiter instance.\n * The `selector` parameter allows you to specify which rate limiter state changes will trigger reactive updates,\n * optimizing performance by preventing unnecessary subscriptions when irrelevant state changes occur.\n *\n * **By default, there will be no reactive state subscriptions** and you must opt-in to state\n * tracking by providing a selector function. This prevents unnecessary reactive updates and gives you\n * full control over when your component subscribes to state changes. Only when you provide a selector will\n * the reactive system track the selected state values.\n *\n * Available rate limiter state properties:\n * - `callsInWindow`: Number of calls made in the current window\n * - `remainingInWindow`: Number of calls remaining in the current window\n * - `windowStart`: Unix timestamp when the current window started\n * - `nextWindowStart`: Unix timestamp when the next window will start\n * - `msUntilNextWindow`: Milliseconds until the next window starts\n * - `isAtLimit`: Whether the call limit for the current window has been reached\n * - `status`: Current status ('disabled' | 'idle' | 'at-limit')\n *\n * @example\n * ```tsx\n * // Default behavior - no reactive state subscriptions\n * const [value, setValue, rateLimiter] = createRateLimitedSignal(0, {\n * limit: 5,\n * window: 60000,\n * windowType: 'sliding'\n * });\n *\n * // Opt-in to reactive updates when limit state changes (optimized for UI feedback)\n * const [value, setValue, rateLimiter] = createRateLimitedSignal(\n * 0,\n * { limit: 5, window: 60000 },\n * (state) => ({ isAtLimit: state.isAtLimit, remainingInWindow: state.remainingInWindow })\n * );\n *\n * // With rejection callback and fixed window\n * const [value, setValue] = createRateLimitedSignal(0, {\n * limit: 3,\n * window: 5000,\n * windowType: 'fixed',\n * onReject: (rateLimiter) => {\n * alert(`Rate limit reached. Try again in ${rateLimiter.getMsUntilNextWindow()}ms`);\n * }\n * });\n *\n * // Access rateLimiter state via signals\n * const handleSubmit = () => {\n * const remaining = rateLimiter.state().remainingInWindow;\n * if (remaining > 0) {\n * setValue(newValue);\n * } else {\n * showRateLimitWarning();\n * }\n * };\n * ```\n */\nexport function createRateLimitedSignal<TValue, TSelected = {}>(\n value: TValue,\n initialOptions: RateLimiterOptions<Setter<TValue>>,\n selector?: (state: RateLimiterState) => TSelected,\n): [\n Accessor<TValue>,\n Setter<TValue>,\n SolidRateLimiter<Setter<TValue>, TSelected>,\n] {\n const [rateLimitedValue, setRateLimitedValue] = createSignal<TValue>(value)\n\n const rateLimiter = createRateLimiter(\n setRateLimitedValue,\n initialOptions,\n selector,\n )\n\n return [\n rateLimitedValue,\n rateLimiter.maybeExecute as Setter<TValue>,\n rateLimiter,\n ]\n}\n"],"names":[],"mappings":";;AA8FO,SAAS,wBACd,OACA,gBACA,UAKA;AACA,QAAM,CAAC,kBAAkB,mBAAmB,IAAI,aAAqB,KAAK;AAE1E,QAAM,cAAc;AAAA,IAClB;AAAA,IACA;AAAA,IACA;AAAA,EAAA;AAGF,SAAO;AAAA,IACL;AAAA,IACA,YAAY;AAAA,IACZ;AAAA,EAAA;AAEJ;"}
|
|
@@ -28,20 +28,50 @@ import { RateLimiterOptions, RateLimiterState } from '@tanstack/pacer/rate-limit
|
|
|
28
28
|
* For more direct control over rate limiting behavior without Solid state management,
|
|
29
29
|
* consider using the lower-level createRateLimiter hook instead.
|
|
30
30
|
*
|
|
31
|
+
* ## State Management and Selector
|
|
32
|
+
*
|
|
33
|
+
* The hook uses TanStack Store for reactive state management via the underlying rate limiter instance.
|
|
34
|
+
* The `selector` parameter allows you to specify which rate limiter state changes will trigger reactive updates,
|
|
35
|
+
* optimizing performance by preventing unnecessary subscriptions when irrelevant state changes occur.
|
|
36
|
+
*
|
|
37
|
+
* **By default, there will be no reactive state subscriptions** and you must opt-in to state
|
|
38
|
+
* tracking by providing a selector function. This prevents unnecessary reactive updates and gives you
|
|
39
|
+
* full control over when your component subscribes to state changes. Only when you provide a selector will
|
|
40
|
+
* the reactive system track the selected state values.
|
|
41
|
+
*
|
|
42
|
+
* Available rate limiter state properties:
|
|
43
|
+
* - `callsInWindow`: Number of calls made in the current window
|
|
44
|
+
* - `remainingInWindow`: Number of calls remaining in the current window
|
|
45
|
+
* - `windowStart`: Unix timestamp when the current window started
|
|
46
|
+
* - `nextWindowStart`: Unix timestamp when the next window will start
|
|
47
|
+
* - `msUntilNextWindow`: Milliseconds until the next window starts
|
|
48
|
+
* - `isAtLimit`: Whether the call limit for the current window has been reached
|
|
49
|
+
* - `status`: Current status ('disabled' | 'idle' | 'at-limit')
|
|
50
|
+
*
|
|
31
51
|
* @example
|
|
32
52
|
* ```tsx
|
|
33
|
-
* //
|
|
53
|
+
* // Default behavior - no reactive state subscriptions
|
|
34
54
|
* const [rateLimitedValue, rateLimiter] = createRateLimitedValue(rawValue, {
|
|
35
55
|
* limit: 5,
|
|
36
56
|
* window: 60000,
|
|
37
57
|
* windowType: 'sliding'
|
|
38
58
|
* });
|
|
39
59
|
*
|
|
60
|
+
* // Opt-in to reactive updates when limit state changes (optimized for UI feedback)
|
|
61
|
+
* const [rateLimitedValue, rateLimiter] = createRateLimitedValue(
|
|
62
|
+
* rawValue,
|
|
63
|
+
* { limit: 5, window: 60000 },
|
|
64
|
+
* (state) => ({ isAtLimit: state.isAtLimit, remainingInWindow: state.remainingInWindow })
|
|
65
|
+
* );
|
|
66
|
+
*
|
|
40
67
|
* // Use the rate-limited value
|
|
41
68
|
* console.log(rateLimitedValue()); // Access the current rate-limited value
|
|
42
69
|
*
|
|
70
|
+
* // Access rate limiter state via signals
|
|
71
|
+
* console.log('Is at limit:', rateLimiter.state().isAtLimit);
|
|
72
|
+
*
|
|
43
73
|
* // Control the rate limiter
|
|
44
74
|
* rateLimiter.reset(); // Reset the rate limit window
|
|
45
75
|
* ```
|
|
46
76
|
*/
|
|
47
|
-
export declare function createRateLimitedValue<TValue, TSelected =
|
|
77
|
+
export declare function createRateLimitedValue<TValue, TSelected = {}>(value: Accessor<TValue>, initialOptions: RateLimiterOptions<Setter<TValue>>, selector?: (state: RateLimiterState) => TSelected): [Accessor<TValue>, SolidRateLimiter<Setter<TValue>, TSelected>];
|
|
@@ -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 {\n RateLimiterOptions,\n RateLimiterState,\n} 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 *
|
|
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 {\n RateLimiterOptions,\n RateLimiterState,\n} 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 * ## State Management and Selector\n *\n * The hook uses TanStack Store for reactive state management via the underlying rate limiter instance.\n * The `selector` parameter allows you to specify which rate limiter state changes will trigger reactive updates,\n * optimizing performance by preventing unnecessary subscriptions when irrelevant state changes occur.\n *\n * **By default, there will be no reactive state subscriptions** and you must opt-in to state\n * tracking by providing a selector function. This prevents unnecessary reactive updates and gives you\n * full control over when your component subscribes to state changes. Only when you provide a selector will\n * the reactive system track the selected state values.\n *\n * Available rate limiter state properties:\n * - `callsInWindow`: Number of calls made in the current window\n * - `remainingInWindow`: Number of calls remaining in the current window\n * - `windowStart`: Unix timestamp when the current window started\n * - `nextWindowStart`: Unix timestamp when the next window will start\n * - `msUntilNextWindow`: Milliseconds until the next window starts\n * - `isAtLimit`: Whether the call limit for the current window has been reached\n * - `status`: Current status ('disabled' | 'idle' | 'at-limit')\n *\n * @example\n * ```tsx\n * // Default behavior - no reactive state subscriptions\n * const [rateLimitedValue, rateLimiter] = createRateLimitedValue(rawValue, {\n * limit: 5,\n * window: 60000,\n * windowType: 'sliding'\n * });\n *\n * // Opt-in to reactive updates when limit state changes (optimized for UI feedback)\n * const [rateLimitedValue, rateLimiter] = createRateLimitedValue(\n * rawValue,\n * { limit: 5, window: 60000 },\n * (state) => ({ isAtLimit: state.isAtLimit, remainingInWindow: state.remainingInWindow })\n * );\n *\n * // Use the rate-limited value\n * console.log(rateLimitedValue()); // Access the current rate-limited value\n *\n * // Access rate limiter state via signals\n * console.log('Is at limit:', rateLimiter.state().isAtLimit);\n *\n * // Control the rate limiter\n * rateLimiter.reset(); // Reset the rate limit window\n * ```\n */\nexport function createRateLimitedValue<TValue, TSelected = {}>(\n value: Accessor<TValue>,\n initialOptions: RateLimiterOptions<Setter<TValue>>,\n selector?: (state: RateLimiterState) => TSelected,\n): [Accessor<TValue>, SolidRateLimiter<Setter<TValue>, TSelected>] {\n const [rateLimitedValue, setRateLimitedValue, rateLimiter] =\n createRateLimitedSignal(value(), initialOptions, selector)\n\n createEffect(() => {\n setRateLimitedValue(value() as any)\n })\n\n return [rateLimitedValue, rateLimiter]\n}\n"],"names":[],"mappings":";;AAkFO,SAAS,uBACd,OACA,gBACA,UACiE;AACjE,QAAM,CAAC,kBAAkB,qBAAqB,WAAW,IACvD,wBAAwB,MAAA,GAAS,gBAAgB,QAAQ;AAE3D,eAAa,MAAM;AACjB,wBAAoB,OAAc;AAAA,EAAA,CACnC;AAED,SAAO,CAAC,kBAAkB,WAAW;AACvC;"}
|
|
@@ -1,13 +1,20 @@
|
|
|
1
1
|
import { RateLimiter, RateLimiterOptions, RateLimiterState } from '@tanstack/pacer/rate-limiter';
|
|
2
|
+
import { Store } from '@tanstack/solid-store';
|
|
2
3
|
import { Accessor } from 'solid-js';
|
|
3
4
|
import { AnyFunction } from '@tanstack/pacer/types';
|
|
4
|
-
export interface SolidRateLimiter<TFn extends AnyFunction, TSelected =
|
|
5
|
+
export interface SolidRateLimiter<TFn extends AnyFunction, TSelected = {}> extends Omit<RateLimiter<TFn>, 'store'> {
|
|
5
6
|
/**
|
|
6
7
|
* Reactive state that will be updated when the rate limiter state changes
|
|
7
8
|
*
|
|
8
9
|
* Use this instead of `rateLimiter.store.state`
|
|
9
10
|
*/
|
|
10
11
|
readonly state: Accessor<Readonly<TSelected>>;
|
|
12
|
+
/**
|
|
13
|
+
* @deprecated Use `rateLimiter.state` instead of `rateLimiter.store.state` if you want to read reactive state.
|
|
14
|
+
* The state on the store object is not reactive, as it has not been wrapped in a `useStore` hook internally.
|
|
15
|
+
* Although, you can make the state reactive by using the `useStore` in your own usage.
|
|
16
|
+
*/
|
|
17
|
+
readonly store: Store<Readonly<RateLimiterState>>;
|
|
11
18
|
}
|
|
12
19
|
/**
|
|
13
20
|
* A low-level Solid hook that creates a `RateLimiter` instance to enforce rate limits on function execution.
|
|
@@ -30,9 +37,27 @@ export interface SolidRateLimiter<TFn extends AnyFunction, TSelected = RateLimit
|
|
|
30
37
|
* - Use debouncing when you want to collapse rapid-fire events (e.g. search input)
|
|
31
38
|
* - Use rate limiting only when you need to enforce hard limits (e.g. API rate limits)
|
|
32
39
|
*
|
|
40
|
+
* ## State Management and Selector
|
|
41
|
+
*
|
|
42
|
+
* The hook uses TanStack Store for reactive state management. The `selector` parameter allows you
|
|
43
|
+
* to specify which state changes will trigger a re-render, optimizing performance by preventing
|
|
44
|
+
* unnecessary re-renders when irrelevant state changes occur.
|
|
45
|
+
*
|
|
46
|
+
* **By default, there will be no reactive state subscriptions** and you must opt-in to state
|
|
47
|
+
* tracking by providing a selector function. This prevents unnecessary re-renders and gives you
|
|
48
|
+
* full control over when your component updates. Only when you provide a selector will the
|
|
49
|
+
* component re-render when the selected state values change.
|
|
50
|
+
*
|
|
51
|
+
* Available state properties:
|
|
52
|
+
* - `executionCount`: Number of function executions that have been completed
|
|
53
|
+
* - `rejectionCount`: Number of function calls that were rejected due to rate limiting
|
|
54
|
+
* - `remainingInWindow`: Number of executions remaining in the current window
|
|
55
|
+
* - `nextWindowTime`: Timestamp when the next window begins
|
|
56
|
+
* - `currentWindowStart`: Timestamp when the current window started
|
|
57
|
+
*
|
|
33
58
|
* @example
|
|
34
59
|
* ```tsx
|
|
35
|
-
* //
|
|
60
|
+
* // Default behavior - no reactive state subscriptions
|
|
36
61
|
* const rateLimiter = createRateLimiter(apiCall, {
|
|
37
62
|
* limit: 5,
|
|
38
63
|
* window: 60000,
|
|
@@ -42,11 +67,28 @@ export interface SolidRateLimiter<TFn extends AnyFunction, TSelected = RateLimit
|
|
|
42
67
|
* }
|
|
43
68
|
* });
|
|
44
69
|
*
|
|
45
|
-
* //
|
|
46
|
-
*
|
|
47
|
-
*
|
|
48
|
-
*
|
|
49
|
-
*
|
|
70
|
+
* // Opt-in to re-render when rate limit state changes (optimized for UI feedback)
|
|
71
|
+
* const rateLimiter = createRateLimiter(
|
|
72
|
+
* apiCall,
|
|
73
|
+
* { limit: 5, window: 60000 },
|
|
74
|
+
* (state) => ({
|
|
75
|
+
* remainingInWindow: state.remainingInWindow,
|
|
76
|
+
* rejectionCount: state.rejectionCount
|
|
77
|
+
* })
|
|
78
|
+
* );
|
|
79
|
+
*
|
|
80
|
+
* // Opt-in to re-render when execution metrics change (optimized for tracking progress)
|
|
81
|
+
* const rateLimiter = createRateLimiter(
|
|
82
|
+
* apiCall,
|
|
83
|
+
* { limit: 5, window: 60000 },
|
|
84
|
+
* (state) => ({
|
|
85
|
+
* executionCount: state.executionCount,
|
|
86
|
+
* nextWindowTime: state.nextWindowTime
|
|
87
|
+
* })
|
|
88
|
+
* );
|
|
89
|
+
*
|
|
90
|
+
* // Access the selected state (will be empty object {} unless selector provided)
|
|
91
|
+
* const { remainingInWindow, rejectionCount } = rateLimiter.state();
|
|
50
92
|
* ```
|
|
51
93
|
*/
|
|
52
|
-
export declare function createRateLimiter<TFn extends AnyFunction, TSelected =
|
|
94
|
+
export declare function createRateLimiter<TFn extends AnyFunction, TSelected = {}>(fn: TFn, initialOptions: RateLimiterOptions<TFn>, selector?: (state: RateLimiterState) => TSelected): SolidRateLimiter<TFn, TSelected>;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { RateLimiter } from "@tanstack/pacer/rate-limiter";
|
|
2
2
|
import { useStore } from "@tanstack/solid-store";
|
|
3
|
-
function createRateLimiter(fn, initialOptions, selector) {
|
|
3
|
+
function createRateLimiter(fn, initialOptions, selector = () => ({})) {
|
|
4
4
|
const rateLimiter = new RateLimiter(fn, initialOptions);
|
|
5
5
|
const state = useStore(rateLimiter.store, selector);
|
|
6
6
|
return {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createRateLimiter.js","sources":["../../../src/rate-limiter/createRateLimiter.ts"],"sourcesContent":["import { RateLimiter } from '@tanstack/pacer/rate-limiter'\nimport { useStore } from '@tanstack/solid-store'\nimport type { Accessor } from 'solid-js'\nimport type { AnyFunction } from '@tanstack/pacer/types'\nimport type {\n RateLimiterOptions,\n RateLimiterState,\n} 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 { useStore } from '@tanstack/solid-store'\nimport type { Store } from '@tanstack/solid-store'\nimport type { Accessor } from 'solid-js'\nimport type { AnyFunction } from '@tanstack/pacer/types'\nimport type {\n RateLimiterOptions,\n RateLimiterState,\n} from '@tanstack/pacer/rate-limiter'\n\nexport interface SolidRateLimiter<TFn extends AnyFunction, TSelected = {}>\n extends Omit<RateLimiter<TFn>, 'store'> {\n /**\n * Reactive state that will be updated when the rate limiter state changes\n *\n * Use this instead of `rateLimiter.store.state`\n */\n readonly state: Accessor<Readonly<TSelected>>\n /**\n * @deprecated Use `rateLimiter.state` instead of `rateLimiter.store.state` if you want to read reactive state.\n * The state on the store object is not reactive, as it has not been wrapped in a `useStore` hook internally.\n * Although, you can make the state reactive by using the `useStore` in your own usage.\n */\n readonly store: Store<Readonly<RateLimiterState>>\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 * ## State Management and Selector\n *\n * The hook uses TanStack Store for reactive state management. The `selector` parameter allows you\n * to specify which state changes will trigger a re-render, optimizing performance by preventing\n * unnecessary re-renders when irrelevant state changes occur.\n *\n * **By default, there will be no reactive state subscriptions** and you must opt-in to state\n * tracking by providing a selector function. This prevents unnecessary re-renders and gives you\n * full control over when your component updates. Only when you provide a selector will the\n * component re-render when the selected state values change.\n *\n * Available state properties:\n * - `executionCount`: Number of function executions that have been completed\n * - `rejectionCount`: Number of function calls that were rejected due to rate limiting\n * - `remainingInWindow`: Number of executions remaining in the current window\n * - `nextWindowTime`: Timestamp when the next window begins\n * - `currentWindowStart`: Timestamp when the current window started\n *\n * @example\n * ```tsx\n * // Default behavior - no reactive state subscriptions\n * const rateLimiter = createRateLimiter(apiCall, {\n * limit: 5,\n * window: 60000,\n * windowType: 'sliding',\n * onReject: (rateLimiter) => {\n * console.log(`Rate limit exceeded. Try again in ${rateLimiter.getMsUntilNextWindow()}ms`);\n * }\n * });\n *\n * // Opt-in to re-render when rate limit state changes (optimized for UI feedback)\n * const rateLimiter = createRateLimiter(\n * apiCall,\n * { limit: 5, window: 60000 },\n * (state) => ({\n * remainingInWindow: state.remainingInWindow,\n * rejectionCount: state.rejectionCount\n * })\n * );\n *\n * // Opt-in to re-render when execution metrics change (optimized for tracking progress)\n * const rateLimiter = createRateLimiter(\n * apiCall,\n * { limit: 5, window: 60000 },\n * (state) => ({\n * executionCount: state.executionCount,\n * nextWindowTime: state.nextWindowTime\n * })\n * );\n *\n * // Access the selected state (will be empty object {} unless selector provided)\n * const { remainingInWindow, rejectionCount } = rateLimiter.state();\n * ```\n */\nexport function createRateLimiter<TFn extends AnyFunction, TSelected = {}>(\n fn: TFn,\n initialOptions: RateLimiterOptions<TFn>,\n selector: (state: RateLimiterState) => TSelected = () => ({}) as TSelected,\n): SolidRateLimiter<TFn, TSelected> {\n const rateLimiter = new RateLimiter<TFn>(fn, initialOptions)\n\n const state = useStore(rateLimiter.store, selector)\n\n return {\n ...rateLimiter,\n state,\n } as SolidRateLimiter<TFn, TSelected> // omit `store` in favor of `state`\n}\n"],"names":[],"mappings":";;AAqGO,SAAS,kBACd,IACA,gBACA,WAAmD,OAAO,CAAA,IACxB;AAClC,QAAM,cAAc,IAAI,YAAiB,IAAI,cAAc;AAE3D,QAAM,QAAQ,SAAS,YAAY,OAAO,QAAQ;AAElD,SAAO;AAAA,IACL,GAAG;AAAA,IACH;AAAA,EAAA;AAEJ;"}
|
|
@@ -16,11 +16,39 @@ import { ThrottlerOptions, ThrottlerState } from '@tanstack/pacer/throttler';
|
|
|
16
16
|
* For more direct control over throttling without state management,
|
|
17
17
|
* consider using the lower-level createThrottler hook instead.
|
|
18
18
|
*
|
|
19
|
+
* ## State Management and Selector
|
|
20
|
+
*
|
|
21
|
+
* The hook uses TanStack Store for reactive state management via the underlying throttler instance.
|
|
22
|
+
* The `selector` parameter allows you to specify which throttler state changes will trigger reactive updates,
|
|
23
|
+
* optimizing performance by preventing unnecessary subscriptions when irrelevant state changes occur.
|
|
24
|
+
*
|
|
25
|
+
* **By default, there will be no reactive state subscriptions** and you must opt-in to state
|
|
26
|
+
* tracking by providing a selector function. This prevents unnecessary reactive updates and gives you
|
|
27
|
+
* full control over when your component subscribes to state changes. Only when you provide a selector will
|
|
28
|
+
* the reactive system track the selected state values.
|
|
29
|
+
*
|
|
30
|
+
* Available throttler state properties:
|
|
31
|
+
* - `canLeadingExecute`: Whether the throttler can execute on the leading edge
|
|
32
|
+
* - `canTrailingExecute`: Whether the throttler can execute on the trailing edge
|
|
33
|
+
* - `executionCount`: Number of function executions that have been completed
|
|
34
|
+
* - `isPending`: Whether the throttler is waiting for the timeout to trigger trailing execution
|
|
35
|
+
* - `lastArgs`: The arguments from the most recent call to maybeExecute
|
|
36
|
+
* - `lastExecutionTime`: Unix timestamp of the last execution
|
|
37
|
+
* - `nextExecutionTime`: Unix timestamp of the next allowed execution
|
|
38
|
+
* - `status`: Current execution status ('disabled' | 'idle' | 'pending')
|
|
39
|
+
*
|
|
19
40
|
* @example
|
|
20
41
|
* ```tsx
|
|
21
|
-
* //
|
|
42
|
+
* // Default behavior - no reactive state subscriptions
|
|
22
43
|
* const [value, setValue, throttler] = createThrottledSignal(0, { wait: 1000 });
|
|
23
44
|
*
|
|
45
|
+
* // Opt-in to reactive updates when pending state changes (optimized for loading indicators)
|
|
46
|
+
* const [value, setValue, throttler] = createThrottledSignal(
|
|
47
|
+
* 0,
|
|
48
|
+
* { wait: 1000 },
|
|
49
|
+
* (state) => ({ isPending: state.isPending })
|
|
50
|
+
* );
|
|
51
|
+
*
|
|
24
52
|
* // With custom leading/trailing behavior
|
|
25
53
|
* const [value, setValue] = createThrottledSignal(0, {
|
|
26
54
|
* wait: 1000,
|
|
@@ -29,13 +57,13 @@ import { ThrottlerOptions, ThrottlerState } from '@tanstack/pacer/throttler';
|
|
|
29
57
|
* });
|
|
30
58
|
*
|
|
31
59
|
* // Access throttler state via signals
|
|
32
|
-
* console.log('Executions:', throttler.
|
|
33
|
-
* console.log('Is pending:', throttler.
|
|
34
|
-
* console.log('Last execution:', throttler.
|
|
35
|
-
* console.log('Next execution:', throttler.
|
|
60
|
+
* console.log('Executions:', throttler.state().executionCount);
|
|
61
|
+
* console.log('Is pending:', throttler.state().isPending);
|
|
62
|
+
* console.log('Last execution:', throttler.state().lastExecutionTime);
|
|
63
|
+
* console.log('Next execution:', throttler.state().nextExecutionTime);
|
|
36
64
|
* ```
|
|
37
65
|
*/
|
|
38
|
-
export declare function createThrottledSignal<TValue, TSelected =
|
|
66
|
+
export declare function createThrottledSignal<TValue, TSelected = {}>(value: TValue, initialOptions: ThrottlerOptions<Setter<TValue>>, selector?: (state: ThrottlerState<Setter<TValue>>) => TSelected): [
|
|
39
67
|
Accessor<TValue>,
|
|
40
68
|
Setter<TValue>,
|
|
41
69
|
SolidThrottler<Setter<TValue>, TSelected>
|
|
@@ -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 {\n ThrottlerOptions,\n ThrottlerState,\n} 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 * //
|
|
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 {\n ThrottlerOptions,\n ThrottlerState,\n} 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 * ## State Management and Selector\n *\n * The hook uses TanStack Store for reactive state management via the underlying throttler instance.\n * The `selector` parameter allows you to specify which throttler state changes will trigger reactive updates,\n * optimizing performance by preventing unnecessary subscriptions when irrelevant state changes occur.\n *\n * **By default, there will be no reactive state subscriptions** and you must opt-in to state\n * tracking by providing a selector function. This prevents unnecessary reactive updates and gives you\n * full control over when your component subscribes to state changes. Only when you provide a selector will\n * the reactive system track the selected state values.\n *\n * Available throttler state properties:\n * - `canLeadingExecute`: Whether the throttler can execute on the leading edge\n * - `canTrailingExecute`: Whether the throttler can execute on the trailing edge\n * - `executionCount`: Number of function executions that have been completed\n * - `isPending`: Whether the throttler is waiting for the timeout to trigger trailing execution\n * - `lastArgs`: The arguments from the most recent call to maybeExecute\n * - `lastExecutionTime`: Unix timestamp of the last execution\n * - `nextExecutionTime`: Unix timestamp of the next allowed execution\n * - `status`: Current execution status ('disabled' | 'idle' | 'pending')\n *\n * @example\n * ```tsx\n * // Default behavior - no reactive state subscriptions\n * const [value, setValue, throttler] = createThrottledSignal(0, { wait: 1000 });\n *\n * // Opt-in to reactive updates when pending state changes (optimized for loading indicators)\n * const [value, setValue, throttler] = createThrottledSignal(\n * 0,\n * { wait: 1000 },\n * (state) => ({ isPending: state.isPending })\n * );\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.state().executionCount);\n * console.log('Is pending:', throttler.state().isPending);\n * console.log('Last execution:', throttler.state().lastExecutionTime);\n * console.log('Next execution:', throttler.state().nextExecutionTime);\n * ```\n */\nexport function createThrottledSignal<TValue, TSelected = {}>(\n value: TValue,\n initialOptions: ThrottlerOptions<Setter<TValue>>,\n selector?: (state: ThrottlerState<Setter<TValue>>) => TSelected,\n): [\n Accessor<TValue>,\n Setter<TValue>,\n SolidThrottler<Setter<TValue>, TSelected>,\n] {\n const [throttledValue, setThrottledValue] = createSignal<TValue>(value)\n const throttler = createThrottler(setThrottledValue, initialOptions, selector)\n return [throttledValue, throttler.maybeExecute as Setter<TValue>, throttler]\n}\n"],"names":[],"mappings":";;AAuEO,SAAS,sBACd,OACA,gBACA,UAKA;AACA,QAAM,CAAC,gBAAgB,iBAAiB,IAAI,aAAqB,KAAK;AACtE,QAAM,YAAY,gBAAgB,mBAAmB,gBAAgB,QAAQ;AAC7E,SAAO,CAAC,gBAAgB,UAAU,cAAgC,SAAS;AAC7E;"}
|
|
@@ -17,16 +17,47 @@ import { ThrottlerOptions, ThrottlerState } from '@tanstack/pacer/throttler';
|
|
|
17
17
|
* For more direct control over throttling behavior without Solid state management,
|
|
18
18
|
* consider using the lower-level createThrottler hook instead.
|
|
19
19
|
*
|
|
20
|
+
* ## State Management and Selector
|
|
21
|
+
*
|
|
22
|
+
* The hook uses TanStack Store for reactive state management via the underlying throttler instance.
|
|
23
|
+
* The `selector` parameter allows you to specify which throttler state changes will trigger reactive updates,
|
|
24
|
+
* optimizing performance by preventing unnecessary subscriptions when irrelevant state changes occur.
|
|
25
|
+
*
|
|
26
|
+
* **By default, there will be no reactive state subscriptions** and you must opt-in to state
|
|
27
|
+
* tracking by providing a selector function. This prevents unnecessary reactive updates and gives you
|
|
28
|
+
* full control over when your component subscribes to state changes. Only when you provide a selector will
|
|
29
|
+
* the reactive system track the selected state values.
|
|
30
|
+
*
|
|
31
|
+
* Available throttler state properties:
|
|
32
|
+
* - `canLeadingExecute`: Whether the throttler can execute on the leading edge
|
|
33
|
+
* - `canTrailingExecute`: Whether the throttler can execute on the trailing edge
|
|
34
|
+
* - `executionCount`: Number of function executions that have been completed
|
|
35
|
+
* - `isPending`: Whether the throttler is waiting for the timeout to trigger trailing execution
|
|
36
|
+
* - `lastArgs`: The arguments from the most recent call to maybeExecute
|
|
37
|
+
* - `lastExecutionTime`: Unix timestamp of the last execution
|
|
38
|
+
* - `nextExecutionTime`: Unix timestamp of the next allowed execution
|
|
39
|
+
* - `status`: Current execution status ('disabled' | 'idle' | 'pending')
|
|
40
|
+
*
|
|
20
41
|
* @example
|
|
21
42
|
* ```tsx
|
|
22
|
-
* //
|
|
43
|
+
* // Default behavior - no reactive state subscriptions
|
|
23
44
|
* const [throttledValue, throttler] = createThrottledValue(rawValue, { wait: 1000 });
|
|
24
45
|
*
|
|
46
|
+
* // Opt-in to reactive updates when pending state changes (optimized for loading indicators)
|
|
47
|
+
* const [throttledValue, throttler] = createThrottledValue(
|
|
48
|
+
* rawValue,
|
|
49
|
+
* { wait: 1000 },
|
|
50
|
+
* (state) => ({ isPending: state.isPending })
|
|
51
|
+
* );
|
|
52
|
+
*
|
|
25
53
|
* // Use the throttled value
|
|
26
54
|
* console.log(throttledValue()); // Access the current throttled value
|
|
27
55
|
*
|
|
56
|
+
* // Access throttler state via signals
|
|
57
|
+
* console.log('Is pending:', throttler.state().isPending);
|
|
58
|
+
*
|
|
28
59
|
* // Control the throttler
|
|
29
60
|
* throttler.cancel(); // Cancel any pending updates
|
|
30
61
|
* ```
|
|
31
62
|
*/
|
|
32
|
-
export declare function createThrottledValue<TValue, TSelected =
|
|
63
|
+
export declare function createThrottledValue<TValue, TSelected = {}>(value: Accessor<TValue>, initialOptions: ThrottlerOptions<Setter<TValue>>, selector?: (state: ThrottlerState<Setter<TValue>>) => TSelected): [Accessor<TValue>, SolidThrottler<Setter<TValue>, TSelected>];
|
|
@@ -1 +1 @@
|
|
|
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 {\n ThrottlerOptions,\n ThrottlerState,\n} 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 * //
|
|
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 {\n ThrottlerOptions,\n ThrottlerState,\n} 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 * ## State Management and Selector\n *\n * The hook uses TanStack Store for reactive state management via the underlying throttler instance.\n * The `selector` parameter allows you to specify which throttler state changes will trigger reactive updates,\n * optimizing performance by preventing unnecessary subscriptions when irrelevant state changes occur.\n *\n * **By default, there will be no reactive state subscriptions** and you must opt-in to state\n * tracking by providing a selector function. This prevents unnecessary reactive updates and gives you\n * full control over when your component subscribes to state changes. Only when you provide a selector will\n * the reactive system track the selected state values.\n *\n * Available throttler state properties:\n * - `canLeadingExecute`: Whether the throttler can execute on the leading edge\n * - `canTrailingExecute`: Whether the throttler can execute on the trailing edge\n * - `executionCount`: Number of function executions that have been completed\n * - `isPending`: Whether the throttler is waiting for the timeout to trigger trailing execution\n * - `lastArgs`: The arguments from the most recent call to maybeExecute\n * - `lastExecutionTime`: Unix timestamp of the last execution\n * - `nextExecutionTime`: Unix timestamp of the next allowed execution\n * - `status`: Current execution status ('disabled' | 'idle' | 'pending')\n *\n * @example\n * ```tsx\n * // Default behavior - no reactive state subscriptions\n * const [throttledValue, throttler] = createThrottledValue(rawValue, { wait: 1000 });\n *\n * // Opt-in to reactive updates when pending state changes (optimized for loading indicators)\n * const [throttledValue, throttler] = createThrottledValue(\n * rawValue,\n * { wait: 1000 },\n * (state) => ({ isPending: state.isPending })\n * );\n *\n * // Use the throttled value\n * console.log(throttledValue()); // Access the current throttled value\n *\n * // Access throttler state via signals\n * console.log('Is pending:', throttler.state().isPending);\n *\n * // Control the throttler\n * throttler.cancel(); // Cancel any pending updates\n * ```\n */\nexport function createThrottledValue<TValue, TSelected = {}>(\n value: Accessor<TValue>,\n initialOptions: ThrottlerOptions<Setter<TValue>>,\n selector?: (state: ThrottlerState<Setter<TValue>>) => TSelected,\n): [Accessor<TValue>, SolidThrottler<Setter<TValue>, TSelected>] {\n const [throttledValue, setThrottledValue, throttler] = createThrottledSignal(\n value(),\n initialOptions,\n selector,\n )\n\n createEffect(() => {\n setThrottledValue(value() as any)\n })\n\n return [throttledValue, throttler]\n}\n"],"names":[],"mappings":";;AAoEO,SAAS,qBACd,OACA,gBACA,UAC+D;AAC/D,QAAM,CAAC,gBAAgB,mBAAmB,SAAS,IAAI;AAAA,IACrD,MAAA;AAAA,IACA;AAAA,IACA;AAAA,EAAA;AAGF,eAAa,MAAM;AACjB,sBAAkB,OAAc;AAAA,EAAA,CACjC;AAED,SAAO,CAAC,gBAAgB,SAAS;AACnC;"}
|