@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
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const batcher = require("@tanstack/pacer/batcher");
|
|
4
|
+
const createBatcher = require("./createBatcher.cjs");
|
|
5
|
+
exports.createBatcher = createBatcher.createBatcher;
|
|
6
|
+
Object.keys(batcher).forEach((k) => {
|
|
7
|
+
if (k !== "default" && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
|
|
8
|
+
enumerable: true,
|
|
9
|
+
get: () => batcher[k]
|
|
10
|
+
});
|
|
11
|
+
});
|
|
12
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;"}
|
package/dist/cjs/index.cjs
CHANGED
|
@@ -5,6 +5,7 @@ const createAsyncDebouncer = require("./async-debouncer/createAsyncDebouncer.cjs
|
|
|
5
5
|
const createAsyncQueuer = require("./async-queuer/createAsyncQueuer.cjs");
|
|
6
6
|
const createAsyncRateLimiter = require("./async-rate-limiter/createAsyncRateLimiter.cjs");
|
|
7
7
|
const createAsyncThrottler = require("./async-throttler/createAsyncThrottler.cjs");
|
|
8
|
+
const createBatcher = require("./batcher/createBatcher.cjs");
|
|
8
9
|
const createDebouncedSignal = require("./debouncer/createDebouncedSignal.cjs");
|
|
9
10
|
const createDebouncedValue = require("./debouncer/createDebouncedValue.cjs");
|
|
10
11
|
const createDebouncer = require("./debouncer/createDebouncer.cjs");
|
|
@@ -19,6 +20,7 @@ exports.createAsyncDebouncer = createAsyncDebouncer.createAsyncDebouncer;
|
|
|
19
20
|
exports.createAsyncQueuer = createAsyncQueuer.createAsyncQueuer;
|
|
20
21
|
exports.createAsyncRateLimiter = createAsyncRateLimiter.createAsyncRateLimiter;
|
|
21
22
|
exports.createAsyncThrottler = createAsyncThrottler.createAsyncThrottler;
|
|
23
|
+
exports.createBatcher = createBatcher.createBatcher;
|
|
22
24
|
exports.createDebouncedSignal = createDebouncedSignal.createDebouncedSignal;
|
|
23
25
|
exports.createDebouncedValue = createDebouncedValue.createDebouncedValue;
|
|
24
26
|
exports.createDebouncer = createDebouncer.createDebouncer;
|
package/dist/cjs/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
package/dist/cjs/index.d.cts
CHANGED
|
@@ -6,6 +6,7 @@ export * from './async-debouncer/createAsyncDebouncer.cjs';
|
|
|
6
6
|
export * from './async-queuer/createAsyncQueuer.cjs';
|
|
7
7
|
export * from './async-rate-limiter/createAsyncRateLimiter.cjs';
|
|
8
8
|
export * from './async-throttler/createAsyncThrottler.cjs';
|
|
9
|
+
export * from './batcher/createBatcher.cjs';
|
|
9
10
|
export * from './debouncer/createDebouncedSignal.cjs';
|
|
10
11
|
export * from './debouncer/createDebouncedValue.cjs';
|
|
11
12
|
export * from './debouncer/createDebouncer.cjs';
|
|
@@ -3,8 +3,8 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
|
3
3
|
const queuer = require("@tanstack/pacer/queuer");
|
|
4
4
|
const solidJs = require("solid-js");
|
|
5
5
|
const utils = require("@tanstack/pacer/utils");
|
|
6
|
-
function createQueuer(initialOptions = {}) {
|
|
7
|
-
const queuer$1 = utils.bindInstanceMethods(new queuer.Queuer(initialOptions));
|
|
6
|
+
function createQueuer(fn, initialOptions = {}) {
|
|
7
|
+
const queuer$1 = utils.bindInstanceMethods(new queuer.Queuer(fn, initialOptions));
|
|
8
8
|
const [allItems, setAllItems] = solidJs.createSignal(
|
|
9
9
|
queuer$1.getAllItems()
|
|
10
10
|
);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createQueuer.cjs","sources":["../../../src/queuer/createQueuer.ts"],"sourcesContent":["import { Queuer } from '@tanstack/pacer/queuer'\nimport { createSignal } from 'solid-js'\nimport { bindInstanceMethods } from '@tanstack/pacer/utils'\nimport type { Accessor } from 'solid-js'\nimport type { QueuerOptions } from '@tanstack/pacer/queuer'\n\nexport interface SolidQueuer<TValue>\n extends Omit<\n Queuer<TValue>,\n | 'getAllItems'\n | 'getExecutionCount'\n | 'getIsEmpty'\n | 'getIsFull'\n | 'getIsIdle'\n | 'getIsRunning'\n | 'getPeek'\n | 'getSize'\n > {\n /**\n * Signal version of `getAllItems`\n */\n allItems: Accessor<Array<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 `getRejectionCount`\n */\n rejectionCount: Accessor<number>\n /**\n * Signal version of `getSize`\n */\n size: Accessor<number>\n}\n\n/**\n *
|
|
1
|
+
{"version":3,"file":"createQueuer.cjs","sources":["../../../src/queuer/createQueuer.ts"],"sourcesContent":["import { Queuer } from '@tanstack/pacer/queuer'\nimport { createSignal } from 'solid-js'\nimport { bindInstanceMethods } from '@tanstack/pacer/utils'\nimport type { Accessor } from 'solid-js'\nimport type { QueuerOptions } from '@tanstack/pacer/queuer'\n\nexport interface SolidQueuer<TValue>\n extends Omit<\n Queuer<TValue>,\n | 'getAllItems'\n | 'getExecutionCount'\n | 'getIsEmpty'\n | 'getIsFull'\n | 'getIsIdle'\n | 'getIsRunning'\n | 'getPeek'\n | 'getSize'\n > {\n /**\n * Signal version of `getAllItems`\n */\n allItems: Accessor<Array<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 `getRejectionCount`\n */\n rejectionCount: Accessor<number>\n /**\n * Signal version of `getSize`\n */\n size: Accessor<number>\n}\n\n/**\n * Creates a Solid-compatible Queuer instance for managing a synchronous queue of items, exposing Solid signals for all stateful properties.\n *\n * Features:\n * - Synchronous processing of items using the provided `fn` function\n * - FIFO (First In First Out) or LIFO (Last In First Out) queue behavior\n * - Priority queueing via `getPriority` or item `priority` property\n * - Item expiration and removal of stale items\n * - Configurable wait time between processing items\n * - Pause/resume processing\n * - Callbacks for queue state changes, execution, rejection, and expiration\n * - All stateful properties (items, counts, etc.) are exposed as Solid signals for reactivity\n *\n * The queue processes items synchronously in order, with optional delays between each item. When started, it will process one item per tick, with an optional wait time between ticks. You can pause and resume processing with `stop()` and `start()`.\n *\n * By default, the queue uses FIFO behavior, but you can configure LIFO or double-ended queueing by specifying the position when adding or removing items.\n *\n * Example usage:\n * ```tsx\n * // Example with Solid signals and scheduling\n * const [items, setItems] = createSignal([]);\n *\n * const queue = createQueuer(\n * (item) => {\n * // process item synchronously\n * console.log('Processing', item);\n * },\n * {\n * started: true, // Start processing immediately\n * wait: 1000, // Process one item every second\n * onItemsChange: (queue) => setItems(queue.getAllItems()),\n * getPriority: (item) => item.priority // Process higher priority items first\n * }\n * );\n *\n * // Add items to process - they'll be handled automatically\n * queue.addItem('task1');\n * queue.addItem('task2');\n *\n * // Control the scheduler\n * queue.stop(); // Pause processing\n * queue.start(); // Resume processing\n *\n * // Access queue state via signals\n * console.log('Items:', queue.allItems());\n * console.log('Size:', queue.size());\n * console.log('Is empty:', queue.isEmpty());\n * console.log('Is running:', queue.isRunning());\n * console.log('Next item:', queue.peek());\n * ```\n */\nexport function createQueuer<TValue>(\n fn: (item: TValue) => void,\n initialOptions: QueuerOptions<TValue> = {},\n): SolidQueuer<TValue> {\n const queuer = bindInstanceMethods(new Queuer<TValue>(fn, initialOptions))\n\n const [allItems, setAllItems] = createSignal<Array<TValue>>(\n queuer.getAllItems(),\n )\n const [executionCount, setExecutionCount] = createSignal(\n queuer.getExecutionCount(),\n )\n const [rejectionCount, setRejectionCount] = createSignal(\n queuer.getRejectionCount(),\n )\n const [isEmpty, setIsEmpty] = createSignal(queuer.getIsEmpty())\n const [isFull, setIsFull] = createSignal(queuer.getIsFull())\n const [isIdle, setIsIdle] = createSignal(queuer.getIsIdle())\n const [isRunning, setIsRunning] = createSignal(queuer.getIsRunning())\n const [peek, setPeek] = createSignal<TValue | undefined>(queuer.getPeek())\n const [size, setSize] = createSignal(queuer.getSize())\n\n function setOptions(newOptions: Partial<QueuerOptions<TValue>>) {\n queuer.setOptions({\n ...newOptions,\n onItemsChange: (queuer) => {\n setAllItems(queuer.getAllItems())\n setExecutionCount(queuer.getExecutionCount())\n setIsEmpty(queuer.getIsEmpty())\n setIsFull(queuer.getIsFull())\n setIsIdle(queuer.getIsIdle())\n setPeek(() => queuer.getPeek())\n setSize(queuer.getSize())\n\n const onItemsChange =\n newOptions.onItemsChange ?? initialOptions.onItemsChange\n onItemsChange?.(queuer)\n },\n onIsRunningChange: (queuer) => {\n setIsRunning(queuer.getIsRunning())\n setIsIdle(queuer.getIsIdle())\n\n const onIsRunningChange =\n newOptions.onIsRunningChange ?? initialOptions.onIsRunningChange\n onIsRunningChange?.(queuer)\n },\n onReject: (item, queuer) => {\n setRejectionCount(queuer.getRejectionCount())\n\n const onReject = newOptions.onReject ?? initialOptions.onReject\n onReject?.(item, queuer)\n },\n })\n }\n\n setOptions(initialOptions)\n\n return {\n ...queuer,\n allItems,\n executionCount,\n isEmpty,\n isFull,\n isIdle,\n isRunning,\n peek,\n rejectionCount,\n size,\n setOptions,\n } as SolidQueuer<TValue>\n}\n"],"names":["queuer","bindInstanceMethods","Queuer","createSignal"],"mappings":";;;;;AA2GO,SAAS,aACd,IACA,iBAAwC,IACnB;AACrB,QAAMA,WAASC,MAAAA,oBAAoB,IAAIC,OAAe,OAAA,IAAI,cAAc,CAAC;AAEnE,QAAA,CAAC,UAAU,WAAW,IAAIC,QAAA;AAAA,IAC9BH,SAAO,YAAY;AAAA,EACrB;AACM,QAAA,CAAC,gBAAgB,iBAAiB,IAAIG,QAAA;AAAA,IAC1CH,SAAO,kBAAkB;AAAA,EAC3B;AACM,QAAA,CAAC,gBAAgB,iBAAiB,IAAIG,QAAA;AAAA,IAC1CH,SAAO,kBAAkB;AAAA,EAC3B;AACA,QAAM,CAAC,SAAS,UAAU,IAAIG,QAAAA,aAAaH,SAAO,YAAY;AAC9D,QAAM,CAAC,QAAQ,SAAS,IAAIG,QAAAA,aAAaH,SAAO,WAAW;AAC3D,QAAM,CAAC,QAAQ,SAAS,IAAIG,QAAAA,aAAaH,SAAO,WAAW;AAC3D,QAAM,CAAC,WAAW,YAAY,IAAIG,QAAAA,aAAaH,SAAO,cAAc;AACpE,QAAM,CAAC,MAAM,OAAO,IAAIG,QAAAA,aAAiCH,SAAO,SAAS;AACzE,QAAM,CAAC,MAAM,OAAO,IAAIG,QAAAA,aAAaH,SAAO,SAAS;AAErD,WAAS,WAAW,YAA4C;AAC9DA,aAAO,WAAW;AAAA,MAChB,GAAG;AAAA,MACH,eAAe,CAACA,YAAW;AACbA,oBAAAA,QAAO,aAAa;AACdA,0BAAAA,QAAO,mBAAmB;AACjCA,mBAAAA,QAAO,YAAY;AACpBA,kBAAAA,QAAO,WAAW;AAClBA,kBAAAA,QAAO,WAAW;AACpB,gBAAA,MAAMA,QAAO,SAAS;AACtBA,gBAAAA,QAAO,SAAS;AAElB,cAAA,gBACJ,WAAW,iBAAiB,eAAe;AAC7C,uDAAgBA;AAAAA,MAClB;AAAA,MACA,mBAAmB,CAACA,YAAW;AAChBA,qBAAAA,QAAO,cAAc;AACxBA,kBAAAA,QAAO,WAAW;AAEtB,cAAA,oBACJ,WAAW,qBAAqB,eAAe;AACjD,+DAAoBA;AAAAA,MACtB;AAAA,MACA,UAAU,CAAC,MAAMA,YAAW;AACRA,0BAAAA,QAAO,mBAAmB;AAEtC,cAAA,WAAW,WAAW,YAAY,eAAe;AACvD,6CAAW,MAAMA;AAAAA,MAAM;AAAA,IACzB,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,IACA;AAAA,EACF;AACF;;"}
|
|
@@ -39,33 +39,39 @@ export interface SolidQueuer<TValue> extends Omit<Queuer<TValue>, 'getAllItems'
|
|
|
39
39
|
size: Accessor<number>;
|
|
40
40
|
}
|
|
41
41
|
/**
|
|
42
|
-
*
|
|
42
|
+
* Creates a Solid-compatible Queuer instance for managing a synchronous queue of items, exposing Solid signals for all stateful properties.
|
|
43
43
|
*
|
|
44
|
-
*
|
|
45
|
-
*
|
|
46
|
-
*
|
|
44
|
+
* Features:
|
|
45
|
+
* - Synchronous processing of items using the provided `fn` function
|
|
46
|
+
* - FIFO (First In First Out) or LIFO (Last In First Out) queue behavior
|
|
47
|
+
* - Priority queueing via `getPriority` or item `priority` property
|
|
48
|
+
* - Item expiration and removal of stale items
|
|
49
|
+
* - Configurable wait time between processing items
|
|
50
|
+
* - Pause/resume processing
|
|
51
|
+
* - Callbacks for queue state changes, execution, rejection, and expiration
|
|
52
|
+
* - All stateful properties (items, counts, etc.) are exposed as Solid signals for reactivity
|
|
47
53
|
*
|
|
48
|
-
*
|
|
54
|
+
* The queue processes items synchronously in order, with optional delays between each item. When started, it will process one item per tick, with an optional wait time between ticks. You can pause and resume processing with `stop()` and `start()`.
|
|
49
55
|
*
|
|
50
|
-
*
|
|
51
|
-
* synchronously in order, with optional delays between processing each item. The queuer includes
|
|
52
|
-
* an internal tick mechanism that can be started and stopped, making it useful as a scheduler.
|
|
53
|
-
* When started, it will process one item per tick, with an optional wait time between ticks.
|
|
56
|
+
* By default, the queue uses FIFO behavior, but you can configure LIFO or double-ended queueing by specifying the position when adding or removing items.
|
|
54
57
|
*
|
|
55
|
-
*
|
|
56
|
-
* (Last In First Out) by specifying 'front' position when adding items.
|
|
57
|
-
*
|
|
58
|
-
* @example
|
|
58
|
+
* Example usage:
|
|
59
59
|
* ```tsx
|
|
60
|
-
* // Example with
|
|
60
|
+
* // Example with Solid signals and scheduling
|
|
61
61
|
* const [items, setItems] = createSignal([]);
|
|
62
62
|
*
|
|
63
|
-
* const queue = createQueuer(
|
|
64
|
-
*
|
|
65
|
-
*
|
|
66
|
-
*
|
|
67
|
-
*
|
|
68
|
-
*
|
|
63
|
+
* const queue = createQueuer(
|
|
64
|
+
* (item) => {
|
|
65
|
+
* // process item synchronously
|
|
66
|
+
* console.log('Processing', item);
|
|
67
|
+
* },
|
|
68
|
+
* {
|
|
69
|
+
* started: true, // Start processing immediately
|
|
70
|
+
* wait: 1000, // Process one item every second
|
|
71
|
+
* onItemsChange: (queue) => setItems(queue.getAllItems()),
|
|
72
|
+
* getPriority: (item) => item.priority // Process higher priority items first
|
|
73
|
+
* }
|
|
74
|
+
* );
|
|
69
75
|
*
|
|
70
76
|
* // Add items to process - they'll be handled automatically
|
|
71
77
|
* queue.addItem('task1');
|
|
@@ -83,4 +89,4 @@ export interface SolidQueuer<TValue> extends Omit<Queuer<TValue>, 'getAllItems'
|
|
|
83
89
|
* console.log('Next item:', queue.peek());
|
|
84
90
|
* ```
|
|
85
91
|
*/
|
|
86
|
-
export declare function createQueuer<TValue>(initialOptions?: QueuerOptions<TValue>): SolidQueuer<TValue>;
|
|
92
|
+
export declare function createQueuer<TValue>(fn: (item: TValue) => void, initialOptions?: QueuerOptions<TValue>): SolidQueuer<TValue>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createRateLimiter.cjs","sources":["../../../src/rate-limiter/createRateLimiter.ts"],"sourcesContent":["import { RateLimiter } from '@tanstack/pacer/rate-limiter'\nimport { createSignal } from 'solid-js'\nimport { bindInstanceMethods } from '@tanstack/pacer/utils'\nimport type { Accessor } from 'solid-js'\nimport type { AnyFunction } from '@tanstack/pacer/types'\nimport type { RateLimiterOptions } from '@tanstack/pacer/rate-limiter'\n\nexport interface SolidRateLimiter<TFn extends AnyFunction>\n extends Omit<\n RateLimiter<TFn>,\n | 'getExecutionCount'\n | 'getMsUntilNextWindow'\n | 'getRejectionCount'\n | 'getRemainingInWindow'\n > {\n executionCount: Accessor<number>\n msUntilNextWindow: Accessor<number>\n rejectionCount: Accessor<number>\n remainingInWindow: Accessor<number>\n}\n\n/**\n * A low-level Solid hook that creates a `RateLimiter` instance to enforce rate limits on function execution.\n *\n * This hook is designed to be flexible and state-management agnostic - it simply returns a rate limiter instance that\n * you can integrate with any state management solution (createSignal, etc).\n *\n * Rate limiting is a simple \"hard limit\" approach that allows executions until a maximum count is reached within\n * a time window, then blocks all subsequent calls until the window resets. Unlike throttling or debouncing,\n * it does not attempt to space out or collapse executions intelligently.\n *\n * The rate limiter supports two types of windows:\n * - 'fixed': A strict window that resets after the window period. All executions within the window count\n * towards the limit, and the window resets completely after the period.\n * - 'sliding': A rolling window that allows executions as old ones expire. This provides a more\n * consistent rate of execution over time.\n *\n * For smoother execution patterns:\n * - Use throttling when you want consistent spacing between executions (e.g. UI updates)\n * - Use debouncing when you want to collapse rapid-fire events (e.g. search input)\n * - Use rate limiting only when you need to enforce hard limits (e.g. API rate limits)\n *\n * @example\n * ```tsx\n * // Basic rate limiting - max 5 calls per minute with a sliding window\n * const rateLimiter = createRateLimiter(apiCall, {\n * limit: 5,\n * window: 60000,\n * windowType: 'sliding'
|
|
1
|
+
{"version":3,"file":"createRateLimiter.cjs","sources":["../../../src/rate-limiter/createRateLimiter.ts"],"sourcesContent":["import { RateLimiter } from '@tanstack/pacer/rate-limiter'\nimport { createSignal } from 'solid-js'\nimport { bindInstanceMethods } from '@tanstack/pacer/utils'\nimport type { Accessor } from 'solid-js'\nimport type { AnyFunction } from '@tanstack/pacer/types'\nimport type { RateLimiterOptions } from '@tanstack/pacer/rate-limiter'\n\nexport interface SolidRateLimiter<TFn extends AnyFunction>\n extends Omit<\n RateLimiter<TFn>,\n | 'getExecutionCount'\n | 'getMsUntilNextWindow'\n | 'getRejectionCount'\n | 'getRemainingInWindow'\n > {\n executionCount: Accessor<number>\n msUntilNextWindow: Accessor<number>\n rejectionCount: Accessor<number>\n remainingInWindow: Accessor<number>\n}\n\n/**\n * A low-level Solid hook that creates a `RateLimiter` instance to enforce rate limits on function execution.\n *\n * This hook is designed to be flexible and state-management agnostic - it simply returns a rate limiter instance that\n * you can integrate with any state management solution (createSignal, etc).\n *\n * Rate limiting is a simple \"hard limit\" approach that allows executions until a maximum count is reached within\n * a time window, then blocks all subsequent calls until the window resets. Unlike throttling or debouncing,\n * it does not attempt to space out or collapse executions intelligently.\n *\n * The rate limiter supports two types of windows:\n * - 'fixed': A strict window that resets after the window period. All executions within the window count\n * towards the limit, and the window resets completely after the period.\n * - 'sliding': A rolling window that allows executions as old ones expire. This provides a more\n * consistent rate of execution over time.\n *\n * For smoother execution patterns:\n * - Use throttling when you want consistent spacing between executions (e.g. UI updates)\n * - Use debouncing when you want to collapse rapid-fire events (e.g. search input)\n * - Use rate limiting only when you need to enforce hard limits (e.g. API rate limits)\n *\n * @example\n * ```tsx\n * // Basic rate limiting - max 5 calls per minute with a sliding window\n * const rateLimiter = createRateLimiter(apiCall, {\n * limit: 5,\n * window: 60000,\n * windowType: 'sliding',\n * onReject: (rateLimiter) => {\n * console.log(`Rate limit exceeded. Try again in ${rateLimiter.getMsUntilNextWindow()}ms`);\n * }\n * });\n *\n * // Access rate limiter state via signals\n * console.log('Executions:', rateLimiter.executionCount());\n * console.log('Rejections:', rateLimiter.rejectionCount());\n * console.log('Remaining:', rateLimiter.remainingInWindow());\n * console.log('Next window in:', rateLimiter.msUntilNextWindow());\n * ```\n */\nexport function createRateLimiter<TFn extends AnyFunction>(\n fn: TFn,\n initialOptions: RateLimiterOptions<TFn>,\n): SolidRateLimiter<TFn> {\n const rateLimiter = bindInstanceMethods(\n new RateLimiter<TFn>(fn, initialOptions),\n )\n\n const [executionCount, setExecutionCount] = createSignal(\n rateLimiter.getExecutionCount(),\n )\n const [rejectionCount, setRejectionCount] = createSignal(\n rateLimiter.getRejectionCount(),\n )\n const [remainingInWindow, setRemainingInWindow] = createSignal(\n rateLimiter.getRemainingInWindow(),\n )\n const [msUntilNextWindow, setMsUntilNextWindow] = createSignal(\n rateLimiter.getMsUntilNextWindow(),\n )\n\n function setOptions(newOptions: Partial<RateLimiterOptions<TFn>>) {\n rateLimiter.setOptions({\n ...newOptions,\n onExecute: (rateLimiter) => {\n setExecutionCount(rateLimiter.getExecutionCount())\n setRemainingInWindow(rateLimiter.getRemainingInWindow())\n setMsUntilNextWindow(rateLimiter.getMsUntilNextWindow())\n\n const onExecute = newOptions.onExecute ?? initialOptions.onExecute\n onExecute?.(rateLimiter)\n },\n onReject: (rateLimiter) => {\n setRejectionCount(rateLimiter.getRejectionCount())\n setRemainingInWindow(rateLimiter.getRemainingInWindow())\n setMsUntilNextWindow(rateLimiter.getMsUntilNextWindow())\n\n const onReject = newOptions.onReject ?? initialOptions.onReject\n onReject?.(rateLimiter)\n },\n })\n }\n\n setOptions(initialOptions)\n\n return {\n ...rateLimiter,\n executionCount,\n rejectionCount,\n remainingInWindow,\n msUntilNextWindow,\n setOptions,\n } as SolidRateLimiter<TFn>\n}\n"],"names":["rateLimiter","bindInstanceMethods","RateLimiter","createSignal"],"mappings":";;;;;AA6DgB,SAAA,kBACd,IACA,gBACuB;AACvB,QAAMA,gBAAcC,MAAA;AAAA,IAClB,IAAIC,YAAAA,YAAiB,IAAI,cAAc;AAAA,EACzC;AAEM,QAAA,CAAC,gBAAgB,iBAAiB,IAAIC,QAAA;AAAA,IAC1CH,cAAY,kBAAkB;AAAA,EAChC;AACM,QAAA,CAAC,gBAAgB,iBAAiB,IAAIG,QAAA;AAAA,IAC1CH,cAAY,kBAAkB;AAAA,EAChC;AACM,QAAA,CAAC,mBAAmB,oBAAoB,IAAIG,QAAA;AAAA,IAChDH,cAAY,qBAAqB;AAAA,EACnC;AACM,QAAA,CAAC,mBAAmB,oBAAoB,IAAIG,QAAA;AAAA,IAChDH,cAAY,qBAAqB;AAAA,EACnC;AAEA,WAAS,WAAW,YAA8C;AAChEA,kBAAY,WAAW;AAAA,MACrB,GAAG;AAAA,MACH,WAAW,CAACA,iBAAgB;AACRA,0BAAAA,aAAY,mBAAmB;AAC5BA,6BAAAA,aAAY,sBAAsB;AAClCA,6BAAAA,aAAY,sBAAsB;AAEjD,cAAA,YAAY,WAAW,aAAa,eAAe;AACzD,+CAAYA;AAAAA,MACd;AAAA,MACA,UAAU,CAACA,iBAAgB;AACPA,0BAAAA,aAAY,mBAAmB;AAC5BA,6BAAAA,aAAY,sBAAsB;AAClCA,6BAAAA,aAAY,sBAAsB;AAEjD,cAAA,WAAW,WAAW,YAAY,eAAe;AACvD,6CAAWA;AAAAA,MAAW;AAAA,IACxB,CACD;AAAA,EAAA;AAGH,aAAW,cAAc;AAElB,SAAA;AAAA,IACL,GAAGA;AAAAA,IACH;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACF;;"}
|
|
@@ -34,17 +34,11 @@ export interface SolidRateLimiter<TFn extends AnyFunction> extends Omit<RateLimi
|
|
|
34
34
|
* const rateLimiter = createRateLimiter(apiCall, {
|
|
35
35
|
* limit: 5,
|
|
36
36
|
* window: 60000,
|
|
37
|
-
* windowType: 'sliding'
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
* // Monitor rate limit status
|
|
41
|
-
* const handleClick = () => {
|
|
42
|
-
* if (rateLimiter.remainingInWindow() > 0) {
|
|
43
|
-
* rateLimiter.maybeExecute(data);
|
|
44
|
-
* } else {
|
|
45
|
-
* showRateLimitWarning();
|
|
37
|
+
* windowType: 'sliding',
|
|
38
|
+
* onReject: (rateLimiter) => {
|
|
39
|
+
* console.log(`Rate limit exceeded. Try again in ${rateLimiter.getMsUntilNextWindow()}ms`);
|
|
46
40
|
* }
|
|
47
|
-
* };
|
|
41
|
+
* });
|
|
48
42
|
*
|
|
49
43
|
* // Access rate limiter state via signals
|
|
50
44
|
* console.log('Executions:', rateLimiter.executionCount());
|
|
@@ -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
|
* ```
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createAsyncDebouncer.js","sources":["../../../src/async-debouncer/createAsyncDebouncer.ts"],"sourcesContent":["import { AsyncDebouncer } from '@tanstack/pacer/async-debouncer'\nimport { createSignal } from 'solid-js'\nimport { bindInstanceMethods } from '@tanstack/pacer/utils'\nimport type { AsyncDebouncerOptions } from '@tanstack/pacer/async-debouncer'\nimport type { AnyAsyncFunction } from '@tanstack/pacer/types'\nimport type { Accessor } from 'solid-js'\n\nexport interface SolidAsyncDebouncer<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.js","sources":["../../../src/async-debouncer/createAsyncDebouncer.ts"],"sourcesContent":["import { AsyncDebouncer } from '@tanstack/pacer/async-debouncer'\nimport { createSignal } from 'solid-js'\nimport { bindInstanceMethods } from '@tanstack/pacer/utils'\nimport type { AsyncDebouncerOptions } from '@tanstack/pacer/async-debouncer'\nimport type { AnyAsyncFunction } from '@tanstack/pacer/types'\nimport type { Accessor } from 'solid-js'\n\nexport interface SolidAsyncDebouncer<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"],"mappings":";;;AA4EgB,SAAA,qBACd,IACA,gBAC0B;AAC1B,QAAM,iBAAiB,IAAI,eAAoB,IAAI,cAAc;AAE3D,QAAA,CAAC,YAAY,aAAa,IAAI;AAAA,IAClC,eAAe,cAAc;AAAA,EAC/B;AACM,QAAA,CAAC,aAAa,cAAc,IAAI;AAAA,IACpC,eAAe,eAAe;AAAA,EAChC;AACM,QAAA,CAAC,cAAc,eAAe,IAAI;AAAA,IACtC,eAAe,gBAAgB;AAAA,EACjC;AACA,QAAM,CAAC,WAAW,YAAY,IAAI,aAAa,eAAe,cAAc;AACtE,QAAA,CAAC,YAAY,aAAa,IAAI;AAAA,IAClC,eAAe,cAAc;AAAA,EAC/B;AAEA,WAAS,WAAW,YAAiD;AACnE,mBAAe,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,GAAG,oBAAoB,cAAc;AAAA,IACrC;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,10 +1,14 @@
|
|
|
1
1
|
import { AsyncQueuer } from "@tanstack/pacer/async-queuer";
|
|
2
2
|
import { createSignal } from "solid-js";
|
|
3
3
|
import { bindInstanceMethods } from "@tanstack/pacer/utils";
|
|
4
|
-
function createAsyncQueuer(initialOptions = {}) {
|
|
5
|
-
const asyncQueuer = new AsyncQueuer(initialOptions);
|
|
6
|
-
const [
|
|
7
|
-
asyncQueuer.
|
|
4
|
+
function createAsyncQueuer(fn, initialOptions = {}) {
|
|
5
|
+
const asyncQueuer = new AsyncQueuer(fn, initialOptions);
|
|
6
|
+
const [successCount, setSuccessCount] = createSignal(
|
|
7
|
+
asyncQueuer.getSuccessCount()
|
|
8
|
+
);
|
|
9
|
+
const [errorCount, setErrorCount] = createSignal(asyncQueuer.getErrorCount());
|
|
10
|
+
const [settledCount, setSettledCount] = createSignal(
|
|
11
|
+
asyncQueuer.getSettledCount()
|
|
8
12
|
);
|
|
9
13
|
const [rejectionCount, setRejectionCount] = createSignal(
|
|
10
14
|
asyncQueuer.getRejectionCount()
|
|
@@ -16,8 +20,12 @@ function createAsyncQueuer(initialOptions = {}) {
|
|
|
16
20
|
const [allItems, setAllItems] = createSignal(
|
|
17
21
|
asyncQueuer.getAllItems()
|
|
18
22
|
);
|
|
19
|
-
const [activeItems, setActiveItems] = createSignal(
|
|
20
|
-
|
|
23
|
+
const [activeItems, setActiveItems] = createSignal(
|
|
24
|
+
asyncQueuer.getActiveItems()
|
|
25
|
+
);
|
|
26
|
+
const [pendingItems, setPendingItems] = createSignal(
|
|
27
|
+
asyncQueuer.getPendingItems()
|
|
28
|
+
);
|
|
21
29
|
const [peek, setPeek] = createSignal(
|
|
22
30
|
asyncQueuer.getPeek()
|
|
23
31
|
);
|
|
@@ -25,15 +33,18 @@ function createAsyncQueuer(initialOptions = {}) {
|
|
|
25
33
|
asyncQueuer.setOptions({
|
|
26
34
|
onItemsChange: (queuer) => {
|
|
27
35
|
var _a;
|
|
28
|
-
|
|
36
|
+
setActiveItems(queuer.getActiveItems());
|
|
37
|
+
setAllItems(queuer.getAllItems());
|
|
38
|
+
setErrorCount(queuer.getErrorCount());
|
|
29
39
|
setIsEmpty(queuer.getIsEmpty());
|
|
30
40
|
setIsFull(queuer.getIsFull());
|
|
31
41
|
setIsIdle(queuer.getIsIdle());
|
|
32
|
-
setAllItems(queuer.getAllItems());
|
|
33
|
-
setActiveItems(queuer.getActiveItems());
|
|
34
|
-
setPendingItems(queuer.getPendingItems());
|
|
35
42
|
setPeek(() => queuer.getPeek());
|
|
43
|
+
setPendingItems(queuer.getPendingItems());
|
|
44
|
+
setRejectionCount(queuer.getRejectionCount());
|
|
45
|
+
setSettledCount(queuer.getSettledCount());
|
|
36
46
|
setSize(queuer.getSize());
|
|
47
|
+
setSuccessCount(queuer.getSuccessCount());
|
|
37
48
|
(_a = initialOptions.onItemsChange) == null ? void 0 : _a.call(initialOptions, queuer);
|
|
38
49
|
},
|
|
39
50
|
onIsRunningChange: (queuer) => {
|
|
@@ -51,16 +62,18 @@ function createAsyncQueuer(initialOptions = {}) {
|
|
|
51
62
|
return {
|
|
52
63
|
...bindInstanceMethods(asyncQueuer),
|
|
53
64
|
activeItems,
|
|
54
|
-
|
|
65
|
+
allItems,
|
|
66
|
+
errorCount,
|
|
55
67
|
isEmpty,
|
|
56
68
|
isFull,
|
|
57
69
|
isIdle,
|
|
58
70
|
isRunning,
|
|
59
|
-
allItems,
|
|
60
71
|
peek,
|
|
61
72
|
pendingItems,
|
|
62
73
|
rejectionCount,
|
|
63
|
-
|
|
74
|
+
settledCount,
|
|
75
|
+
size,
|
|
76
|
+
successCount
|
|
64
77
|
};
|
|
65
78
|
}
|
|
66
79
|
export {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createAsyncQueuer.js","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.js","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":[],"mappings":";;;AAiIO,SAAS,kBACd,IACA,iBAA6C,IACnB;AAC1B,QAAM,cAAc,IAAI,YAAoB,IAAI,cAAc;AAExD,QAAA,CAAC,cAAc,eAAe,IAAI;AAAA,IACtC,YAAY,gBAAgB;AAAA,EAC9B;AACA,QAAM,CAAC,YAAY,aAAa,IAAI,aAAa,YAAY,eAAe;AACtE,QAAA,CAAC,cAAc,eAAe,IAAI;AAAA,IACtC,YAAY,gBAAgB;AAAA,EAC9B;AACM,QAAA,CAAC,gBAAgB,iBAAiB,IAAI;AAAA,IAC1C,YAAY,kBAAkB;AAAA,EAChC;AACA,QAAM,CAAC,SAAS,UAAU,IAAI,aAAa,YAAY,YAAY;AACnE,QAAM,CAAC,QAAQ,SAAS,IAAI,aAAa,YAAY,WAAW;AAChE,QAAM,CAAC,QAAQ,SAAS,IAAI,aAAa,YAAY,WAAW;AAChE,QAAM,CAAC,WAAW,YAAY,IAAI,aAAa,YAAY,cAAc;AACnE,QAAA,CAAC,UAAU,WAAW,IAAI;AAAA,IAC9B,YAAY,YAAY;AAAA,EAC1B;AACM,QAAA,CAAC,aAAa,cAAc,IAAI;AAAA,IACpC,YAAY,eAAe;AAAA,EAC7B;AACM,QAAA,CAAC,cAAc,eAAe,IAAI;AAAA,IACtC,YAAY,gBAAgB;AAAA,EAC9B;AACM,QAAA,CAAC,MAAM,OAAO,IAAI;AAAA,IACtB,YAAY,QAAQ;AAAA,EACtB;AACA,QAAM,CAAC,MAAM,OAAO,IAAI,aAAa,YAAY,SAAS;AAE1D,cAAY,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,GAAG,oBAAoB,WAAW;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;"}
|
|
@@ -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
|