@tanstack/solid-pacer 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/dist/cjs/async-debouncer/createAsyncDebouncer.cjs +32 -0
- package/dist/cjs/async-debouncer/createAsyncDebouncer.cjs.map +1 -0
- package/dist/cjs/async-debouncer/createAsyncDebouncer.d.cts +42 -0
- package/dist/cjs/async-debouncer/index.cjs +12 -0
- package/dist/cjs/async-debouncer/index.cjs.map +1 -0
- package/dist/cjs/async-debouncer/index.d.cts +2 -0
- package/dist/cjs/async-queuer/createAsyncQueuer.cjs +69 -0
- package/dist/cjs/async-queuer/createAsyncQueuer.cjs.map +1 -0
- package/dist/cjs/async-queuer/createAsyncQueuer.d.cts +90 -0
- package/dist/cjs/async-queuer/index.cjs +12 -0
- package/dist/cjs/async-queuer/index.cjs.map +1 -0
- package/dist/cjs/async-queuer/index.d.cts +2 -0
- package/dist/cjs/async-rate-limiter/createAsyncRateLimiter.cjs +50 -0
- package/dist/cjs/async-rate-limiter/createAsyncRateLimiter.cjs.map +1 -0
- package/dist/cjs/async-rate-limiter/createAsyncRateLimiter.d.cts +46 -0
- package/dist/cjs/async-rate-limiter/index.cjs +12 -0
- package/dist/cjs/async-rate-limiter/index.cjs.map +1 -0
- package/dist/cjs/async-rate-limiter/index.d.cts +2 -0
- package/dist/cjs/async-throttler/createAsyncThrottler.cjs +42 -0
- package/dist/cjs/async-throttler/createAsyncThrottler.cjs.map +1 -0
- package/dist/cjs/async-throttler/createAsyncThrottler.d.cts +46 -0
- package/dist/cjs/async-throttler/index.cjs +12 -0
- package/dist/cjs/async-throttler/index.cjs.map +1 -0
- package/dist/cjs/async-throttler/index.d.cts +2 -0
- package/dist/cjs/compare/index.cjs +10 -0
- package/dist/cjs/compare/index.cjs.map +1 -0
- package/dist/cjs/compare/index.d.cts +1 -0
- package/dist/cjs/debouncer/createDebouncedSignal.cjs +11 -0
- package/dist/cjs/debouncer/createDebouncedSignal.cjs.map +1 -0
- package/dist/cjs/debouncer/createDebouncedSignal.d.cts +47 -0
- package/dist/cjs/debouncer/createDebouncedValue.cjs +19 -0
- package/dist/cjs/debouncer/createDebouncedValue.cjs.map +1 -0
- package/dist/cjs/debouncer/createDebouncedValue.d.cts +44 -0
- package/dist/cjs/debouncer/createDebouncer.cjs +32 -0
- package/dist/cjs/debouncer/createDebouncer.cjs.map +1 -0
- package/dist/cjs/debouncer/createDebouncer.d.cts +47 -0
- package/dist/cjs/debouncer/index.cjs +16 -0
- package/dist/cjs/debouncer/index.cjs.map +1 -0
- package/dist/cjs/debouncer/index.d.cts +4 -0
- package/dist/cjs/index.cjs +38 -0
- package/dist/cjs/index.cjs.map +1 -0
- package/dist/cjs/index.d.cts +18 -0
- package/dist/cjs/queuer/createQueuer.cjs +66 -0
- package/dist/cjs/queuer/createQueuer.cjs.map +1 -0
- package/dist/cjs/queuer/createQueuer.d.cts +82 -0
- package/dist/cjs/queuer/index.cjs +12 -0
- package/dist/cjs/queuer/index.cjs.map +1 -0
- package/dist/cjs/queuer/index.d.cts +2 -0
- package/dist/cjs/rate-limiter/createRateLimitedSignal.cjs +15 -0
- package/dist/cjs/rate-limiter/createRateLimitedSignal.cjs.map +1 -0
- package/dist/cjs/rate-limiter/createRateLimitedSignal.d.cts +58 -0
- package/dist/cjs/rate-limiter/createRateLimitedValue.cjs +13 -0
- package/dist/cjs/rate-limiter/createRateLimitedValue.cjs.map +1 -0
- package/dist/cjs/rate-limiter/createRateLimitedValue.d.cts +51 -0
- package/dist/cjs/rate-limiter/createRateLimiter.cjs +50 -0
- package/dist/cjs/rate-limiter/createRateLimiter.cjs.map +1 -0
- package/dist/cjs/rate-limiter/createRateLimiter.d.cts +49 -0
- package/dist/cjs/rate-limiter/index.cjs +16 -0
- package/dist/cjs/rate-limiter/index.cjs.map +1 -0
- package/dist/cjs/rate-limiter/index.d.cts +4 -0
- package/dist/cjs/throttler/createThrottledSignal.cjs +11 -0
- package/dist/cjs/throttler/createThrottledSignal.cjs.map +1 -0
- package/dist/cjs/throttler/createThrottledSignal.d.cts +42 -0
- package/dist/cjs/throttler/createThrottledValue.cjs +19 -0
- package/dist/cjs/throttler/createThrottledValue.cjs.map +1 -0
- package/dist/cjs/throttler/createThrottledValue.d.cts +36 -0
- package/dist/cjs/throttler/createThrottler.cjs +42 -0
- package/dist/cjs/throttler/createThrottler.cjs.map +1 -0
- package/dist/cjs/throttler/createThrottler.d.cts +47 -0
- package/dist/cjs/throttler/index.cjs +16 -0
- package/dist/cjs/throttler/index.cjs.map +1 -0
- package/dist/cjs/throttler/index.d.cts +4 -0
- package/dist/cjs/types/index.cjs +10 -0
- package/dist/cjs/types/index.cjs.map +1 -0
- package/dist/cjs/types/index.d.cts +1 -0
- package/dist/cjs/utils/index.cjs +10 -0
- package/dist/cjs/utils/index.cjs.map +1 -0
- package/dist/cjs/utils/index.d.cts +1 -0
- package/dist/esm/async-debouncer/createAsyncDebouncer.d.ts +42 -0
- package/dist/esm/async-debouncer/createAsyncDebouncer.js +32 -0
- package/dist/esm/async-debouncer/createAsyncDebouncer.js.map +1 -0
- package/dist/esm/async-debouncer/index.d.ts +2 -0
- package/dist/esm/async-debouncer/index.js +6 -0
- package/dist/esm/async-debouncer/index.js.map +1 -0
- package/dist/esm/async-queuer/createAsyncQueuer.d.ts +90 -0
- package/dist/esm/async-queuer/createAsyncQueuer.js +69 -0
- package/dist/esm/async-queuer/createAsyncQueuer.js.map +1 -0
- package/dist/esm/async-queuer/index.d.ts +2 -0
- package/dist/esm/async-queuer/index.js +6 -0
- package/dist/esm/async-queuer/index.js.map +1 -0
- package/dist/esm/async-rate-limiter/createAsyncRateLimiter.d.ts +46 -0
- package/dist/esm/async-rate-limiter/createAsyncRateLimiter.js +50 -0
- package/dist/esm/async-rate-limiter/createAsyncRateLimiter.js.map +1 -0
- package/dist/esm/async-rate-limiter/index.d.ts +2 -0
- package/dist/esm/async-rate-limiter/index.js +6 -0
- package/dist/esm/async-rate-limiter/index.js.map +1 -0
- package/dist/esm/async-throttler/createAsyncThrottler.d.ts +46 -0
- package/dist/esm/async-throttler/createAsyncThrottler.js +42 -0
- package/dist/esm/async-throttler/createAsyncThrottler.js.map +1 -0
- package/dist/esm/async-throttler/index.d.ts +2 -0
- package/dist/esm/async-throttler/index.js +6 -0
- package/dist/esm/async-throttler/index.js.map +1 -0
- package/dist/esm/compare/index.d.ts +1 -0
- package/dist/esm/compare/index.js +2 -0
- package/dist/esm/compare/index.js.map +1 -0
- package/dist/esm/debouncer/createDebouncedSignal.d.ts +47 -0
- package/dist/esm/debouncer/createDebouncedSignal.js +11 -0
- package/dist/esm/debouncer/createDebouncedSignal.js.map +1 -0
- package/dist/esm/debouncer/createDebouncedValue.d.ts +44 -0
- package/dist/esm/debouncer/createDebouncedValue.js +19 -0
- package/dist/esm/debouncer/createDebouncedValue.js.map +1 -0
- package/dist/esm/debouncer/createDebouncer.d.ts +47 -0
- package/dist/esm/debouncer/createDebouncer.js +32 -0
- package/dist/esm/debouncer/createDebouncer.js.map +1 -0
- package/dist/esm/debouncer/index.d.ts +4 -0
- package/dist/esm/debouncer/index.js +10 -0
- package/dist/esm/debouncer/index.js.map +1 -0
- package/dist/esm/index.d.ts +18 -0
- package/dist/esm/index.js +32 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/queuer/createQueuer.d.ts +82 -0
- package/dist/esm/queuer/createQueuer.js +66 -0
- package/dist/esm/queuer/createQueuer.js.map +1 -0
- package/dist/esm/queuer/index.d.ts +2 -0
- package/dist/esm/queuer/index.js +6 -0
- package/dist/esm/queuer/index.js.map +1 -0
- package/dist/esm/rate-limiter/createRateLimitedSignal.d.ts +58 -0
- package/dist/esm/rate-limiter/createRateLimitedSignal.js +15 -0
- package/dist/esm/rate-limiter/createRateLimitedSignal.js.map +1 -0
- package/dist/esm/rate-limiter/createRateLimitedValue.d.ts +51 -0
- package/dist/esm/rate-limiter/createRateLimitedValue.js +13 -0
- package/dist/esm/rate-limiter/createRateLimitedValue.js.map +1 -0
- package/dist/esm/rate-limiter/createRateLimiter.d.ts +49 -0
- package/dist/esm/rate-limiter/createRateLimiter.js +50 -0
- package/dist/esm/rate-limiter/createRateLimiter.js.map +1 -0
- package/dist/esm/rate-limiter/index.d.ts +4 -0
- package/dist/esm/rate-limiter/index.js +10 -0
- package/dist/esm/rate-limiter/index.js.map +1 -0
- package/dist/esm/throttler/createThrottledSignal.d.ts +42 -0
- package/dist/esm/throttler/createThrottledSignal.js +11 -0
- package/dist/esm/throttler/createThrottledSignal.js.map +1 -0
- package/dist/esm/throttler/createThrottledValue.d.ts +36 -0
- package/dist/esm/throttler/createThrottledValue.js +19 -0
- package/dist/esm/throttler/createThrottledValue.js.map +1 -0
- package/dist/esm/throttler/createThrottler.d.ts +47 -0
- package/dist/esm/throttler/createThrottler.js +42 -0
- package/dist/esm/throttler/createThrottler.js.map +1 -0
- package/dist/esm/throttler/index.d.ts +4 -0
- package/dist/esm/throttler/index.js +10 -0
- package/dist/esm/throttler/index.js.map +1 -0
- package/dist/esm/types/index.d.ts +1 -0
- package/dist/esm/types/index.js +2 -0
- package/dist/esm/types/index.js.map +1 -0
- package/dist/esm/utils/index.d.ts +1 -0
- package/dist/esm/utils/index.js +2 -0
- package/dist/esm/utils/index.js.map +1 -0
- package/package.json +163 -0
- package/src/async-debouncer/createAsyncDebouncer.ts +89 -0
- package/src/async-debouncer/index.ts +4 -0
- package/src/async-queuer/createAsyncQueuer.ts +175 -0
- package/src/async-queuer/index.ts +3 -0
- package/src/async-rate-limiter/createAsyncRateLimiter.ts +116 -0
- package/src/async-rate-limiter/index.ts +4 -0
- package/src/async-throttler/createAsyncThrottler.ts +106 -0
- package/src/async-throttler/index.ts +4 -0
- package/src/compare/index.ts +1 -0
- package/src/debouncer/createDebouncedSignal.ts +59 -0
- package/src/debouncer/createDebouncedValue.ts +64 -0
- package/src/debouncer/createDebouncer.ts +90 -0
- package/src/debouncer/index.ts +6 -0
- package/src/index.ts +36 -0
- package/src/queuer/createQueuer.ts +167 -0
- package/src/queuer/index.ts +3 -0
- package/src/rate-limiter/createRateLimitedSignal.ts +74 -0
- package/src/rate-limiter/createRateLimitedValue.ts +66 -0
- package/src/rate-limiter/createRateLimiter.ts +115 -0
- package/src/rate-limiter/index.ts +5 -0
- package/src/throttler/createThrottledSignal.ts +52 -0
- package/src/throttler/createThrottledValue.ts +56 -0
- package/src/throttler/createThrottler.ts +106 -0
- package/src/throttler/index.ts +6 -0
- package/src/types/index.ts +1 -0
- package/src/utils/index.ts +1 -0
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { SolidRateLimiter } from './createRateLimiter.cjs';
|
|
2
|
+
import { Accessor, Setter } from 'solid-js';
|
|
3
|
+
import { RateLimiterOptions } from '@tanstack/pacer/rate-limiter';
|
|
4
|
+
/**
|
|
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
|
+
* This hook uses Solid's createSignal internally to manage the rate-limited state.
|
|
7
|
+
*
|
|
8
|
+
* Rate limiting is a simple "hard limit" approach - it allows all updates until the limit is reached, then blocks
|
|
9
|
+
* subsequent updates until the window resets. Unlike throttling or debouncing, it does not attempt to space out
|
|
10
|
+
* or intelligently collapse updates. This can lead to bursts of rapid updates followed by periods of no updates.
|
|
11
|
+
*
|
|
12
|
+
* For smoother update patterns, consider:
|
|
13
|
+
* - createThrottledValue: When you want consistent spacing between updates (e.g. UI changes)
|
|
14
|
+
* - createDebouncedValue: When you want to collapse rapid updates into a single update (e.g. search input)
|
|
15
|
+
*
|
|
16
|
+
* Rate limiting should primarily be used when you need to enforce strict limits, like API rate limits.
|
|
17
|
+
*
|
|
18
|
+
* The hook returns both the rate-limited value and the underlying rateLimiter instance for additional control.
|
|
19
|
+
*
|
|
20
|
+
* For more direct control over rate limiting behavior without Solid state management,
|
|
21
|
+
* consider using the lower-level createRateLimiter hook instead.
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```tsx
|
|
25
|
+
* // Basic rate limiting - update at most 5 times per minute
|
|
26
|
+
* const [rateLimitedValue] = createRateLimitedValue(rawValue, {
|
|
27
|
+
* limit: 5,
|
|
28
|
+
* window: 60000
|
|
29
|
+
* });
|
|
30
|
+
*
|
|
31
|
+
* // With rejection callback
|
|
32
|
+
* const [rateLimitedValue, rateLimiter] = createRateLimitedValue(rawValue, {
|
|
33
|
+
* limit: 3,
|
|
34
|
+
* window: 5000,
|
|
35
|
+
* onReject: (rateLimiter) => {
|
|
36
|
+
* console.log(`Update rejected. Try again in ${rateLimiter.getMsUntilNextWindow()}ms`);
|
|
37
|
+
* }
|
|
38
|
+
* });
|
|
39
|
+
*
|
|
40
|
+
* // Optionally access rateLimiter state via signals
|
|
41
|
+
* const handleSubmit = () => {
|
|
42
|
+
* const remaining = rateLimiter.remainingInWindow();
|
|
43
|
+
* if (remaining > 0) {
|
|
44
|
+
* console.log(`${remaining} updates remaining in this window`);
|
|
45
|
+
* } else {
|
|
46
|
+
* console.log('Rate limit reached for this window');
|
|
47
|
+
* }
|
|
48
|
+
* };
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
export declare function createRateLimitedValue<TValue>(value: Accessor<TValue>, initialOptions: RateLimiterOptions<Setter<TValue>, [Accessor<TValue>]>): [Accessor<TValue>, SolidRateLimiter<Setter<TValue>, [Accessor<TValue>]>];
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const rateLimiter = require("@tanstack/pacer/rate-limiter");
|
|
4
|
+
const solidJs = require("solid-js");
|
|
5
|
+
const utils = require("@tanstack/pacer/utils");
|
|
6
|
+
function createRateLimiter(fn, initialOptions) {
|
|
7
|
+
const rateLimiter$1 = new rateLimiter.RateLimiter(fn, initialOptions);
|
|
8
|
+
const [executionCount, setExecutionCount] = solidJs.createSignal(
|
|
9
|
+
rateLimiter$1.getExecutionCount()
|
|
10
|
+
);
|
|
11
|
+
const [rejectionCount, setRejectionCount] = solidJs.createSignal(
|
|
12
|
+
rateLimiter$1.getRejectionCount()
|
|
13
|
+
);
|
|
14
|
+
const [remainingInWindow, setRemainingInWindow] = solidJs.createSignal(
|
|
15
|
+
rateLimiter$1.getRemainingInWindow()
|
|
16
|
+
);
|
|
17
|
+
const [msUntilNextWindow, setMsUntilNextWindow] = solidJs.createSignal(
|
|
18
|
+
rateLimiter$1.getMsUntilNextWindow()
|
|
19
|
+
);
|
|
20
|
+
function setOptions(newOptions) {
|
|
21
|
+
rateLimiter$1.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);
|
|
40
|
+
return {
|
|
41
|
+
...utils.bindInstanceMethods(rateLimiter$1),
|
|
42
|
+
executionCount,
|
|
43
|
+
rejectionCount,
|
|
44
|
+
remainingInWindow,
|
|
45
|
+
msUntilNextWindow,
|
|
46
|
+
setOptions
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
exports.createRateLimiter = createRateLimiter;
|
|
50
|
+
//# sourceMappingURL=createRateLimiter.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createRateLimiter.cjs","sources":["../../../src/rate-limiter/createRateLimiter.ts"],"sourcesContent":["import { RateLimiter } from '@tanstack/pacer/rate-limiter'\nimport { createSignal } from 'solid-js'\nimport { bindInstanceMethods } from '@tanstack/pacer/utils'\nimport type { Accessor } from 'solid-js'\nimport type { AnyFunction } from '@tanstack/pacer/types'\nimport type { RateLimiterOptions } from '@tanstack/pacer/rate-limiter'\n\nexport interface SolidRateLimiter<\n TFn extends AnyFunction,\n TArgs extends Parameters<TFn>,\n> extends Omit<\n RateLimiter<TFn, TArgs>,\n | 'getExecutionCount'\n | 'getMsUntilNextWindow'\n | 'getRejectionCount'\n | 'getRemainingInWindow'\n > {\n executionCount: Accessor<number>\n msUntilNextWindow: Accessor<number>\n rejectionCount: Accessor<number>\n remainingInWindow: Accessor<number>\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 * 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\n * const rateLimiter = createRateLimiter(apiCall, {\n * limit: 5,\n * window: 60000,\n * });\n *\n * // Monitor rate limit status\n * const handleClick = () => {\n * if (rateLimiter.remainingInWindow() > 0) {\n * rateLimiter.maybeExecute(data);\n * } else {\n * showRateLimitWarning();\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 TArgs extends Parameters<TFn>,\n>(\n fn: TFn,\n initialOptions: RateLimiterOptions<TFn, TArgs>,\n): SolidRateLimiter<TFn, TArgs> {\n const rateLimiter = new RateLimiter<TFn, TArgs>(fn, initialOptions)\n\n const [executionCount, setExecutionCount] = createSignal(\n rateLimiter.getExecutionCount(),\n )\n const [rejectionCount, setRejectionCount] = createSignal(\n rateLimiter.getRejectionCount(),\n )\n const [remainingInWindow, setRemainingInWindow] = createSignal(\n rateLimiter.getRemainingInWindow(),\n )\n const [msUntilNextWindow, setMsUntilNextWindow] = createSignal(\n rateLimiter.getMsUntilNextWindow(),\n )\n\n function setOptions(newOptions: Partial<RateLimiterOptions<TFn, TArgs>>) {\n rateLimiter.setOptions({\n ...newOptions,\n onExecute: (rateLimiter) => {\n setExecutionCount(rateLimiter.getExecutionCount())\n setRemainingInWindow(rateLimiter.getRemainingInWindow())\n setMsUntilNextWindow(rateLimiter.getMsUntilNextWindow())\n const onExecute = newOptions.onExecute ?? initialOptions.onExecute\n onExecute?.(rateLimiter)\n },\n onReject: (rateLimiter) => {\n setRejectionCount(rateLimiter.getRejectionCount())\n setRemainingInWindow(rateLimiter.getRemainingInWindow())\n setMsUntilNextWindow(rateLimiter.getMsUntilNextWindow())\n const onReject = newOptions.onReject ?? initialOptions.onReject\n onReject?.(rateLimiter)\n },\n })\n }\n\n setOptions(initialOptions)\n\n return {\n ...bindInstanceMethods(rateLimiter),\n executionCount,\n rejectionCount,\n remainingInWindow,\n msUntilNextWindow,\n setOptions,\n }\n}\n"],"names":["rateLimiter","RateLimiter","createSignal","bindInstanceMethods"],"mappings":";;;;;AA8DgB,SAAA,kBAId,IACA,gBAC8B;AAC9B,QAAMA,gBAAc,IAAIC,wBAAwB,IAAI,cAAc;AAE5D,QAAA,CAAC,gBAAgB,iBAAiB,IAAIC,QAAA;AAAA,IAC1CF,cAAY,kBAAkB;AAAA,EAChC;AACM,QAAA,CAAC,gBAAgB,iBAAiB,IAAIE,QAAA;AAAA,IAC1CF,cAAY,kBAAkB;AAAA,EAChC;AACM,QAAA,CAAC,mBAAmB,oBAAoB,IAAIE,QAAA;AAAA,IAChDF,cAAY,qBAAqB;AAAA,EACnC;AACM,QAAA,CAAC,mBAAmB,oBAAoB,IAAIE,QAAA;AAAA,IAChDF,cAAY,qBAAqB;AAAA,EACnC;AAEA,WAAS,WAAW,YAAqD;AACvEA,kBAAY,WAAW;AAAA,MACrB,GAAG;AAAA,MACH,WAAW,CAACA,iBAAgB;AACRA,0BAAAA,aAAY,mBAAmB;AAC5BA,6BAAAA,aAAY,sBAAsB;AAClCA,6BAAAA,aAAY,sBAAsB;AACjD,cAAA,YAAY,WAAW,aAAa,eAAe;AACzD,+CAAYA;AAAAA,MACd;AAAA,MACA,UAAU,CAACA,iBAAgB;AACPA,0BAAAA,aAAY,mBAAmB;AAC5BA,6BAAAA,aAAY,sBAAsB;AAClCA,6BAAAA,aAAY,sBAAsB;AACjD,cAAA,WAAW,WAAW,YAAY,eAAe;AACvD,6CAAWA;AAAAA,MAAW;AAAA,IACxB,CACD;AAAA,EAAA;AAGH,aAAW,cAAc;AAElB,SAAA;AAAA,IACL,GAAGG,MAAAA,oBAAoBH,aAAW;AAAA,IAClC;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACF;;"}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { RateLimiter, RateLimiterOptions } from '@tanstack/pacer/rate-limiter';
|
|
2
|
+
import { Accessor } from 'solid-js';
|
|
3
|
+
import { AnyFunction } from '@tanstack/pacer/types';
|
|
4
|
+
export interface SolidRateLimiter<TFn extends AnyFunction, TArgs extends Parameters<TFn>> extends Omit<RateLimiter<TFn, TArgs>, 'getExecutionCount' | 'getMsUntilNextWindow' | 'getRejectionCount' | 'getRemainingInWindow'> {
|
|
5
|
+
executionCount: Accessor<number>;
|
|
6
|
+
msUntilNextWindow: Accessor<number>;
|
|
7
|
+
rejectionCount: Accessor<number>;
|
|
8
|
+
remainingInWindow: Accessor<number>;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* A low-level Solid hook that creates a `RateLimiter` instance to enforce rate limits on function execution.
|
|
12
|
+
*
|
|
13
|
+
* This hook is designed to be flexible and state-management agnostic - it simply returns a rate limiter instance that
|
|
14
|
+
* you can integrate with any state management solution (createSignal, etc).
|
|
15
|
+
*
|
|
16
|
+
* Rate limiting is a simple "hard limit" approach that allows executions until a maximum count is reached within
|
|
17
|
+
* a time window, then blocks all subsequent calls until the window resets. Unlike throttling or debouncing,
|
|
18
|
+
* it does not attempt to space out or collapse executions intelligently.
|
|
19
|
+
*
|
|
20
|
+
* For smoother execution patterns:
|
|
21
|
+
* - Use throttling when you want consistent spacing between executions (e.g. UI updates)
|
|
22
|
+
* - Use debouncing when you want to collapse rapid-fire events (e.g. search input)
|
|
23
|
+
* - Use rate limiting only when you need to enforce hard limits (e.g. API rate limits)
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```tsx
|
|
27
|
+
* // Basic rate limiting - max 5 calls per minute
|
|
28
|
+
* const rateLimiter = createRateLimiter(apiCall, {
|
|
29
|
+
* limit: 5,
|
|
30
|
+
* window: 60000,
|
|
31
|
+
* });
|
|
32
|
+
*
|
|
33
|
+
* // Monitor rate limit status
|
|
34
|
+
* const handleClick = () => {
|
|
35
|
+
* if (rateLimiter.remainingInWindow() > 0) {
|
|
36
|
+
* rateLimiter.maybeExecute(data);
|
|
37
|
+
* } else {
|
|
38
|
+
* showRateLimitWarning();
|
|
39
|
+
* }
|
|
40
|
+
* };
|
|
41
|
+
*
|
|
42
|
+
* // Access rate limiter state via signals
|
|
43
|
+
* console.log('Executions:', rateLimiter.executionCount());
|
|
44
|
+
* console.log('Rejections:', rateLimiter.rejectionCount());
|
|
45
|
+
* console.log('Remaining:', rateLimiter.remainingInWindow());
|
|
46
|
+
* console.log('Next window in:', rateLimiter.msUntilNextWindow());
|
|
47
|
+
* ```
|
|
48
|
+
*/
|
|
49
|
+
export declare function createRateLimiter<TFn extends AnyFunction, TArgs extends Parameters<TFn>>(fn: TFn, initialOptions: RateLimiterOptions<TFn, TArgs>): SolidRateLimiter<TFn, TArgs>;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const rateLimiter = require("@tanstack/pacer/rate-limiter");
|
|
4
|
+
const createRateLimiter = require("./createRateLimiter.cjs");
|
|
5
|
+
const createRateLimitedSignal = require("./createRateLimitedSignal.cjs");
|
|
6
|
+
const createRateLimitedValue = require("./createRateLimitedValue.cjs");
|
|
7
|
+
exports.createRateLimiter = createRateLimiter.createRateLimiter;
|
|
8
|
+
exports.createRateLimitedSignal = createRateLimitedSignal.createRateLimitedSignal;
|
|
9
|
+
exports.createRateLimitedValue = createRateLimitedValue.createRateLimitedValue;
|
|
10
|
+
Object.keys(rateLimiter).forEach((k) => {
|
|
11
|
+
if (k !== "default" && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
|
|
12
|
+
enumerable: true,
|
|
13
|
+
get: () => rateLimiter[k]
|
|
14
|
+
});
|
|
15
|
+
});
|
|
16
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const solidJs = require("solid-js");
|
|
4
|
+
const createThrottler = require("./createThrottler.cjs");
|
|
5
|
+
function createThrottledSignal(value, initialOptions) {
|
|
6
|
+
const [throttledValue, setThrottledValue] = solidJs.createSignal(value);
|
|
7
|
+
const throttler = createThrottler.createThrottler(setThrottledValue, initialOptions);
|
|
8
|
+
return [throttledValue, throttler.maybeExecute, throttler];
|
|
9
|
+
}
|
|
10
|
+
exports.createThrottledSignal = createThrottledSignal;
|
|
11
|
+
//# sourceMappingURL=createThrottledSignal.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createThrottledSignal.cjs","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 { ThrottlerOptions } 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<TValue>(\n value: TValue,\n initialOptions: ThrottlerOptions<Setter<TValue>, [Accessor<TValue>]>,\n): [\n Accessor<TValue>,\n Setter<TValue>,\n SolidThrottler<Setter<TValue>, [Accessor<TValue>]>,\n] {\n const [throttledValue, setThrottledValue] = createSignal<TValue>(value)\n const throttler = createThrottler(setThrottledValue, initialOptions)\n return [throttledValue, throttler.maybeExecute as Setter<TValue>, throttler]\n}\n"],"names":["createSignal","createThrottler"],"mappings":";;;;AAwCgB,SAAA,sBACd,OACA,gBAKA;AACA,QAAM,CAAC,gBAAgB,iBAAiB,IAAIA,QAAAA,aAAqB,KAAK;AAChE,QAAA,YAAYC,gBAAAA,gBAAgB,mBAAmB,cAAc;AACnE,SAAO,CAAC,gBAAgB,UAAU,cAAgC,SAAS;AAC7E;;"}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { SolidThrottler } from './createThrottler.cjs';
|
|
2
|
+
import { Accessor, Setter } from 'solid-js';
|
|
3
|
+
import { ThrottlerOptions } from '@tanstack/pacer/throttler';
|
|
4
|
+
/**
|
|
5
|
+
* A Solid hook that creates a throttled state value that updates at most once within a specified time window.
|
|
6
|
+
* This hook combines Solid's createSignal with throttling functionality to provide controlled state updates.
|
|
7
|
+
*
|
|
8
|
+
* Throttling ensures state updates occur at a controlled rate regardless of how frequently the setter is called.
|
|
9
|
+
* This is useful for rate-limiting expensive re-renders or operations that depend on rapidly changing state.
|
|
10
|
+
*
|
|
11
|
+
* The hook returns a tuple containing:
|
|
12
|
+
* - The throttled state value accessor
|
|
13
|
+
* - A throttled setter function that respects the configured wait time
|
|
14
|
+
* - The throttler instance for additional control
|
|
15
|
+
*
|
|
16
|
+
* For more direct control over throttling without state management,
|
|
17
|
+
* consider using the lower-level createThrottler hook instead.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```tsx
|
|
21
|
+
* // Basic throttling - update state at most once per second
|
|
22
|
+
* const [value, setValue, throttler] = createThrottledSignal(0, { wait: 1000 });
|
|
23
|
+
*
|
|
24
|
+
* // With custom leading/trailing behavior
|
|
25
|
+
* const [value, setValue] = createThrottledSignal(0, {
|
|
26
|
+
* wait: 1000,
|
|
27
|
+
* leading: true, // Update immediately on first change
|
|
28
|
+
* trailing: false // Skip trailing edge updates
|
|
29
|
+
* });
|
|
30
|
+
*
|
|
31
|
+
* // Access throttler state via signals
|
|
32
|
+
* console.log('Executions:', throttler.executionCount());
|
|
33
|
+
* console.log('Is pending:', throttler.isPending());
|
|
34
|
+
* console.log('Last execution:', throttler.lastExecutionTime());
|
|
35
|
+
* console.log('Next execution:', throttler.nextExecutionTime());
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
export declare function createThrottledSignal<TValue>(value: TValue, initialOptions: ThrottlerOptions<Setter<TValue>, [Accessor<TValue>]>): [
|
|
39
|
+
Accessor<TValue>,
|
|
40
|
+
Setter<TValue>,
|
|
41
|
+
SolidThrottler<Setter<TValue>, [Accessor<TValue>]>
|
|
42
|
+
];
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const solidJs = require("solid-js");
|
|
4
|
+
const createThrottledSignal = require("./createThrottledSignal.cjs");
|
|
5
|
+
function createThrottledValue(value, initialOptions) {
|
|
6
|
+
const [throttledValue, setThrottledValue, throttler] = createThrottledSignal.createThrottledSignal(
|
|
7
|
+
value(),
|
|
8
|
+
initialOptions
|
|
9
|
+
);
|
|
10
|
+
solidJs.createEffect(() => {
|
|
11
|
+
setThrottledValue(value());
|
|
12
|
+
solidJs.onCleanup(() => {
|
|
13
|
+
throttler.cancel();
|
|
14
|
+
});
|
|
15
|
+
});
|
|
16
|
+
return [throttledValue, throttler];
|
|
17
|
+
}
|
|
18
|
+
exports.createThrottledValue = createThrottledValue;
|
|
19
|
+
//# sourceMappingURL=createThrottledValue.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createThrottledValue.cjs","sources":["../../../src/throttler/createThrottledValue.ts"],"sourcesContent":["import { createEffect, onCleanup } from 'solid-js'\nimport { createThrottledSignal } from './createThrottledSignal'\nimport type { SolidThrottler } from './createThrottler'\nimport type { Accessor, Setter } from 'solid-js'\nimport type { ThrottlerOptions } 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 both the throttled value and the underlying throttler instance for additional control.\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] = createThrottledValue(rawValue, { wait: 1000 });\n *\n * // With custom leading/trailing behavior\n * const [throttledValue, throttler] = createThrottledValue(rawValue, {\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 createThrottledValue<TValue>(\n value: Accessor<TValue>,\n initialOptions: ThrottlerOptions<Setter<TValue>, [Accessor<TValue>]>,\n): [Accessor<TValue>, SolidThrottler<Setter<TValue>, [Accessor<TValue>]>] {\n const [throttledValue, setThrottledValue, throttler] = createThrottledSignal(\n value(),\n initialOptions,\n )\n\n createEffect(() => {\n setThrottledValue(value() as any)\n onCleanup(() => {\n throttler.cancel()\n })\n })\n\n return [throttledValue, throttler]\n}\n"],"names":["createThrottledSignal","createEffect","onCleanup"],"mappings":";;;;AAsCgB,SAAA,qBACd,OACA,gBACwE;AACxE,QAAM,CAAC,gBAAgB,mBAAmB,SAAS,IAAIA,sBAAA;AAAA,IACrD,MAAM;AAAA,IACN;AAAA,EACF;AAEAC,UAAAA,aAAa,MAAM;AACjB,sBAAkB,OAAc;AAChCC,YAAAA,UAAU,MAAM;AACd,gBAAU,OAAO;AAAA,IAAA,CAClB;AAAA,EAAA,CACF;AAEM,SAAA,CAAC,gBAAgB,SAAS;AACnC;;"}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { SolidThrottler } from './createThrottler.cjs';
|
|
2
|
+
import { Accessor, Setter } from 'solid-js';
|
|
3
|
+
import { ThrottlerOptions } from '@tanstack/pacer/throttler';
|
|
4
|
+
/**
|
|
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
|
+
* This hook uses Solid's createSignal internally to manage the throttled state.
|
|
7
|
+
*
|
|
8
|
+
* Throttling ensures the value updates occur at a controlled rate regardless of how frequently the input value changes.
|
|
9
|
+
* This is useful for rate-limiting expensive re-renders or API calls that depend on rapidly changing values.
|
|
10
|
+
*
|
|
11
|
+
* The hook returns both the throttled value and the underlying throttler instance for additional control.
|
|
12
|
+
* The throttled value will update according to the leading/trailing edge behavior specified in the options.
|
|
13
|
+
*
|
|
14
|
+
* For more direct control over throttling behavior without Solid state management,
|
|
15
|
+
* consider using the lower-level createThrottler hook instead.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```tsx
|
|
19
|
+
* // Basic throttling - update at most once per second
|
|
20
|
+
* const [throttledValue] = createThrottledValue(rawValue, { wait: 1000 });
|
|
21
|
+
*
|
|
22
|
+
* // With custom leading/trailing behavior
|
|
23
|
+
* const [throttledValue, throttler] = createThrottledValue(rawValue, {
|
|
24
|
+
* wait: 1000,
|
|
25
|
+
* leading: true, // Update immediately on first change
|
|
26
|
+
* trailing: false // Skip trailing edge updates
|
|
27
|
+
* });
|
|
28
|
+
*
|
|
29
|
+
* // Access throttler state via signals
|
|
30
|
+
* console.log('Executions:', throttler.executionCount());
|
|
31
|
+
* console.log('Is pending:', throttler.isPending());
|
|
32
|
+
* console.log('Last execution:', throttler.lastExecutionTime());
|
|
33
|
+
* console.log('Next execution:', throttler.nextExecutionTime());
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
export declare function createThrottledValue<TValue>(value: Accessor<TValue>, initialOptions: ThrottlerOptions<Setter<TValue>, [Accessor<TValue>]>): [Accessor<TValue>, SolidThrottler<Setter<TValue>, [Accessor<TValue>]>];
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const throttler = require("@tanstack/pacer/throttler");
|
|
4
|
+
const solidJs = require("solid-js");
|
|
5
|
+
const utils = require("@tanstack/pacer/utils");
|
|
6
|
+
function createThrottler(fn, initialOptions) {
|
|
7
|
+
const throttler$1 = new throttler.Throttler(fn, initialOptions);
|
|
8
|
+
const [executionCount, setExecutionCount] = solidJs.createSignal(
|
|
9
|
+
throttler$1.getExecutionCount()
|
|
10
|
+
);
|
|
11
|
+
const [isPending, setIsPending] = solidJs.createSignal(throttler$1.getIsPending());
|
|
12
|
+
const [lastExecutionTime, setLastExecutionTime] = solidJs.createSignal(
|
|
13
|
+
throttler$1.getLastExecutionTime()
|
|
14
|
+
);
|
|
15
|
+
const [nextExecutionTime, setNextExecutionTime] = solidJs.createSignal(
|
|
16
|
+
throttler$1.getNextExecutionTime()
|
|
17
|
+
);
|
|
18
|
+
function setOptions(newOptions) {
|
|
19
|
+
throttler$1.setOptions({
|
|
20
|
+
...newOptions,
|
|
21
|
+
onExecute: (throttler2) => {
|
|
22
|
+
setExecutionCount(throttler2.getExecutionCount());
|
|
23
|
+
setIsPending(throttler2.getIsPending());
|
|
24
|
+
setLastExecutionTime(throttler2.getLastExecutionTime());
|
|
25
|
+
setNextExecutionTime(throttler2.getNextExecutionTime());
|
|
26
|
+
const onExecute = newOptions.onExecute ?? initialOptions.onExecute;
|
|
27
|
+
onExecute == null ? void 0 : onExecute(throttler2);
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
setOptions(initialOptions);
|
|
32
|
+
return {
|
|
33
|
+
...utils.bindInstanceMethods(throttler$1),
|
|
34
|
+
executionCount,
|
|
35
|
+
isPending,
|
|
36
|
+
lastExecutionTime,
|
|
37
|
+
nextExecutionTime,
|
|
38
|
+
setOptions
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
exports.createThrottler = createThrottler;
|
|
42
|
+
//# sourceMappingURL=createThrottler.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createThrottler.cjs","sources":["../../../src/throttler/createThrottler.ts"],"sourcesContent":["import { Throttler } from '@tanstack/pacer/throttler'\nimport { createSignal } from 'solid-js'\nimport { bindInstanceMethods } from '@tanstack/pacer/utils'\nimport type { Accessor } from 'solid-js'\nimport type { AnyFunction } from '@tanstack/pacer/types'\nimport type { ThrottlerOptions } from '@tanstack/pacer/throttler'\n\n/**\n * An extension of the Throttler class that adds Solid signals to access the internal state of the throttler\n */\nexport interface SolidThrottler<\n TFn extends AnyFunction,\n TArgs extends Parameters<TFn>,\n> extends Omit<\n Throttler<TFn, TArgs>,\n | 'getExecutionCount'\n | 'getIsPending'\n | 'getLastExecutionTime'\n | 'getNextExecutionTime'\n > {\n executionCount: Accessor<number>\n isPending: Accessor<boolean>\n lastExecutionTime: Accessor<number>\n nextExecutionTime: Accessor<number>\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 TArgs extends Parameters<TFn>,\n>(\n fn: TFn,\n initialOptions: ThrottlerOptions<TFn, TArgs>,\n): SolidThrottler<TFn, TArgs> {\n const throttler = new Throttler<TFn, TArgs>(fn, initialOptions)\n\n const [executionCount, setExecutionCount] = createSignal(\n throttler.getExecutionCount(),\n )\n const [isPending, setIsPending] = createSignal(throttler.getIsPending())\n const [lastExecutionTime, setLastExecutionTime] = createSignal(\n throttler.getLastExecutionTime(),\n )\n const [nextExecutionTime, setNextExecutionTime] = createSignal(\n throttler.getNextExecutionTime(),\n )\n\n function setOptions(newOptions: Partial<ThrottlerOptions<TFn, TArgs>>) {\n throttler.setOptions({\n ...newOptions,\n onExecute: (throttler) => {\n setExecutionCount(throttler.getExecutionCount())\n setIsPending(throttler.getIsPending())\n setLastExecutionTime(throttler.getLastExecutionTime())\n setNextExecutionTime(throttler.getNextExecutionTime())\n\n const onExecute = newOptions.onExecute ?? initialOptions.onExecute\n onExecute?.(throttler)\n },\n })\n }\n\n setOptions(initialOptions)\n\n return {\n ...bindInstanceMethods(throttler),\n executionCount,\n isPending,\n lastExecutionTime,\n nextExecutionTime,\n setOptions,\n }\n}\n"],"names":["throttler","Throttler","createSignal","bindInstanceMethods"],"mappings":";;;;;AA4DgB,SAAA,gBAId,IACA,gBAC4B;AAC5B,QAAMA,cAAY,IAAIC,oBAAsB,IAAI,cAAc;AAExD,QAAA,CAAC,gBAAgB,iBAAiB,IAAIC,QAAA;AAAA,IAC1CF,YAAU,kBAAkB;AAAA,EAC9B;AACA,QAAM,CAAC,WAAW,YAAY,IAAIE,QAAAA,aAAaF,YAAU,cAAc;AACjE,QAAA,CAAC,mBAAmB,oBAAoB,IAAIE,QAAA;AAAA,IAChDF,YAAU,qBAAqB;AAAA,EACjC;AACM,QAAA,CAAC,mBAAmB,oBAAoB,IAAIE,QAAA;AAAA,IAChDF,YAAU,qBAAqB;AAAA,EACjC;AAEA,WAAS,WAAW,YAAmD;AACrEA,gBAAU,WAAW;AAAA,MACnB,GAAG;AAAA,MACH,WAAW,CAACA,eAAc;AACNA,0BAAAA,WAAU,mBAAmB;AAClCA,qBAAAA,WAAU,cAAc;AAChBA,6BAAAA,WAAU,sBAAsB;AAChCA,6BAAAA,WAAU,sBAAsB;AAE/C,cAAA,YAAY,WAAW,aAAa,eAAe;AACzD,+CAAYA;AAAAA,MAAS;AAAA,IACvB,CACD;AAAA,EAAA;AAGH,aAAW,cAAc;AAElB,SAAA;AAAA,IACL,GAAGG,MAAAA,oBAAoBH,WAAS;AAAA,IAChC;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACF;;"}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { Throttler, ThrottlerOptions } from '@tanstack/pacer/throttler';
|
|
2
|
+
import { Accessor } from 'solid-js';
|
|
3
|
+
import { AnyFunction } from '@tanstack/pacer/types';
|
|
4
|
+
/**
|
|
5
|
+
* An extension of the Throttler class that adds Solid signals to access the internal state of the throttler
|
|
6
|
+
*/
|
|
7
|
+
export interface SolidThrottler<TFn extends AnyFunction, TArgs extends Parameters<TFn>> extends Omit<Throttler<TFn, TArgs>, 'getExecutionCount' | 'getIsPending' | 'getLastExecutionTime' | 'getNextExecutionTime'> {
|
|
8
|
+
executionCount: Accessor<number>;
|
|
9
|
+
isPending: Accessor<boolean>;
|
|
10
|
+
lastExecutionTime: Accessor<number>;
|
|
11
|
+
nextExecutionTime: Accessor<number>;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* A low-level Solid hook that creates a `Throttler` instance that limits how often the provided function can execute.
|
|
15
|
+
*
|
|
16
|
+
* This hook is designed to be flexible and state-management agnostic - it simply returns a throttler instance that
|
|
17
|
+
* you can integrate with any state management solution (createSignal, Redux, Zustand, Jotai, etc). For a simpler and higher-level hook that
|
|
18
|
+
* integrates directly with Solid's createSignal, see createThrottledSignal.
|
|
19
|
+
*
|
|
20
|
+
* Throttling ensures a function executes at most once within a specified time window,
|
|
21
|
+
* regardless of how many times it is called. This is useful for rate-limiting
|
|
22
|
+
* expensive operations or UI updates.
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```tsx
|
|
26
|
+
* // Basic throttling with custom state
|
|
27
|
+
* const [value, setValue] = createSignal(0);
|
|
28
|
+
* const throttler = createThrottler(setValue, { wait: 1000 });
|
|
29
|
+
*
|
|
30
|
+
* // With any state manager
|
|
31
|
+
* const throttler = createThrottler(
|
|
32
|
+
* (value) => stateManager.setState(value),
|
|
33
|
+
* {
|
|
34
|
+
* wait: 2000,
|
|
35
|
+
* leading: true, // Execute immediately on first call
|
|
36
|
+
* trailing: false // Skip trailing edge updates
|
|
37
|
+
* }
|
|
38
|
+
* );
|
|
39
|
+
*
|
|
40
|
+
* // Access throttler state via signals
|
|
41
|
+
* console.log(throttler.executionCount()); // number of times executed
|
|
42
|
+
* console.log(throttler.isPending()); // whether throttled function is pending
|
|
43
|
+
* console.log(throttler.lastExecutionTime()); // timestamp of last execution
|
|
44
|
+
* console.log(throttler.nextExecutionTime()); // timestamp of next allowed execution
|
|
45
|
+
* ```
|
|
46
|
+
*/
|
|
47
|
+
export declare function createThrottler<TFn extends AnyFunction, TArgs extends Parameters<TFn>>(fn: TFn, initialOptions: ThrottlerOptions<TFn, TArgs>): SolidThrottler<TFn, TArgs>;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const throttler = require("@tanstack/pacer/throttler");
|
|
4
|
+
const createThrottledSignal = require("./createThrottledSignal.cjs");
|
|
5
|
+
const createThrottledValue = require("./createThrottledValue.cjs");
|
|
6
|
+
const createThrottler = require("./createThrottler.cjs");
|
|
7
|
+
exports.createThrottledSignal = createThrottledSignal.createThrottledSignal;
|
|
8
|
+
exports.createThrottledValue = createThrottledValue.createThrottledValue;
|
|
9
|
+
exports.createThrottler = createThrottler.createThrottler;
|
|
10
|
+
Object.keys(throttler).forEach((k) => {
|
|
11
|
+
if (k !== "default" && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
|
|
12
|
+
enumerable: true,
|
|
13
|
+
get: () => throttler[k]
|
|
14
|
+
});
|
|
15
|
+
});
|
|
16
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const types = require("@tanstack/pacer/types");
|
|
4
|
+
Object.keys(types).forEach((k) => {
|
|
5
|
+
if (k !== "default" && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: () => types[k]
|
|
8
|
+
});
|
|
9
|
+
});
|
|
10
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '@tanstack/pacer/types';
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const utils = require("@tanstack/pacer/utils");
|
|
4
|
+
Object.keys(utils).forEach((k) => {
|
|
5
|
+
if (k !== "default" && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: () => utils[k]
|
|
8
|
+
});
|
|
9
|
+
});
|
|
10
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '@tanstack/pacer/utils';
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { AsyncDebouncer, AsyncDebouncerOptions } from '@tanstack/pacer/async-debouncer';
|
|
2
|
+
import { AnyAsyncFunction } from '@tanstack/pacer/types';
|
|
3
|
+
import { Accessor } from 'solid-js';
|
|
4
|
+
export interface SolidAsyncDebouncer<TFn extends AnyAsyncFunction, TArgs extends Parameters<TFn>> extends Omit<AsyncDebouncer<TFn, TArgs>, 'getExecutionCount' | 'getIsPending'> {
|
|
5
|
+
executionCount: Accessor<number>;
|
|
6
|
+
isPending: Accessor<boolean>;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* A low-level Solid hook that creates an `AsyncDebouncer` instance to delay execution of an async function.
|
|
10
|
+
*
|
|
11
|
+
* This hook is designed to be flexible and state-management agnostic - it simply returns a debouncer instance that
|
|
12
|
+
* you can integrate with any state management solution (createSignal, etc).
|
|
13
|
+
*
|
|
14
|
+
* Async debouncing ensures that an async function only executes after a specified delay has passed since its last invocation.
|
|
15
|
+
* This is useful for handling fast-changing inputs like search fields, form validation, or any scenario where you want to
|
|
16
|
+
* wait for user input to settle before making expensive async calls.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```tsx
|
|
20
|
+
* // Basic API call debouncing
|
|
21
|
+
* const { maybeExecute } = createAsyncDebouncer(
|
|
22
|
+
* async (query: string) => {
|
|
23
|
+
* const results = await api.search(query);
|
|
24
|
+
* return results;
|
|
25
|
+
* },
|
|
26
|
+
* { wait: 500 }
|
|
27
|
+
* );
|
|
28
|
+
*
|
|
29
|
+
* // With state management
|
|
30
|
+
* const [results, setResults] = createSignal([]);
|
|
31
|
+
* const { maybeExecute } = createAsyncDebouncer(
|
|
32
|
+
* async (searchTerm) => {
|
|
33
|
+
* const data = await searchAPI(searchTerm);
|
|
34
|
+
* setResults(data);
|
|
35
|
+
* },
|
|
36
|
+
* {
|
|
37
|
+
* wait: 300,
|
|
38
|
+
* }
|
|
39
|
+
* );
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
export declare function createAsyncDebouncer<TFn extends AnyAsyncFunction, TArgs extends Parameters<TFn>>(fn: TFn, initialOptions: AsyncDebouncerOptions<TFn, TArgs>): SolidAsyncDebouncer<TFn, TArgs>;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { AsyncDebouncer } from "@tanstack/pacer/async-debouncer";
|
|
2
|
+
import { createSignal } from "solid-js";
|
|
3
|
+
import { bindInstanceMethods } from "@tanstack/pacer/utils";
|
|
4
|
+
function createAsyncDebouncer(fn, initialOptions) {
|
|
5
|
+
const asyncDebouncer = new AsyncDebouncer(fn, initialOptions);
|
|
6
|
+
const [executionCount, setExecutionCount] = createSignal(
|
|
7
|
+
asyncDebouncer.getExecutionCount()
|
|
8
|
+
);
|
|
9
|
+
const [isPending, setIsPending] = createSignal(asyncDebouncer.getIsPending());
|
|
10
|
+
function setOptions(newOptions) {
|
|
11
|
+
asyncDebouncer.setOptions({
|
|
12
|
+
...newOptions,
|
|
13
|
+
onExecute: (asyncDebouncer2) => {
|
|
14
|
+
setExecutionCount(asyncDebouncer2.getExecutionCount());
|
|
15
|
+
setIsPending(asyncDebouncer2.getIsPending());
|
|
16
|
+
const onExecute = newOptions.onExecute ?? initialOptions.onExecute;
|
|
17
|
+
onExecute == null ? void 0 : onExecute(asyncDebouncer2);
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
setOptions(initialOptions);
|
|
22
|
+
return {
|
|
23
|
+
...bindInstanceMethods(asyncDebouncer),
|
|
24
|
+
executionCount,
|
|
25
|
+
isPending,
|
|
26
|
+
setOptions
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
export {
|
|
30
|
+
createAsyncDebouncer
|
|
31
|
+
};
|
|
32
|
+
//# sourceMappingURL=createAsyncDebouncer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createAsyncDebouncer.js","sources":["../../../src/async-debouncer/createAsyncDebouncer.ts"],"sourcesContent":["import { AsyncDebouncer } from '@tanstack/pacer/async-debouncer'\nimport { createSignal } from 'solid-js'\nimport { bindInstanceMethods } from '@tanstack/pacer/utils'\nimport type { AsyncDebouncerOptions } from '@tanstack/pacer/async-debouncer'\nimport type { AnyAsyncFunction } from '@tanstack/pacer/types'\nimport type { Accessor } from 'solid-js'\n\nexport interface SolidAsyncDebouncer<\n TFn extends AnyAsyncFunction,\n TArgs extends Parameters<TFn>,\n> extends Omit<\n AsyncDebouncer<TFn, TArgs>,\n 'getExecutionCount' | 'getIsPending'\n > {\n executionCount: Accessor<number>\n isPending: Accessor<boolean>\n}\n\n/**\n * A low-level Solid hook that creates an `AsyncDebouncer` instance to delay execution of an async function.\n *\n * This hook is designed to be flexible and state-management agnostic - it simply returns a debouncer instance that\n * you can integrate with any state management solution (createSignal, etc).\n *\n * Async debouncing ensures that an async function only executes after a specified delay has passed since its last invocation.\n * This is useful for handling fast-changing inputs like search fields, form validation, or any scenario where you want to\n * wait for user input to settle before making expensive async calls.\n *\n * @example\n * ```tsx\n * // Basic API call debouncing\n * const { maybeExecute } = createAsyncDebouncer(\n * async (query: string) => {\n * const results = await api.search(query);\n * return results;\n * },\n * { wait: 500 }\n * );\n *\n * // With state management\n * const [results, setResults] = createSignal([]);\n * const { maybeExecute } = createAsyncDebouncer(\n * async (searchTerm) => {\n * const data = await searchAPI(searchTerm);\n * setResults(data);\n * },\n * {\n * wait: 300,\n * }\n * );\n * ```\n */\n\nexport function createAsyncDebouncer<\n TFn extends AnyAsyncFunction,\n TArgs extends Parameters<TFn>,\n>(\n fn: TFn,\n initialOptions: AsyncDebouncerOptions<TFn, TArgs>,\n): SolidAsyncDebouncer<TFn, TArgs> {\n const asyncDebouncer = new AsyncDebouncer<TFn, TArgs>(fn, initialOptions)\n\n const [executionCount, setExecutionCount] = createSignal(\n asyncDebouncer.getExecutionCount(),\n )\n const [isPending, setIsPending] = createSignal(asyncDebouncer.getIsPending())\n\n function setOptions(newOptions: Partial<AsyncDebouncerOptions<TFn, TArgs>>) {\n asyncDebouncer.setOptions({\n ...newOptions,\n onExecute: (asyncDebouncer) => {\n setExecutionCount(asyncDebouncer.getExecutionCount())\n setIsPending(asyncDebouncer.getIsPending())\n\n const onExecute = newOptions.onExecute ?? initialOptions.onExecute\n onExecute?.(asyncDebouncer)\n },\n })\n }\n\n setOptions(initialOptions)\n\n return {\n ...bindInstanceMethods(asyncDebouncer),\n executionCount,\n isPending,\n setOptions,\n }\n}\n"],"names":["asyncDebouncer"],"mappings":";;;AAqDgB,SAAA,qBAId,IACA,gBACiC;AACjC,QAAM,iBAAiB,IAAI,eAA2B,IAAI,cAAc;AAElE,QAAA,CAAC,gBAAgB,iBAAiB,IAAI;AAAA,IAC1C,eAAe,kBAAkB;AAAA,EACnC;AACA,QAAM,CAAC,WAAW,YAAY,IAAI,aAAa,eAAe,cAAc;AAE5E,WAAS,WAAW,YAAwD;AAC1E,mBAAe,WAAW;AAAA,MACxB,GAAG;AAAA,MACH,WAAW,CAACA,oBAAmB;AACXA,0BAAAA,gBAAe,mBAAmB;AACvCA,qBAAAA,gBAAe,cAAc;AAEpC,cAAA,YAAY,WAAW,aAAa,eAAe;AACzD,+CAAYA;AAAAA,MAAc;AAAA,IAC5B,CACD;AAAA,EAAA;AAGH,aAAW,cAAc;AAElB,SAAA;AAAA,IACL,GAAG,oBAAoB,cAAc;AAAA,IACrC;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACF;"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;"}
|