@tanstack/solid-pacer 0.8.0 → 0.9.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 +14 -0
- package/dist/cjs/async-batcher/createAsyncBatcher.cjs.map +1 -0
- package/dist/cjs/async-batcher/createAsyncBatcher.d.cts +72 -0
- package/dist/cjs/async-batcher/index.cjs +12 -0
- package/dist/cjs/{compare → async-batcher}/index.cjs.map +1 -1
- package/dist/cjs/async-batcher/index.d.cts +2 -0
- package/dist/cjs/async-debouncer/createAsyncDebouncer.cjs +9 -36
- package/dist/cjs/async-debouncer/createAsyncDebouncer.cjs.map +1 -1
- package/dist/cjs/async-debouncer/createAsyncDebouncer.d.cts +10 -9
- package/dist/cjs/async-queuer/createAsyncQueuer.cjs +5 -73
- package/dist/cjs/async-queuer/createAsyncQueuer.cjs.map +1 -1
- package/dist/cjs/async-queuer/createAsyncQueuer.d.cts +7 -53
- package/dist/cjs/async-rate-limiter/createAsyncRateLimiter.cjs +5 -52
- package/dist/cjs/async-rate-limiter/createAsyncRateLimiter.cjs.map +1 -1
- package/dist/cjs/async-rate-limiter/createAsyncRateLimiter.d.cts +4 -9
- package/dist/cjs/async-throttler/createAsyncThrottler.cjs +5 -55
- package/dist/cjs/async-throttler/createAsyncThrottler.cjs.map +1 -1
- package/dist/cjs/async-throttler/createAsyncThrottler.d.cts +9 -11
- package/dist/cjs/batcher/createBatcher.cjs +5 -49
- package/dist/cjs/batcher/createBatcher.cjs.map +1 -1
- package/dist/cjs/batcher/createBatcher.d.cts +9 -27
- package/dist/cjs/debouncer/createDebouncedSignal.cjs +2 -2
- package/dist/cjs/debouncer/createDebouncedSignal.cjs.map +1 -1
- package/dist/cjs/debouncer/createDebouncedSignal.d.cts +6 -2
- package/dist/cjs/debouncer/createDebouncedValue.cjs +3 -2
- package/dist/cjs/debouncer/createDebouncedValue.cjs.map +1 -1
- package/dist/cjs/debouncer/createDebouncedValue.d.cts +2 -2
- package/dist/cjs/debouncer/createDebouncer.cjs +7 -24
- package/dist/cjs/debouncer/createDebouncer.cjs.map +1 -1
- package/dist/cjs/debouncer/createDebouncer.d.cts +9 -8
- package/dist/cjs/index.cjs +2 -0
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.cts +1 -0
- package/dist/cjs/queuer/createQueuer.cjs +5 -59
- package/dist/cjs/queuer/createQueuer.cjs.map +1 -1
- package/dist/cjs/queuer/createQueuer.d.cts +7 -37
- package/dist/cjs/rate-limiter/createRateLimitedSignal.cjs +7 -3
- package/dist/cjs/rate-limiter/createRateLimitedSignal.cjs.map +1 -1
- package/dist/cjs/rate-limiter/createRateLimitedSignal.d.cts +6 -2
- package/dist/cjs/rate-limiter/createRateLimitedValue.cjs +2 -2
- package/dist/cjs/rate-limiter/createRateLimitedValue.cjs.map +1 -1
- package/dist/cjs/rate-limiter/createRateLimitedValue.d.cts +2 -2
- package/dist/cjs/rate-limiter/createRateLimiter.cjs +5 -43
- package/dist/cjs/rate-limiter/createRateLimiter.cjs.map +1 -1
- package/dist/cjs/rate-limiter/createRateLimiter.d.cts +9 -7
- package/dist/cjs/throttler/createThrottledSignal.cjs +2 -2
- package/dist/cjs/throttler/createThrottledSignal.cjs.map +1 -1
- package/dist/cjs/throttler/createThrottledSignal.d.cts +6 -2
- package/dist/cjs/throttler/createThrottledValue.cjs +3 -2
- package/dist/cjs/throttler/createThrottledValue.cjs.map +1 -1
- package/dist/cjs/throttler/createThrottledValue.d.cts +2 -2
- package/dist/cjs/throttler/createThrottler.cjs +7 -34
- package/dist/cjs/throttler/createThrottler.cjs.map +1 -1
- package/dist/cjs/throttler/createThrottler.d.cts +9 -10
- package/dist/esm/async-batcher/createAsyncBatcher.d.ts +72 -0
- package/dist/esm/async-batcher/createAsyncBatcher.js +14 -0
- package/dist/esm/async-batcher/createAsyncBatcher.js.map +1 -0
- package/dist/esm/async-batcher/index.d.ts +2 -0
- package/dist/esm/async-batcher/index.js +6 -0
- package/dist/esm/{compare → async-batcher}/index.js.map +1 -1
- package/dist/esm/async-debouncer/createAsyncDebouncer.d.ts +10 -9
- package/dist/esm/async-debouncer/createAsyncDebouncer.js +10 -37
- package/dist/esm/async-debouncer/createAsyncDebouncer.js.map +1 -1
- package/dist/esm/async-queuer/createAsyncQueuer.d.ts +7 -53
- package/dist/esm/async-queuer/createAsyncQueuer.js +5 -73
- package/dist/esm/async-queuer/createAsyncQueuer.js.map +1 -1
- package/dist/esm/async-rate-limiter/createAsyncRateLimiter.d.ts +4 -9
- package/dist/esm/async-rate-limiter/createAsyncRateLimiter.js +5 -52
- package/dist/esm/async-rate-limiter/createAsyncRateLimiter.js.map +1 -1
- package/dist/esm/async-throttler/createAsyncThrottler.d.ts +9 -11
- package/dist/esm/async-throttler/createAsyncThrottler.js +5 -55
- package/dist/esm/async-throttler/createAsyncThrottler.js.map +1 -1
- package/dist/esm/batcher/createBatcher.d.ts +9 -27
- package/dist/esm/batcher/createBatcher.js +5 -49
- package/dist/esm/batcher/createBatcher.js.map +1 -1
- package/dist/esm/debouncer/createDebouncedSignal.d.ts +6 -2
- package/dist/esm/debouncer/createDebouncedSignal.js +2 -2
- package/dist/esm/debouncer/createDebouncedSignal.js.map +1 -1
- package/dist/esm/debouncer/createDebouncedValue.d.ts +2 -2
- package/dist/esm/debouncer/createDebouncedValue.js +3 -2
- package/dist/esm/debouncer/createDebouncedValue.js.map +1 -1
- package/dist/esm/debouncer/createDebouncer.d.ts +9 -8
- package/dist/esm/debouncer/createDebouncer.js +8 -25
- package/dist/esm/debouncer/createDebouncer.js.map +1 -1
- package/dist/esm/index.d.ts +1 -0
- package/dist/esm/index.js +2 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/queuer/createQueuer.d.ts +7 -37
- package/dist/esm/queuer/createQueuer.js +5 -59
- package/dist/esm/queuer/createQueuer.js.map +1 -1
- package/dist/esm/rate-limiter/createRateLimitedSignal.d.ts +6 -2
- package/dist/esm/rate-limiter/createRateLimitedSignal.js +7 -3
- package/dist/esm/rate-limiter/createRateLimitedSignal.js.map +1 -1
- package/dist/esm/rate-limiter/createRateLimitedValue.d.ts +2 -2
- package/dist/esm/rate-limiter/createRateLimitedValue.js +2 -2
- package/dist/esm/rate-limiter/createRateLimitedValue.js.map +1 -1
- package/dist/esm/rate-limiter/createRateLimiter.d.ts +9 -7
- package/dist/esm/rate-limiter/createRateLimiter.js +5 -43
- package/dist/esm/rate-limiter/createRateLimiter.js.map +1 -1
- package/dist/esm/throttler/createThrottledSignal.d.ts +6 -2
- package/dist/esm/throttler/createThrottledSignal.js +2 -2
- package/dist/esm/throttler/createThrottledSignal.js.map +1 -1
- package/dist/esm/throttler/createThrottledValue.d.ts +2 -2
- package/dist/esm/throttler/createThrottledValue.js +3 -2
- package/dist/esm/throttler/createThrottledValue.js.map +1 -1
- package/dist/esm/throttler/createThrottler.d.ts +9 -10
- package/dist/esm/throttler/createThrottler.js +8 -35
- package/dist/esm/throttler/createThrottler.js.map +1 -1
- package/package.json +14 -3
- package/src/async-batcher/createAsyncBatcher.ts +98 -0
- package/src/async-batcher/index.ts +3 -0
- package/src/async-debouncer/createAsyncDebouncer.ts +31 -56
- package/src/async-queuer/createAsyncQueuer.ts +18 -141
- package/src/async-rate-limiter/createAsyncRateLimiter.ts +20 -75
- package/src/async-throttler/createAsyncThrottler.ts +25 -81
- package/src/batcher/createBatcher.ts +17 -94
- package/src/debouncer/createDebouncedSignal.ts +15 -4
- package/src/debouncer/createDebouncedValue.ts +11 -3
- package/src/debouncer/createDebouncer.ts +28 -38
- package/src/index.ts +3 -0
- package/src/queuer/createQueuer.ts +15 -115
- package/src/rate-limiter/createRateLimitedSignal.ts +17 -5
- package/src/rate-limiter/createRateLimitedValue.ts +8 -4
- package/src/rate-limiter/createRateLimiter.ts +25 -62
- package/src/throttler/createThrottledSignal.ts +15 -4
- package/src/throttler/createThrottledValue.ts +11 -3
- package/src/throttler/createThrottler.ts +28 -56
- package/dist/cjs/compare/index.cjs +0 -10
- package/dist/cjs/compare/index.d.cts +0 -1
- package/dist/esm/compare/index.d.ts +0 -1
- package/dist/esm/compare/index.js +0 -2
- package/src/compare/index.ts +0 -1
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { createSignal } from "solid-js";
|
|
2
2
|
import { createDebouncer } from "./createDebouncer.js";
|
|
3
|
-
function createDebouncedSignal(value, initialOptions) {
|
|
3
|
+
function createDebouncedSignal(value, initialOptions, selector) {
|
|
4
4
|
const [debouncedValue, setDebouncedValue] = createSignal(value);
|
|
5
|
-
const debouncer = createDebouncer(setDebouncedValue, initialOptions);
|
|
5
|
+
const debouncer = createDebouncer(setDebouncedValue, initialOptions, selector);
|
|
6
6
|
return [debouncedValue, debouncer.maybeExecute, debouncer];
|
|
7
7
|
}
|
|
8
8
|
export {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createDebouncedSignal.js","sources":["../../../src/debouncer/createDebouncedSignal.ts"],"sourcesContent":["import { createSignal } from 'solid-js'\nimport { createDebouncer } from './createDebouncer'\nimport type { SolidDebouncer } from './createDebouncer'\nimport type { Accessor, Setter } from 'solid-js'\nimport type {
|
|
1
|
+
{"version":3,"file":"createDebouncedSignal.js","sources":["../../../src/debouncer/createDebouncedSignal.ts"],"sourcesContent":["import { createSignal } from 'solid-js'\nimport { createDebouncer } from './createDebouncer'\nimport type { SolidDebouncer } from './createDebouncer'\nimport type { Accessor, Setter } from 'solid-js'\nimport type {\n DebouncerOptions,\n DebouncerState,\n} from '@tanstack/pacer/debouncer'\n\n/**\n * A Solid hook that creates a debounced state value, combining Solid's createSignal with debouncing functionality.\n * This hook provides both the current debounced value and methods to update it.\n *\n * The state value is only updated after the specified wait time has elapsed since the last update attempt.\n * If another update is attempted before the wait time expires, the timer resets and starts waiting again.\n * This is useful for handling frequent state updates that should be throttled, like search input values\n * or window resize dimensions.\n *\n * The hook returns a tuple containing:\n * - The current debounced value accessor\n * - A function to update the debounced value\n * - The debouncer instance with additional control methods and state signals\n *\n * @example\n * ```tsx\n * // Debounced search input\n * const [searchTerm, setSearchTerm, debouncer] = createDebouncedSignal('', {\n * wait: 500 // Wait 500ms after last keystroke\n * });\n *\n * // Update value - will be debounced\n * const handleChange = (e) => {\n * setSearchTerm(e.target.value);\n * };\n *\n * // Access debouncer state via signals\n * console.log('Executions:', debouncer.executionCount());\n * console.log('Is pending:', debouncer.isPending());\n *\n * // In onExecute callback, use get* methods\n * const [searchTerm, setSearchTerm, debouncer] = createDebouncedSignal('', {\n * wait: 500,\n * onExecute: (debouncer) => {\n * console.log('Total executions:', debouncer.getExecutionCount());\n * }\n * });\n * ```\n */\nexport function createDebouncedSignal<\n TValue,\n TSelected = DebouncerState<Setter<TValue>>,\n>(\n value: TValue,\n initialOptions: DebouncerOptions<Setter<TValue>>,\n selector?: (state: DebouncerState<Setter<TValue>>) => TSelected,\n): [\n Accessor<TValue>,\n Setter<TValue>,\n SolidDebouncer<Setter<TValue>, TSelected>,\n] {\n const [debouncedValue, setDebouncedValue] = createSignal<TValue>(value)\n\n const debouncer = createDebouncer(setDebouncedValue, initialOptions, selector)\n\n return [debouncedValue, debouncer.maybeExecute as Setter<TValue>, debouncer]\n}\n"],"names":[],"mappings":";;AAgDO,SAAS,sBAId,OACA,gBACA,UAKA;AACA,QAAM,CAAC,gBAAgB,iBAAiB,IAAI,aAAqB,KAAK;AAEtE,QAAM,YAAY,gBAAgB,mBAAmB,gBAAgB,QAAQ;AAE7E,SAAO,CAAC,gBAAgB,UAAU,cAAgC,SAAS;AAC7E;"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { SolidDebouncer } from './createDebouncer.js';
|
|
2
2
|
import { Accessor, Setter } from 'solid-js';
|
|
3
|
-
import { DebouncerOptions } from '@tanstack/pacer/debouncer';
|
|
3
|
+
import { DebouncerOptions, DebouncerState } from '@tanstack/pacer/debouncer';
|
|
4
4
|
/**
|
|
5
5
|
* A Solid hook that creates a debounced value that updates only after a specified delay.
|
|
6
6
|
* Unlike createDebouncedSignal, this hook automatically tracks changes to the input value
|
|
@@ -35,4 +35,4 @@ import { DebouncerOptions } from '@tanstack/pacer/debouncer';
|
|
|
35
35
|
* debouncer.cancel(); // Cancel any pending updates
|
|
36
36
|
* ```
|
|
37
37
|
*/
|
|
38
|
-
export declare function createDebouncedValue<TValue
|
|
38
|
+
export declare function createDebouncedValue<TValue, TSelected = DebouncerState<Setter<TValue>>>(value: Accessor<TValue>, initialOptions: DebouncerOptions<Setter<TValue>>, selector?: (state: DebouncerState<Setter<TValue>>) => TSelected): [Accessor<TValue>, SolidDebouncer<Setter<TValue>, TSelected>];
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { createEffect } from "solid-js";
|
|
2
2
|
import { createDebouncedSignal } from "./createDebouncedSignal.js";
|
|
3
|
-
function createDebouncedValue(value, initialOptions) {
|
|
3
|
+
function createDebouncedValue(value, initialOptions, selector) {
|
|
4
4
|
const [debouncedValue, setDebouncedValue, debouncer] = createDebouncedSignal(
|
|
5
5
|
value(),
|
|
6
|
-
initialOptions
|
|
6
|
+
initialOptions,
|
|
7
|
+
selector
|
|
7
8
|
);
|
|
8
9
|
createEffect(() => {
|
|
9
10
|
setDebouncedValue(value());
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createDebouncedValue.js","sources":["../../../src/debouncer/createDebouncedValue.ts"],"sourcesContent":["import { createEffect } from 'solid-js'\nimport { createDebouncedSignal } from './createDebouncedSignal'\nimport type { SolidDebouncer } from './createDebouncer'\nimport type { Accessor, Setter } from 'solid-js'\nimport type {
|
|
1
|
+
{"version":3,"file":"createDebouncedValue.js","sources":["../../../src/debouncer/createDebouncedValue.ts"],"sourcesContent":["import { createEffect } from 'solid-js'\nimport { createDebouncedSignal } from './createDebouncedSignal'\nimport type { SolidDebouncer } from './createDebouncer'\nimport type { Accessor, Setter } from 'solid-js'\nimport type {\n DebouncerOptions,\n DebouncerState,\n} from '@tanstack/pacer/debouncer'\n\n/**\n * A Solid hook that creates a debounced value that updates only after a specified delay.\n * Unlike createDebouncedSignal, this hook automatically tracks changes to the input value\n * and updates the debounced value accordingly.\n *\n * The debounced value will only update after the specified wait time has elapsed since\n * the last change to the input value. If the input value changes again before the wait\n * time expires, the timer resets and starts waiting again.\n *\n * This is useful for deriving debounced values from props or state that change frequently,\n * like search queries or form inputs, where you want to limit how often downstream effects\n * or calculations occur.\n *\n * The hook returns a tuple containing:\n * - An Accessor that provides the current debounced value\n * - The debouncer instance with control methods\n *\n * @example\n * ```tsx\n * // Debounce a search query\n * const [searchQuery, setSearchQuery] = createSignal('');\n * const [debouncedQuery, debouncer] = createDebouncedValue(searchQuery, {\n * wait: 500 // Wait 500ms after last change\n * });\n *\n * // debouncedQuery will update 500ms after searchQuery stops changing\n * createEffect(() => {\n * fetchSearchResults(debouncedQuery());\n * });\n *\n * // Control the debouncer\n * debouncer.cancel(); // Cancel any pending updates\n * ```\n */\nexport function createDebouncedValue<\n TValue,\n TSelected = DebouncerState<Setter<TValue>>,\n>(\n value: Accessor<TValue>,\n initialOptions: DebouncerOptions<Setter<TValue>>,\n selector?: (state: DebouncerState<Setter<TValue>>) => TSelected,\n): [Accessor<TValue>, SolidDebouncer<Setter<TValue>, TSelected>] {\n const [debouncedValue, setDebouncedValue, debouncer] = createDebouncedSignal(\n value(),\n initialOptions,\n selector,\n )\n\n createEffect(() => {\n setDebouncedValue(value() as any)\n })\n\n return [debouncedValue, debouncer]\n}\n"],"names":[],"mappings":";;AA2CO,SAAS,qBAId,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;"}
|
|
@@ -1,12 +1,13 @@
|
|
|
1
|
-
import { Debouncer, DebouncerOptions } from '@tanstack/pacer/debouncer';
|
|
1
|
+
import { Debouncer, DebouncerOptions, DebouncerState } from '@tanstack/pacer/debouncer';
|
|
2
2
|
import { Accessor } from 'solid-js';
|
|
3
3
|
import { AnyFunction } from '@tanstack/pacer/types';
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
4
|
+
export interface SolidDebouncer<TFn extends AnyFunction, TSelected = DebouncerState<TFn>> extends Omit<Debouncer<TFn>, 'store'> {
|
|
5
|
+
/**
|
|
6
|
+
* Reactive state that will be updated when the debouncer state changes
|
|
7
|
+
*
|
|
8
|
+
* Use this instead of `debouncer.store.state`
|
|
9
|
+
*/
|
|
10
|
+
readonly state: Accessor<Readonly<TSelected>>;
|
|
10
11
|
}
|
|
11
12
|
/**
|
|
12
13
|
* A Solid hook that creates and manages a Debouncer instance.
|
|
@@ -44,4 +45,4 @@ export interface SolidDebouncer<TFn extends AnyFunction> extends Omit<Debouncer<
|
|
|
44
45
|
* debouncer.setOptions({ wait: 1000 });
|
|
45
46
|
* ```
|
|
46
47
|
*/
|
|
47
|
-
export declare function createDebouncer<TFn extends AnyFunction
|
|
48
|
+
export declare function createDebouncer<TFn extends AnyFunction, TSelected = DebouncerState<TFn>>(fn: TFn, initialOptions: DebouncerOptions<TFn>, selector?: (state: DebouncerState<TFn>) => TSelected): SolidDebouncer<TFn, TSelected>;
|
|
@@ -1,34 +1,17 @@
|
|
|
1
1
|
import { Debouncer } from "@tanstack/pacer/debouncer";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
function createDebouncer(fn, initialOptions) {
|
|
5
|
-
const
|
|
6
|
-
const
|
|
7
|
-
debouncer.getExecutionCount()
|
|
8
|
-
);
|
|
9
|
-
const [isPending, setIsPending] = createSignal(debouncer.getIsPending());
|
|
10
|
-
function setOptions(newOptions) {
|
|
11
|
-
debouncer.setOptions({
|
|
12
|
-
...newOptions,
|
|
13
|
-
onExecute: (debouncer2) => {
|
|
14
|
-
setExecutionCount(debouncer2.getExecutionCount());
|
|
15
|
-
setIsPending(debouncer2.getIsPending());
|
|
16
|
-
const onExecute = newOptions.onExecute ?? initialOptions.onExecute;
|
|
17
|
-
onExecute == null ? void 0 : onExecute(debouncer2);
|
|
18
|
-
}
|
|
19
|
-
});
|
|
20
|
-
}
|
|
21
|
-
setOptions(initialOptions);
|
|
2
|
+
import { createEffect, onCleanup } from "solid-js";
|
|
3
|
+
import { useStore } from "@tanstack/solid-store";
|
|
4
|
+
function createDebouncer(fn, initialOptions, selector) {
|
|
5
|
+
const asyncDebouncer = new Debouncer(fn, initialOptions);
|
|
6
|
+
const state = useStore(asyncDebouncer.store, selector);
|
|
22
7
|
createEffect(() => {
|
|
23
8
|
onCleanup(() => {
|
|
24
|
-
|
|
9
|
+
asyncDebouncer.cancel();
|
|
25
10
|
});
|
|
26
11
|
});
|
|
27
12
|
return {
|
|
28
|
-
...
|
|
29
|
-
|
|
30
|
-
isPending,
|
|
31
|
-
setOptions
|
|
13
|
+
...asyncDebouncer,
|
|
14
|
+
state
|
|
32
15
|
};
|
|
33
16
|
}
|
|
34
17
|
export {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createDebouncer.js","sources":["../../../src/debouncer/createDebouncer.ts"],"sourcesContent":["import { Debouncer } from '@tanstack/pacer/debouncer'\nimport { createEffect,
|
|
1
|
+
{"version":3,"file":"createDebouncer.js","sources":["../../../src/debouncer/createDebouncer.ts"],"sourcesContent":["import { Debouncer } from '@tanstack/pacer/debouncer'\nimport { createEffect, onCleanup } from 'solid-js'\nimport { useStore } from '@tanstack/solid-store'\nimport type { Accessor } from 'solid-js'\nimport type { AnyFunction } from '@tanstack/pacer/types'\nimport type {\n DebouncerOptions,\n DebouncerState,\n} from '@tanstack/pacer/debouncer'\n\nexport interface SolidDebouncer<\n TFn extends AnyFunction,\n TSelected = DebouncerState<TFn>,\n> extends Omit<Debouncer<TFn>, 'store'> {\n /**\n * Reactive state that will be updated when the debouncer state changes\n *\n * Use this instead of `debouncer.store.state`\n */\n readonly state: Accessor<Readonly<TSelected>>\n}\n\n/**\n * A Solid hook that creates and manages a Debouncer instance.\n *\n * This is a lower-level hook that provides direct access to the Debouncer's functionality without\n * any built-in state management. This allows you to integrate it with any state management solution\n * you prefer (createSignal, Redux, Zustand, etc.).\n *\n * This hook provides debouncing functionality to limit how often a function can be called,\n * waiting for a specified delay before executing the latest call. This is useful for handling\n * frequent events like window resizing, scroll events, or real-time search inputs.\n *\n * The debouncer will only execute the function after the specified wait time has elapsed\n * since the last call. If the function is called again before the wait time expires, the\n * timer resets and starts waiting again.\n *\n * @example\n * ```tsx\n * // Debounce a search function to limit API calls\n * const debouncer = createDebouncer(\n * (query: string) => fetchSearchResults(query),\n * { wait: 500 } // Wait 500ms after last keystroke\n * );\n *\n * // In an event handler\n * const handleChange = (e) => {\n * debouncer.maybeExecute(e.target.value);\n * };\n *\n * // Access debouncer state via signals\n * console.log('Executions:', debouncer.executionCount());\n * console.log('Is pending:', debouncer.isPending());\n *\n * // Update options\n * debouncer.setOptions({ wait: 1000 });\n * ```\n */\nexport function createDebouncer<\n TFn extends AnyFunction,\n TSelected = DebouncerState<TFn>,\n>(\n fn: TFn,\n initialOptions: DebouncerOptions<TFn>,\n selector?: (state: DebouncerState<TFn>) => TSelected,\n): SolidDebouncer<TFn, TSelected> {\n const asyncDebouncer = new Debouncer<TFn>(fn, initialOptions)\n\n const state = useStore(asyncDebouncer.store, selector)\n\n createEffect(() => {\n onCleanup(() => {\n asyncDebouncer.cancel()\n })\n })\n\n return {\n ...asyncDebouncer,\n state,\n } as SolidDebouncer<TFn, TSelected> // omit `store` in favor of `state`\n}\n"],"names":[],"mappings":";;;AA0DO,SAAS,gBAId,IACA,gBACA,UACgC;AAChC,QAAM,iBAAiB,IAAI,UAAe,IAAI,cAAc;AAE5D,QAAM,QAAQ,SAAS,eAAe,OAAO,QAAQ;AAErD,eAAa,MAAM;AACjB,cAAU,MAAM;AACd,qBAAe,OAAA;AAAA,IAAO,CACvB;AAAA,EAAA,CACF;AAED,SAAO;AAAA,IACL,GAAG;AAAA,IACH;AAAA,EAAA;AAEJ;"}
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -2,6 +2,7 @@ export * from '@tanstack/pacer';
|
|
|
2
2
|
/**
|
|
3
3
|
* Export every hook individually - DON'T export from barrel files
|
|
4
4
|
*/
|
|
5
|
+
export * from './async-batcher/createAsyncBatcher.js';
|
|
5
6
|
export * from './async-debouncer/createAsyncDebouncer.js';
|
|
6
7
|
export * from './async-queuer/createAsyncQueuer.js';
|
|
7
8
|
export * from './async-rate-limiter/createAsyncRateLimiter.js';
|
package/dist/esm/index.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
export * from "@tanstack/pacer";
|
|
2
|
+
import { createAsyncBatcher } from "./async-batcher/createAsyncBatcher.js";
|
|
2
3
|
import { createAsyncDebouncer } from "./async-debouncer/createAsyncDebouncer.js";
|
|
3
4
|
import { createAsyncQueuer } from "./async-queuer/createAsyncQueuer.js";
|
|
4
5
|
import { createAsyncRateLimiter } from "./async-rate-limiter/createAsyncRateLimiter.js";
|
|
@@ -15,6 +16,7 @@ import { createThrottledSignal } from "./throttler/createThrottledSignal.js";
|
|
|
15
16
|
import { createThrottledValue } from "./throttler/createThrottledValue.js";
|
|
16
17
|
import { createThrottler } from "./throttler/createThrottler.js";
|
|
17
18
|
export {
|
|
19
|
+
createAsyncBatcher,
|
|
18
20
|
createAsyncDebouncer,
|
|
19
21
|
createAsyncQueuer,
|
|
20
22
|
createAsyncRateLimiter,
|
package/dist/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;"}
|
|
@@ -1,42 +1,12 @@
|
|
|
1
|
-
import { Queuer, QueuerOptions } from '@tanstack/pacer/queuer';
|
|
1
|
+
import { Queuer, QueuerOptions, QueuerState } from '@tanstack/pacer/queuer';
|
|
2
2
|
import { Accessor } from 'solid-js';
|
|
3
|
-
export interface SolidQueuer<TValue
|
|
3
|
+
export interface SolidQueuer<TValue, TSelected = QueuerState<TValue>> extends Omit<Queuer<TValue>, 'store'> {
|
|
4
4
|
/**
|
|
5
|
-
*
|
|
5
|
+
* Reactive state that will be updated when the queuer state changes
|
|
6
|
+
*
|
|
7
|
+
* Use this instead of `queuer.store.state`
|
|
6
8
|
*/
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Signal version of `getExecutionCount`
|
|
10
|
-
*/
|
|
11
|
-
executionCount: Accessor<number>;
|
|
12
|
-
/**
|
|
13
|
-
* Signal version of `getIsEmpty`
|
|
14
|
-
*/
|
|
15
|
-
isEmpty: Accessor<boolean>;
|
|
16
|
-
/**
|
|
17
|
-
* Signal version of `getIsFull`
|
|
18
|
-
*/
|
|
19
|
-
isFull: Accessor<boolean>;
|
|
20
|
-
/**
|
|
21
|
-
* Signal version of `getIsIdle`
|
|
22
|
-
*/
|
|
23
|
-
isIdle: Accessor<boolean>;
|
|
24
|
-
/**
|
|
25
|
-
* Signal version of `getIsRunning`
|
|
26
|
-
*/
|
|
27
|
-
isRunning: Accessor<boolean>;
|
|
28
|
-
/**
|
|
29
|
-
* Signal version of `peekNextItem`
|
|
30
|
-
*/
|
|
31
|
-
nextItem: Accessor<TValue | undefined>;
|
|
32
|
-
/**
|
|
33
|
-
* Signal version of `getRejectionCount`
|
|
34
|
-
*/
|
|
35
|
-
rejectionCount: Accessor<number>;
|
|
36
|
-
/**
|
|
37
|
-
* Signal version of `getSize`
|
|
38
|
-
*/
|
|
39
|
-
size: Accessor<number>;
|
|
9
|
+
readonly state: Accessor<Readonly<TSelected>>;
|
|
40
10
|
}
|
|
41
11
|
/**
|
|
42
12
|
* Creates a Solid-compatible Queuer instance for managing a synchronous queue of items, exposing Solid signals for all stateful properties.
|
|
@@ -89,4 +59,4 @@ export interface SolidQueuer<TValue> extends Omit<Queuer<TValue>, 'getExecutionC
|
|
|
89
59
|
* console.log('Next item:', queue.nextItem());
|
|
90
60
|
* ```
|
|
91
61
|
*/
|
|
92
|
-
export declare function createQueuer<TValue
|
|
62
|
+
export declare function createQueuer<TValue, TSelected = QueuerState<TValue>>(fn: (item: TValue) => void, initialOptions?: QueuerOptions<TValue>, selector?: (state: QueuerState<TValue>) => TSelected): SolidQueuer<TValue, TSelected>;
|
|
@@ -1,65 +1,11 @@
|
|
|
1
1
|
import { Queuer } from "@tanstack/pacer/queuer";
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const
|
|
6
|
-
const [allItems, setAllItems] = createSignal(
|
|
7
|
-
queuer.peekAllItems()
|
|
8
|
-
);
|
|
9
|
-
const [executionCount, setExecutionCount] = createSignal(
|
|
10
|
-
queuer.getExecutionCount()
|
|
11
|
-
);
|
|
12
|
-
const [rejectionCount, setRejectionCount] = createSignal(
|
|
13
|
-
queuer.getRejectionCount()
|
|
14
|
-
);
|
|
15
|
-
const [isEmpty, setIsEmpty] = createSignal(queuer.getIsEmpty());
|
|
16
|
-
const [isFull, setIsFull] = createSignal(queuer.getIsFull());
|
|
17
|
-
const [isIdle, setIsIdle] = createSignal(queuer.getIsIdle());
|
|
18
|
-
const [isRunning, setIsRunning] = createSignal(queuer.getIsRunning());
|
|
19
|
-
const [nextItem, setNextItem] = createSignal(
|
|
20
|
-
queuer.peekNextItem()
|
|
21
|
-
);
|
|
22
|
-
const [size, setSize] = createSignal(queuer.getSize());
|
|
23
|
-
function setOptions(newOptions) {
|
|
24
|
-
queuer.setOptions({
|
|
25
|
-
...newOptions,
|
|
26
|
-
onItemsChange: (queuer2) => {
|
|
27
|
-
setAllItems(queuer2.peekAllItems());
|
|
28
|
-
setExecutionCount(queuer2.getExecutionCount());
|
|
29
|
-
setIsEmpty(queuer2.getIsEmpty());
|
|
30
|
-
setIsFull(queuer2.getIsFull());
|
|
31
|
-
setIsIdle(queuer2.getIsIdle());
|
|
32
|
-
setNextItem(() => queuer2.peekNextItem());
|
|
33
|
-
setSize(queuer2.getSize());
|
|
34
|
-
const onItemsChange = newOptions.onItemsChange ?? initialOptions.onItemsChange;
|
|
35
|
-
onItemsChange == null ? void 0 : onItemsChange(queuer2);
|
|
36
|
-
},
|
|
37
|
-
onIsRunningChange: (queuer2) => {
|
|
38
|
-
setIsRunning(queuer2.getIsRunning());
|
|
39
|
-
setIsIdle(queuer2.getIsIdle());
|
|
40
|
-
const onIsRunningChange = newOptions.onIsRunningChange ?? initialOptions.onIsRunningChange;
|
|
41
|
-
onIsRunningChange == null ? void 0 : onIsRunningChange(queuer2);
|
|
42
|
-
},
|
|
43
|
-
onReject: (item, queuer2) => {
|
|
44
|
-
setRejectionCount(queuer2.getRejectionCount());
|
|
45
|
-
const onReject = newOptions.onReject ?? initialOptions.onReject;
|
|
46
|
-
onReject == null ? void 0 : onReject(item, queuer2);
|
|
47
|
-
}
|
|
48
|
-
});
|
|
49
|
-
}
|
|
50
|
-
setOptions(initialOptions);
|
|
2
|
+
import { useStore } from "@tanstack/solid-store";
|
|
3
|
+
function createQueuer(fn, initialOptions = {}, selector) {
|
|
4
|
+
const queuer = new Queuer(fn, initialOptions);
|
|
5
|
+
const state = useStore(queuer.store, selector);
|
|
51
6
|
return {
|
|
52
7
|
...queuer,
|
|
53
|
-
|
|
54
|
-
executionCount,
|
|
55
|
-
isEmpty,
|
|
56
|
-
isFull,
|
|
57
|
-
isIdle,
|
|
58
|
-
isRunning,
|
|
59
|
-
nextItem,
|
|
60
|
-
rejectionCount,
|
|
61
|
-
size,
|
|
62
|
-
setOptions
|
|
8
|
+
state
|
|
63
9
|
};
|
|
64
10
|
}
|
|
65
11
|
export {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createQueuer.js","sources":["../../../src/queuer/createQueuer.ts"],"sourcesContent":["import { Queuer } from '@tanstack/pacer/queuer'\nimport {
|
|
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 = QueuerState<TValue>>\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\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 * Example usage:\n * ```tsx\n * // Example with Solid signals and scheduling\n * const [items, setItems] = createSignal([]);\n *\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 * onItemsChange: (queue) => setItems(queue.peekAllItems()),\n * getPriority: (item) => item.priority // Process higher priority items first\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 queue state via signals\n * console.log('Items:', queue.allItems());\n * console.log('Size:', queue.size());\n * console.log('Is empty:', queue.isEmpty());\n * console.log('Is running:', queue.isRunning());\n * console.log('Next item:', queue.nextItem());\n * ```\n */\nexport function createQueuer<TValue, TSelected = QueuerState<TValue>>(\n fn: (item: TValue) => void,\n initialOptions: QueuerOptions<TValue> = {},\n selector?: (state: QueuerState<TValue>) => 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 unknown as SolidQueuer<TValue, TSelected> // omit `store` in favor of `state`\n}\n"],"names":[],"mappings":";;AAkEO,SAAS,aACd,IACA,iBAAwC,CAAA,GACxC,UACgC;AAChC,QAAM,SAAS,IAAI,OAAO,IAAI,cAAc;AAE5C,QAAM,QAAQ,SAAS,OAAO,OAAO,QAAQ;AAE7C,SAAO;AAAA,IACL,GAAG;AAAA,IACH;AAAA,EAAA;AAEJ;"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { SolidRateLimiter } from './createRateLimiter.js';
|
|
2
2
|
import { Accessor, Setter } from 'solid-js';
|
|
3
|
-
import { RateLimiterOptions } from '@tanstack/pacer/rate-limiter';
|
|
3
|
+
import { RateLimiterOptions, RateLimiterState } from '@tanstack/pacer/rate-limiter';
|
|
4
4
|
/**
|
|
5
5
|
* A Solid hook that creates a rate-limited state value that enforces a hard limit on state updates within a time window.
|
|
6
6
|
* This hook combines Solid's createSignal with rate limiting functionality to provide controlled state updates.
|
|
@@ -59,4 +59,8 @@ import { RateLimiterOptions } from '@tanstack/pacer/rate-limiter';
|
|
|
59
59
|
* };
|
|
60
60
|
* ```
|
|
61
61
|
*/
|
|
62
|
-
export declare function createRateLimitedSignal<TValue>(value: TValue, initialOptions: RateLimiterOptions<Setter<TValue
|
|
62
|
+
export declare function createRateLimitedSignal<TValue, TSelected = RateLimiterState>(value: TValue, initialOptions: RateLimiterOptions<Setter<TValue>>, selector?: (state: RateLimiterState) => TSelected): [
|
|
63
|
+
Accessor<TValue>,
|
|
64
|
+
Setter<TValue>,
|
|
65
|
+
SolidRateLimiter<Setter<TValue>, TSelected>
|
|
66
|
+
];
|
|
@@ -1,11 +1,15 @@
|
|
|
1
1
|
import { createSignal } from "solid-js";
|
|
2
2
|
import { createRateLimiter } from "./createRateLimiter.js";
|
|
3
|
-
function createRateLimitedSignal(value, initialOptions) {
|
|
3
|
+
function createRateLimitedSignal(value, initialOptions, selector) {
|
|
4
4
|
const [rateLimitedValue, setRateLimitedValue] = createSignal(value);
|
|
5
|
-
const rateLimiter = createRateLimiter(
|
|
5
|
+
const rateLimiter = createRateLimiter(
|
|
6
|
+
setRateLimitedValue,
|
|
7
|
+
initialOptions,
|
|
8
|
+
selector
|
|
9
|
+
);
|
|
6
10
|
return [
|
|
7
11
|
rateLimitedValue,
|
|
8
|
-
rateLimiter.maybeExecute
|
|
12
|
+
rateLimiter.maybeExecute,
|
|
9
13
|
rateLimiter
|
|
10
14
|
];
|
|
11
15
|
}
|
|
@@ -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 {
|
|
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 * @example\n * ```tsx\n * // Basic rate limiting - update state at most 5 times per minute with a sliding window\n * const [value, setValue, rateLimiter] = createRateLimitedSignal(0, {\n * limit: 5,\n * window: 60000,\n * windowType: 'sliding'\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.remainingInWindow();\n * if (remaining > 0) {\n * setValue(newValue);\n * } else {\n * showRateLimitWarning();\n * }\n * };\n * ```\n */\nexport function createRateLimitedSignal<TValue, TSelected = RateLimiterState>(\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":";;AAmEO,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;"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { SolidRateLimiter } from './createRateLimiter.js';
|
|
2
2
|
import { Accessor, Setter } from 'solid-js';
|
|
3
|
-
import { RateLimiterOptions } from '@tanstack/pacer/rate-limiter';
|
|
3
|
+
import { RateLimiterOptions, RateLimiterState } from '@tanstack/pacer/rate-limiter';
|
|
4
4
|
/**
|
|
5
5
|
* 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.
|
|
6
6
|
* This hook uses Solid's createSignal internally to manage the rate-limited state.
|
|
@@ -44,4 +44,4 @@ import { RateLimiterOptions } from '@tanstack/pacer/rate-limiter';
|
|
|
44
44
|
* rateLimiter.reset(); // Reset the rate limit window
|
|
45
45
|
* ```
|
|
46
46
|
*/
|
|
47
|
-
export declare function createRateLimitedValue<TValue>(value: Accessor<TValue>, initialOptions: RateLimiterOptions<Setter<TValue
|
|
47
|
+
export declare function createRateLimitedValue<TValue, TSelected = RateLimiterState>(value: Accessor<TValue>, initialOptions: RateLimiterOptions<Setter<TValue>>, selector?: (state: RateLimiterState) => TSelected): [Accessor<TValue>, SolidRateLimiter<Setter<TValue>, TSelected>];
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { createEffect } from "solid-js";
|
|
2
2
|
import { createRateLimitedSignal } from "./createRateLimitedSignal.js";
|
|
3
|
-
function createRateLimitedValue(value, initialOptions) {
|
|
4
|
-
const [rateLimitedValue, setRateLimitedValue, rateLimiter] = createRateLimitedSignal(value(), initialOptions);
|
|
3
|
+
function createRateLimitedValue(value, initialOptions, selector) {
|
|
4
|
+
const [rateLimitedValue, setRateLimitedValue, rateLimiter] = createRateLimitedSignal(value(), initialOptions, selector);
|
|
5
5
|
createEffect(() => {
|
|
6
6
|
setRateLimitedValue(value());
|
|
7
7
|
});
|
|
@@ -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 {
|
|
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 * @example\n * ```tsx\n * // Basic rate limiting - update at most 5 times per minute with a sliding window\n * const [rateLimitedValue, rateLimiter] = createRateLimitedValue(rawValue, {\n * limit: 5,\n * window: 60000,\n * windowType: 'sliding'\n * });\n *\n * // Use the rate-limited value\n * console.log(rateLimitedValue()); // Access the current rate-limited value\n *\n * // Control the rate limiter\n * rateLimiter.reset(); // Reset the rate limit window\n * ```\n */\nexport function createRateLimitedValue<TValue, TSelected = RateLimiterState>(\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":";;AAoDO,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,11 +1,13 @@
|
|
|
1
|
-
import { RateLimiter, RateLimiterOptions } from '@tanstack/pacer/rate-limiter';
|
|
1
|
+
import { RateLimiter, RateLimiterOptions, RateLimiterState } from '@tanstack/pacer/rate-limiter';
|
|
2
2
|
import { Accessor } from 'solid-js';
|
|
3
3
|
import { AnyFunction } from '@tanstack/pacer/types';
|
|
4
|
-
export interface SolidRateLimiter<TFn extends AnyFunction> extends Omit<RateLimiter<TFn>, '
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
4
|
+
export interface SolidRateLimiter<TFn extends AnyFunction, TSelected = RateLimiterState> extends Omit<RateLimiter<TFn>, 'store'> {
|
|
5
|
+
/**
|
|
6
|
+
* Reactive state that will be updated when the rate limiter state changes
|
|
7
|
+
*
|
|
8
|
+
* Use this instead of `rateLimiter.store.state`
|
|
9
|
+
*/
|
|
10
|
+
readonly state: Accessor<Readonly<TSelected>>;
|
|
9
11
|
}
|
|
10
12
|
/**
|
|
11
13
|
* A low-level Solid hook that creates a `RateLimiter` instance to enforce rate limits on function execution.
|
|
@@ -47,4 +49,4 @@ export interface SolidRateLimiter<TFn extends AnyFunction> extends Omit<RateLimi
|
|
|
47
49
|
* console.log('Next window in:', rateLimiter.msUntilNextWindow());
|
|
48
50
|
* ```
|
|
49
51
|
*/
|
|
50
|
-
export declare function createRateLimiter<TFn extends AnyFunction>(fn: TFn, initialOptions: RateLimiterOptions<TFn
|
|
52
|
+
export declare function createRateLimiter<TFn extends AnyFunction, TSelected = RateLimiterState>(fn: TFn, initialOptions: RateLimiterOptions<TFn>, selector?: (state: RateLimiterState) => TSelected): SolidRateLimiter<TFn, TSelected>;
|
|
@@ -1,49 +1,11 @@
|
|
|
1
1
|
import { RateLimiter } from "@tanstack/pacer/rate-limiter";
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const
|
|
6
|
-
new RateLimiter(fn, initialOptions)
|
|
7
|
-
);
|
|
8
|
-
const [executionCount, setExecutionCount] = createSignal(
|
|
9
|
-
rateLimiter.getExecutionCount()
|
|
10
|
-
);
|
|
11
|
-
const [rejectionCount, setRejectionCount] = createSignal(
|
|
12
|
-
rateLimiter.getRejectionCount()
|
|
13
|
-
);
|
|
14
|
-
const [remainingInWindow, setRemainingInWindow] = createSignal(
|
|
15
|
-
rateLimiter.getRemainingInWindow()
|
|
16
|
-
);
|
|
17
|
-
const [msUntilNextWindow, setMsUntilNextWindow] = createSignal(
|
|
18
|
-
rateLimiter.getMsUntilNextWindow()
|
|
19
|
-
);
|
|
20
|
-
function setOptions(newOptions) {
|
|
21
|
-
rateLimiter.setOptions({
|
|
22
|
-
...newOptions,
|
|
23
|
-
onExecute: (rateLimiter2) => {
|
|
24
|
-
setExecutionCount(rateLimiter2.getExecutionCount());
|
|
25
|
-
setRemainingInWindow(rateLimiter2.getRemainingInWindow());
|
|
26
|
-
setMsUntilNextWindow(rateLimiter2.getMsUntilNextWindow());
|
|
27
|
-
const onExecute = newOptions.onExecute ?? initialOptions.onExecute;
|
|
28
|
-
onExecute == null ? void 0 : onExecute(rateLimiter2);
|
|
29
|
-
},
|
|
30
|
-
onReject: (rateLimiter2) => {
|
|
31
|
-
setRejectionCount(rateLimiter2.getRejectionCount());
|
|
32
|
-
setRemainingInWindow(rateLimiter2.getRemainingInWindow());
|
|
33
|
-
setMsUntilNextWindow(rateLimiter2.getMsUntilNextWindow());
|
|
34
|
-
const onReject = newOptions.onReject ?? initialOptions.onReject;
|
|
35
|
-
onReject == null ? void 0 : onReject(rateLimiter2);
|
|
36
|
-
}
|
|
37
|
-
});
|
|
38
|
-
}
|
|
39
|
-
setOptions(initialOptions);
|
|
2
|
+
import { useStore } from "@tanstack/solid-store";
|
|
3
|
+
function createRateLimiter(fn, initialOptions, selector) {
|
|
4
|
+
const rateLimiter = new RateLimiter(fn, initialOptions);
|
|
5
|
+
const state = useStore(rateLimiter.store, selector);
|
|
40
6
|
return {
|
|
41
7
|
...rateLimiter,
|
|
42
|
-
|
|
43
|
-
rejectionCount,
|
|
44
|
-
remainingInWindow,
|
|
45
|
-
msUntilNextWindow,
|
|
46
|
-
setOptions
|
|
8
|
+
state
|
|
47
9
|
};
|
|
48
10
|
}
|
|
49
11
|
export {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createRateLimiter.js","sources":["../../../src/rate-limiter/createRateLimiter.ts"],"sourcesContent":["import { RateLimiter } from '@tanstack/pacer/rate-limiter'\nimport {
|
|
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<\n TFn extends AnyFunction,\n TSelected = RateLimiterState,\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\n/**\n * A low-level Solid hook that creates a `RateLimiter` instance to enforce rate limits on function execution.\n *\n * This hook is designed to be flexible and state-management agnostic - it simply returns a rate limiter instance that\n * you can integrate with any state management solution (createSignal, etc).\n *\n * Rate limiting is a simple \"hard limit\" approach that allows executions until a maximum count is reached within\n * a time window, then blocks all subsequent calls until the window resets. Unlike throttling or debouncing,\n * it does not attempt to space out or collapse executions intelligently.\n *\n * The rate limiter supports two types of windows:\n * - 'fixed': A strict window that resets after the window period. All executions within the window count\n * towards the limit, and the window resets completely after the period.\n * - 'sliding': A rolling window that allows executions as old ones expire. This provides a more\n * consistent rate of execution over time.\n *\n * For smoother execution patterns:\n * - Use throttling when you want consistent spacing between executions (e.g. UI updates)\n * - Use debouncing when you want to collapse rapid-fire events (e.g. search input)\n * - Use rate limiting only when you need to enforce hard limits (e.g. API rate limits)\n *\n * @example\n * ```tsx\n * // Basic rate limiting - max 5 calls per minute with a sliding window\n * const rateLimiter = createRateLimiter(apiCall, {\n * limit: 5,\n * window: 60000,\n * windowType: 'sliding',\n * onReject: (rateLimiter) => {\n * console.log(`Rate limit exceeded. Try again in ${rateLimiter.getMsUntilNextWindow()}ms`);\n * }\n * });\n *\n * // Access rate limiter state via signals\n * console.log('Executions:', rateLimiter.executionCount());\n * console.log('Rejections:', rateLimiter.rejectionCount());\n * console.log('Remaining:', rateLimiter.remainingInWindow());\n * console.log('Next window in:', rateLimiter.msUntilNextWindow());\n * ```\n */\nexport function createRateLimiter<\n TFn extends AnyFunction,\n TSelected = RateLimiterState,\n>(\n fn: TFn,\n initialOptions: RateLimiterOptions<TFn>,\n selector?: (state: RateLimiterState) => 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 unknown as SolidRateLimiter<TFn, TSelected> // omit `store` in favor of `state`\n}\n"],"names":[],"mappings":";;AA6DO,SAAS,kBAId,IACA,gBACA,UACkC;AAClC,QAAM,cAAc,IAAI,YAAiB,IAAI,cAAc;AAE3D,QAAM,QAAQ,SAAS,YAAY,OAAO,QAAQ;AAElD,SAAO;AAAA,IACL,GAAG;AAAA,IACH;AAAA,EAAA;AAEJ;"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { SolidThrottler } from './createThrottler.js';
|
|
2
2
|
import { Accessor, Setter } from 'solid-js';
|
|
3
|
-
import { ThrottlerOptions } from '@tanstack/pacer/throttler';
|
|
3
|
+
import { ThrottlerOptions, ThrottlerState } from '@tanstack/pacer/throttler';
|
|
4
4
|
/**
|
|
5
5
|
* A Solid hook that creates a throttled state value that updates at most once within a specified time window.
|
|
6
6
|
* This hook combines Solid's createSignal with throttling functionality to provide controlled state updates.
|
|
@@ -35,4 +35,8 @@ import { ThrottlerOptions } from '@tanstack/pacer/throttler';
|
|
|
35
35
|
* console.log('Next execution:', throttler.nextExecutionTime());
|
|
36
36
|
* ```
|
|
37
37
|
*/
|
|
38
|
-
export declare function createThrottledSignal<TValue
|
|
38
|
+
export declare function createThrottledSignal<TValue, TSelected = ThrottlerState<Setter<TValue>>>(value: TValue, initialOptions: ThrottlerOptions<Setter<TValue>>, selector?: (state: ThrottlerState<Setter<TValue>>) => TSelected): [
|
|
39
|
+
Accessor<TValue>,
|
|
40
|
+
Setter<TValue>,
|
|
41
|
+
SolidThrottler<Setter<TValue>, TSelected>
|
|
42
|
+
];
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { createSignal } from "solid-js";
|
|
2
2
|
import { createThrottler } from "./createThrottler.js";
|
|
3
|
-
function createThrottledSignal(value, initialOptions) {
|
|
3
|
+
function createThrottledSignal(value, initialOptions, selector) {
|
|
4
4
|
const [throttledValue, setThrottledValue] = createSignal(value);
|
|
5
|
-
const throttler = createThrottler(setThrottledValue, initialOptions);
|
|
5
|
+
const throttler = createThrottler(setThrottledValue, initialOptions, selector);
|
|
6
6
|
return [throttledValue, throttler.maybeExecute, throttler];
|
|
7
7
|
}
|
|
8
8
|
export {
|