@tanstack/solid-pacer 0.5.0 → 0.7.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.map +1 -1
- package/dist/cjs/async-debouncer/createAsyncDebouncer.d.cts +21 -2
- package/dist/cjs/async-queuer/createAsyncQueuer.cjs +26 -13
- package/dist/cjs/async-queuer/createAsyncQueuer.cjs.map +1 -1
- package/dist/cjs/async-queuer/createAsyncQueuer.d.cts +47 -32
- package/dist/cjs/async-rate-limiter/createAsyncRateLimiter.cjs.map +1 -1
- package/dist/cjs/async-rate-limiter/createAsyncRateLimiter.d.cts +22 -7
- package/dist/cjs/async-throttler/createAsyncThrottler.cjs.map +1 -1
- package/dist/cjs/async-throttler/createAsyncThrottler.d.cts +15 -1
- package/dist/cjs/batcher/createBatcher.cjs +58 -0
- package/dist/cjs/batcher/createBatcher.cjs.map +1 -0
- package/dist/cjs/batcher/createBatcher.d.cts +76 -0
- package/dist/cjs/batcher/index.cjs +12 -0
- package/dist/cjs/batcher/index.cjs.map +1 -0
- package/dist/cjs/batcher/index.d.cts +2 -0
- package/dist/cjs/index.cjs +2 -0
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.cts +1 -0
- package/dist/cjs/queuer/createQueuer.cjs +2 -2
- package/dist/cjs/queuer/createQueuer.cjs.map +1 -1
- package/dist/cjs/queuer/createQueuer.d.cts +27 -21
- package/dist/cjs/rate-limiter/createRateLimiter.cjs.map +1 -1
- package/dist/cjs/rate-limiter/createRateLimiter.d.cts +4 -10
- package/dist/esm/async-debouncer/createAsyncDebouncer.d.ts +21 -2
- package/dist/esm/async-debouncer/createAsyncDebouncer.js.map +1 -1
- package/dist/esm/async-queuer/createAsyncQueuer.d.ts +47 -32
- package/dist/esm/async-queuer/createAsyncQueuer.js +26 -13
- package/dist/esm/async-queuer/createAsyncQueuer.js.map +1 -1
- package/dist/esm/async-rate-limiter/createAsyncRateLimiter.d.ts +22 -7
- package/dist/esm/async-rate-limiter/createAsyncRateLimiter.js.map +1 -1
- package/dist/esm/async-throttler/createAsyncThrottler.d.ts +15 -1
- package/dist/esm/async-throttler/createAsyncThrottler.js.map +1 -1
- package/dist/esm/batcher/createBatcher.d.ts +76 -0
- package/dist/esm/batcher/createBatcher.js +58 -0
- package/dist/esm/batcher/createBatcher.js.map +1 -0
- package/dist/esm/batcher/index.d.ts +2 -0
- package/dist/esm/batcher/index.js +6 -0
- package/dist/esm/batcher/index.js.map +1 -0
- package/dist/esm/index.d.ts +1 -0
- package/dist/esm/index.js +2 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/queuer/createQueuer.d.ts +27 -21
- package/dist/esm/queuer/createQueuer.js +2 -2
- package/dist/esm/queuer/createQueuer.js.map +1 -1
- package/dist/esm/rate-limiter/createRateLimiter.d.ts +4 -10
- package/dist/esm/rate-limiter/createRateLimiter.js.map +1 -1
- package/package.json +12 -2
- package/src/async-debouncer/createAsyncDebouncer.ts +21 -3
- package/src/async-queuer/createAsyncQueuer.ts +76 -48
- package/src/async-rate-limiter/createAsyncRateLimiter.ts +22 -7
- package/src/async-throttler/createAsyncThrottler.ts +15 -2
- package/src/batcher/createBatcher.ts +152 -0
- package/src/batcher/index.ts +3 -0
- package/src/index.ts +3 -0
- package/src/queuer/createQueuer.ts +28 -21
- package/src/rate-limiter/createRateLimiter.ts +4 -10
|
@@ -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<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
|
|
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 * Each new invocation resets the delay timer. This is useful for handling frequent events like window resizing\n * or input changes where you only want to execute the handler after the events have stopped occurring.\n *\n * Unlike throttling which allows execution at regular intervals, debouncing prevents any execution until\n * the function stops being called for the specified delay period.\n *\n * Unlike the non-async Debouncer, this async version supports returning values from the debounced 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 debounced function.\n *\n * Error Handling:\n * - If an `onError` handler is provided, it will be called with the error and debouncer instance\n * - If `throwOnError` is true (default when no onError handler is provided), the error will be thrown\n * - If `throwOnError` is false (default when onError handler is provided), the error will be swallowed\n * - Both onError and throwOnError can be used together - the handler will be called before any error is thrown\n * - The error state can be checked using the underlying AsyncDebouncer instance\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 * leading: true, // Execute immediately on first call\n * trailing: false, // Skip trailing edge updates\n * onError: (error) => {\n * console.error('API call failed:', error);\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":";;;;;AA4EgB,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;;"}
|
|
@@ -15,8 +15,22 @@ export interface SolidAsyncDebouncer<TFn extends AnyAsyncFunction> extends Omit<
|
|
|
15
15
|
* you can integrate with any state management solution (createSignal, etc).
|
|
16
16
|
*
|
|
17
17
|
* Async debouncing ensures that an async function only executes after a specified delay has passed since its last invocation.
|
|
18
|
-
*
|
|
19
|
-
*
|
|
18
|
+
* Each new invocation resets the delay timer. This is useful for handling frequent events like window resizing
|
|
19
|
+
* or input changes where you only want to execute the handler after the events have stopped occurring.
|
|
20
|
+
*
|
|
21
|
+
* Unlike throttling which allows execution at regular intervals, debouncing prevents any execution until
|
|
22
|
+
* the function stops being called for the specified delay period.
|
|
23
|
+
*
|
|
24
|
+
* Unlike the non-async Debouncer, this async version supports returning values from the debounced function,
|
|
25
|
+
* making it ideal for API calls and other async operations where you want the result of the `maybeExecute` call
|
|
26
|
+
* instead of setting the result on a state variable from within the debounced function.
|
|
27
|
+
*
|
|
28
|
+
* Error Handling:
|
|
29
|
+
* - If an `onError` handler is provided, it will be called with the error and debouncer instance
|
|
30
|
+
* - If `throwOnError` is true (default when no onError handler is provided), the error will be thrown
|
|
31
|
+
* - If `throwOnError` is false (default when onError handler is provided), the error will be swallowed
|
|
32
|
+
* - Both onError and throwOnError can be used together - the handler will be called before any error is thrown
|
|
33
|
+
* - The error state can be checked using the underlying AsyncDebouncer instance
|
|
20
34
|
*
|
|
21
35
|
* @example
|
|
22
36
|
* ```tsx
|
|
@@ -38,6 +52,11 @@ export interface SolidAsyncDebouncer<TFn extends AnyAsyncFunction> extends Omit<
|
|
|
38
52
|
* },
|
|
39
53
|
* {
|
|
40
54
|
* wait: 300,
|
|
55
|
+
* leading: true, // Execute immediately on first call
|
|
56
|
+
* trailing: false, // Skip trailing edge updates
|
|
57
|
+
* onError: (error) => {
|
|
58
|
+
* console.error('API call failed:', error);
|
|
59
|
+
* }
|
|
41
60
|
* }
|
|
42
61
|
* );
|
|
43
62
|
* ```
|
|
@@ -3,10 +3,14 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
|
3
3
|
const asyncQueuer = require("@tanstack/pacer/async-queuer");
|
|
4
4
|
const solidJs = require("solid-js");
|
|
5
5
|
const utils = require("@tanstack/pacer/utils");
|
|
6
|
-
function createAsyncQueuer(initialOptions = {}) {
|
|
7
|
-
const asyncQueuer$1 = new asyncQueuer.AsyncQueuer(initialOptions);
|
|
8
|
-
const [
|
|
9
|
-
asyncQueuer$1.
|
|
6
|
+
function createAsyncQueuer(fn, initialOptions = {}) {
|
|
7
|
+
const asyncQueuer$1 = new asyncQueuer.AsyncQueuer(fn, initialOptions);
|
|
8
|
+
const [successCount, setSuccessCount] = solidJs.createSignal(
|
|
9
|
+
asyncQueuer$1.getSuccessCount()
|
|
10
|
+
);
|
|
11
|
+
const [errorCount, setErrorCount] = solidJs.createSignal(asyncQueuer$1.getErrorCount());
|
|
12
|
+
const [settledCount, setSettledCount] = solidJs.createSignal(
|
|
13
|
+
asyncQueuer$1.getSettledCount()
|
|
10
14
|
);
|
|
11
15
|
const [rejectionCount, setRejectionCount] = solidJs.createSignal(
|
|
12
16
|
asyncQueuer$1.getRejectionCount()
|
|
@@ -18,8 +22,12 @@ function createAsyncQueuer(initialOptions = {}) {
|
|
|
18
22
|
const [allItems, setAllItems] = solidJs.createSignal(
|
|
19
23
|
asyncQueuer$1.getAllItems()
|
|
20
24
|
);
|
|
21
|
-
const [activeItems, setActiveItems] = solidJs.createSignal(
|
|
22
|
-
|
|
25
|
+
const [activeItems, setActiveItems] = solidJs.createSignal(
|
|
26
|
+
asyncQueuer$1.getActiveItems()
|
|
27
|
+
);
|
|
28
|
+
const [pendingItems, setPendingItems] = solidJs.createSignal(
|
|
29
|
+
asyncQueuer$1.getPendingItems()
|
|
30
|
+
);
|
|
23
31
|
const [peek, setPeek] = solidJs.createSignal(
|
|
24
32
|
asyncQueuer$1.getPeek()
|
|
25
33
|
);
|
|
@@ -27,15 +35,18 @@ function createAsyncQueuer(initialOptions = {}) {
|
|
|
27
35
|
asyncQueuer$1.setOptions({
|
|
28
36
|
onItemsChange: (queuer) => {
|
|
29
37
|
var _a;
|
|
30
|
-
|
|
38
|
+
setActiveItems(queuer.getActiveItems());
|
|
39
|
+
setAllItems(queuer.getAllItems());
|
|
40
|
+
setErrorCount(queuer.getErrorCount());
|
|
31
41
|
setIsEmpty(queuer.getIsEmpty());
|
|
32
42
|
setIsFull(queuer.getIsFull());
|
|
33
43
|
setIsIdle(queuer.getIsIdle());
|
|
34
|
-
setAllItems(queuer.getAllItems());
|
|
35
|
-
setActiveItems(queuer.getActiveItems());
|
|
36
|
-
setPendingItems(queuer.getPendingItems());
|
|
37
44
|
setPeek(() => queuer.getPeek());
|
|
45
|
+
setPendingItems(queuer.getPendingItems());
|
|
46
|
+
setRejectionCount(queuer.getRejectionCount());
|
|
47
|
+
setSettledCount(queuer.getSettledCount());
|
|
38
48
|
setSize(queuer.getSize());
|
|
49
|
+
setSuccessCount(queuer.getSuccessCount());
|
|
39
50
|
(_a = initialOptions.onItemsChange) == null ? void 0 : _a.call(initialOptions, queuer);
|
|
40
51
|
},
|
|
41
52
|
onIsRunningChange: (queuer) => {
|
|
@@ -53,16 +64,18 @@ function createAsyncQueuer(initialOptions = {}) {
|
|
|
53
64
|
return {
|
|
54
65
|
...utils.bindInstanceMethods(asyncQueuer$1),
|
|
55
66
|
activeItems,
|
|
56
|
-
|
|
67
|
+
allItems,
|
|
68
|
+
errorCount,
|
|
57
69
|
isEmpty,
|
|
58
70
|
isFull,
|
|
59
71
|
isIdle,
|
|
60
72
|
isRunning,
|
|
61
|
-
allItems,
|
|
62
73
|
peek,
|
|
63
74
|
pendingItems,
|
|
64
75
|
rejectionCount,
|
|
65
|
-
|
|
76
|
+
settledCount,
|
|
77
|
+
size,
|
|
78
|
+
successCount
|
|
66
79
|
};
|
|
67
80
|
}
|
|
68
81
|
exports.createAsyncQueuer = createAsyncQueuer;
|
|
@@ -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 | '
|
|
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 | 'getErrorCount'\n | 'getIsEmpty'\n | 'getIsFull'\n | 'getIsIdle'\n | 'getIsRunning'\n | 'getPeek'\n | 'getPendingItems'\n | 'getRejectionCount'\n | 'getSettledCount'\n | 'getSize'\n | 'getSuccessCount'\n > {\n /**\n * Signal version of `getActiveItems`\n */\n activeItems: Accessor<Array<TValue>>\n /**\n * Signal version of `getAllItems`\n */\n allItems: Accessor<Array<TValue>>\n /**\n * Signal version of `getErrorCount`\n */\n errorCount: 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<TValue>>\n /**\n * Signal version of `getRejectionCount`\n */\n rejectionCount: Accessor<number>\n /**\n * Signal version of `getSettledCount`\n */\n settledCount: Accessor<number>\n /**\n * Signal version of `getSize`\n */\n size: Accessor<number>\n /**\n * Signal version of `getSuccessCount`\n */\n successCount: Accessor<number>\n}\n\n/**\n * Creates a Solid-compatible AsyncQueuer instance for managing an asynchronous queue of items, exposing Solid signals for all stateful properties.\n *\n * Features:\n * - Priority queueing via `getPriority` or item `priority` property\n * - Configurable concurrency limit\n * - FIFO (First In First Out) or LIFO (Last In First Out) queue behavior\n * - Pause/resume processing\n * - Task cancellation\n * - Item expiration\n * - Lifecycle callbacks for success, error, settled, items change, etc.\n * - All stateful properties (active items, pending items, counts, etc.) are exposed as Solid signals for reactivity\n *\n * Tasks are processed concurrently up to the configured concurrency limit. When a task completes,\n * the next pending task is processed if the concurrency limit allows.\n *\n * Error Handling:\n * - If an `onError` handler is provided, it will be called with the error and queuer instance\n * - If `throwOnError` is true (default when no onError handler is provided), the error will be thrown\n * - If `throwOnError` is false (default when onError handler is provided), the error will be swallowed\n * - Both onError and throwOnError can be used together; the handler will be called before any error is thrown\n * - The error state can be checked using the underlying AsyncQueuer instance\n *\n * Example usage:\n * ```tsx\n * // Basic async queuer for API requests\n * const asyncQueuer = createAsyncQueuer(async (item) => {\n * // process item\n * return await fetchData(item);\n * }, {\n * initialItems: [],\n * concurrency: 2,\n * maxSize: 100,\n * started: false,\n * onSuccess: (result) => {\n * console.log('Item processed:', result);\n * },\n * onError: (error) => {\n * console.error('Processing failed:', error);\n * }\n * });\n *\n * // Add items to queue\n * asyncQueuer.addItem(newItem);\n *\n * // Start processing\n * asyncQueuer.start();\n *\n * // Use Solid signals in your UI\n * const pending = asyncQueuer.pendingItems();\n * ```\n */\nexport function createAsyncQueuer<TValue>(\n fn: (value: TValue) => Promise<any>,\n initialOptions: AsyncQueuerOptions<TValue> = {},\n): SolidAsyncQueuer<TValue> {\n const asyncQueuer = new AsyncQueuer<TValue>(fn, initialOptions)\n\n const [successCount, setSuccessCount] = createSignal(\n asyncQueuer.getSuccessCount(),\n )\n const [errorCount, setErrorCount] = createSignal(asyncQueuer.getErrorCount())\n const [settledCount, setSettledCount] = createSignal(\n asyncQueuer.getSettledCount(),\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<TValue>>(\n asyncQueuer.getAllItems(),\n )\n const [activeItems, setActiveItems] = createSignal<Array<TValue>>(\n asyncQueuer.getActiveItems(),\n )\n const [pendingItems, setPendingItems] = createSignal<Array<TValue>>(\n asyncQueuer.getPendingItems(),\n )\n const [peek, setPeek] = createSignal<TValue | undefined>(\n asyncQueuer.getPeek(),\n )\n const [size, setSize] = createSignal(asyncQueuer.getSize())\n\n asyncQueuer.setOptions({\n onItemsChange: (queuer) => {\n setActiveItems(queuer.getActiveItems())\n setAllItems(queuer.getAllItems())\n setErrorCount(queuer.getErrorCount())\n setIsEmpty(queuer.getIsEmpty())\n setIsFull(queuer.getIsFull())\n setIsIdle(queuer.getIsIdle())\n setPeek(() => queuer.getPeek())\n setPendingItems(queuer.getPendingItems())\n setRejectionCount(queuer.getRejectionCount())\n setSettledCount(queuer.getSettledCount())\n setSize(queuer.getSize())\n setSuccessCount(queuer.getSuccessCount())\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 allItems,\n errorCount,\n isEmpty,\n isFull,\n isIdle,\n isRunning,\n peek,\n pendingItems,\n rejectionCount,\n settledCount,\n size,\n successCount,\n } as SolidAsyncQueuer<TValue>\n}\n"],"names":["asyncQueuer","AsyncQueuer","createSignal","bindInstanceMethods"],"mappings":";;;;;AAiIO,SAAS,kBACd,IACA,iBAA6C,IACnB;AAC1B,QAAMA,gBAAc,IAAIC,wBAAoB,IAAI,cAAc;AAExD,QAAA,CAAC,cAAc,eAAe,IAAIC,QAAA;AAAA,IACtCF,cAAY,gBAAgB;AAAA,EAC9B;AACA,QAAM,CAAC,YAAY,aAAa,IAAIE,QAAAA,aAAaF,cAAY,eAAe;AACtE,QAAA,CAAC,cAAc,eAAe,IAAIE,QAAA;AAAA,IACtCF,cAAY,gBAAgB;AAAA,EAC9B;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;AACM,QAAA,CAAC,aAAa,cAAc,IAAIE,QAAA;AAAA,IACpCF,cAAY,eAAe;AAAA,EAC7B;AACM,QAAA,CAAC,cAAc,eAAe,IAAIE,QAAA;AAAA,IACtCF,cAAY,gBAAgB;AAAA,EAC9B;AACM,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;;AACV,qBAAA,OAAO,gBAAgB;AAC1B,kBAAA,OAAO,aAAa;AAClB,oBAAA,OAAO,eAAe;AACzB,iBAAA,OAAO,YAAY;AACpB,gBAAA,OAAO,WAAW;AAClB,gBAAA,OAAO,WAAW;AACpB,cAAA,MAAM,OAAO,SAAS;AACd,sBAAA,OAAO,iBAAiB;AACtB,wBAAA,OAAO,mBAAmB;AAC5B,sBAAA,OAAO,iBAAiB;AAChC,cAAA,OAAO,SAAS;AACR,sBAAA,OAAO,iBAAiB;AACxC,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,IACA;AAAA,IACA;AAAA,EACF;AACF;;"}
|
|
@@ -1,18 +1,18 @@
|
|
|
1
1
|
import { AsyncQueuer, AsyncQueuerOptions } from '@tanstack/pacer/async-queuer';
|
|
2
2
|
import { Accessor } from 'solid-js';
|
|
3
|
-
export interface SolidAsyncQueuer<TValue> extends Omit<AsyncQueuer<TValue>, 'getActiveItems' | 'getAllItems' | '
|
|
3
|
+
export interface SolidAsyncQueuer<TValue> extends Omit<AsyncQueuer<TValue>, 'getActiveItems' | 'getAllItems' | 'getErrorCount' | 'getIsEmpty' | 'getIsFull' | 'getIsIdle' | 'getIsRunning' | 'getPeek' | 'getPendingItems' | 'getRejectionCount' | 'getSettledCount' | 'getSize' | 'getSuccessCount'> {
|
|
4
4
|
/**
|
|
5
5
|
* Signal version of `getActiveItems`
|
|
6
6
|
*/
|
|
7
|
-
activeItems: Accessor<Array<
|
|
7
|
+
activeItems: Accessor<Array<TValue>>;
|
|
8
8
|
/**
|
|
9
9
|
* Signal version of `getAllItems`
|
|
10
10
|
*/
|
|
11
|
-
allItems: Accessor<Array<
|
|
11
|
+
allItems: Accessor<Array<TValue>>;
|
|
12
12
|
/**
|
|
13
|
-
* Signal version of `
|
|
13
|
+
* Signal version of `getErrorCount`
|
|
14
14
|
*/
|
|
15
|
-
|
|
15
|
+
errorCount: Accessor<number>;
|
|
16
16
|
/**
|
|
17
17
|
* Signal version of `getIsEmpty`
|
|
18
18
|
*/
|
|
@@ -32,47 +32,68 @@ export interface SolidAsyncQueuer<TValue> extends Omit<AsyncQueuer<TValue>, 'get
|
|
|
32
32
|
/**
|
|
33
33
|
* Signal version of `getPeek`
|
|
34
34
|
*/
|
|
35
|
-
peek: Accessor<
|
|
35
|
+
peek: Accessor<TValue | undefined>;
|
|
36
36
|
/**
|
|
37
37
|
* Signal version of `getPendingItems`
|
|
38
38
|
*/
|
|
39
|
-
pendingItems: Accessor<Array<
|
|
39
|
+
pendingItems: Accessor<Array<TValue>>;
|
|
40
40
|
/**
|
|
41
41
|
* Signal version of `getRejectionCount`
|
|
42
42
|
*/
|
|
43
43
|
rejectionCount: Accessor<number>;
|
|
44
|
+
/**
|
|
45
|
+
* Signal version of `getSettledCount`
|
|
46
|
+
*/
|
|
47
|
+
settledCount: Accessor<number>;
|
|
44
48
|
/**
|
|
45
49
|
* Signal version of `getSize`
|
|
46
50
|
*/
|
|
47
51
|
size: Accessor<number>;
|
|
52
|
+
/**
|
|
53
|
+
* Signal version of `getSuccessCount`
|
|
54
|
+
*/
|
|
55
|
+
successCount: Accessor<number>;
|
|
48
56
|
}
|
|
49
57
|
/**
|
|
50
|
-
*
|
|
58
|
+
* Creates a Solid-compatible AsyncQueuer instance for managing an asynchronous queue of items, exposing Solid signals for all stateful properties.
|
|
51
59
|
*
|
|
52
|
-
*
|
|
53
|
-
*
|
|
60
|
+
* Features:
|
|
61
|
+
* - Priority queueing via `getPriority` or item `priority` property
|
|
62
|
+
* - Configurable concurrency limit
|
|
63
|
+
* - FIFO (First In First Out) or LIFO (Last In First Out) queue behavior
|
|
64
|
+
* - Pause/resume processing
|
|
65
|
+
* - Task cancellation
|
|
66
|
+
* - Item expiration
|
|
67
|
+
* - Lifecycle callbacks for success, error, settled, items change, etc.
|
|
68
|
+
* - All stateful properties (active items, pending items, counts, etc.) are exposed as Solid signals for reactivity
|
|
54
69
|
*
|
|
55
|
-
*
|
|
56
|
-
*
|
|
57
|
-
* - Maximum queue size
|
|
58
|
-
* - Processing function for queue items
|
|
59
|
-
* - Various lifecycle callbacks
|
|
70
|
+
* Tasks are processed concurrently up to the configured concurrency limit. When a task completes,
|
|
71
|
+
* the next pending task is processed if the concurrency limit allows.
|
|
60
72
|
*
|
|
61
|
-
*
|
|
62
|
-
* -
|
|
63
|
-
* -
|
|
64
|
-
* -
|
|
65
|
-
* -
|
|
66
|
-
* -
|
|
73
|
+
* Error Handling:
|
|
74
|
+
* - If an `onError` handler is provided, it will be called with the error and queuer instance
|
|
75
|
+
* - If `throwOnError` is true (default when no onError handler is provided), the error will be thrown
|
|
76
|
+
* - If `throwOnError` is false (default when onError handler is provided), the error will be swallowed
|
|
77
|
+
* - Both onError and throwOnError can be used together; the handler will be called before any error is thrown
|
|
78
|
+
* - The error state can be checked using the underlying AsyncQueuer instance
|
|
67
79
|
*
|
|
68
|
-
*
|
|
80
|
+
* Example usage:
|
|
69
81
|
* ```tsx
|
|
70
82
|
* // Basic async queuer for API requests
|
|
71
|
-
* const asyncQueuer = createAsyncQueuer({
|
|
83
|
+
* const asyncQueuer = createAsyncQueuer(async (item) => {
|
|
84
|
+
* // process item
|
|
85
|
+
* return await fetchData(item);
|
|
86
|
+
* }, {
|
|
72
87
|
* initialItems: [],
|
|
73
88
|
* concurrency: 2,
|
|
74
89
|
* maxSize: 100,
|
|
75
90
|
* started: false,
|
|
91
|
+
* onSuccess: (result) => {
|
|
92
|
+
* console.log('Item processed:', result);
|
|
93
|
+
* },
|
|
94
|
+
* onError: (error) => {
|
|
95
|
+
* console.error('Processing failed:', error);
|
|
96
|
+
* }
|
|
76
97
|
* });
|
|
77
98
|
*
|
|
78
99
|
* // Add items to queue
|
|
@@ -81,14 +102,8 @@ export interface SolidAsyncQueuer<TValue> extends Omit<AsyncQueuer<TValue>, 'get
|
|
|
81
102
|
* // Start processing
|
|
82
103
|
* asyncQueuer.start();
|
|
83
104
|
*
|
|
84
|
-
* //
|
|
85
|
-
* asyncQueuer.
|
|
86
|
-
* console.log('Item processed:', result);
|
|
87
|
-
* });
|
|
88
|
-
*
|
|
89
|
-
* asyncQueuer.onError((error) => {
|
|
90
|
-
* console.error('Processing failed:', error);
|
|
91
|
-
* });
|
|
105
|
+
* // Use Solid signals in your UI
|
|
106
|
+
* const pending = asyncQueuer.pendingItems();
|
|
92
107
|
* ```
|
|
93
108
|
*/
|
|
94
|
-
export declare function createAsyncQueuer<TValue>(initialOptions?: AsyncQueuerOptions<TValue>): SolidAsyncQueuer<TValue>;
|
|
109
|
+
export declare function createAsyncQueuer<TValue>(fn: (value: TValue) => Promise<any>, initialOptions?: AsyncQueuerOptions<TValue>): SolidAsyncQueuer<TValue>;
|
|
@@ -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<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
|
|
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 is a simple approach that allows a function to execute up to a limit within a time window,\n * then blocks all subsequent calls until the window passes. This can lead to \"bursty\" behavior where\n * all executions happen immediately, followed by a complete block.\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 * 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 * For smoother execution patterns, consider using:\n * - Throttling: Ensures consistent spacing between executions (e.g. max once per 200ms)\n * - Debouncing: Waits for a pause in calls before executing (e.g. after 500ms of no calls)\n *\n * Rate limiting is best used for hard API limits or resource constraints. For UI updates or\n * smoothing out frequent events, throttling or debouncing usually provide better user experience.\n *\n * Error Handling:\n * - If an `onError` handler is provided, it will be called with the error and rate limiter instance\n * - If `throwOnError` is true (default when no onError handler is provided), the error will be thrown\n * - If `throwOnError` is false (default when onError handler is provided), the error will be swallowed\n * - Both onError and throwOnError can be used together - the handler will be called before any error is thrown\n * - The error state can be checked using the underlying AsyncRateLimiter instance\n * - Rate limit rejections (when limit is exceeded) are handled separately from execution errors via the `onReject` handler\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":";;;;;AAuFgB,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;;"}
|
|
@@ -15,13 +15,9 @@ export interface SolidAsyncRateLimiter<TFn extends AnyAsyncFunction> extends Omi
|
|
|
15
15
|
* This hook is designed to be flexible and state-management agnostic - it simply returns a rate limiter instance that
|
|
16
16
|
* you can integrate with any state management solution (createSignal, etc).
|
|
17
17
|
*
|
|
18
|
-
* Rate limiting allows
|
|
19
|
-
* then blocks subsequent calls until the window passes. This
|
|
20
|
-
*
|
|
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.
|
|
18
|
+
* Rate limiting is a simple approach that allows a function to execute up to a limit within a time window,
|
|
19
|
+
* then blocks all subsequent calls until the window passes. This can lead to "bursty" behavior where
|
|
20
|
+
* all executions happen immediately, followed by a complete block.
|
|
25
21
|
*
|
|
26
22
|
* The rate limiter supports two types of windows:
|
|
27
23
|
* - 'fixed': A strict window that resets after the window period. All executions within the window count
|
|
@@ -29,6 +25,25 @@ export interface SolidAsyncRateLimiter<TFn extends AnyAsyncFunction> extends Omi
|
|
|
29
25
|
* - 'sliding': A rolling window that allows executions as old ones expire. This provides a more
|
|
30
26
|
* consistent rate of execution over time.
|
|
31
27
|
*
|
|
28
|
+
* Unlike the non-async RateLimiter, this async version supports returning values from the rate-limited function,
|
|
29
|
+
* making it ideal for API calls and other async operations where you want the result of the `maybeExecute` call
|
|
30
|
+
* instead of setting the result on a state variable from within the rate-limited function.
|
|
31
|
+
*
|
|
32
|
+
* For smoother execution patterns, consider using:
|
|
33
|
+
* - Throttling: Ensures consistent spacing between executions (e.g. max once per 200ms)
|
|
34
|
+
* - Debouncing: Waits for a pause in calls before executing (e.g. after 500ms of no calls)
|
|
35
|
+
*
|
|
36
|
+
* Rate limiting is best used for hard API limits or resource constraints. For UI updates or
|
|
37
|
+
* smoothing out frequent events, throttling or debouncing usually provide better user experience.
|
|
38
|
+
*
|
|
39
|
+
* Error Handling:
|
|
40
|
+
* - If an `onError` handler is provided, it will be called with the error and rate limiter instance
|
|
41
|
+
* - If `throwOnError` is true (default when no onError handler is provided), the error will be thrown
|
|
42
|
+
* - If `throwOnError` is false (default when onError handler is provided), the error will be swallowed
|
|
43
|
+
* - Both onError and throwOnError can be used together - the handler will be called before any error is thrown
|
|
44
|
+
* - The error state can be checked using the underlying AsyncRateLimiter instance
|
|
45
|
+
* - Rate limit rejections (when limit is exceeded) are handled separately from execution errors via the `onReject` handler
|
|
46
|
+
*
|
|
32
47
|
* @example
|
|
33
48
|
* ```tsx
|
|
34
49
|
* // Basic API call rate limiting with return value
|
|
@@ -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<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
|
|
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 * Unlike the non-async Throttler, this async version supports returning values from the throttled 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 throttled function.\n *\n * Error Handling:\n * - If an `onError` handler is provided, it will be called with the error and throttler instance\n * - If `throwOnError` is true (default when no onError handler is provided), the error will be thrown\n * - If `throwOnError` is false (default when onError handler is provided), the error will be swallowed\n * - Both onError and throwOnError can be used together - the handler will be called before any error is thrown\n * - The error state can be checked using the underlying AsyncThrottler instance\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 * onError: (error) => {\n * console.error('API call failed:', error);\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":";;;;;AA+EgB,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;;"}
|
|
@@ -21,6 +21,17 @@ export interface SolidAsyncThrottler<TFn extends AnyAsyncFunction> extends Omit<
|
|
|
21
21
|
* regardless of how many times it is called. This is useful for rate-limiting expensive API calls,
|
|
22
22
|
* database operations, or other async tasks.
|
|
23
23
|
*
|
|
24
|
+
* Unlike the non-async Throttler, this async version supports returning values from the throttled function,
|
|
25
|
+
* making it ideal for API calls and other async operations where you want the result of the `maybeExecute` call
|
|
26
|
+
* instead of setting the result on a state variable from within the throttled function.
|
|
27
|
+
*
|
|
28
|
+
* Error Handling:
|
|
29
|
+
* - If an `onError` handler is provided, it will be called with the error and throttler instance
|
|
30
|
+
* - If `throwOnError` is true (default when no onError handler is provided), the error will be thrown
|
|
31
|
+
* - If `throwOnError` is false (default when onError handler is provided), the error will be swallowed
|
|
32
|
+
* - Both onError and throwOnError can be used together - the handler will be called before any error is thrown
|
|
33
|
+
* - The error state can be checked using the underlying AsyncThrottler instance
|
|
34
|
+
*
|
|
24
35
|
* @example
|
|
25
36
|
* ```tsx
|
|
26
37
|
* // Basic API call throttling
|
|
@@ -42,7 +53,10 @@ export interface SolidAsyncThrottler<TFn extends AnyAsyncFunction> extends Omit<
|
|
|
42
53
|
* {
|
|
43
54
|
* wait: 2000,
|
|
44
55
|
* leading: true, // Execute immediately on first call
|
|
45
|
-
* trailing: false
|
|
56
|
+
* trailing: false, // Skip trailing edge updates
|
|
57
|
+
* onError: (error) => {
|
|
58
|
+
* console.error('API call failed:', error);
|
|
59
|
+
* }
|
|
46
60
|
* }
|
|
47
61
|
* );
|
|
48
62
|
* ```
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const batcher = require("@tanstack/pacer/batcher");
|
|
4
|
+
const solidJs = require("solid-js");
|
|
5
|
+
const utils = require("@tanstack/pacer/utils");
|
|
6
|
+
function createBatcher(fn, initialOptions = {}) {
|
|
7
|
+
const batcher$1 = utils.bindInstanceMethods(new batcher.Batcher(fn, initialOptions));
|
|
8
|
+
const [allItems, setAllItems] = solidJs.createSignal(
|
|
9
|
+
batcher$1.getAllItems()
|
|
10
|
+
);
|
|
11
|
+
const [batchExecutionCount, setBatchExecutionCount] = solidJs.createSignal(
|
|
12
|
+
batcher$1.getBatchExecutionCount()
|
|
13
|
+
);
|
|
14
|
+
const [itemExecutionCount, setItemExecutionCount] = solidJs.createSignal(
|
|
15
|
+
batcher$1.getItemExecutionCount()
|
|
16
|
+
);
|
|
17
|
+
const [isEmpty, setIsEmpty] = solidJs.createSignal(batcher$1.getIsEmpty());
|
|
18
|
+
const [isRunning, setIsRunning] = solidJs.createSignal(batcher$1.getIsRunning());
|
|
19
|
+
const [size, setSize] = solidJs.createSignal(batcher$1.getSize());
|
|
20
|
+
function setOptions(newOptions) {
|
|
21
|
+
batcher$1.setOptions({
|
|
22
|
+
...newOptions,
|
|
23
|
+
onItemsChange: (batcher2) => {
|
|
24
|
+
setAllItems(batcher2.getAllItems());
|
|
25
|
+
setBatchExecutionCount(batcher2.getBatchExecutionCount());
|
|
26
|
+
setItemExecutionCount(batcher2.getItemExecutionCount());
|
|
27
|
+
setIsEmpty(batcher2.getIsEmpty());
|
|
28
|
+
setSize(batcher2.getSize());
|
|
29
|
+
const onItemsChange = newOptions.onItemsChange ?? initialOptions.onItemsChange;
|
|
30
|
+
onItemsChange == null ? void 0 : onItemsChange(batcher2);
|
|
31
|
+
},
|
|
32
|
+
onExecute: (batcher2) => {
|
|
33
|
+
setBatchExecutionCount(batcher2.getBatchExecutionCount());
|
|
34
|
+
setItemExecutionCount(batcher2.getItemExecutionCount());
|
|
35
|
+
const onExecute = newOptions.onExecute ?? initialOptions.onExecute;
|
|
36
|
+
onExecute == null ? void 0 : onExecute(batcher2);
|
|
37
|
+
},
|
|
38
|
+
onIsRunningChange: (batcher2) => {
|
|
39
|
+
setIsRunning(batcher2.getIsRunning());
|
|
40
|
+
const onIsRunningChange = newOptions.onIsRunningChange ?? initialOptions.onIsRunningChange;
|
|
41
|
+
onIsRunningChange == null ? void 0 : onIsRunningChange(batcher2);
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
setOptions(initialOptions);
|
|
46
|
+
return {
|
|
47
|
+
...batcher$1,
|
|
48
|
+
allItems,
|
|
49
|
+
batchExecutionCount,
|
|
50
|
+
isEmpty,
|
|
51
|
+
isRunning,
|
|
52
|
+
itemExecutionCount,
|
|
53
|
+
size,
|
|
54
|
+
setOptions
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
exports.createBatcher = createBatcher;
|
|
58
|
+
//# sourceMappingURL=createBatcher.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createBatcher.cjs","sources":["../../../src/batcher/createBatcher.ts"],"sourcesContent":["import { Batcher } from '@tanstack/pacer/batcher'\nimport { createSignal } from 'solid-js'\nimport { bindInstanceMethods } from '@tanstack/pacer/utils'\nimport type { Accessor } from 'solid-js'\nimport type { BatcherOptions } from '@tanstack/pacer/batcher'\n\nexport interface SolidBatcher<TValue>\n extends Omit<\n Batcher<TValue>,\n | 'getAllItems'\n | 'getBatchExecutionCount'\n | 'getIsEmpty'\n | 'getIsRunning'\n | 'getItemExecutionCount'\n | 'getSize'\n > {\n /**\n * Signal version of `getAllItems`\n */\n allItems: Accessor<Array<TValue>>\n /**\n * Signal version of `getBatchExecutionCount`\n */\n batchExecutionCount: Accessor<number>\n /**\n * Signal version of `getIsEmpty`\n */\n isEmpty: Accessor<boolean>\n /**\n * Signal version of `getIsRunning`\n */\n isRunning: Accessor<boolean>\n /**\n * Signal version of `getItemExecutionCount`\n */\n itemExecutionCount: Accessor<number>\n /**\n * Signal version of `getSize`\n */\n size: Accessor<number>\n}\n\n/**\n * Creates a Solid-compatible Batcher instance for managing batches of items, exposing Solid signals for all stateful properties.\n *\n * Features:\n * - Batch processing of items using the provided `fn` function\n * - Configurable batch size and wait time\n * - Custom batch processing logic via getShouldExecute\n * - Event callbacks for monitoring batch operations\n * - All stateful properties (items, counts, etc.) are exposed as Solid signals for reactivity\n *\n * The batcher collects items and processes them in batches based on:\n * - Maximum batch size\n * - Time-based batching (process after X milliseconds)\n * - Custom batch processing logic via getShouldExecute\n *\n * Example usage:\n * ```tsx\n * const batcher = createBatcher(\n * (items) => {\n * // Process batch of items\n * console.log('Processing batch:', items);\n * },\n * {\n * maxSize: 5,\n * wait: 2000,\n * onExecute: (batcher) => console.log('Batch executed'),\n * getShouldExecute: (items) => items.length >= 3\n * }\n * );\n *\n * // Add items to batch\n * batcher.addItem('task1');\n * batcher.addItem('task2');\n *\n * // Control the batcher\n * batcher.stop(); // Pause processing\n * batcher.start(); // Resume processing\n *\n * // Access batcher state via signals\n * console.log('Items:', batcher.allItems());\n * console.log('Size:', batcher.size());\n * console.log('Is empty:', batcher.isEmpty());\n * console.log('Is running:', batcher.isRunning());\n * console.log('Batch count:', batcher.batchExecutionCount());\n * console.log('Item count:', batcher.itemExecutionCount());\n * ```\n */\nexport function createBatcher<TValue>(\n fn: (items: Array<TValue>) => void,\n initialOptions: BatcherOptions<TValue> = {},\n): SolidBatcher<TValue> {\n const batcher = bindInstanceMethods(new Batcher<TValue>(fn, initialOptions))\n\n const [allItems, setAllItems] = createSignal<Array<TValue>>(\n batcher.getAllItems(),\n )\n const [batchExecutionCount, setBatchExecutionCount] = createSignal(\n batcher.getBatchExecutionCount(),\n )\n const [itemExecutionCount, setItemExecutionCount] = createSignal(\n batcher.getItemExecutionCount(),\n )\n const [isEmpty, setIsEmpty] = createSignal(batcher.getIsEmpty())\n const [isRunning, setIsRunning] = createSignal(batcher.getIsRunning())\n const [size, setSize] = createSignal(batcher.getSize())\n\n function setOptions(newOptions: Partial<BatcherOptions<TValue>>) {\n batcher.setOptions({\n ...newOptions,\n onItemsChange: (batcher) => {\n setAllItems(batcher.getAllItems())\n setBatchExecutionCount(batcher.getBatchExecutionCount())\n setItemExecutionCount(batcher.getItemExecutionCount())\n setIsEmpty(batcher.getIsEmpty())\n setSize(batcher.getSize())\n\n const onItemsChange =\n newOptions.onItemsChange ?? initialOptions.onItemsChange\n onItemsChange?.(batcher)\n },\n onExecute: (batcher) => {\n setBatchExecutionCount(batcher.getBatchExecutionCount())\n setItemExecutionCount(batcher.getItemExecutionCount())\n\n const onExecute = newOptions.onExecute ?? initialOptions.onExecute\n onExecute?.(batcher)\n },\n onIsRunningChange: (batcher) => {\n setIsRunning(batcher.getIsRunning())\n\n const onIsRunningChange =\n newOptions.onIsRunningChange ?? initialOptions.onIsRunningChange\n onIsRunningChange?.(batcher)\n },\n })\n }\n\n setOptions(initialOptions)\n\n return {\n ...batcher,\n allItems,\n batchExecutionCount,\n isEmpty,\n isRunning,\n itemExecutionCount,\n size,\n setOptions,\n } as SolidBatcher<TValue>\n}\n"],"names":["batcher","bindInstanceMethods","Batcher","createSignal"],"mappings":";;;;;AAyFO,SAAS,cACd,IACA,iBAAyC,IACnB;AACtB,QAAMA,YAAUC,MAAAA,oBAAoB,IAAIC,QAAgB,QAAA,IAAI,cAAc,CAAC;AAErE,QAAA,CAAC,UAAU,WAAW,IAAIC,QAAA;AAAA,IAC9BH,UAAQ,YAAY;AAAA,EACtB;AACM,QAAA,CAAC,qBAAqB,sBAAsB,IAAIG,QAAA;AAAA,IACpDH,UAAQ,uBAAuB;AAAA,EACjC;AACM,QAAA,CAAC,oBAAoB,qBAAqB,IAAIG,QAAA;AAAA,IAClDH,UAAQ,sBAAsB;AAAA,EAChC;AACA,QAAM,CAAC,SAAS,UAAU,IAAIG,QAAAA,aAAaH,UAAQ,YAAY;AAC/D,QAAM,CAAC,WAAW,YAAY,IAAIG,QAAAA,aAAaH,UAAQ,cAAc;AACrE,QAAM,CAAC,MAAM,OAAO,IAAIG,QAAAA,aAAaH,UAAQ,SAAS;AAEtD,WAAS,WAAW,YAA6C;AAC/DA,cAAQ,WAAW;AAAA,MACjB,GAAG;AAAA,MACH,eAAe,CAACA,aAAY;AACdA,oBAAAA,SAAQ,aAAa;AACVA,+BAAAA,SAAQ,wBAAwB;AACjCA,8BAAAA,SAAQ,uBAAuB;AAC1CA,mBAAAA,SAAQ,YAAY;AACvBA,gBAAAA,SAAQ,SAAS;AAEnB,cAAA,gBACJ,WAAW,iBAAiB,eAAe;AAC7C,uDAAgBA;AAAAA,MAClB;AAAA,MACA,WAAW,CAACA,aAAY;AACCA,+BAAAA,SAAQ,wBAAwB;AACjCA,8BAAAA,SAAQ,uBAAuB;AAE/C,cAAA,YAAY,WAAW,aAAa,eAAe;AACzD,+CAAYA;AAAAA,MACd;AAAA,MACA,mBAAmB,CAACA,aAAY;AACjBA,qBAAAA,SAAQ,cAAc;AAE7B,cAAA,oBACJ,WAAW,qBAAqB,eAAe;AACjD,+DAAoBA;AAAAA,MAAO;AAAA,IAC7B,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,EACF;AACF;;"}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { Batcher, BatcherOptions } from '@tanstack/pacer/batcher';
|
|
2
|
+
import { Accessor } from 'solid-js';
|
|
3
|
+
export interface SolidBatcher<TValue> extends Omit<Batcher<TValue>, 'getAllItems' | 'getBatchExecutionCount' | 'getIsEmpty' | 'getIsRunning' | 'getItemExecutionCount' | 'getSize'> {
|
|
4
|
+
/**
|
|
5
|
+
* Signal version of `getAllItems`
|
|
6
|
+
*/
|
|
7
|
+
allItems: Accessor<Array<TValue>>;
|
|
8
|
+
/**
|
|
9
|
+
* Signal version of `getBatchExecutionCount`
|
|
10
|
+
*/
|
|
11
|
+
batchExecutionCount: Accessor<number>;
|
|
12
|
+
/**
|
|
13
|
+
* Signal version of `getIsEmpty`
|
|
14
|
+
*/
|
|
15
|
+
isEmpty: Accessor<boolean>;
|
|
16
|
+
/**
|
|
17
|
+
* Signal version of `getIsRunning`
|
|
18
|
+
*/
|
|
19
|
+
isRunning: Accessor<boolean>;
|
|
20
|
+
/**
|
|
21
|
+
* Signal version of `getItemExecutionCount`
|
|
22
|
+
*/
|
|
23
|
+
itemExecutionCount: Accessor<number>;
|
|
24
|
+
/**
|
|
25
|
+
* Signal version of `getSize`
|
|
26
|
+
*/
|
|
27
|
+
size: Accessor<number>;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Creates a Solid-compatible Batcher instance for managing batches of items, exposing Solid signals for all stateful properties.
|
|
31
|
+
*
|
|
32
|
+
* Features:
|
|
33
|
+
* - Batch processing of items using the provided `fn` function
|
|
34
|
+
* - Configurable batch size and wait time
|
|
35
|
+
* - Custom batch processing logic via getShouldExecute
|
|
36
|
+
* - Event callbacks for monitoring batch operations
|
|
37
|
+
* - All stateful properties (items, counts, etc.) are exposed as Solid signals for reactivity
|
|
38
|
+
*
|
|
39
|
+
* The batcher collects items and processes them in batches based on:
|
|
40
|
+
* - Maximum batch size
|
|
41
|
+
* - Time-based batching (process after X milliseconds)
|
|
42
|
+
* - Custom batch processing logic via getShouldExecute
|
|
43
|
+
*
|
|
44
|
+
* Example usage:
|
|
45
|
+
* ```tsx
|
|
46
|
+
* const batcher = createBatcher(
|
|
47
|
+
* (items) => {
|
|
48
|
+
* // Process batch of items
|
|
49
|
+
* console.log('Processing batch:', items);
|
|
50
|
+
* },
|
|
51
|
+
* {
|
|
52
|
+
* maxSize: 5,
|
|
53
|
+
* wait: 2000,
|
|
54
|
+
* onExecute: (batcher) => console.log('Batch executed'),
|
|
55
|
+
* getShouldExecute: (items) => items.length >= 3
|
|
56
|
+
* }
|
|
57
|
+
* );
|
|
58
|
+
*
|
|
59
|
+
* // Add items to batch
|
|
60
|
+
* batcher.addItem('task1');
|
|
61
|
+
* batcher.addItem('task2');
|
|
62
|
+
*
|
|
63
|
+
* // Control the batcher
|
|
64
|
+
* batcher.stop(); // Pause processing
|
|
65
|
+
* batcher.start(); // Resume processing
|
|
66
|
+
*
|
|
67
|
+
* // Access batcher state via signals
|
|
68
|
+
* console.log('Items:', batcher.allItems());
|
|
69
|
+
* console.log('Size:', batcher.size());
|
|
70
|
+
* console.log('Is empty:', batcher.isEmpty());
|
|
71
|
+
* console.log('Is running:', batcher.isRunning());
|
|
72
|
+
* console.log('Batch count:', batcher.batchExecutionCount());
|
|
73
|
+
* console.log('Item count:', batcher.itemExecutionCount());
|
|
74
|
+
* ```
|
|
75
|
+
*/
|
|
76
|
+
export declare function createBatcher<TValue>(fn: (items: Array<TValue>) => void, initialOptions?: BatcherOptions<TValue>): SolidBatcher<TValue>;
|