@tanstack/solid-pacer 0.7.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 -57
- package/dist/cjs/queuer/createQueuer.cjs.map +1 -1
- package/dist/cjs/queuer/createQueuer.d.cts +9 -39
- 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 +9 -39
- package/dist/esm/queuer/createQueuer.js +5 -57
- 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 +15 -4
- 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 +17 -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 +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 {
|
|
@@ -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 {
|
|
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 * // Basic throttling - update state at most once per second\n * const [value, setValue, throttler] = createThrottledSignal(0, { wait: 1000 });\n *\n * // With custom leading/trailing behavior\n * const [value, setValue] = createThrottledSignal(0, {\n * wait: 1000,\n * leading: true, // Update immediately on first change\n * trailing: false // Skip trailing edge updates\n * });\n *\n * // Access throttler state via signals\n * console.log('Executions:', throttler.executionCount());\n * console.log('Is pending:', throttler.isPending());\n * console.log('Last execution:', throttler.lastExecutionTime());\n * console.log('Next execution:', throttler.nextExecutionTime());\n * ```\n */\nexport function createThrottledSignal<\n TValue,\n TSelected = ThrottlerState<Setter<TValue>>,\n>(\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":";;AA2CO,SAAS,sBAId,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;"}
|
|
@@ -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 high-level Solid hook that creates a throttled version of a value that updates at most once within a specified time window.
|
|
6
6
|
* This hook uses Solid's createSignal internally to manage the throttled state.
|
|
@@ -29,4 +29,4 @@ import { ThrottlerOptions } from '@tanstack/pacer/throttler';
|
|
|
29
29
|
* throttler.cancel(); // Cancel any pending updates
|
|
30
30
|
* ```
|
|
31
31
|
*/
|
|
32
|
-
export declare function createThrottledValue<TValue
|
|
32
|
+
export declare function createThrottledValue<TValue, TSelected = ThrottlerState<Setter<TValue>>>(value: Accessor<TValue>, initialOptions: ThrottlerOptions<Setter<TValue>>, selector?: (state: ThrottlerState<Setter<TValue>>) => TSelected): [Accessor<TValue>, SolidThrottler<Setter<TValue>, TSelected>];
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { createEffect } from "solid-js";
|
|
2
2
|
import { createThrottledSignal } from "./createThrottledSignal.js";
|
|
3
|
-
function createThrottledValue(value, initialOptions) {
|
|
3
|
+
function createThrottledValue(value, initialOptions, selector) {
|
|
4
4
|
const [throttledValue, setThrottledValue, throttler] = createThrottledSignal(
|
|
5
5
|
value(),
|
|
6
|
-
initialOptions
|
|
6
|
+
initialOptions,
|
|
7
|
+
selector
|
|
7
8
|
);
|
|
8
9
|
createEffect(() => {
|
|
9
10
|
setThrottledValue(value());
|
|
@@ -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 {
|
|
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 * // Basic throttling - update at most once per second\n * const [throttledValue, throttler] = createThrottledValue(rawValue, { wait: 1000 });\n *\n * // Use the throttled value\n * console.log(throttledValue()); // Access the current throttled value\n *\n * // Control the throttler\n * throttler.cancel(); // Cancel any pending updates\n * ```\n */\nexport function createThrottledValue<\n TValue,\n TSelected = ThrottlerState<Setter<TValue>>,\n>(\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":";;AAqCO,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,14 +1,13 @@
|
|
|
1
|
-
import { Throttler, ThrottlerOptions } from '@tanstack/pacer/throttler';
|
|
1
|
+
import { Throttler, ThrottlerOptions, ThrottlerState } from '@tanstack/pacer/throttler';
|
|
2
2
|
import { Accessor } from 'solid-js';
|
|
3
3
|
import { AnyFunction } from '@tanstack/pacer/types';
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
nextExecutionTime: Accessor<number>;
|
|
4
|
+
export interface SolidThrottler<TFn extends AnyFunction, TSelected = ThrottlerState<TFn>> extends Omit<Throttler<TFn>, 'store'> {
|
|
5
|
+
/**
|
|
6
|
+
* Reactive state that will be updated when the throttler state changes
|
|
7
|
+
*
|
|
8
|
+
* Use this instead of `throttler.store.state`
|
|
9
|
+
*/
|
|
10
|
+
readonly state: Accessor<Readonly<TSelected>>;
|
|
12
11
|
}
|
|
13
12
|
/**
|
|
14
13
|
* A low-level Solid hook that creates a `Throttler` instance that limits how often the provided function can execute.
|
|
@@ -44,4 +43,4 @@ export interface SolidThrottler<TFn extends AnyFunction> extends Omit<Throttler<
|
|
|
44
43
|
* console.log(throttler.nextExecutionTime()); // timestamp of next allowed execution
|
|
45
44
|
* ```
|
|
46
45
|
*/
|
|
47
|
-
export declare function createThrottler<TFn extends AnyFunction
|
|
46
|
+
export declare function createThrottler<TFn extends AnyFunction, TSelected = ThrottlerState<TFn>>(fn: TFn, initialOptions: ThrottlerOptions<TFn>, selector?: (state: ThrottlerState<TFn>) => TSelected): SolidThrottler<TFn, TSelected>;
|
|
@@ -1,44 +1,17 @@
|
|
|
1
1
|
import { Throttler } from "@tanstack/pacer/throttler";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
function createThrottler(fn, initialOptions) {
|
|
5
|
-
const
|
|
6
|
-
const
|
|
7
|
-
throttler.getExecutionCount()
|
|
8
|
-
);
|
|
9
|
-
const [isPending, setIsPending] = createSignal(throttler.getIsPending());
|
|
10
|
-
const [lastExecutionTime, setLastExecutionTime] = createSignal(
|
|
11
|
-
throttler.getLastExecutionTime()
|
|
12
|
-
);
|
|
13
|
-
const [nextExecutionTime, setNextExecutionTime] = createSignal(
|
|
14
|
-
throttler.getNextExecutionTime()
|
|
15
|
-
);
|
|
16
|
-
function setOptions(newOptions) {
|
|
17
|
-
throttler.setOptions({
|
|
18
|
-
...newOptions,
|
|
19
|
-
onExecute: (throttler2) => {
|
|
20
|
-
setExecutionCount(throttler2.getExecutionCount());
|
|
21
|
-
setIsPending(throttler2.getIsPending());
|
|
22
|
-
setLastExecutionTime(throttler2.getLastExecutionTime());
|
|
23
|
-
setNextExecutionTime(throttler2.getNextExecutionTime());
|
|
24
|
-
const onExecute = newOptions.onExecute ?? initialOptions.onExecute;
|
|
25
|
-
onExecute == null ? void 0 : onExecute(throttler2);
|
|
26
|
-
}
|
|
27
|
-
});
|
|
28
|
-
}
|
|
29
|
-
setOptions(initialOptions);
|
|
2
|
+
import { createEffect, onCleanup } from "solid-js";
|
|
3
|
+
import { useStore } from "@tanstack/solid-store";
|
|
4
|
+
function createThrottler(fn, initialOptions, selector) {
|
|
5
|
+
const asyncThrottler = new Throttler(fn, initialOptions);
|
|
6
|
+
const state = useStore(asyncThrottler.store, selector);
|
|
30
7
|
createEffect(() => {
|
|
31
8
|
onCleanup(() => {
|
|
32
|
-
|
|
9
|
+
asyncThrottler.cancel();
|
|
33
10
|
});
|
|
34
11
|
});
|
|
35
12
|
return {
|
|
36
|
-
...
|
|
37
|
-
|
|
38
|
-
isPending,
|
|
39
|
-
lastExecutionTime,
|
|
40
|
-
nextExecutionTime,
|
|
41
|
-
setOptions
|
|
13
|
+
...asyncThrottler,
|
|
14
|
+
state
|
|
42
15
|
};
|
|
43
16
|
}
|
|
44
17
|
export {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createThrottler.js","sources":["../../../src/throttler/createThrottler.ts"],"sourcesContent":["import { Throttler } from '@tanstack/pacer/throttler'\nimport { createEffect,
|
|
1
|
+
{"version":3,"file":"createThrottler.js","sources":["../../../src/throttler/createThrottler.ts"],"sourcesContent":["import { Throttler } from '@tanstack/pacer/throttler'\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 ThrottlerOptions,\n ThrottlerState,\n} from '@tanstack/pacer/throttler'\n\nexport interface SolidThrottler<\n TFn extends AnyFunction,\n TSelected = ThrottlerState<TFn>,\n> extends Omit<Throttler<TFn>, 'store'> {\n /**\n * Reactive state that will be updated when the throttler state changes\n *\n * Use this instead of `throttler.store.state`\n */\n readonly state: Accessor<Readonly<TSelected>>\n}\n\n/**\n * A low-level Solid hook that creates a `Throttler` instance that limits how often the provided function can execute.\n *\n * This hook is designed to be flexible and state-management agnostic - it simply returns a throttler instance that\n * you can integrate with any state management solution (createSignal, Redux, Zustand, Jotai, etc). For a simpler and higher-level hook that\n * integrates directly with Solid's createSignal, see createThrottledSignal.\n *\n * Throttling ensures a function executes at most once within a specified time window,\n * regardless of how many times it is called. This is useful for rate-limiting\n * expensive operations or UI updates.\n *\n * @example\n * ```tsx\n * // Basic throttling with custom state\n * const [value, setValue] = createSignal(0);\n * const throttler = createThrottler(setValue, { wait: 1000 });\n *\n * // With any state manager\n * const throttler = createThrottler(\n * (value) => stateManager.setState(value),\n * {\n * wait: 2000,\n * leading: true, // Execute immediately on first call\n * trailing: false // Skip trailing edge updates\n * }\n * );\n *\n * // Access throttler state via signals\n * console.log(throttler.executionCount()); // number of times executed\n * console.log(throttler.isPending()); // whether throttled function is pending\n * console.log(throttler.lastExecutionTime()); // timestamp of last execution\n * console.log(throttler.nextExecutionTime()); // timestamp of next allowed execution\n * ```\n */\nexport function createThrottler<\n TFn extends AnyFunction,\n TSelected = ThrottlerState<TFn>,\n>(\n fn: TFn,\n initialOptions: ThrottlerOptions<TFn>,\n selector?: (state: ThrottlerState<TFn>) => TSelected,\n): SolidThrottler<TFn, TSelected> {\n const asyncThrottler = new Throttler<TFn>(fn, initialOptions)\n\n const state = useStore(asyncThrottler.store, selector)\n\n createEffect(() => {\n onCleanup(() => {\n asyncThrottler.cancel()\n })\n })\n\n return {\n ...asyncThrottler,\n state,\n } as SolidThrottler<TFn, TSelected> // omit `store` in favor of `state`\n}\n"],"names":[],"mappings":";;;AAwDO,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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/solid-pacer",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.9.0",
|
|
4
4
|
"description": "Utilities for debouncing and throttling functions in Solid.",
|
|
5
5
|
"author": "Tanner Linsley",
|
|
6
6
|
"license": "MIT",
|
|
@@ -37,6 +37,16 @@
|
|
|
37
37
|
"default": "./dist/cjs/index.cjs"
|
|
38
38
|
}
|
|
39
39
|
},
|
|
40
|
+
"./async-batcher": {
|
|
41
|
+
"import": {
|
|
42
|
+
"types": "./dist/esm/async-batcher/index.d.ts",
|
|
43
|
+
"default": "./dist/esm/async-batcher/index.js"
|
|
44
|
+
},
|
|
45
|
+
"require": {
|
|
46
|
+
"types": "./dist/cjs/async-batcher/index.d.cts",
|
|
47
|
+
"default": "./dist/cjs/async-batcher/index.cjs"
|
|
48
|
+
}
|
|
49
|
+
},
|
|
40
50
|
"./async-debouncer": {
|
|
41
51
|
"import": {
|
|
42
52
|
"types": "./dist/esm/async-debouncer/index.d.ts",
|
|
@@ -152,11 +162,12 @@
|
|
|
152
162
|
"src"
|
|
153
163
|
],
|
|
154
164
|
"dependencies": {
|
|
155
|
-
"@tanstack/
|
|
165
|
+
"@tanstack/solid-store": "^0.7.3",
|
|
166
|
+
"@tanstack/pacer": "0.9.0"
|
|
156
167
|
},
|
|
157
168
|
"devDependencies": {
|
|
158
|
-
"solid-js": "^1.9.
|
|
159
|
-
"vite-plugin-solid": "^2.11.
|
|
169
|
+
"solid-js": "^1.9.7",
|
|
170
|
+
"vite-plugin-solid": "^2.11.7"
|
|
160
171
|
},
|
|
161
172
|
"peerDependencies": {
|
|
162
173
|
"solid-js": ">=1.9.5"
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { AsyncBatcher } from '@tanstack/pacer/async-batcher'
|
|
2
|
+
import { useStore } from '@tanstack/solid-store'
|
|
3
|
+
import type { Accessor } from 'solid-js'
|
|
4
|
+
import type {
|
|
5
|
+
AsyncBatcherOptions,
|
|
6
|
+
AsyncBatcherState,
|
|
7
|
+
} from '@tanstack/pacer/async-batcher'
|
|
8
|
+
|
|
9
|
+
export interface SolidAsyncBatcher<
|
|
10
|
+
TValue,
|
|
11
|
+
TSelected = AsyncBatcherState<TValue>,
|
|
12
|
+
> extends Omit<AsyncBatcher<TValue>, 'store'> {
|
|
13
|
+
/**
|
|
14
|
+
* Reactive state that will be updated when the batcher state changes
|
|
15
|
+
*
|
|
16
|
+
* Use this instead of `batcher.store.state`
|
|
17
|
+
*/
|
|
18
|
+
readonly state: Accessor<Readonly<TSelected>>
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Creates a Solid-compatible AsyncBatcher instance for managing asynchronous batches of items, exposing Solid signals for all stateful properties.
|
|
23
|
+
*
|
|
24
|
+
* This is the async version of the createBatcher hook. Unlike the sync version, this async batcher:
|
|
25
|
+
* - Handles promises and returns results from batch executions
|
|
26
|
+
* - Provides error handling with configurable error behavior
|
|
27
|
+
* - Tracks success, error, and settle counts separately
|
|
28
|
+
* - Has state tracking for when batches are executing
|
|
29
|
+
* - Returns the result of the batch function execution
|
|
30
|
+
*
|
|
31
|
+
* Features:
|
|
32
|
+
* - Configurable batch size and wait time
|
|
33
|
+
* - Custom batch processing logic via getShouldExecute
|
|
34
|
+
* - Event callbacks for monitoring batch operations
|
|
35
|
+
* - Error handling for failed batch operations
|
|
36
|
+
* - Automatic or manual batch processing
|
|
37
|
+
* - All stateful properties (items, counts, etc.) are exposed as Solid signals for reactivity
|
|
38
|
+
*
|
|
39
|
+
* The batcher collects items and processes them in batches based on:
|
|
40
|
+
* - Maximum batch size (number of items per batch)
|
|
41
|
+
* - Time-based batching (process after X milliseconds)
|
|
42
|
+
* - Custom batch processing logic via getShouldExecute
|
|
43
|
+
*
|
|
44
|
+
* Error Handling:
|
|
45
|
+
* - If an `onError` handler is provided, it will be called with the error and batcher instance
|
|
46
|
+
* - If `throwOnError` is true (default when no onError handler is provided), the error will be thrown
|
|
47
|
+
* - If `throwOnError` is false (default when onError handler is provided), the error will be swallowed
|
|
48
|
+
* - Both onError and throwOnError can be used together; the handler will be called before any error is thrown
|
|
49
|
+
* - The error state can be checked using the underlying AsyncBatcher instance
|
|
50
|
+
*
|
|
51
|
+
* Example usage:
|
|
52
|
+
* ```tsx
|
|
53
|
+
* // Basic async batcher for API requests
|
|
54
|
+
* const asyncBatcher = createAsyncBatcher(
|
|
55
|
+
* async (items) => {
|
|
56
|
+
* const results = await Promise.all(items.map(item => processItem(item)));
|
|
57
|
+
* return results;
|
|
58
|
+
* },
|
|
59
|
+
* {
|
|
60
|
+
* maxSize: 10,
|
|
61
|
+
* wait: 2000,
|
|
62
|
+
* onSuccess: (result) => {
|
|
63
|
+
* console.log('Batch processed successfully:', result);
|
|
64
|
+
* },
|
|
65
|
+
* onError: (error) => {
|
|
66
|
+
* console.error('Batch processing failed:', error);
|
|
67
|
+
* }
|
|
68
|
+
* }
|
|
69
|
+
* );
|
|
70
|
+
*
|
|
71
|
+
* // Add items to batch
|
|
72
|
+
* asyncBatcher.addItem(newItem);
|
|
73
|
+
*
|
|
74
|
+
* // Manually execute batch
|
|
75
|
+
* const result = await asyncBatcher.execute();
|
|
76
|
+
*
|
|
77
|
+
* // Use Solid signals in your UI
|
|
78
|
+
* const items = asyncBatcher.state().items;
|
|
79
|
+
* const isExecuting = asyncBatcher.state().isExecuting;
|
|
80
|
+
* ```
|
|
81
|
+
*/
|
|
82
|
+
export function createAsyncBatcher<
|
|
83
|
+
TValue,
|
|
84
|
+
TSelected = AsyncBatcherState<TValue>,
|
|
85
|
+
>(
|
|
86
|
+
fn: (items: Array<TValue>) => Promise<any>,
|
|
87
|
+
initialOptions: AsyncBatcherOptions<TValue> = {},
|
|
88
|
+
selector?: (state: AsyncBatcherState<TValue>) => TSelected,
|
|
89
|
+
): SolidAsyncBatcher<TValue, TSelected> {
|
|
90
|
+
const asyncBatcher = new AsyncBatcher<TValue>(fn, initialOptions)
|
|
91
|
+
|
|
92
|
+
const state = useStore(asyncBatcher.store, selector)
|
|
93
|
+
|
|
94
|
+
return {
|
|
95
|
+
...asyncBatcher,
|
|
96
|
+
state,
|
|
97
|
+
} as unknown as SolidAsyncBatcher<TValue, TSelected> // omit `store` in favor of `state`
|
|
98
|
+
}
|
|
@@ -1,24 +1,23 @@
|
|
|
1
1
|
import { AsyncDebouncer } from '@tanstack/pacer/async-debouncer'
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import type { AsyncDebouncerOptions } from '@tanstack/pacer/async-debouncer'
|
|
5
|
-
import type { AnyAsyncFunction } from '@tanstack/pacer/types'
|
|
2
|
+
import { useStore } from '@tanstack/solid-store'
|
|
3
|
+
import { createEffect, onCleanup } from 'solid-js'
|
|
6
4
|
import type { Accessor } from 'solid-js'
|
|
5
|
+
import type {
|
|
6
|
+
AsyncDebouncerOptions,
|
|
7
|
+
AsyncDebouncerState,
|
|
8
|
+
} from '@tanstack/pacer/async-debouncer'
|
|
9
|
+
import type { AnyAsyncFunction } from '@tanstack/pacer/types'
|
|
7
10
|
|
|
8
|
-
export interface SolidAsyncDebouncer<
|
|
9
|
-
extends
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
isPending: Accessor<boolean>
|
|
19
|
-
lastResult: Accessor<ReturnType<TFn> | undefined>
|
|
20
|
-
settleCount: Accessor<number>
|
|
21
|
-
successCount: Accessor<number>
|
|
11
|
+
export interface SolidAsyncDebouncer<
|
|
12
|
+
TFn extends AnyAsyncFunction,
|
|
13
|
+
TSelected = AsyncDebouncerState<TFn>,
|
|
14
|
+
> extends Omit<AsyncDebouncer<TFn>, 'store'> {
|
|
15
|
+
/**
|
|
16
|
+
* Reactive state that will be updated when the debouncer state changes
|
|
17
|
+
*
|
|
18
|
+
* Use this instead of `debouncer.store.state`
|
|
19
|
+
*/
|
|
20
|
+
readonly state: Accessor<Readonly<TSelected>>
|
|
22
21
|
}
|
|
23
22
|
|
|
24
23
|
/**
|
|
@@ -74,50 +73,26 @@ export interface SolidAsyncDebouncer<TFn extends AnyAsyncFunction>
|
|
|
74
73
|
* );
|
|
75
74
|
* ```
|
|
76
75
|
*/
|
|
77
|
-
export function createAsyncDebouncer<
|
|
76
|
+
export function createAsyncDebouncer<
|
|
77
|
+
TFn extends AnyAsyncFunction,
|
|
78
|
+
TSelected = AsyncDebouncerState<TFn>,
|
|
79
|
+
>(
|
|
78
80
|
fn: TFn,
|
|
79
81
|
initialOptions: AsyncDebouncerOptions<TFn>,
|
|
80
|
-
|
|
82
|
+
selector?: (state: AsyncDebouncerState<TFn>) => TSelected,
|
|
83
|
+
): SolidAsyncDebouncer<TFn, TSelected> {
|
|
81
84
|
const asyncDebouncer = new AsyncDebouncer<TFn>(fn, initialOptions)
|
|
82
85
|
|
|
83
|
-
const
|
|
84
|
-
asyncDebouncer.getErrorCount(),
|
|
85
|
-
)
|
|
86
|
-
const [settleCount, setSettleCount] = createSignal(
|
|
87
|
-
asyncDebouncer.getSettleCount(),
|
|
88
|
-
)
|
|
89
|
-
const [successCount, setSuccessCount] = createSignal(
|
|
90
|
-
asyncDebouncer.getSuccessCount(),
|
|
91
|
-
)
|
|
92
|
-
const [isPending, setIsPending] = createSignal(asyncDebouncer.getIsPending())
|
|
93
|
-
const [lastResult, setLastResult] = createSignal(
|
|
94
|
-
asyncDebouncer.getLastResult(),
|
|
95
|
-
)
|
|
86
|
+
const state = useStore(asyncDebouncer.store, selector)
|
|
96
87
|
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
onSettled: (asyncDebouncer) => {
|
|
101
|
-
setSuccessCount(asyncDebouncer.getSuccessCount())
|
|
102
|
-
setErrorCount(asyncDebouncer.getErrorCount())
|
|
103
|
-
setSettleCount(asyncDebouncer.getSettleCount())
|
|
104
|
-
setIsPending(asyncDebouncer.getIsPending())
|
|
105
|
-
setLastResult(asyncDebouncer.getLastResult())
|
|
106
|
-
const onSettled = newOptions.onSettled ?? initialOptions.onSettled
|
|
107
|
-
onSettled?.(asyncDebouncer)
|
|
108
|
-
},
|
|
88
|
+
createEffect(() => {
|
|
89
|
+
onCleanup(() => {
|
|
90
|
+
asyncDebouncer.cancel()
|
|
109
91
|
})
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
setOptions(initialOptions)
|
|
92
|
+
})
|
|
113
93
|
|
|
114
94
|
return {
|
|
115
|
-
...
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
lastResult,
|
|
119
|
-
settleCount,
|
|
120
|
-
successCount,
|
|
121
|
-
setOptions,
|
|
122
|
-
} as SolidAsyncDebouncer<TFn>
|
|
95
|
+
...asyncDebouncer,
|
|
96
|
+
state,
|
|
97
|
+
} as unknown as SolidAsyncDebouncer<TFn, TSelected> // omit `store` in favor of `state`
|
|
123
98
|
}
|