@tanstack/solid-pacer 0.2.0 → 0.4.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-debouncer/createAsyncDebouncer.cjs +22 -7
- package/dist/cjs/async-debouncer/createAsyncDebouncer.cjs.map +1 -1
- package/dist/cjs/async-debouncer/createAsyncDebouncer.d.cts +6 -3
- package/dist/cjs/async-queuer/createAsyncQueuer.cjs.map +1 -1
- package/dist/cjs/async-queuer/createAsyncQueuer.d.cts +5 -1
- package/dist/cjs/async-rate-limiter/createAsyncRateLimiter.cjs +20 -9
- package/dist/cjs/async-rate-limiter/createAsyncRateLimiter.cjs.map +1 -1
- package/dist/cjs/async-rate-limiter/createAsyncRateLimiter.d.cts +19 -6
- package/dist/cjs/async-throttler/createAsyncThrottler.cjs +32 -10
- package/dist/cjs/async-throttler/createAsyncThrottler.cjs.map +1 -1
- package/dist/cjs/async-throttler/createAsyncThrottler.d.cts +7 -3
- package/dist/cjs/debouncer/createDebouncedSignal.cjs.map +1 -1
- package/dist/cjs/debouncer/createDebouncedSignal.d.cts +1 -5
- package/dist/cjs/debouncer/createDebouncedValue.cjs +0 -3
- package/dist/cjs/debouncer/createDebouncedValue.cjs.map +1 -1
- package/dist/cjs/debouncer/createDebouncedValue.d.cts +5 -11
- package/dist/cjs/debouncer/createDebouncer.cjs +7 -2
- package/dist/cjs/debouncer/createDebouncer.cjs.map +1 -1
- package/dist/cjs/debouncer/createDebouncer.d.cts +2 -2
- package/dist/cjs/queuer/createQueuer.cjs +2 -2
- package/dist/cjs/queuer/createQueuer.cjs.map +1 -1
- package/dist/cjs/queuer/createQueuer.d.cts +4 -0
- package/dist/cjs/rate-limiter/createRateLimitedSignal.cjs.map +1 -1
- package/dist/cjs/rate-limiter/createRateLimitedSignal.d.cts +12 -8
- package/dist/cjs/rate-limiter/createRateLimitedValue.cjs.map +1 -1
- package/dist/cjs/rate-limiter/createRateLimitedValue.d.cts +18 -22
- package/dist/cjs/rate-limiter/createRateLimiter.cjs +4 -2
- package/dist/cjs/rate-limiter/createRateLimiter.cjs.map +1 -1
- package/dist/cjs/rate-limiter/createRateLimiter.d.cts +10 -3
- package/dist/cjs/throttler/createThrottledSignal.cjs.map +1 -1
- package/dist/cjs/throttler/createThrottledSignal.d.cts +1 -5
- package/dist/cjs/throttler/createThrottledValue.cjs +0 -3
- package/dist/cjs/throttler/createThrottledValue.cjs.map +1 -1
- package/dist/cjs/throttler/createThrottledValue.d.cts +10 -14
- package/dist/cjs/throttler/createThrottler.cjs +7 -2
- package/dist/cjs/throttler/createThrottler.cjs.map +1 -1
- package/dist/cjs/throttler/createThrottler.d.cts +2 -2
- package/dist/esm/async-debouncer/createAsyncDebouncer.d.ts +6 -3
- package/dist/esm/async-debouncer/createAsyncDebouncer.js +22 -7
- package/dist/esm/async-debouncer/createAsyncDebouncer.js.map +1 -1
- package/dist/esm/async-queuer/createAsyncQueuer.d.ts +5 -1
- package/dist/esm/async-queuer/createAsyncQueuer.js.map +1 -1
- package/dist/esm/async-rate-limiter/createAsyncRateLimiter.d.ts +19 -6
- package/dist/esm/async-rate-limiter/createAsyncRateLimiter.js +20 -9
- package/dist/esm/async-rate-limiter/createAsyncRateLimiter.js.map +1 -1
- package/dist/esm/async-throttler/createAsyncThrottler.d.ts +7 -3
- package/dist/esm/async-throttler/createAsyncThrottler.js +32 -10
- package/dist/esm/async-throttler/createAsyncThrottler.js.map +1 -1
- package/dist/esm/debouncer/createDebouncedSignal.d.ts +1 -5
- package/dist/esm/debouncer/createDebouncedSignal.js.map +1 -1
- package/dist/esm/debouncer/createDebouncedValue.d.ts +5 -11
- package/dist/esm/debouncer/createDebouncedValue.js +1 -4
- package/dist/esm/debouncer/createDebouncedValue.js.map +1 -1
- package/dist/esm/debouncer/createDebouncer.d.ts +2 -2
- package/dist/esm/debouncer/createDebouncer.js +8 -3
- package/dist/esm/debouncer/createDebouncer.js.map +1 -1
- package/dist/esm/queuer/createQueuer.d.ts +4 -0
- package/dist/esm/queuer/createQueuer.js +2 -2
- package/dist/esm/queuer/createQueuer.js.map +1 -1
- package/dist/esm/rate-limiter/createRateLimitedSignal.d.ts +12 -8
- package/dist/esm/rate-limiter/createRateLimitedSignal.js.map +1 -1
- package/dist/esm/rate-limiter/createRateLimitedValue.d.ts +18 -22
- package/dist/esm/rate-limiter/createRateLimitedValue.js.map +1 -1
- package/dist/esm/rate-limiter/createRateLimiter.d.ts +10 -3
- package/dist/esm/rate-limiter/createRateLimiter.js +4 -2
- package/dist/esm/rate-limiter/createRateLimiter.js.map +1 -1
- package/dist/esm/throttler/createThrottledSignal.d.ts +1 -5
- package/dist/esm/throttler/createThrottledSignal.js.map +1 -1
- package/dist/esm/throttler/createThrottledValue.d.ts +10 -14
- package/dist/esm/throttler/createThrottledValue.js +1 -4
- package/dist/esm/throttler/createThrottledValue.js.map +1 -1
- package/dist/esm/throttler/createThrottler.d.ts +2 -2
- package/dist/esm/throttler/createThrottler.js +8 -3
- package/dist/esm/throttler/createThrottler.js.map +1 -1
- package/package.json +3 -3
- package/src/async-debouncer/createAsyncDebouncer.ts +40 -24
- package/src/async-queuer/createAsyncQueuer.ts +6 -2
- package/src/async-rate-limiter/createAsyncRateLimiter.ts +48 -29
- package/src/async-throttler/createAsyncThrottler.ts +49 -24
- package/src/debouncer/createDebouncedSignal.ts +2 -6
- package/src/debouncer/createDebouncedValue.ts +7 -16
- package/src/debouncer/createDebouncer.ts +16 -15
- package/src/queuer/createQueuer.ts +7 -3
- package/src/rate-limiter/createRateLimitedSignal.ts +13 -9
- package/src/rate-limiter/createRateLimitedValue.ts +19 -23
- package/src/rate-limiter/createRateLimiter.ts +22 -16
- package/src/throttler/createThrottledSignal.ts +2 -6
- package/src/throttler/createThrottledValue.ts +12 -19
- package/src/throttler/createThrottler.ts +17 -16
|
@@ -5,26 +5,41 @@ const solidJs = require("solid-js");
|
|
|
5
5
|
const utils = require("@tanstack/pacer/utils");
|
|
6
6
|
function createAsyncDebouncer(fn, initialOptions) {
|
|
7
7
|
const asyncDebouncer$1 = new asyncDebouncer.AsyncDebouncer(fn, initialOptions);
|
|
8
|
-
const [
|
|
9
|
-
asyncDebouncer$1.
|
|
8
|
+
const [errorCount, setErrorCount] = solidJs.createSignal(
|
|
9
|
+
asyncDebouncer$1.getErrorCount()
|
|
10
|
+
);
|
|
11
|
+
const [settleCount, setSettleCount] = solidJs.createSignal(
|
|
12
|
+
asyncDebouncer$1.getSettleCount()
|
|
13
|
+
);
|
|
14
|
+
const [successCount, setSuccessCount] = solidJs.createSignal(
|
|
15
|
+
asyncDebouncer$1.getSuccessCount()
|
|
10
16
|
);
|
|
11
17
|
const [isPending, setIsPending] = solidJs.createSignal(asyncDebouncer$1.getIsPending());
|
|
18
|
+
const [lastResult, setLastResult] = solidJs.createSignal(
|
|
19
|
+
asyncDebouncer$1.getLastResult()
|
|
20
|
+
);
|
|
12
21
|
function setOptions(newOptions) {
|
|
13
22
|
asyncDebouncer$1.setOptions({
|
|
14
23
|
...newOptions,
|
|
15
|
-
|
|
16
|
-
|
|
24
|
+
onSettled: (asyncDebouncer2) => {
|
|
25
|
+
setSuccessCount(asyncDebouncer2.getSuccessCount());
|
|
26
|
+
setErrorCount(asyncDebouncer2.getErrorCount());
|
|
27
|
+
setSettleCount(asyncDebouncer2.getSettleCount());
|
|
17
28
|
setIsPending(asyncDebouncer2.getIsPending());
|
|
18
|
-
|
|
19
|
-
|
|
29
|
+
setLastResult(asyncDebouncer2.getLastResult());
|
|
30
|
+
const onSettled = newOptions.onSettled ?? initialOptions.onSettled;
|
|
31
|
+
onSettled == null ? void 0 : onSettled(asyncDebouncer2);
|
|
20
32
|
}
|
|
21
33
|
});
|
|
22
34
|
}
|
|
23
35
|
setOptions(initialOptions);
|
|
24
36
|
return {
|
|
25
37
|
...utils.bindInstanceMethods(asyncDebouncer$1),
|
|
26
|
-
|
|
38
|
+
errorCount,
|
|
27
39
|
isPending,
|
|
40
|
+
lastResult,
|
|
41
|
+
settleCount,
|
|
42
|
+
successCount,
|
|
28
43
|
setOptions
|
|
29
44
|
};
|
|
30
45
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createAsyncDebouncer.cjs","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
|
|
1
|
+
{"version":3,"file":"createAsyncDebouncer.cjs","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<TFn extends AnyAsyncFunction>\n extends Omit<\n AsyncDebouncer<TFn>,\n | 'getErrorCount'\n | 'getIsPending'\n | 'getLastResult'\n | 'getSettleCount'\n | 'getSuccessCount'\n > {\n errorCount: Accessor<number>\n isPending: Accessor<boolean>\n lastResult: Accessor<ReturnType<TFn> | undefined>\n settleCount: Accessor<number>\n successCount: Accessor<number>\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<TFn extends AnyAsyncFunction>(\n fn: TFn,\n initialOptions: AsyncDebouncerOptions<TFn>,\n): SolidAsyncDebouncer<TFn> {\n const asyncDebouncer = new AsyncDebouncer<TFn>(fn, initialOptions)\n\n const [errorCount, setErrorCount] = createSignal(\n asyncDebouncer.getErrorCount(),\n )\n const [settleCount, setSettleCount] = createSignal(\n asyncDebouncer.getSettleCount(),\n )\n const [successCount, setSuccessCount] = createSignal(\n asyncDebouncer.getSuccessCount(),\n )\n const [isPending, setIsPending] = createSignal(asyncDebouncer.getIsPending())\n const [lastResult, setLastResult] = createSignal(\n asyncDebouncer.getLastResult(),\n )\n\n function setOptions(newOptions: Partial<AsyncDebouncerOptions<TFn>>) {\n asyncDebouncer.setOptions({\n ...newOptions,\n onSettled: (asyncDebouncer) => {\n setSuccessCount(asyncDebouncer.getSuccessCount())\n setErrorCount(asyncDebouncer.getErrorCount())\n setSettleCount(asyncDebouncer.getSettleCount())\n setIsPending(asyncDebouncer.getIsPending())\n setLastResult(asyncDebouncer.getLastResult())\n const onSettled = newOptions.onSettled ?? initialOptions.onSettled\n onSettled?.(asyncDebouncer)\n },\n })\n }\n\n setOptions(initialOptions)\n\n return {\n ...bindInstanceMethods(asyncDebouncer),\n errorCount,\n isPending,\n lastResult,\n settleCount,\n successCount,\n setOptions,\n } as SolidAsyncDebouncer<TFn>\n}\n"],"names":["asyncDebouncer","AsyncDebouncer","createSignal","bindInstanceMethods"],"mappings":";;;;;AA0DgB,SAAA,qBACd,IACA,gBAC0B;AAC1B,QAAMA,mBAAiB,IAAIC,8BAAoB,IAAI,cAAc;AAE3D,QAAA,CAAC,YAAY,aAAa,IAAIC,QAAA;AAAA,IAClCF,iBAAe,cAAc;AAAA,EAC/B;AACM,QAAA,CAAC,aAAa,cAAc,IAAIE,QAAA;AAAA,IACpCF,iBAAe,eAAe;AAAA,EAChC;AACM,QAAA,CAAC,cAAc,eAAe,IAAIE,QAAA;AAAA,IACtCF,iBAAe,gBAAgB;AAAA,EACjC;AACA,QAAM,CAAC,WAAW,YAAY,IAAIE,QAAAA,aAAaF,iBAAe,cAAc;AACtE,QAAA,CAAC,YAAY,aAAa,IAAIE,QAAA;AAAA,IAClCF,iBAAe,cAAc;AAAA,EAC/B;AAEA,WAAS,WAAW,YAAiD;AACnEA,qBAAe,WAAW;AAAA,MACxB,GAAG;AAAA,MACH,WAAW,CAACA,oBAAmB;AACbA,wBAAAA,gBAAe,iBAAiB;AAClCA,sBAAAA,gBAAe,eAAe;AAC7BA,uBAAAA,gBAAe,gBAAgB;AACjCA,qBAAAA,gBAAe,cAAc;AAC5BA,sBAAAA,gBAAe,eAAe;AACtC,cAAA,YAAY,WAAW,aAAa,eAAe;AACzD,+CAAYA;AAAAA,MAAc;AAAA,IAC5B,CACD;AAAA,EAAA;AAGH,aAAW,cAAc;AAElB,SAAA;AAAA,IACL,GAAGG,MAAAA,oBAAoBH,gBAAc;AAAA,IACrC;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACF;;"}
|
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
import { AsyncDebouncer, AsyncDebouncerOptions } from '@tanstack/pacer/async-debouncer';
|
|
2
2
|
import { AnyAsyncFunction } from '@tanstack/pacer/types';
|
|
3
3
|
import { Accessor } from 'solid-js';
|
|
4
|
-
export interface SolidAsyncDebouncer<TFn extends AnyAsyncFunction
|
|
5
|
-
|
|
4
|
+
export interface SolidAsyncDebouncer<TFn extends AnyAsyncFunction> extends Omit<AsyncDebouncer<TFn>, 'getErrorCount' | 'getIsPending' | 'getLastResult' | 'getSettleCount' | 'getSuccessCount'> {
|
|
5
|
+
errorCount: Accessor<number>;
|
|
6
6
|
isPending: Accessor<boolean>;
|
|
7
|
+
lastResult: Accessor<ReturnType<TFn> | undefined>;
|
|
8
|
+
settleCount: Accessor<number>;
|
|
9
|
+
successCount: Accessor<number>;
|
|
7
10
|
}
|
|
8
11
|
/**
|
|
9
12
|
* A low-level Solid hook that creates an `AsyncDebouncer` instance to delay execution of an async function.
|
|
@@ -39,4 +42,4 @@ export interface SolidAsyncDebouncer<TFn extends AnyAsyncFunction, TArgs extends
|
|
|
39
42
|
* );
|
|
40
43
|
* ```
|
|
41
44
|
*/
|
|
42
|
-
export declare function createAsyncDebouncer<TFn extends AnyAsyncFunction
|
|
45
|
+
export declare function createAsyncDebouncer<TFn extends AnyAsyncFunction>(fn: TFn, initialOptions: AsyncDebouncerOptions<TFn>): SolidAsyncDebouncer<TFn>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createAsyncQueuer.cjs","sources":["../../../src/async-queuer/createAsyncQueuer.ts"],"sourcesContent":["import { AsyncQueuer } from '@tanstack/pacer/async-queuer'\nimport { createSignal } from 'solid-js'\nimport { bindInstanceMethods } from '@tanstack/pacer/utils'\nimport type { Accessor } from 'solid-js'\nimport type { AsyncQueuerOptions } from '@tanstack/pacer/async-queuer'\n\nexport interface SolidAsyncQueuer<TValue>\n extends Omit<\n AsyncQueuer<TValue>,\n | 'getActiveItems'\n | 'getAllItems'\n | 'getExecutionCount'\n | 'getIsEmpty'\n | 'getIsFull'\n | 'getIsIdle'\n | 'getIsRunning'\n | 'getPeek'\n | 'getPendingItems'\n | 'getSize'\n > {\n /**\n * Signal version of `getActiveItems`\n */\n activeItems: Accessor<Array<() => Promise<TValue>>>\n /**\n * Signal version of `getAllItems`\n */\n allItems: Accessor<Array<() => Promise<TValue>>>\n /**\n * Signal version of `getExecutionCount`\n */\n executionCount: Accessor<number>\n /**\n * Signal version of `getIsEmpty`\n */\n isEmpty: Accessor<boolean>\n /**\n * Signal version of `getIsFull`\n */\n isFull: Accessor<boolean>\n /**\n * Signal version of `getIsIdle`\n */\n isIdle: Accessor<boolean>\n /**\n * Signal version of `getIsRunning`\n */\n isRunning: Accessor<boolean>\n /**\n * Signal version of `getPeek`\n */\n peek: Accessor<TValue | undefined>\n /**\n * Signal version of `getPendingItems`\n */\n pendingItems: Accessor<Array<() => Promise<TValue>>>\n /**\n * Signal version of `getSize`\n */\n size: Accessor<number>\n}\n\n/**\n * A lower-level React hook that creates an `AsyncQueuer` instance for managing an async queue of items.\n *\n * This hook provides a flexible, state-management agnostic way to handle queued async operations.\n * It returns a queuer instance with methods to add items, control queue execution, and monitor queue state.\n *\n * The queue can be configured with:\n * - Maximum concurrent operations\n * - Maximum queue size\n * - Processing function for queue items\n * - Various lifecycle callbacks\n *\n * The hook returns an object containing methods to:\n * - Add/remove items from the queue\n * - Start/stop queue processing\n * - Get queue status and items\n * - Register event handlers\n * - Control execution throttling\n *\n * @example\n * ```tsx\n * // Basic async queuer for API requests\n * const asyncQueuer = createAsyncQueuer({\n * initialItems: [],\n * concurrency: 2,\n * maxSize: 100,\n * started: false,\n * });\n *\n * // Add items to queue\n * asyncQueuer.addItem(newItem);\n *\n * // Start processing\n * asyncQueuer.start();\n *\n * // Handle results\n * asyncQueuer.onSuccess((result) => {\n * console.log('Item processed:', result);\n * });\n *\n * asyncQueuer.onError((error) => {\n * console.error('Processing failed:', error);\n * });\n * ```\n */\nexport function createAsyncQueuer<TValue>(\n initialOptions: AsyncQueuerOptions<TValue> = {},\n): SolidAsyncQueuer<TValue> {\n const asyncQueuer = new AsyncQueuer<TValue>(initialOptions)\n\n const [executionCount, setExecutionCount] = createSignal(\n asyncQueuer.getExecutionCount(),\n )\n const [rejectionCount, setRejectionCount] = createSignal(\n asyncQueuer.getRejectionCount(),\n )\n const [isEmpty, setIsEmpty] = createSignal(asyncQueuer.getIsEmpty())\n const [isFull, setIsFull] = createSignal(asyncQueuer.getIsFull())\n const [isIdle, setIsIdle] = createSignal(asyncQueuer.getIsIdle())\n const [isRunning, setIsRunning] = createSignal(asyncQueuer.getIsRunning())\n const [allItems, setAllItems] = createSignal<Array<() => Promise<TValue>>>(\n asyncQueuer.getAllItems(),\n )\n const [activeItems, setActiveItems] = createSignal<\n Array<() => Promise<TValue>>\n >(asyncQueuer.getActiveItems())\n const [pendingItems, setPendingItems] = createSignal<\n Array<() => Promise<TValue>>\n >(asyncQueuer.getPendingItems())\n const [peek, setPeek] = createSignal<(() => Promise<TValue>) | undefined>(\n asyncQueuer.getPeek(),\n )\n const [size, setSize] = createSignal(asyncQueuer.getSize())\n\n asyncQueuer.setOptions({\n onItemsChange: (queuer) => {\n setExecutionCount(queuer.getExecutionCount())\n setIsEmpty(queuer.getIsEmpty())\n setIsFull(queuer.getIsFull())\n setIsIdle(queuer.getIsIdle())\n setAllItems(queuer.getAllItems())\n setActiveItems(queuer.getActiveItems())\n setPendingItems(queuer.getPendingItems())\n setPeek(() => queuer.getPeek())\n setSize(queuer.getSize())\n initialOptions.onItemsChange?.(queuer)\n },\n onIsRunningChange: (queuer) => {\n setIsRunning(queuer.getIsRunning())\n setIsIdle(queuer.getIsIdle())\n initialOptions.onIsRunningChange?.(queuer)\n },\n onReject: (item, queuer) => {\n setRejectionCount(queuer.getRejectionCount())\n initialOptions.onReject?.(item, queuer)\n },\n })\n\n return {\n ...bindInstanceMethods(asyncQueuer),\n activeItems,\n executionCount,\n isEmpty,\n isFull,\n isIdle,\n isRunning,\n allItems,\n peek,\n pendingItems,\n rejectionCount,\n size,\n }
|
|
1
|
+
{"version":3,"file":"createAsyncQueuer.cjs","sources":["../../../src/async-queuer/createAsyncQueuer.ts"],"sourcesContent":["import { AsyncQueuer } from '@tanstack/pacer/async-queuer'\nimport { createSignal } from 'solid-js'\nimport { bindInstanceMethods } from '@tanstack/pacer/utils'\nimport type { Accessor } from 'solid-js'\nimport type { AsyncQueuerOptions } from '@tanstack/pacer/async-queuer'\n\nexport interface SolidAsyncQueuer<TValue>\n extends Omit<\n AsyncQueuer<TValue>,\n | 'getActiveItems'\n | 'getAllItems'\n | 'getExecutionCount'\n | 'getIsEmpty'\n | 'getIsFull'\n | 'getIsIdle'\n | 'getIsRunning'\n | 'getPeek'\n | 'getPendingItems'\n | 'getSize'\n > {\n /**\n * Signal version of `getActiveItems`\n */\n activeItems: Accessor<Array<() => Promise<TValue>>>\n /**\n * Signal version of `getAllItems`\n */\n allItems: Accessor<Array<() => Promise<TValue>>>\n /**\n * Signal version of `getExecutionCount`\n */\n executionCount: Accessor<number>\n /**\n * Signal version of `getIsEmpty`\n */\n isEmpty: Accessor<boolean>\n /**\n * Signal version of `getIsFull`\n */\n isFull: Accessor<boolean>\n /**\n * Signal version of `getIsIdle`\n */\n isIdle: Accessor<boolean>\n /**\n * Signal version of `getIsRunning`\n */\n isRunning: Accessor<boolean>\n /**\n * Signal version of `getPeek`\n */\n peek: Accessor<(() => Promise<TValue>) | undefined>\n /**\n * Signal version of `getPendingItems`\n */\n pendingItems: Accessor<Array<() => Promise<TValue>>>\n /**\n * Signal version of `getRejectionCount`\n */\n rejectionCount: Accessor<number>\n /**\n * Signal version of `getSize`\n */\n size: Accessor<number>\n}\n\n/**\n * A lower-level React hook that creates an `AsyncQueuer` instance for managing an async queue of items.\n *\n * This hook provides a flexible, state-management agnostic way to handle queued async operations.\n * It returns a queuer instance with methods to add items, control queue execution, and monitor queue state.\n *\n * The queue can be configured with:\n * - Maximum concurrent operations\n * - Maximum queue size\n * - Processing function for queue items\n * - Various lifecycle callbacks\n *\n * The hook returns an object containing methods to:\n * - Add/remove items from the queue\n * - Start/stop queue processing\n * - Get queue status and items\n * - Register event handlers\n * - Control execution throttling\n *\n * @example\n * ```tsx\n * // Basic async queuer for API requests\n * const asyncQueuer = createAsyncQueuer({\n * initialItems: [],\n * concurrency: 2,\n * maxSize: 100,\n * started: false,\n * });\n *\n * // Add items to queue\n * asyncQueuer.addItem(newItem);\n *\n * // Start processing\n * asyncQueuer.start();\n *\n * // Handle results\n * asyncQueuer.onSuccess((result) => {\n * console.log('Item processed:', result);\n * });\n *\n * asyncQueuer.onError((error) => {\n * console.error('Processing failed:', error);\n * });\n * ```\n */\nexport function createAsyncQueuer<TValue>(\n initialOptions: AsyncQueuerOptions<TValue> = {},\n): SolidAsyncQueuer<TValue> {\n const asyncQueuer = new AsyncQueuer<TValue>(initialOptions)\n\n const [executionCount, setExecutionCount] = createSignal(\n asyncQueuer.getExecutionCount(),\n )\n const [rejectionCount, setRejectionCount] = createSignal(\n asyncQueuer.getRejectionCount(),\n )\n const [isEmpty, setIsEmpty] = createSignal(asyncQueuer.getIsEmpty())\n const [isFull, setIsFull] = createSignal(asyncQueuer.getIsFull())\n const [isIdle, setIsIdle] = createSignal(asyncQueuer.getIsIdle())\n const [isRunning, setIsRunning] = createSignal(asyncQueuer.getIsRunning())\n const [allItems, setAllItems] = createSignal<Array<() => Promise<TValue>>>(\n asyncQueuer.getAllItems(),\n )\n const [activeItems, setActiveItems] = createSignal<\n Array<() => Promise<TValue>>\n >(asyncQueuer.getActiveItems())\n const [pendingItems, setPendingItems] = createSignal<\n Array<() => Promise<TValue>>\n >(asyncQueuer.getPendingItems())\n const [peek, setPeek] = createSignal<(() => Promise<TValue>) | undefined>(\n asyncQueuer.getPeek(),\n )\n const [size, setSize] = createSignal(asyncQueuer.getSize())\n\n asyncQueuer.setOptions({\n onItemsChange: (queuer) => {\n setExecutionCount(queuer.getExecutionCount())\n setIsEmpty(queuer.getIsEmpty())\n setIsFull(queuer.getIsFull())\n setIsIdle(queuer.getIsIdle())\n setAllItems(queuer.getAllItems())\n setActiveItems(queuer.getActiveItems())\n setPendingItems(queuer.getPendingItems())\n setPeek(() => queuer.getPeek())\n setSize(queuer.getSize())\n initialOptions.onItemsChange?.(queuer)\n },\n onIsRunningChange: (queuer) => {\n setIsRunning(queuer.getIsRunning())\n setIsIdle(queuer.getIsIdle())\n initialOptions.onIsRunningChange?.(queuer)\n },\n onReject: (item, queuer) => {\n setRejectionCount(queuer.getRejectionCount())\n initialOptions.onReject?.(item, queuer)\n },\n })\n\n return {\n ...bindInstanceMethods(asyncQueuer),\n activeItems,\n executionCount,\n isEmpty,\n isFull,\n isIdle,\n isRunning,\n allItems,\n peek,\n pendingItems,\n rejectionCount,\n size,\n } as SolidAsyncQueuer<TValue>\n}\n"],"names":["asyncQueuer","AsyncQueuer","createSignal","bindInstanceMethods"],"mappings":";;;;;AA+GgB,SAAA,kBACd,iBAA6C,IACnB;AACpB,QAAAA,gBAAc,IAAIC,YAAA,YAAoB,cAAc;AAEpD,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;AACA,QAAM,CAAC,SAAS,UAAU,IAAIE,QAAAA,aAAaF,cAAY,YAAY;AACnE,QAAM,CAAC,QAAQ,SAAS,IAAIE,QAAAA,aAAaF,cAAY,WAAW;AAChE,QAAM,CAAC,QAAQ,SAAS,IAAIE,QAAAA,aAAaF,cAAY,WAAW;AAChE,QAAM,CAAC,WAAW,YAAY,IAAIE,QAAAA,aAAaF,cAAY,cAAc;AACnE,QAAA,CAAC,UAAU,WAAW,IAAIE,QAAA;AAAA,IAC9BF,cAAY,YAAY;AAAA,EAC1B;AACA,QAAM,CAAC,aAAa,cAAc,IAAIE,QAAAA,aAEpCF,cAAY,gBAAgB;AAC9B,QAAM,CAAC,cAAc,eAAe,IAAIE,QAAAA,aAEtCF,cAAY,iBAAiB;AACzB,QAAA,CAAC,MAAM,OAAO,IAAIE,QAAA;AAAA,IACtBF,cAAY,QAAQ;AAAA,EACtB;AACA,QAAM,CAAC,MAAM,OAAO,IAAIE,QAAAA,aAAaF,cAAY,SAAS;AAE1DA,gBAAY,WAAW;AAAA,IACrB,eAAe,CAAC,WAAW;;AACP,wBAAA,OAAO,mBAAmB;AACjC,iBAAA,OAAO,YAAY;AACpB,gBAAA,OAAO,WAAW;AAClB,gBAAA,OAAO,WAAW;AAChB,kBAAA,OAAO,aAAa;AACjB,qBAAA,OAAO,gBAAgB;AACtB,sBAAA,OAAO,iBAAiB;AAChC,cAAA,MAAM,OAAO,SAAS;AACtB,cAAA,OAAO,SAAS;AACxB,2BAAe,kBAAf,wCAA+B;AAAA,IACjC;AAAA,IACA,mBAAmB,CAAC,WAAW;;AAChB,mBAAA,OAAO,cAAc;AACxB,gBAAA,OAAO,WAAW;AAC5B,2BAAe,sBAAf,wCAAmC;AAAA,IACrC;AAAA,IACA,UAAU,CAAC,MAAM,WAAW;;AACR,wBAAA,OAAO,mBAAmB;AAC7B,2BAAA,aAAA,wCAAW,MAAM;AAAA,IAAM;AAAA,EACxC,CACD;AAEM,SAAA;AAAA,IACL,GAAGG,MAAAA,oBAAoBH,aAAW;AAAA,IAClC;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACF;;"}
|
|
@@ -32,11 +32,15 @@ export interface SolidAsyncQueuer<TValue> extends Omit<AsyncQueuer<TValue>, 'get
|
|
|
32
32
|
/**
|
|
33
33
|
* Signal version of `getPeek`
|
|
34
34
|
*/
|
|
35
|
-
peek: Accessor<TValue | undefined>;
|
|
35
|
+
peek: Accessor<(() => Promise<TValue>) | undefined>;
|
|
36
36
|
/**
|
|
37
37
|
* Signal version of `getPendingItems`
|
|
38
38
|
*/
|
|
39
39
|
pendingItems: Accessor<Array<() => Promise<TValue>>>;
|
|
40
|
+
/**
|
|
41
|
+
* Signal version of `getRejectionCount`
|
|
42
|
+
*/
|
|
43
|
+
rejectionCount: Accessor<number>;
|
|
40
44
|
/**
|
|
41
45
|
* Signal version of `getSize`
|
|
42
46
|
*/
|
|
@@ -5,12 +5,18 @@ const solidJs = require("solid-js");
|
|
|
5
5
|
const utils = require("@tanstack/pacer/utils");
|
|
6
6
|
function createAsyncRateLimiter(fn, initialOptions) {
|
|
7
7
|
const asyncRateLimiter$1 = new asyncRateLimiter.AsyncRateLimiter(fn, initialOptions);
|
|
8
|
-
const [
|
|
9
|
-
asyncRateLimiter$1.
|
|
8
|
+
const [successCount, setSuccessCount] = solidJs.createSignal(
|
|
9
|
+
asyncRateLimiter$1.getSuccessCount()
|
|
10
10
|
);
|
|
11
11
|
const [rejectionCount, setRejectionCount] = solidJs.createSignal(
|
|
12
12
|
asyncRateLimiter$1.getRejectionCount()
|
|
13
13
|
);
|
|
14
|
+
const [errorCount, setErrorCount] = solidJs.createSignal(
|
|
15
|
+
asyncRateLimiter$1.getErrorCount()
|
|
16
|
+
);
|
|
17
|
+
const [settleCount, setSettleCount] = solidJs.createSignal(
|
|
18
|
+
asyncRateLimiter$1.getSettleCount()
|
|
19
|
+
);
|
|
14
20
|
const [remainingInWindow, setRemainingInWindow] = solidJs.createSignal(
|
|
15
21
|
asyncRateLimiter$1.getRemainingInWindow()
|
|
16
22
|
);
|
|
@@ -20,12 +26,15 @@ function createAsyncRateLimiter(fn, initialOptions) {
|
|
|
20
26
|
function setOptions(newOptions) {
|
|
21
27
|
asyncRateLimiter$1.setOptions({
|
|
22
28
|
...newOptions,
|
|
23
|
-
|
|
24
|
-
|
|
29
|
+
onSettled: (rateLimiter) => {
|
|
30
|
+
setSuccessCount(rateLimiter.getSuccessCount());
|
|
31
|
+
setSettleCount(rateLimiter.getSettleCount());
|
|
32
|
+
setErrorCount(rateLimiter.getErrorCount());
|
|
33
|
+
setRejectionCount(rateLimiter.getRejectionCount());
|
|
25
34
|
setRemainingInWindow(rateLimiter.getRemainingInWindow());
|
|
26
35
|
setMsUntilNextWindow(rateLimiter.getMsUntilNextWindow());
|
|
27
|
-
const
|
|
28
|
-
|
|
36
|
+
const onSettled = newOptions.onSettled ?? initialOptions.onSettled;
|
|
37
|
+
onSettled == null ? void 0 : onSettled(rateLimiter);
|
|
29
38
|
},
|
|
30
39
|
onReject: (rateLimiter) => {
|
|
31
40
|
setRejectionCount(rateLimiter.getRejectionCount());
|
|
@@ -39,11 +48,13 @@ function createAsyncRateLimiter(fn, initialOptions) {
|
|
|
39
48
|
setOptions(initialOptions);
|
|
40
49
|
return {
|
|
41
50
|
...utils.bindInstanceMethods(asyncRateLimiter$1),
|
|
42
|
-
|
|
43
|
-
rejectionCount,
|
|
51
|
+
errorCount,
|
|
44
52
|
remainingInWindow,
|
|
45
53
|
msUntilNextWindow,
|
|
46
|
-
|
|
54
|
+
rejectionCount,
|
|
55
|
+
setOptions,
|
|
56
|
+
settleCount,
|
|
57
|
+
successCount
|
|
47
58
|
};
|
|
48
59
|
}
|
|
49
60
|
exports.createAsyncRateLimiter = createAsyncRateLimiter;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createAsyncRateLimiter.cjs","sources":["../../../src/async-rate-limiter/createAsyncRateLimiter.ts"],"sourcesContent":["import { AsyncRateLimiter } from '@tanstack/pacer/async-rate-limiter'\nimport { createSignal } from 'solid-js'\nimport { bindInstanceMethods } from '@tanstack/pacer/utils'\nimport type { Accessor } from 'solid-js'\nimport type { AnyAsyncFunction } from '@tanstack/pacer/types'\nimport type { AsyncRateLimiterOptions } from '@tanstack/pacer/async-rate-limiter'\n\nexport interface SolidAsyncRateLimiter
|
|
1
|
+
{"version":3,"file":"createAsyncRateLimiter.cjs","sources":["../../../src/async-rate-limiter/createAsyncRateLimiter.ts"],"sourcesContent":["import { AsyncRateLimiter } from '@tanstack/pacer/async-rate-limiter'\nimport { createSignal } from 'solid-js'\nimport { bindInstanceMethods } from '@tanstack/pacer/utils'\nimport type { Accessor } from 'solid-js'\nimport type { AnyAsyncFunction } from '@tanstack/pacer/types'\nimport type { AsyncRateLimiterOptions } from '@tanstack/pacer/async-rate-limiter'\n\nexport interface SolidAsyncRateLimiter<TFn extends AnyAsyncFunction>\n extends Omit<\n AsyncRateLimiter<TFn>,\n | 'getSuccessCount'\n | 'getSettleCount'\n | 'getErrorCount'\n | 'getRejectionCount'\n | 'getRemainingInWindow'\n | 'getMsUntilNextWindow'\n > {\n successCount: Accessor<number>\n settleCount: Accessor<number>\n errorCount: Accessor<number>\n rejectionCount: Accessor<number>\n remainingInWindow: Accessor<number>\n msUntilNextWindow: Accessor<number>\n}\n\n/**\n * A low-level Solid hook that creates an `AsyncRateLimiter` instance to limit how many times an async function can execute within a time window.\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 allows an async function to execute up to a specified limit within a time window,\n * then blocks subsequent calls until the window passes. This is useful for respecting API rate limits,\n * managing resource constraints, or controlling bursts of async operations.\n *\n * Unlike the non-async RateLimiter, this async version supports returning values from the rate-limited function,\n * making it ideal for API calls and other async operations where you want the result of the `maybeExecute` call\n * instead of setting the result on a state variable from within the rate-limited function.\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 * @example\n * ```tsx\n * // Basic API call rate limiting with return value\n * const { maybeExecute } = createAsyncRateLimiter(\n * async (id: string) => {\n * const data = await api.fetchData(id);\n * return data; // Return value is preserved\n * },\n * { limit: 5, window: 1000 } // 5 calls per second\n * );\n *\n * // With state management and return value\n * const [data, setData] = createSignal(null);\n * const { maybeExecute } = createAsyncRateLimiter(\n * async (query) => {\n * const result = await searchAPI(query);\n * setData(result);\n * return result; // Return value can be used by the caller\n * },\n * {\n * limit: 10,\n * window: 60000, // 10 calls per minute\n * onReject: (info) => console.log(`Rate limit exceeded: ${info.nextValidTime - Date.now()}ms until next window`)\n * }\n * );\n * ```\n */\nexport function createAsyncRateLimiter<TFn extends AnyAsyncFunction>(\n fn: TFn,\n initialOptions: AsyncRateLimiterOptions<TFn>,\n): SolidAsyncRateLimiter<TFn> {\n const asyncRateLimiter = new AsyncRateLimiter<TFn>(fn, initialOptions)\n\n const [successCount, setSuccessCount] = createSignal(\n asyncRateLimiter.getSuccessCount(),\n )\n const [rejectionCount, setRejectionCount] = createSignal(\n asyncRateLimiter.getRejectionCount(),\n )\n const [errorCount, setErrorCount] = createSignal(\n asyncRateLimiter.getErrorCount(),\n )\n const [settleCount, setSettleCount] = createSignal(\n asyncRateLimiter.getSettleCount(),\n )\n const [remainingInWindow, setRemainingInWindow] = createSignal(\n asyncRateLimiter.getRemainingInWindow(),\n )\n const [msUntilNextWindow, setMsUntilNextWindow] = createSignal(\n asyncRateLimiter.getMsUntilNextWindow(),\n )\n\n function setOptions(newOptions: Partial<AsyncRateLimiterOptions<TFn>>) {\n asyncRateLimiter.setOptions({\n ...newOptions,\n onSettled: (rateLimiter) => {\n setSuccessCount(rateLimiter.getSuccessCount())\n setSettleCount(rateLimiter.getSettleCount())\n setErrorCount(rateLimiter.getErrorCount())\n setRejectionCount(rateLimiter.getRejectionCount())\n setRemainingInWindow(rateLimiter.getRemainingInWindow())\n setMsUntilNextWindow(rateLimiter.getMsUntilNextWindow())\n\n const onSettled = newOptions.onSettled ?? initialOptions.onSettled\n onSettled?.(rateLimiter)\n },\n onReject: (rateLimiter) => {\n setRejectionCount(rateLimiter.getRejectionCount())\n setRemainingInWindow(rateLimiter.getRemainingInWindow())\n setMsUntilNextWindow(rateLimiter.getMsUntilNextWindow())\n\n const onReject = newOptions.onReject ?? initialOptions.onReject\n onReject?.(rateLimiter)\n },\n })\n }\n\n setOptions(initialOptions)\n\n return {\n ...bindInstanceMethods(asyncRateLimiter),\n errorCount,\n remainingInWindow,\n msUntilNextWindow,\n rejectionCount,\n setOptions,\n settleCount,\n successCount,\n } as SolidAsyncRateLimiter<TFn>\n}\n"],"names":["asyncRateLimiter","AsyncRateLimiter","createSignal","bindInstanceMethods"],"mappings":";;;;;AAwEgB,SAAA,uBACd,IACA,gBAC4B;AAC5B,QAAMA,qBAAmB,IAAIC,kCAAsB,IAAI,cAAc;AAE/D,QAAA,CAAC,cAAc,eAAe,IAAIC,QAAA;AAAA,IACtCF,mBAAiB,gBAAgB;AAAA,EACnC;AACM,QAAA,CAAC,gBAAgB,iBAAiB,IAAIE,QAAA;AAAA,IAC1CF,mBAAiB,kBAAkB;AAAA,EACrC;AACM,QAAA,CAAC,YAAY,aAAa,IAAIE,QAAA;AAAA,IAClCF,mBAAiB,cAAc;AAAA,EACjC;AACM,QAAA,CAAC,aAAa,cAAc,IAAIE,QAAA;AAAA,IACpCF,mBAAiB,eAAe;AAAA,EAClC;AACM,QAAA,CAAC,mBAAmB,oBAAoB,IAAIE,QAAA;AAAA,IAChDF,mBAAiB,qBAAqB;AAAA,EACxC;AACM,QAAA,CAAC,mBAAmB,oBAAoB,IAAIE,QAAA;AAAA,IAChDF,mBAAiB,qBAAqB;AAAA,EACxC;AAEA,WAAS,WAAW,YAAmD;AACrEA,uBAAiB,WAAW;AAAA,MAC1B,GAAG;AAAA,MACH,WAAW,CAAC,gBAAgB;AACV,wBAAA,YAAY,iBAAiB;AAC9B,uBAAA,YAAY,gBAAgB;AAC7B,sBAAA,YAAY,eAAe;AACvB,0BAAA,YAAY,mBAAmB;AAC5B,6BAAA,YAAY,sBAAsB;AAClC,6BAAA,YAAY,sBAAsB;AAEjD,cAAA,YAAY,WAAW,aAAa,eAAe;AACzD,+CAAY;AAAA,MACd;AAAA,MACA,UAAU,CAAC,gBAAgB;AACP,0BAAA,YAAY,mBAAmB;AAC5B,6BAAA,YAAY,sBAAsB;AAClC,6BAAA,YAAY,sBAAsB;AAEjD,cAAA,WAAW,WAAW,YAAY,eAAe;AACvD,6CAAW;AAAA,MAAW;AAAA,IACxB,CACD;AAAA,EAAA;AAGH,aAAW,cAAc;AAElB,SAAA;AAAA,IACL,GAAGG,MAAAA,oBAAoBH,kBAAgB;AAAA,IACvC;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACF;;"}
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import { AsyncRateLimiter, AsyncRateLimiterOptions } from '@tanstack/pacer/async-rate-limiter';
|
|
2
2
|
import { Accessor } from 'solid-js';
|
|
3
3
|
import { AnyAsyncFunction } from '@tanstack/pacer/types';
|
|
4
|
-
export interface SolidAsyncRateLimiter<TFn extends AnyAsyncFunction
|
|
5
|
-
|
|
4
|
+
export interface SolidAsyncRateLimiter<TFn extends AnyAsyncFunction> extends Omit<AsyncRateLimiter<TFn>, 'getSuccessCount' | 'getSettleCount' | 'getErrorCount' | 'getRejectionCount' | 'getRemainingInWindow' | 'getMsUntilNextWindow'> {
|
|
5
|
+
successCount: Accessor<number>;
|
|
6
|
+
settleCount: Accessor<number>;
|
|
7
|
+
errorCount: Accessor<number>;
|
|
6
8
|
rejectionCount: Accessor<number>;
|
|
7
9
|
remainingInWindow: Accessor<number>;
|
|
8
10
|
msUntilNextWindow: Accessor<number>;
|
|
@@ -17,23 +19,34 @@ export interface SolidAsyncRateLimiter<TFn extends AnyAsyncFunction, TArgs exten
|
|
|
17
19
|
* then blocks subsequent calls until the window passes. This is useful for respecting API rate limits,
|
|
18
20
|
* managing resource constraints, or controlling bursts of async operations.
|
|
19
21
|
*
|
|
22
|
+
* Unlike the non-async RateLimiter, this async version supports returning values from the rate-limited function,
|
|
23
|
+
* making it ideal for API calls and other async operations where you want the result of the `maybeExecute` call
|
|
24
|
+
* instead of setting the result on a state variable from within the rate-limited function.
|
|
25
|
+
*
|
|
26
|
+
* The rate limiter supports two types of windows:
|
|
27
|
+
* - 'fixed': A strict window that resets after the window period. All executions within the window count
|
|
28
|
+
* towards the limit, and the window resets completely after the period.
|
|
29
|
+
* - 'sliding': A rolling window that allows executions as old ones expire. This provides a more
|
|
30
|
+
* consistent rate of execution over time.
|
|
31
|
+
*
|
|
20
32
|
* @example
|
|
21
33
|
* ```tsx
|
|
22
|
-
* // Basic API call rate limiting
|
|
34
|
+
* // Basic API call rate limiting with return value
|
|
23
35
|
* const { maybeExecute } = createAsyncRateLimiter(
|
|
24
36
|
* async (id: string) => {
|
|
25
37
|
* const data = await api.fetchData(id);
|
|
26
|
-
* return data;
|
|
38
|
+
* return data; // Return value is preserved
|
|
27
39
|
* },
|
|
28
40
|
* { limit: 5, window: 1000 } // 5 calls per second
|
|
29
41
|
* );
|
|
30
42
|
*
|
|
31
|
-
* // With state management
|
|
43
|
+
* // With state management and return value
|
|
32
44
|
* const [data, setData] = createSignal(null);
|
|
33
45
|
* const { maybeExecute } = createAsyncRateLimiter(
|
|
34
46
|
* async (query) => {
|
|
35
47
|
* const result = await searchAPI(query);
|
|
36
48
|
* setData(result);
|
|
49
|
+
* return result; // Return value can be used by the caller
|
|
37
50
|
* },
|
|
38
51
|
* {
|
|
39
52
|
* limit: 10,
|
|
@@ -43,4 +56,4 @@ export interface SolidAsyncRateLimiter<TFn extends AnyAsyncFunction, TArgs exten
|
|
|
43
56
|
* );
|
|
44
57
|
* ```
|
|
45
58
|
*/
|
|
46
|
-
export declare function createAsyncRateLimiter<TFn extends AnyAsyncFunction
|
|
59
|
+
export declare function createAsyncRateLimiter<TFn extends AnyAsyncFunction>(fn: TFn, initialOptions: AsyncRateLimiterOptions<TFn>): SolidAsyncRateLimiter<TFn>;
|
|
@@ -4,11 +4,25 @@ const asyncThrottler = require("@tanstack/pacer/async-throttler");
|
|
|
4
4
|
const solidJs = require("solid-js");
|
|
5
5
|
const utils = require("@tanstack/pacer/utils");
|
|
6
6
|
function createAsyncThrottler(fn, initialOptions) {
|
|
7
|
-
const asyncThrottler$1 =
|
|
8
|
-
|
|
9
|
-
|
|
7
|
+
const asyncThrottler$1 = utils.bindInstanceMethods(
|
|
8
|
+
new asyncThrottler.AsyncThrottler(fn, initialOptions)
|
|
9
|
+
);
|
|
10
|
+
const [successCount, setSuccessCount] = solidJs.createSignal(
|
|
11
|
+
asyncThrottler$1.getSuccessCount()
|
|
12
|
+
);
|
|
13
|
+
const [settleCount, setSettleCount] = solidJs.createSignal(
|
|
14
|
+
asyncThrottler$1.getSettleCount()
|
|
15
|
+
);
|
|
16
|
+
const [errorCount, setErrorCount] = solidJs.createSignal(
|
|
17
|
+
asyncThrottler$1.getErrorCount()
|
|
10
18
|
);
|
|
11
19
|
const [isPending, setIsPending] = solidJs.createSignal(asyncThrottler$1.getIsPending());
|
|
20
|
+
const [isExecuting, setIsExecuting] = solidJs.createSignal(
|
|
21
|
+
asyncThrottler$1.getIsExecuting()
|
|
22
|
+
);
|
|
23
|
+
const [lastResult, setLastResult] = solidJs.createSignal(
|
|
24
|
+
asyncThrottler$1.getLastResult()
|
|
25
|
+
);
|
|
12
26
|
const [lastExecutionTime, setLastExecutionTime] = solidJs.createSignal(
|
|
13
27
|
asyncThrottler$1.getLastExecutionTime()
|
|
14
28
|
);
|
|
@@ -18,24 +32,32 @@ function createAsyncThrottler(fn, initialOptions) {
|
|
|
18
32
|
function setOptions(newOptions) {
|
|
19
33
|
asyncThrottler$1.setOptions({
|
|
20
34
|
...newOptions,
|
|
21
|
-
|
|
22
|
-
|
|
35
|
+
onSettled: (throttler) => {
|
|
36
|
+
setSuccessCount(throttler.getSuccessCount());
|
|
37
|
+
setSettleCount(throttler.getSettleCount());
|
|
38
|
+
setErrorCount(throttler.getErrorCount());
|
|
23
39
|
setIsPending(throttler.getIsPending());
|
|
40
|
+
setIsExecuting(throttler.getIsExecuting());
|
|
24
41
|
setLastExecutionTime(throttler.getLastExecutionTime());
|
|
25
42
|
setNextExecutionTime(throttler.getNextExecutionTime());
|
|
26
|
-
|
|
27
|
-
|
|
43
|
+
setLastResult(throttler.getLastResult());
|
|
44
|
+
const onSettled = newOptions.onSettled ?? initialOptions.onSettled;
|
|
45
|
+
onSettled == null ? void 0 : onSettled(throttler);
|
|
28
46
|
}
|
|
29
47
|
});
|
|
30
48
|
}
|
|
31
49
|
setOptions(initialOptions);
|
|
32
50
|
return {
|
|
33
|
-
...
|
|
34
|
-
|
|
51
|
+
...asyncThrottler$1,
|
|
52
|
+
errorCount,
|
|
53
|
+
isExecuting,
|
|
35
54
|
isPending,
|
|
36
55
|
lastExecutionTime,
|
|
56
|
+
lastResult,
|
|
37
57
|
nextExecutionTime,
|
|
38
|
-
setOptions
|
|
58
|
+
setOptions,
|
|
59
|
+
settleCount,
|
|
60
|
+
successCount
|
|
39
61
|
};
|
|
40
62
|
}
|
|
41
63
|
exports.createAsyncThrottler = createAsyncThrottler;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createAsyncThrottler.cjs","sources":["../../../src/async-throttler/createAsyncThrottler.ts"],"sourcesContent":["import { AsyncThrottler } from '@tanstack/pacer/async-throttler'\nimport { createSignal } from 'solid-js'\nimport { bindInstanceMethods } from '@tanstack/pacer/utils'\nimport type { Accessor } from 'solid-js'\nimport type { AnyAsyncFunction } from '@tanstack/pacer/types'\nimport type { AsyncThrottlerOptions } from '@tanstack/pacer/async-throttler'\n\nexport interface SolidAsyncThrottler
|
|
1
|
+
{"version":3,"file":"createAsyncThrottler.cjs","sources":["../../../src/async-throttler/createAsyncThrottler.ts"],"sourcesContent":["import { AsyncThrottler } from '@tanstack/pacer/async-throttler'\nimport { createSignal } from 'solid-js'\nimport { bindInstanceMethods } from '@tanstack/pacer/utils'\nimport type { Accessor } from 'solid-js'\nimport type { AnyAsyncFunction } from '@tanstack/pacer/types'\nimport type { AsyncThrottlerOptions } from '@tanstack/pacer/async-throttler'\n\nexport interface SolidAsyncThrottler<TFn extends AnyAsyncFunction>\n extends Omit<\n AsyncThrottler<TFn>,\n | 'getSuccessCount'\n | 'getSettleCount'\n | 'getErrorCount'\n | 'getIsPending'\n | 'getIsExecuting'\n | 'getLastResult'\n | 'getLastExecutionTime'\n | 'getNextExecutionTime'\n > {\n successCount: Accessor<number>\n settleCount: Accessor<number>\n errorCount: Accessor<number>\n isPending: Accessor<boolean>\n isExecuting: Accessor<boolean>\n lastResult: Accessor<ReturnType<TFn> | undefined>\n lastExecutionTime: Accessor<number>\n nextExecutionTime: Accessor<number>\n}\n\n/**\n * A low-level Solid hook that creates an `AsyncThrottler` instance to limit how often an async 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, etc).\n *\n * Async throttling ensures an async 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 expensive API calls,\n * database operations, or other async tasks.\n *\n * @example\n * ```tsx\n * // Basic API call throttling\n * const { maybeExecute } = createAsyncThrottler(\n * async (id: string) => {\n * const data = await api.fetchData(id);\n * return data;\n * },\n * { wait: 1000 }\n * );\n *\n * // With state management\n * const [data, setData] = createSignal(null);\n * const { maybeExecute } = createAsyncThrottler(\n * async (query) => {\n * const result = await searchAPI(query);\n * setData(result);\n * },\n * {\n * wait: 2000,\n * leading: true, // Execute immediately on first call\n * trailing: false // Skip trailing edge updates\n * }\n * );\n * ```\n */\n\nexport function createAsyncThrottler<TFn extends AnyAsyncFunction>(\n fn: TFn,\n initialOptions: AsyncThrottlerOptions<TFn>,\n): SolidAsyncThrottler<TFn> {\n const asyncThrottler = bindInstanceMethods(\n new AsyncThrottler<TFn>(fn, initialOptions),\n )\n\n const [successCount, setSuccessCount] = createSignal(\n asyncThrottler.getSuccessCount(),\n )\n const [settleCount, setSettleCount] = createSignal(\n asyncThrottler.getSettleCount(),\n )\n const [errorCount, setErrorCount] = createSignal(\n asyncThrottler.getErrorCount(),\n )\n const [isPending, setIsPending] = createSignal(asyncThrottler.getIsPending())\n const [isExecuting, setIsExecuting] = createSignal(\n asyncThrottler.getIsExecuting(),\n )\n const [lastResult, setLastResult] = createSignal(\n asyncThrottler.getLastResult(),\n )\n const [lastExecutionTime, setLastExecutionTime] = createSignal(\n asyncThrottler.getLastExecutionTime(),\n )\n const [nextExecutionTime, setNextExecutionTime] = createSignal(\n asyncThrottler.getNextExecutionTime(),\n )\n\n function setOptions(newOptions: Partial<AsyncThrottlerOptions<TFn>>) {\n asyncThrottler.setOptions({\n ...newOptions,\n onSettled: (throttler) => {\n setSuccessCount(throttler.getSuccessCount())\n setSettleCount(throttler.getSettleCount())\n setErrorCount(throttler.getErrorCount())\n setIsPending(throttler.getIsPending())\n setIsExecuting(throttler.getIsExecuting())\n setLastExecutionTime(throttler.getLastExecutionTime())\n setNextExecutionTime(throttler.getNextExecutionTime())\n setLastResult(throttler.getLastResult())\n\n const onSettled = newOptions.onSettled ?? initialOptions.onSettled\n onSettled?.(throttler)\n },\n })\n }\n\n setOptions(initialOptions)\n\n return {\n ...asyncThrottler,\n errorCount,\n isExecuting,\n isPending,\n lastExecutionTime,\n lastResult,\n nextExecutionTime,\n setOptions,\n settleCount,\n successCount,\n } as SolidAsyncThrottler<TFn>\n}\n"],"names":["asyncThrottler","bindInstanceMethods","AsyncThrottler","createSignal"],"mappings":";;;;;AAkEgB,SAAA,qBACd,IACA,gBAC0B;AAC1B,QAAMA,mBAAiBC,MAAA;AAAA,IACrB,IAAIC,eAAAA,eAAoB,IAAI,cAAc;AAAA,EAC5C;AAEM,QAAA,CAAC,cAAc,eAAe,IAAIC,QAAA;AAAA,IACtCH,iBAAe,gBAAgB;AAAA,EACjC;AACM,QAAA,CAAC,aAAa,cAAc,IAAIG,QAAA;AAAA,IACpCH,iBAAe,eAAe;AAAA,EAChC;AACM,QAAA,CAAC,YAAY,aAAa,IAAIG,QAAA;AAAA,IAClCH,iBAAe,cAAc;AAAA,EAC/B;AACA,QAAM,CAAC,WAAW,YAAY,IAAIG,QAAAA,aAAaH,iBAAe,cAAc;AACtE,QAAA,CAAC,aAAa,cAAc,IAAIG,QAAA;AAAA,IACpCH,iBAAe,eAAe;AAAA,EAChC;AACM,QAAA,CAAC,YAAY,aAAa,IAAIG,QAAA;AAAA,IAClCH,iBAAe,cAAc;AAAA,EAC/B;AACM,QAAA,CAAC,mBAAmB,oBAAoB,IAAIG,QAAA;AAAA,IAChDH,iBAAe,qBAAqB;AAAA,EACtC;AACM,QAAA,CAAC,mBAAmB,oBAAoB,IAAIG,QAAA;AAAA,IAChDH,iBAAe,qBAAqB;AAAA,EACtC;AAEA,WAAS,WAAW,YAAiD;AACnEA,qBAAe,WAAW;AAAA,MACxB,GAAG;AAAA,MACH,WAAW,CAAC,cAAc;AACR,wBAAA,UAAU,iBAAiB;AAC5B,uBAAA,UAAU,gBAAgB;AAC3B,sBAAA,UAAU,eAAe;AAC1B,qBAAA,UAAU,cAAc;AACtB,uBAAA,UAAU,gBAAgB;AACpB,6BAAA,UAAU,sBAAsB;AAChC,6BAAA,UAAU,sBAAsB;AACvC,sBAAA,UAAU,eAAe;AAEjC,cAAA,YAAY,WAAW,aAAa,eAAe;AACzD,+CAAY;AAAA,MAAS;AAAA,IACvB,CACD;AAAA,EAAA;AAGH,aAAW,cAAc;AAElB,SAAA;AAAA,IACL,GAAGA;AAAAA,IACH;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACF;;"}
|
|
@@ -1,9 +1,13 @@
|
|
|
1
1
|
import { AsyncThrottler, AsyncThrottlerOptions } from '@tanstack/pacer/async-throttler';
|
|
2
2
|
import { Accessor } from 'solid-js';
|
|
3
3
|
import { AnyAsyncFunction } from '@tanstack/pacer/types';
|
|
4
|
-
export interface SolidAsyncThrottler<TFn extends AnyAsyncFunction
|
|
5
|
-
|
|
4
|
+
export interface SolidAsyncThrottler<TFn extends AnyAsyncFunction> extends Omit<AsyncThrottler<TFn>, 'getSuccessCount' | 'getSettleCount' | 'getErrorCount' | 'getIsPending' | 'getIsExecuting' | 'getLastResult' | 'getLastExecutionTime' | 'getNextExecutionTime'> {
|
|
5
|
+
successCount: Accessor<number>;
|
|
6
|
+
settleCount: Accessor<number>;
|
|
7
|
+
errorCount: Accessor<number>;
|
|
6
8
|
isPending: Accessor<boolean>;
|
|
9
|
+
isExecuting: Accessor<boolean>;
|
|
10
|
+
lastResult: Accessor<ReturnType<TFn> | undefined>;
|
|
7
11
|
lastExecutionTime: Accessor<number>;
|
|
8
12
|
nextExecutionTime: Accessor<number>;
|
|
9
13
|
}
|
|
@@ -43,4 +47,4 @@ export interface SolidAsyncThrottler<TFn extends AnyAsyncFunction, TArgs extends
|
|
|
43
47
|
* );
|
|
44
48
|
* ```
|
|
45
49
|
*/
|
|
46
|
-
export declare function createAsyncThrottler<TFn extends AnyAsyncFunction
|
|
50
|
+
export declare function createAsyncThrottler<TFn extends AnyAsyncFunction>(fn: TFn, initialOptions: AsyncThrottlerOptions<TFn>): SolidAsyncThrottler<TFn>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createDebouncedSignal.cjs","sources":["../../../src/debouncer/createDebouncedSignal.ts"],"sourcesContent":["import { createSignal } from 'solid-js'\nimport { createDebouncer } from './createDebouncer'\nimport type { SolidDebouncer } from './createDebouncer'\nimport type { Accessor, Setter } from 'solid-js'\nimport type { DebouncerOptions } from '@tanstack/pacer/debouncer'\n\n/**\n * A Solid hook that creates a debounced state value, combining Solid's createSignal with debouncing functionality.\n * This hook provides both the current debounced value and methods to update it.\n *\n * The state value is only updated after the specified wait time has elapsed since the last update attempt.\n * If another update is attempted before the wait time expires, the timer resets and starts waiting again.\n * This is useful for handling frequent state updates that should be throttled, like search input values\n * or window resize dimensions.\n *\n * The hook returns a tuple containing:\n * - The current debounced value accessor\n * - A function to update the debounced value\n * - The debouncer instance with additional control methods and state signals\n *\n * @example\n * ```tsx\n * // Debounced search input\n * const [searchTerm, setSearchTerm, debouncer] = createDebouncedSignal('', {\n * wait: 500 // Wait 500ms after last keystroke\n * });\n *\n * // Update value - will be debounced\n * const handleChange = (e) => {\n * setSearchTerm(e.target.value);\n * };\n *\n * // Access debouncer state via signals\n * console.log('Executions:', debouncer.executionCount());\n * console.log('Is pending:', debouncer.isPending());\n *\n * // In onExecute callback, use get* methods\n * const [searchTerm, setSearchTerm, debouncer] = createDebouncedSignal('', {\n * wait: 500,\n * onExecute: (debouncer) => {\n * console.log('Total executions:', debouncer.getExecutionCount());\n * }\n * });\n * ```\n */\nexport function createDebouncedSignal<TValue>(\n value: TValue,\n initialOptions: DebouncerOptions<Setter<TValue
|
|
1
|
+
{"version":3,"file":"createDebouncedSignal.cjs","sources":["../../../src/debouncer/createDebouncedSignal.ts"],"sourcesContent":["import { createSignal } from 'solid-js'\nimport { createDebouncer } from './createDebouncer'\nimport type { SolidDebouncer } from './createDebouncer'\nimport type { Accessor, Setter } from 'solid-js'\nimport type { DebouncerOptions } from '@tanstack/pacer/debouncer'\n\n/**\n * A Solid hook that creates a debounced state value, combining Solid's createSignal with debouncing functionality.\n * This hook provides both the current debounced value and methods to update it.\n *\n * The state value is only updated after the specified wait time has elapsed since the last update attempt.\n * If another update is attempted before the wait time expires, the timer resets and starts waiting again.\n * This is useful for handling frequent state updates that should be throttled, like search input values\n * or window resize dimensions.\n *\n * The hook returns a tuple containing:\n * - The current debounced value accessor\n * - A function to update the debounced value\n * - The debouncer instance with additional control methods and state signals\n *\n * @example\n * ```tsx\n * // Debounced search input\n * const [searchTerm, setSearchTerm, debouncer] = createDebouncedSignal('', {\n * wait: 500 // Wait 500ms after last keystroke\n * });\n *\n * // Update value - will be debounced\n * const handleChange = (e) => {\n * setSearchTerm(e.target.value);\n * };\n *\n * // Access debouncer state via signals\n * console.log('Executions:', debouncer.executionCount());\n * console.log('Is pending:', debouncer.isPending());\n *\n * // In onExecute callback, use get* methods\n * const [searchTerm, setSearchTerm, debouncer] = createDebouncedSignal('', {\n * wait: 500,\n * onExecute: (debouncer) => {\n * console.log('Total executions:', debouncer.getExecutionCount());\n * }\n * });\n * ```\n */\nexport function createDebouncedSignal<TValue>(\n value: TValue,\n initialOptions: DebouncerOptions<Setter<TValue>>,\n): [Accessor<TValue>, Setter<TValue>, SolidDebouncer<Setter<TValue>>] {\n const [debouncedValue, setDebouncedValue] = createSignal<TValue>(value)\n\n const debouncer = createDebouncer(setDebouncedValue, initialOptions)\n\n return [debouncedValue, debouncer.maybeExecute as Setter<TValue>, debouncer]\n}\n"],"names":["createSignal","createDebouncer"],"mappings":";;;;AA6CgB,SAAA,sBACd,OACA,gBACoE;AACpE,QAAM,CAAC,gBAAgB,iBAAiB,IAAIA,QAAAA,aAAqB,KAAK;AAEhE,QAAA,YAAYC,gBAAAA,gBAAgB,mBAAmB,cAAc;AAEnE,SAAO,CAAC,gBAAgB,UAAU,cAAgC,SAAS;AAC7E;;"}
|
|
@@ -40,8 +40,4 @@ import { DebouncerOptions } from '@tanstack/pacer/debouncer';
|
|
|
40
40
|
* });
|
|
41
41
|
* ```
|
|
42
42
|
*/
|
|
43
|
-
export declare function createDebouncedSignal<TValue>(value: TValue, initialOptions: DebouncerOptions<Setter<TValue
|
|
44
|
-
Accessor<TValue>,
|
|
45
|
-
Setter<TValue>,
|
|
46
|
-
SolidDebouncer<Setter<TValue>, [Accessor<TValue>]>
|
|
47
|
-
];
|
|
43
|
+
export declare function createDebouncedSignal<TValue>(value: TValue, initialOptions: DebouncerOptions<Setter<TValue>>): [Accessor<TValue>, Setter<TValue>, SolidDebouncer<Setter<TValue>>];
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createDebouncedValue.cjs","sources":["../../../src/debouncer/createDebouncedValue.ts"],"sourcesContent":["import { createEffect
|
|
1
|
+
{"version":3,"file":"createDebouncedValue.cjs","sources":["../../../src/debouncer/createDebouncedValue.ts"],"sourcesContent":["import { createEffect } from 'solid-js'\nimport { createDebouncedSignal } from './createDebouncedSignal'\nimport type { SolidDebouncer } from './createDebouncer'\nimport type { Accessor, Setter } from 'solid-js'\nimport type { DebouncerOptions } from '@tanstack/pacer/debouncer'\n\n/**\n * A Solid hook that creates a debounced value that updates only after a specified delay.\n * Unlike createDebouncedSignal, this hook automatically tracks changes to the input value\n * and updates the debounced value accordingly.\n *\n * The debounced value will only update after the specified wait time has elapsed since\n * the last change to the input value. If the input value changes again before the wait\n * time expires, the timer resets and starts waiting again.\n *\n * This is useful for deriving debounced values from props or state that change frequently,\n * like search queries or form inputs, where you want to limit how often downstream effects\n * or calculations occur.\n *\n * The hook returns a tuple containing:\n * - An Accessor that provides the current debounced value\n * - The debouncer instance with control methods\n *\n * @example\n * ```tsx\n * // Debounce a search query\n * const [searchQuery, setSearchQuery] = createSignal('');\n * const [debouncedQuery, debouncer] = createDebouncedValue(searchQuery, {\n * wait: 500 // Wait 500ms after last change\n * });\n *\n * // debouncedQuery will update 500ms after searchQuery stops changing\n * createEffect(() => {\n * fetchSearchResults(debouncedQuery());\n * });\n *\n * // Control the debouncer\n * debouncer.cancel(); // Cancel any pending updates\n * ```\n */\nexport function createDebouncedValue<TValue>(\n value: Accessor<TValue>,\n initialOptions: DebouncerOptions<Setter<TValue>>,\n): [Accessor<TValue>, SolidDebouncer<Setter<TValue>>] {\n const [debouncedValue, setDebouncedValue, debouncer] = createDebouncedSignal(\n value(),\n initialOptions,\n )\n\n createEffect(() => {\n setDebouncedValue(value() as any)\n })\n\n return [debouncedValue, debouncer]\n}\n"],"names":["createDebouncedSignal","createEffect"],"mappings":";;;;AAwCgB,SAAA,qBACd,OACA,gBACoD;AACpD,QAAM,CAAC,gBAAgB,mBAAmB,SAAS,IAAIA,sBAAA;AAAA,IACrD,MAAM;AAAA,IACN;AAAA,EACF;AAEAC,UAAAA,aAAa,MAAM;AACjB,sBAAkB,OAAc;AAAA,EAAA,CACjC;AAEM,SAAA,CAAC,gBAAgB,SAAS;AACnC;;"}
|
|
@@ -15,8 +15,8 @@ import { DebouncerOptions } from '@tanstack/pacer/debouncer';
|
|
|
15
15
|
* or calculations occur.
|
|
16
16
|
*
|
|
17
17
|
* The hook returns a tuple containing:
|
|
18
|
-
* -
|
|
19
|
-
* - The debouncer instance with control methods
|
|
18
|
+
* - An Accessor that provides the current debounced value
|
|
19
|
+
* - The debouncer instance with control methods
|
|
20
20
|
*
|
|
21
21
|
* @example
|
|
22
22
|
* ```tsx
|
|
@@ -31,14 +31,8 @@ import { DebouncerOptions } from '@tanstack/pacer/debouncer';
|
|
|
31
31
|
* fetchSearchResults(debouncedQuery());
|
|
32
32
|
* });
|
|
33
33
|
*
|
|
34
|
-
* //
|
|
35
|
-
*
|
|
36
|
-
* console.log('Is pending:', debouncer.isPending());
|
|
37
|
-
*
|
|
38
|
-
* // Handle input changes
|
|
39
|
-
* const handleChange = (e) => {
|
|
40
|
-
* setSearchQuery(e.target.value);
|
|
41
|
-
* };
|
|
34
|
+
* // Control the debouncer
|
|
35
|
+
* debouncer.cancel(); // Cancel any pending updates
|
|
42
36
|
* ```
|
|
43
37
|
*/
|
|
44
|
-
export declare function createDebouncedValue<TValue>(value: Accessor<TValue>, initialOptions: DebouncerOptions<Setter<TValue
|
|
38
|
+
export declare function createDebouncedValue<TValue>(value: Accessor<TValue>, initialOptions: DebouncerOptions<Setter<TValue>>): [Accessor<TValue>, SolidDebouncer<Setter<TValue>>];
|
|
@@ -4,7 +4,7 @@ const debouncer = require("@tanstack/pacer/debouncer");
|
|
|
4
4
|
const solidJs = require("solid-js");
|
|
5
5
|
const utils = require("@tanstack/pacer/utils");
|
|
6
6
|
function createDebouncer(fn, initialOptions) {
|
|
7
|
-
const debouncer$1 = new debouncer.Debouncer(fn, initialOptions);
|
|
7
|
+
const debouncer$1 = utils.bindInstanceMethods(new debouncer.Debouncer(fn, initialOptions));
|
|
8
8
|
const [executionCount, setExecutionCount] = solidJs.createSignal(
|
|
9
9
|
debouncer$1.getExecutionCount()
|
|
10
10
|
);
|
|
@@ -21,8 +21,13 @@ function createDebouncer(fn, initialOptions) {
|
|
|
21
21
|
});
|
|
22
22
|
}
|
|
23
23
|
setOptions(initialOptions);
|
|
24
|
+
solidJs.createEffect(() => {
|
|
25
|
+
solidJs.onCleanup(() => {
|
|
26
|
+
debouncer$1.cancel();
|
|
27
|
+
});
|
|
28
|
+
});
|
|
24
29
|
return {
|
|
25
|
-
...
|
|
30
|
+
...debouncer$1,
|
|
26
31
|
executionCount,
|
|
27
32
|
isPending,
|
|
28
33
|
setOptions
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createDebouncer.cjs","sources":["../../../src/debouncer/createDebouncer.ts"],"sourcesContent":["import { Debouncer } from '@tanstack/pacer/debouncer'\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 { DebouncerOptions } from '@tanstack/pacer/debouncer'\n\n/**\n * An extension of the Debouncer class that adds Solid signals to access the internal state of the debouncer\n */\nexport interface SolidDebouncer
|
|
1
|
+
{"version":3,"file":"createDebouncer.cjs","sources":["../../../src/debouncer/createDebouncer.ts"],"sourcesContent":["import { Debouncer } from '@tanstack/pacer/debouncer'\nimport { createEffect, createSignal, onCleanup } from 'solid-js'\nimport { bindInstanceMethods } from '@tanstack/pacer/utils'\nimport type { Accessor } from 'solid-js'\nimport type { AnyFunction } from '@tanstack/pacer/types'\nimport type { DebouncerOptions } from '@tanstack/pacer/debouncer'\n\n/**\n * An extension of the Debouncer class that adds Solid signals to access the internal state of the debouncer\n */\nexport interface SolidDebouncer<TFn extends AnyFunction>\n extends Omit<Debouncer<TFn>, 'getExecutionCount' | 'getIsPending'> {\n executionCount: Accessor<number>\n isPending: Accessor<boolean>\n}\n\n/**\n * A Solid hook that creates and manages a Debouncer instance.\n *\n * This is a lower-level hook that provides direct access to the Debouncer's functionality without\n * any built-in state management. This allows you to integrate it with any state management solution\n * you prefer (createSignal, Redux, Zustand, etc.).\n *\n * This hook provides debouncing functionality to limit how often a function can be called,\n * waiting for a specified delay before executing the latest call. This is useful for handling\n * frequent events like window resizing, scroll events, or real-time search inputs.\n *\n * The debouncer will only execute the function after the specified wait time has elapsed\n * since the last call. If the function is called again before the wait time expires, the\n * timer resets and starts waiting again.\n *\n * @example\n * ```tsx\n * // Debounce a search function to limit API calls\n * const debouncer = createDebouncer(\n * (query: string) => fetchSearchResults(query),\n * { wait: 500 } // Wait 500ms after last keystroke\n * );\n *\n * // In an event handler\n * const handleChange = (e) => {\n * debouncer.maybeExecute(e.target.value);\n * };\n *\n * // Access debouncer state via signals\n * console.log('Executions:', debouncer.executionCount());\n * console.log('Is pending:', debouncer.isPending());\n *\n * // Update options\n * debouncer.setOptions({ wait: 1000 });\n * ```\n */\nexport function createDebouncer<TFn extends AnyFunction>(\n fn: TFn,\n initialOptions: DebouncerOptions<TFn>,\n): SolidDebouncer<TFn> {\n const debouncer = bindInstanceMethods(new Debouncer<TFn>(fn, initialOptions))\n\n const [executionCount, setExecutionCount] = createSignal(\n debouncer.getExecutionCount(),\n )\n const [isPending, setIsPending] = createSignal(debouncer.getIsPending())\n\n function setOptions(newOptions: Partial<DebouncerOptions<TFn>>) {\n debouncer.setOptions({\n ...newOptions,\n onExecute: (debouncer) => {\n setExecutionCount(debouncer.getExecutionCount())\n setIsPending(debouncer.getIsPending())\n\n const onExecute = newOptions.onExecute ?? initialOptions.onExecute\n onExecute?.(debouncer)\n },\n })\n }\n\n setOptions(initialOptions)\n\n createEffect(() => {\n onCleanup(() => {\n debouncer.cancel()\n })\n })\n\n return {\n ...debouncer,\n executionCount,\n isPending,\n setOptions,\n } as SolidDebouncer<TFn>\n}\n"],"names":["debouncer","bindInstanceMethods","Debouncer","createSignal","createEffect","onCleanup"],"mappings":";;;;;AAoDgB,SAAA,gBACd,IACA,gBACqB;AACrB,QAAMA,cAAYC,MAAAA,oBAAoB,IAAIC,UAAe,UAAA,IAAI,cAAc,CAAC;AAEtE,QAAA,CAAC,gBAAgB,iBAAiB,IAAIC,QAAA;AAAA,IAC1CH,YAAU,kBAAkB;AAAA,EAC9B;AACA,QAAM,CAAC,WAAW,YAAY,IAAIG,QAAAA,aAAaH,YAAU,cAAc;AAEvE,WAAS,WAAW,YAA4C;AAC9DA,gBAAU,WAAW;AAAA,MACnB,GAAG;AAAA,MACH,WAAW,CAACA,eAAc;AACNA,0BAAAA,WAAU,mBAAmB;AAClCA,qBAAAA,WAAU,cAAc;AAE/B,cAAA,YAAY,WAAW,aAAa,eAAe;AACzD,+CAAYA;AAAAA,MAAS;AAAA,IACvB,CACD;AAAA,EAAA;AAGH,aAAW,cAAc;AAEzBI,UAAAA,aAAa,MAAM;AACjBC,YAAAA,UAAU,MAAM;AACdL,kBAAU,OAAO;AAAA,IAAA,CAClB;AAAA,EAAA,CACF;AAEM,SAAA;AAAA,IACL,GAAGA;AAAAA,IACH;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACF;;"}
|
|
@@ -4,7 +4,7 @@ import { AnyFunction } from '@tanstack/pacer/types';
|
|
|
4
4
|
/**
|
|
5
5
|
* An extension of the Debouncer class that adds Solid signals to access the internal state of the debouncer
|
|
6
6
|
*/
|
|
7
|
-
export interface SolidDebouncer<TFn extends AnyFunction
|
|
7
|
+
export interface SolidDebouncer<TFn extends AnyFunction> extends Omit<Debouncer<TFn>, 'getExecutionCount' | 'getIsPending'> {
|
|
8
8
|
executionCount: Accessor<number>;
|
|
9
9
|
isPending: Accessor<boolean>;
|
|
10
10
|
}
|
|
@@ -44,4 +44,4 @@ export interface SolidDebouncer<TFn extends AnyFunction, TArgs extends Parameter
|
|
|
44
44
|
* debouncer.setOptions({ wait: 1000 });
|
|
45
45
|
* ```
|
|
46
46
|
*/
|
|
47
|
-
export declare function createDebouncer<TFn extends AnyFunction
|
|
47
|
+
export declare function createDebouncer<TFn extends AnyFunction>(fn: TFn, initialOptions: DebouncerOptions<TFn>): SolidDebouncer<TFn>;
|
|
@@ -4,7 +4,7 @@ const queuer = require("@tanstack/pacer/queuer");
|
|
|
4
4
|
const solidJs = require("solid-js");
|
|
5
5
|
const utils = require("@tanstack/pacer/utils");
|
|
6
6
|
function createQueuer(initialOptions = {}) {
|
|
7
|
-
const queuer$1 = new queuer.Queuer(initialOptions);
|
|
7
|
+
const queuer$1 = utils.bindInstanceMethods(new queuer.Queuer(initialOptions));
|
|
8
8
|
const [allItems, setAllItems] = solidJs.createSignal(
|
|
9
9
|
queuer$1.getAllItems()
|
|
10
10
|
);
|
|
@@ -49,7 +49,7 @@ function createQueuer(initialOptions = {}) {
|
|
|
49
49
|
}
|
|
50
50
|
setOptions(initialOptions);
|
|
51
51
|
return {
|
|
52
|
-
...
|
|
52
|
+
...queuer$1,
|
|
53
53
|
allItems,
|
|
54
54
|
executionCount,
|
|
55
55
|
isEmpty,
|