@tanstack/preact-pacer 0.0.1 → 0.19.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +165 -45
- package/dist/async-batcher/index.cjs +12 -0
- package/dist/async-batcher/index.d.cts +4 -0
- package/dist/async-batcher/index.d.ts +4 -0
- package/dist/async-batcher/index.js +6 -0
- package/dist/async-batcher/useAsyncBatchedCallback.cjs +51 -0
- package/dist/async-batcher/useAsyncBatchedCallback.cjs.map +1 -0
- package/dist/async-batcher/useAsyncBatchedCallback.d.cts +46 -0
- package/dist/async-batcher/useAsyncBatchedCallback.d.ts +46 -0
- package/dist/async-batcher/useAsyncBatchedCallback.js +51 -0
- package/dist/async-batcher/useAsyncBatchedCallback.js.map +1 -0
- package/dist/async-batcher/useAsyncBatcher.cjs +189 -0
- package/dist/async-batcher/useAsyncBatcher.cjs.map +1 -0
- package/dist/async-batcher/useAsyncBatcher.d.cts +198 -0
- package/dist/async-batcher/useAsyncBatcher.d.ts +198 -0
- package/dist/async-batcher/useAsyncBatcher.js +189 -0
- package/dist/async-batcher/useAsyncBatcher.js.map +1 -0
- package/dist/async-debouncer/index.cjs +12 -0
- package/dist/async-debouncer/index.d.cts +4 -0
- package/dist/async-debouncer/index.d.ts +4 -0
- package/dist/async-debouncer/index.js +6 -0
- package/dist/async-debouncer/useAsyncDebouncedCallback.cjs +50 -0
- package/dist/async-debouncer/useAsyncDebouncedCallback.cjs.map +1 -0
- package/dist/async-debouncer/useAsyncDebouncedCallback.d.cts +47 -0
- package/dist/async-debouncer/useAsyncDebouncedCallback.d.ts +47 -0
- package/dist/async-debouncer/useAsyncDebouncedCallback.js +50 -0
- package/dist/async-debouncer/useAsyncDebouncedCallback.js.map +1 -0
- package/dist/async-debouncer/useAsyncDebouncer.cjs +173 -0
- package/dist/async-debouncer/useAsyncDebouncer.cjs.map +1 -0
- package/dist/async-debouncer/useAsyncDebouncer.d.cts +178 -0
- package/dist/async-debouncer/useAsyncDebouncer.d.ts +178 -0
- package/dist/async-debouncer/useAsyncDebouncer.js +173 -0
- package/dist/async-debouncer/useAsyncDebouncer.js.map +1 -0
- package/dist/async-queuer/index.cjs +12 -0
- package/dist/async-queuer/index.d.cts +4 -0
- package/dist/async-queuer/index.d.ts +4 -0
- package/dist/async-queuer/index.js +6 -0
- package/dist/async-queuer/useAsyncQueuedState.cjs +154 -0
- package/dist/async-queuer/useAsyncQueuedState.cjs.map +1 -0
- package/dist/async-queuer/useAsyncQueuedState.d.cts +152 -0
- package/dist/async-queuer/useAsyncQueuedState.d.ts +152 -0
- package/dist/async-queuer/useAsyncQueuedState.js +154 -0
- package/dist/async-queuer/useAsyncQueuedState.js.map +1 -0
- package/dist/async-queuer/useAsyncQueuer.cjs +189 -0
- package/dist/async-queuer/useAsyncQueuer.cjs.map +1 -0
- package/dist/async-queuer/useAsyncQueuer.d.cts +198 -0
- package/dist/async-queuer/useAsyncQueuer.d.ts +198 -0
- package/dist/async-queuer/useAsyncQueuer.js +189 -0
- package/dist/async-queuer/useAsyncQueuer.js.map +1 -0
- package/dist/async-rate-limiter/index.cjs +12 -0
- package/dist/async-rate-limiter/index.d.cts +4 -0
- package/dist/async-rate-limiter/index.d.ts +4 -0
- package/dist/async-rate-limiter/index.js +6 -0
- package/dist/async-rate-limiter/useAsyncRateLimitedCallback.cjs +65 -0
- package/dist/async-rate-limiter/useAsyncRateLimitedCallback.cjs.map +1 -0
- package/dist/async-rate-limiter/useAsyncRateLimitedCallback.d.cts +62 -0
- package/dist/async-rate-limiter/useAsyncRateLimitedCallback.d.ts +62 -0
- package/dist/async-rate-limiter/useAsyncRateLimitedCallback.js +65 -0
- package/dist/async-rate-limiter/useAsyncRateLimitedCallback.js.map +1 -0
- package/dist/async-rate-limiter/useAsyncRateLimiter.cjs +197 -0
- package/dist/async-rate-limiter/useAsyncRateLimiter.cjs.map +1 -0
- package/dist/async-rate-limiter/useAsyncRateLimiter.d.cts +207 -0
- package/dist/async-rate-limiter/useAsyncRateLimiter.d.ts +207 -0
- package/dist/async-rate-limiter/useAsyncRateLimiter.js +197 -0
- package/dist/async-rate-limiter/useAsyncRateLimiter.js.map +1 -0
- package/dist/async-retryer/index.cjs +9 -0
- package/dist/async-retryer/index.d.cts +1 -0
- package/dist/async-retryer/index.d.ts +1 -0
- package/dist/async-retryer/index.js +3 -0
- package/dist/async-throttler/index.cjs +12 -0
- package/dist/async-throttler/index.d.cts +4 -0
- package/dist/async-throttler/index.d.ts +4 -0
- package/dist/async-throttler/index.js +6 -0
- package/dist/async-throttler/useAsyncThrottledCallback.cjs +48 -0
- package/dist/async-throttler/useAsyncThrottledCallback.cjs.map +1 -0
- package/dist/async-throttler/useAsyncThrottledCallback.d.cts +45 -0
- package/dist/async-throttler/useAsyncThrottledCallback.d.ts +45 -0
- package/dist/async-throttler/useAsyncThrottledCallback.js +48 -0
- package/dist/async-throttler/useAsyncThrottledCallback.js.map +1 -0
- package/dist/async-throttler/useAsyncThrottler.cjs +182 -0
- package/dist/async-throttler/useAsyncThrottler.cjs.map +1 -0
- package/dist/async-throttler/useAsyncThrottler.d.cts +189 -0
- package/dist/async-throttler/useAsyncThrottler.d.ts +189 -0
- package/dist/async-throttler/useAsyncThrottler.js +182 -0
- package/dist/async-throttler/useAsyncThrottler.js.map +1 -0
- package/dist/batcher/index.cjs +12 -0
- package/dist/batcher/index.d.cts +4 -0
- package/dist/batcher/index.d.ts +4 -0
- package/dist/batcher/index.js +6 -0
- package/dist/batcher/useBatchedCallback.cjs +47 -0
- package/dist/batcher/useBatchedCallback.cjs.map +1 -0
- package/dist/batcher/useBatchedCallback.d.cts +44 -0
- package/dist/batcher/useBatchedCallback.d.ts +44 -0
- package/dist/batcher/useBatchedCallback.js +47 -0
- package/dist/batcher/useBatchedCallback.js.map +1 -0
- package/dist/batcher/useBatcher.cjs +146 -0
- package/dist/batcher/useBatcher.cjs.map +1 -0
- package/dist/batcher/useBatcher.d.cts +155 -0
- package/dist/batcher/useBatcher.d.ts +155 -0
- package/dist/batcher/useBatcher.js +146 -0
- package/dist/batcher/useBatcher.js.map +1 -0
- package/dist/debouncer/index.cjs +16 -0
- package/dist/debouncer/index.d.cts +6 -0
- package/dist/debouncer/index.d.ts +6 -0
- package/dist/debouncer/index.js +8 -0
- package/dist/debouncer/useDebouncedCallback.cjs +48 -0
- package/dist/debouncer/useDebouncedCallback.cjs.map +1 -0
- package/dist/debouncer/useDebouncedCallback.d.cts +45 -0
- package/dist/debouncer/useDebouncedCallback.d.ts +45 -0
- package/dist/debouncer/useDebouncedCallback.js +48 -0
- package/dist/debouncer/useDebouncedCallback.js.map +1 -0
- package/dist/debouncer/useDebouncedState.cjs +89 -0
- package/dist/debouncer/useDebouncedState.cjs.map +1 -0
- package/dist/debouncer/useDebouncedState.d.cts +82 -0
- package/dist/debouncer/useDebouncedState.d.ts +82 -0
- package/dist/debouncer/useDebouncedState.js +89 -0
- package/dist/debouncer/useDebouncedState.js.map +1 -0
- package/dist/debouncer/useDebouncedValue.cjs +96 -0
- package/dist/debouncer/useDebouncedValue.cjs.map +1 -0
- package/dist/debouncer/useDebouncedValue.d.cts +91 -0
- package/dist/debouncer/useDebouncedValue.d.ts +91 -0
- package/dist/debouncer/useDebouncedValue.js +96 -0
- package/dist/debouncer/useDebouncedValue.js.map +1 -0
- package/dist/debouncer/useDebouncer.cjs +128 -0
- package/dist/debouncer/useDebouncer.cjs.map +1 -0
- package/dist/debouncer/useDebouncer.d.cts +133 -0
- package/dist/debouncer/useDebouncer.d.ts +133 -0
- package/dist/debouncer/useDebouncer.js +128 -0
- package/dist/debouncer/useDebouncer.js.map +1 -0
- package/dist/index.cjs +66 -0
- package/dist/index.d.cts +30 -0
- package/dist/index.d.ts +30 -0
- package/dist/index.js +32 -0
- package/dist/provider/PacerProvider.cjs +26 -0
- package/dist/provider/PacerProvider.cjs.map +1 -0
- package/dist/provider/PacerProvider.d.cts +33 -0
- package/dist/provider/PacerProvider.d.ts +33 -0
- package/dist/provider/PacerProvider.js +24 -0
- package/dist/provider/PacerProvider.js.map +1 -0
- package/dist/provider/index.cjs +5 -0
- package/dist/provider/index.d.cts +2 -0
- package/dist/provider/index.d.ts +2 -0
- package/dist/provider/index.js +3 -0
- package/dist/queuer/index.cjs +14 -0
- package/dist/queuer/index.d.cts +5 -0
- package/dist/queuer/index.d.ts +5 -0
- package/dist/queuer/index.js +7 -0
- package/dist/queuer/useQueuedState.cjs +129 -0
- package/dist/queuer/useQueuedState.cjs.map +1 -0
- package/dist/queuer/useQueuedState.d.cts +123 -0
- package/dist/queuer/useQueuedState.d.ts +123 -0
- package/dist/queuer/useQueuedState.js +129 -0
- package/dist/queuer/useQueuedState.js.map +1 -0
- package/dist/queuer/useQueuedValue.cjs +115 -0
- package/dist/queuer/useQueuedValue.cjs.map +1 -0
- package/dist/queuer/useQueuedValue.d.cts +106 -0
- package/dist/queuer/useQueuedValue.d.ts +106 -0
- package/dist/queuer/useQueuedValue.js +115 -0
- package/dist/queuer/useQueuedValue.js.map +1 -0
- package/dist/queuer/useQueuer.cjs +157 -0
- package/dist/queuer/useQueuer.cjs.map +1 -0
- package/dist/queuer/useQueuer.d.cts +166 -0
- package/dist/queuer/useQueuer.d.ts +166 -0
- package/dist/queuer/useQueuer.js +157 -0
- package/dist/queuer/useQueuer.js.map +1 -0
- package/dist/rate-limiter/index.cjs +16 -0
- package/dist/rate-limiter/index.d.cts +6 -0
- package/dist/rate-limiter/index.d.ts +6 -0
- package/dist/rate-limiter/index.js +8 -0
- package/dist/rate-limiter/useRateLimitedCallback.cjs +65 -0
- package/dist/rate-limiter/useRateLimitedCallback.cjs.map +1 -0
- package/dist/rate-limiter/useRateLimitedCallback.d.cts +62 -0
- package/dist/rate-limiter/useRateLimitedCallback.d.ts +62 -0
- package/dist/rate-limiter/useRateLimitedCallback.js +65 -0
- package/dist/rate-limiter/useRateLimitedCallback.js.map +1 -0
- package/dist/rate-limiter/useRateLimitedState.cjs +115 -0
- package/dist/rate-limiter/useRateLimitedState.cjs.map +1 -0
- package/dist/rate-limiter/useRateLimitedState.d.cts +108 -0
- package/dist/rate-limiter/useRateLimitedState.d.ts +108 -0
- package/dist/rate-limiter/useRateLimitedState.js +115 -0
- package/dist/rate-limiter/useRateLimitedState.js.map +1 -0
- package/dist/rate-limiter/useRateLimitedValue.cjs +102 -0
- package/dist/rate-limiter/useRateLimitedValue.cjs.map +1 -0
- package/dist/rate-limiter/useRateLimitedValue.d.cts +97 -0
- package/dist/rate-limiter/useRateLimitedValue.d.ts +97 -0
- package/dist/rate-limiter/useRateLimitedValue.js +102 -0
- package/dist/rate-limiter/useRateLimitedValue.js.map +1 -0
- package/dist/rate-limiter/useRateLimiter.cjs +162 -0
- package/dist/rate-limiter/useRateLimiter.cjs.map +1 -0
- package/dist/rate-limiter/useRateLimiter.d.cts +172 -0
- package/dist/rate-limiter/useRateLimiter.d.ts +172 -0
- package/dist/rate-limiter/useRateLimiter.js +162 -0
- package/dist/rate-limiter/useRateLimiter.js.map +1 -0
- package/dist/throttler/index.cjs +16 -0
- package/dist/throttler/index.d.cts +6 -0
- package/dist/throttler/index.d.ts +6 -0
- package/dist/throttler/index.js +8 -0
- package/dist/throttler/useThrottledCallback.cjs +49 -0
- package/dist/throttler/useThrottledCallback.cjs.map +1 -0
- package/dist/throttler/useThrottledCallback.d.cts +46 -0
- package/dist/throttler/useThrottledCallback.d.ts +46 -0
- package/dist/throttler/useThrottledCallback.js +49 -0
- package/dist/throttler/useThrottledCallback.js.map +1 -0
- package/dist/throttler/useThrottledState.cjs +100 -0
- package/dist/throttler/useThrottledState.cjs.map +1 -0
- package/dist/throttler/useThrottledState.d.cts +93 -0
- package/dist/throttler/useThrottledState.d.ts +93 -0
- package/dist/throttler/useThrottledState.js +100 -0
- package/dist/throttler/useThrottledState.js.map +1 -0
- package/dist/throttler/useThrottledValue.cjs +91 -0
- package/dist/throttler/useThrottledValue.cjs.map +1 -0
- package/dist/throttler/useThrottledValue.d.cts +86 -0
- package/dist/throttler/useThrottledValue.d.ts +86 -0
- package/dist/throttler/useThrottledValue.js +91 -0
- package/dist/throttler/useThrottledValue.js.map +1 -0
- package/dist/throttler/useThrottler.cjs +133 -0
- package/dist/throttler/useThrottler.cjs.map +1 -0
- package/dist/throttler/useThrottler.d.cts +138 -0
- package/dist/throttler/useThrottler.d.ts +138 -0
- package/dist/throttler/useThrottler.js +133 -0
- package/dist/throttler/useThrottler.js.map +1 -0
- package/dist/types/index.cjs +9 -0
- package/dist/types/index.d.cts +1 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/index.js +3 -0
- package/dist/utils/index.cjs +9 -0
- package/dist/utils/index.d.cts +1 -0
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/index.js +3 -0
- package/package.json +118 -7
- package/src/async-batcher/index.ts +4 -0
- package/src/async-batcher/useAsyncBatchedCallback.ts +54 -0
- package/src/async-batcher/useAsyncBatcher.ts +249 -0
- package/src/async-debouncer/index.ts +5 -0
- package/src/async-debouncer/useAsyncDebouncedCallback.ts +54 -0
- package/src/async-debouncer/useAsyncDebouncer.ts +235 -0
- package/src/async-queuer/index.ts +4 -0
- package/src/async-queuer/useAsyncQueuedState.ts +165 -0
- package/src/async-queuer/useAsyncQueuer.ts +249 -0
- package/src/async-rate-limiter/index.ts +5 -0
- package/src/async-rate-limiter/useAsyncRateLimitedCallback.ts +69 -0
- package/src/async-rate-limiter/useAsyncRateLimiter.ts +261 -0
- package/src/async-retryer/index.ts +2 -0
- package/src/async-throttler/index.ts +5 -0
- package/src/async-throttler/useAsyncThrottledCallback.ts +52 -0
- package/src/async-throttler/useAsyncThrottler.ts +244 -0
- package/src/batcher/index.ts +4 -0
- package/src/batcher/useBatchedCallback.ts +50 -0
- package/src/batcher/useBatcher.ts +203 -0
- package/src/debouncer/index.ts +7 -0
- package/src/debouncer/useDebouncedCallback.ts +51 -0
- package/src/debouncer/useDebouncedState.ts +109 -0
- package/src/debouncer/useDebouncedValue.ts +112 -0
- package/src/debouncer/useDebouncer.ts +189 -0
- package/src/index.ts +56 -0
- package/src/provider/PacerProvider.tsx +70 -0
- package/src/provider/index.ts +1 -0
- package/src/queuer/index.ts +5 -0
- package/src/queuer/useQueuedState.ts +133 -0
- package/src/queuer/useQueuedValue.ts +129 -0
- package/src/queuer/useQueuer.ts +213 -0
- package/src/rate-limiter/index.ts +6 -0
- package/src/rate-limiter/useRateLimitedCallback.ts +68 -0
- package/src/rate-limiter/useRateLimitedState.ts +124 -0
- package/src/rate-limiter/useRateLimitedValue.ts +110 -0
- package/src/rate-limiter/useRateLimiter.ts +222 -0
- package/src/throttler/index.ts +7 -0
- package/src/throttler/useThrottledCallback.ts +52 -0
- package/src/throttler/useThrottledState.ts +121 -0
- package/src/throttler/useThrottledValue.ts +107 -0
- package/src/throttler/useThrottler.ts +194 -0
- package/src/types/index.ts +1 -0
- package/src/utils/index.ts +1 -0
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
const require_PacerProvider = require('../provider/PacerProvider.cjs');
|
|
2
|
+
let preact_hooks = require("preact/hooks");
|
|
3
|
+
let _tanstack_preact_store = require("@tanstack/preact-store");
|
|
4
|
+
let _tanstack_pacer_async_queuer = require("@tanstack/pacer/async-queuer");
|
|
5
|
+
|
|
6
|
+
//#region src/async-queuer/useAsyncQueuer.ts
|
|
7
|
+
/**
|
|
8
|
+
* A lower-level Preact hook that creates an `AsyncQueuer` instance for managing an async queue of items.
|
|
9
|
+
*
|
|
10
|
+
* Features:
|
|
11
|
+
* - Priority queue support via getPriority option
|
|
12
|
+
* - Configurable concurrency limit
|
|
13
|
+
* - Task success/error/completion callbacks
|
|
14
|
+
* - FIFO (First In First Out) or LIFO (Last In First Out) queue behavior
|
|
15
|
+
* - Pause/resume task processing
|
|
16
|
+
* - Task cancellation
|
|
17
|
+
* - Item expiration to clear stale items from the queue
|
|
18
|
+
*
|
|
19
|
+
* Tasks are processed concurrently up to the configured concurrency limit. When a task completes,
|
|
20
|
+
* the next pending task is processed if below the concurrency limit.
|
|
21
|
+
*
|
|
22
|
+
* Error Handling:
|
|
23
|
+
* - If an `onError` handler is provided, it will be called with the error and queuer instance
|
|
24
|
+
* - If `throwOnError` is true (default when no onError handler is provided), the error will be thrown
|
|
25
|
+
* - If `throwOnError` is false (default when onError handler is provided), the error will be swallowed
|
|
26
|
+
* - Both onError and throwOnError can be used together - the handler will be called before any error is thrown
|
|
27
|
+
* - The error state can be checked using the underlying AsyncQueuer instance
|
|
28
|
+
*
|
|
29
|
+
* ## State Management and Selector
|
|
30
|
+
*
|
|
31
|
+
* The hook uses TanStack Store for reactive state management. You can subscribe to state changes
|
|
32
|
+
* in two ways:
|
|
33
|
+
*
|
|
34
|
+
* **1. Using `queuer.Subscribe` HOC (Recommended for component tree subscriptions)**
|
|
35
|
+
*
|
|
36
|
+
* Use the `Subscribe` HOC to subscribe to state changes deep in your component tree without
|
|
37
|
+
* needing to pass a selector to the hook. This is ideal when you want to subscribe to state
|
|
38
|
+
* in child components.
|
|
39
|
+
*
|
|
40
|
+
* **2. Using the `selector` parameter (For hook-level subscriptions)**
|
|
41
|
+
*
|
|
42
|
+
* The `selector` parameter allows you to specify which state changes will trigger a re-render
|
|
43
|
+
* at the hook level, optimizing performance by preventing unnecessary re-renders when irrelevant
|
|
44
|
+
* state changes occur.
|
|
45
|
+
*
|
|
46
|
+
* **By default, there will be no reactive state subscriptions** and you must opt-in to state
|
|
47
|
+
* tracking by providing a selector function or using the `Subscribe` HOC. This prevents unnecessary
|
|
48
|
+
* re-renders and gives you full control over when your component updates.
|
|
49
|
+
*
|
|
50
|
+
* Available state properties:
|
|
51
|
+
* - `activeItems`: Items currently being processed by the queuer
|
|
52
|
+
* - `errorCount`: Number of task executions that have resulted in errors
|
|
53
|
+
* - `expirationCount`: Number of items that have been removed due to expiration
|
|
54
|
+
* - `isEmpty`: Whether the queuer has no items to process
|
|
55
|
+
* - `isFull`: Whether the queuer has reached its maximum capacity
|
|
56
|
+
* - `isIdle`: Whether the queuer is not currently processing any items
|
|
57
|
+
* - `isRunning`: Whether the queuer is active and will process items automatically
|
|
58
|
+
* - `items`: Array of items currently waiting to be processed
|
|
59
|
+
* - `itemTimestamps`: Timestamps when items were added for expiration tracking
|
|
60
|
+
* - `lastResult`: The result from the most recent task execution
|
|
61
|
+
* - `pendingTick`: Whether the queuer has a pending timeout for processing the next item
|
|
62
|
+
* - `rejectionCount`: Number of items that have been rejected from being added
|
|
63
|
+
* - `settledCount`: Number of task executions that have completed (success or error)
|
|
64
|
+
* - `size`: Number of items currently in the queue
|
|
65
|
+
* - `status`: Current processing status ('idle' | 'running' | 'stopped')
|
|
66
|
+
* - `successCount`: Number of task executions that have completed successfully
|
|
67
|
+
*
|
|
68
|
+
* @example
|
|
69
|
+
* ```tsx
|
|
70
|
+
* // Default behavior - no reactive state subscriptions
|
|
71
|
+
* const asyncQueuer = useAsyncQueuer(
|
|
72
|
+
* async (item) => {
|
|
73
|
+
* const result = await processItem(item);
|
|
74
|
+
* return result;
|
|
75
|
+
* },
|
|
76
|
+
* { concurrency: 2, maxSize: 100, started: false }
|
|
77
|
+
* );
|
|
78
|
+
*
|
|
79
|
+
* // Subscribe to state changes deep in component tree using Subscribe HOC
|
|
80
|
+
* <asyncQueuer.Subscribe selector={(state) => ({ size: state.size })}>
|
|
81
|
+
* {({ size }) => (
|
|
82
|
+
* <div>Queue Size: {size}</div>
|
|
83
|
+
* )}
|
|
84
|
+
* </asyncQueuer.Subscribe>
|
|
85
|
+
*
|
|
86
|
+
* // Opt-in to re-render when queue size changes at hook level (optimized for displaying queue length)
|
|
87
|
+
* const asyncQueuer = useAsyncQueuer(
|
|
88
|
+
* async (item) => {
|
|
89
|
+
* const result = await processItem(item);
|
|
90
|
+
* return result;
|
|
91
|
+
* },
|
|
92
|
+
* { concurrency: 2, maxSize: 100, started: false },
|
|
93
|
+
* (state) => ({
|
|
94
|
+
* size: state.size,
|
|
95
|
+
* isEmpty: state.isEmpty,
|
|
96
|
+
* isFull: state.isFull
|
|
97
|
+
* })
|
|
98
|
+
* );
|
|
99
|
+
*
|
|
100
|
+
* // Opt-in to re-render when processing state changes (optimized for loading indicators)
|
|
101
|
+
* const asyncQueuer = useAsyncQueuer(
|
|
102
|
+
* async (item) => {
|
|
103
|
+
* const result = await processItem(item);
|
|
104
|
+
* return result;
|
|
105
|
+
* },
|
|
106
|
+
* { concurrency: 2, maxSize: 100, started: false },
|
|
107
|
+
* (state) => ({
|
|
108
|
+
* isRunning: state.isRunning,
|
|
109
|
+
* isIdle: state.isIdle,
|
|
110
|
+
* status: state.status,
|
|
111
|
+
* activeItems: state.activeItems,
|
|
112
|
+
* pendingTick: state.pendingTick
|
|
113
|
+
* })
|
|
114
|
+
* );
|
|
115
|
+
*
|
|
116
|
+
* // Opt-in to re-render when execution metrics change (optimized for stats display)
|
|
117
|
+
* const asyncQueuer = useAsyncQueuer(
|
|
118
|
+
* async (item) => {
|
|
119
|
+
* const result = await processItem(item);
|
|
120
|
+
* return result;
|
|
121
|
+
* },
|
|
122
|
+
* { concurrency: 2, maxSize: 100, started: false },
|
|
123
|
+
* (state) => ({
|
|
124
|
+
* successCount: state.successCount,
|
|
125
|
+
* errorCount: state.errorCount,
|
|
126
|
+
* settledCount: state.settledCount,
|
|
127
|
+
* expirationCount: state.expirationCount,
|
|
128
|
+
* rejectionCount: state.rejectionCount
|
|
129
|
+
* })
|
|
130
|
+
* );
|
|
131
|
+
*
|
|
132
|
+
* // Opt-in to re-render when results are available (optimized for data display)
|
|
133
|
+
* const asyncQueuer = useAsyncQueuer(
|
|
134
|
+
* async (item) => {
|
|
135
|
+
* const result = await processItem(item);
|
|
136
|
+
* return result;
|
|
137
|
+
* },
|
|
138
|
+
* {
|
|
139
|
+
* concurrency: 2,
|
|
140
|
+
* maxSize: 100,
|
|
141
|
+
* started: false,
|
|
142
|
+
* onSuccess: (result) => {
|
|
143
|
+
* console.log('Item processed:', result);
|
|
144
|
+
* },
|
|
145
|
+
* onError: (error) => {
|
|
146
|
+
* console.error('Processing failed:', error);
|
|
147
|
+
* }
|
|
148
|
+
* },
|
|
149
|
+
* (state) => ({
|
|
150
|
+
* lastResult: state.lastResult,
|
|
151
|
+
* successCount: state.successCount
|
|
152
|
+
* })
|
|
153
|
+
* );
|
|
154
|
+
*
|
|
155
|
+
* // Add items to queue
|
|
156
|
+
* asyncQueuer.addItem(newItem);
|
|
157
|
+
*
|
|
158
|
+
* // Start processing
|
|
159
|
+
* asyncQueuer.start();
|
|
160
|
+
*
|
|
161
|
+
* // Access the selected state (will be empty object {} unless selector provided)
|
|
162
|
+
* const { size, isRunning, activeItems } = asyncQueuer.state;
|
|
163
|
+
* ```
|
|
164
|
+
*/
|
|
165
|
+
function useAsyncQueuer(fn, options = {}, selector = () => ({})) {
|
|
166
|
+
const mergedOptions = {
|
|
167
|
+
...require_PacerProvider.useDefaultPacerOptions().asyncQueuer,
|
|
168
|
+
...options
|
|
169
|
+
};
|
|
170
|
+
const [asyncQueuer] = (0, preact_hooks.useState)(() => {
|
|
171
|
+
const queuerInstance = new _tanstack_pacer_async_queuer.AsyncQueuer(fn, mergedOptions);
|
|
172
|
+
queuerInstance.Subscribe = function Subscribe(props) {
|
|
173
|
+
const selected = (0, _tanstack_preact_store.useStore)(queuerInstance.store, props.selector);
|
|
174
|
+
return typeof props.children === "function" ? props.children(selected) : props.children;
|
|
175
|
+
};
|
|
176
|
+
return queuerInstance;
|
|
177
|
+
});
|
|
178
|
+
asyncQueuer.fn = fn;
|
|
179
|
+
asyncQueuer.setOptions(mergedOptions);
|
|
180
|
+
const state = (0, _tanstack_preact_store.useStore)(asyncQueuer.store, selector);
|
|
181
|
+
return (0, preact_hooks.useMemo)(() => ({
|
|
182
|
+
...asyncQueuer,
|
|
183
|
+
state
|
|
184
|
+
}), [asyncQueuer, state]);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
//#endregion
|
|
188
|
+
exports.useAsyncQueuer = useAsyncQueuer;
|
|
189
|
+
//# sourceMappingURL=useAsyncQueuer.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useAsyncQueuer.cjs","names":["useDefaultPacerOptions","AsyncQueuer"],"sources":["../../src/async-queuer/useAsyncQueuer.ts"],"sourcesContent":["import { useMemo, useState } from 'preact/hooks'\nimport { AsyncQueuer } from '@tanstack/pacer/async-queuer'\nimport { useStore } from '@tanstack/preact-store'\nimport { useDefaultPacerOptions } from '../provider/PacerProvider'\nimport type { Store } from '@tanstack/preact-store'\nimport type {\n AsyncQueuerOptions,\n AsyncQueuerState,\n} from '@tanstack/pacer/async-queuer'\nimport type { ComponentChildren } from 'preact'\n\nexport interface PreactAsyncQueuer<TValue, TSelected = {}> extends Omit<\n AsyncQueuer<TValue>,\n 'store'\n> {\n /**\n * A Preact HOC (Higher Order Component) that allows you to subscribe to the async queuer state.\n *\n * This is useful for opting into state re-renders for specific parts of the queuer state\n * deep in your component tree without needing to pass a selector to the hook.\n *\n * @example\n * <queuer.Subscribe selector={(state) => ({ size: state.size })}>\n * {({ size }) => (\n * <div>Queue Size: {size}</div>\n * )}\n * </queuer.Subscribe>\n */\n Subscribe: <TSelected>(props: {\n selector: (state: AsyncQueuerState<TValue>) => TSelected\n children: ((state: TSelected) => ComponentChildren) | ComponentChildren\n }) => ComponentChildren\n /**\n * Reactive state that will be updated and re-rendered when the queuer state changes\n *\n * Use this instead of `queuer.store.state`\n */\n readonly state: Readonly<TSelected>\n /**\n * @deprecated Use `queuer.state` instead of `queuer.store.state` if you want to read reactive state.\n * The state on the store object is not reactive, as it has not been wrapped in a `useStore` hook internally.\n * Although, you can make the state reactive by using the `useStore` in your own usage.\n */\n readonly store: Store<Readonly<AsyncQueuerState<TValue>>>\n}\n\n/**\n * A lower-level Preact hook that creates an `AsyncQueuer` instance for managing an async queue of items.\n *\n * Features:\n * - Priority queue support via getPriority option\n * - Configurable concurrency limit\n * - Task success/error/completion callbacks\n * - FIFO (First In First Out) or LIFO (Last In First Out) queue behavior\n * - Pause/resume task processing\n * - Task cancellation\n * - Item expiration to clear stale items from the queue\n *\n * Tasks are processed concurrently up to the configured concurrency limit. When a task completes,\n * the next pending task is processed if below the concurrency limit.\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 * ## State Management and Selector\n *\n * The hook uses TanStack Store for reactive state management. You can subscribe to state changes\n * in two ways:\n *\n * **1. Using `queuer.Subscribe` HOC (Recommended for component tree subscriptions)**\n *\n * Use the `Subscribe` HOC to subscribe to state changes deep in your component tree without\n * needing to pass a selector to the hook. This is ideal when you want to subscribe to state\n * in child components.\n *\n * **2. Using the `selector` parameter (For hook-level subscriptions)**\n *\n * The `selector` parameter allows you to specify which state changes will trigger a re-render\n * at the hook level, optimizing performance by preventing unnecessary re-renders when irrelevant\n * state changes occur.\n *\n * **By default, there will be no reactive state subscriptions** and you must opt-in to state\n * tracking by providing a selector function or using the `Subscribe` HOC. This prevents unnecessary\n * re-renders and gives you full control over when your component updates.\n *\n * Available state properties:\n * - `activeItems`: Items currently being processed by the queuer\n * - `errorCount`: Number of task executions that have resulted in errors\n * - `expirationCount`: Number of items that have been removed due to expiration\n * - `isEmpty`: Whether the queuer has no items to process\n * - `isFull`: Whether the queuer has reached its maximum capacity\n * - `isIdle`: Whether the queuer is not currently processing any items\n * - `isRunning`: Whether the queuer is active and will process items automatically\n * - `items`: Array of items currently waiting to be processed\n * - `itemTimestamps`: Timestamps when items were added for expiration tracking\n * - `lastResult`: The result from the most recent task execution\n * - `pendingTick`: Whether the queuer has a pending timeout for processing the next item\n * - `rejectionCount`: Number of items that have been rejected from being added\n * - `settledCount`: Number of task executions that have completed (success or error)\n * - `size`: Number of items currently in the queue\n * - `status`: Current processing status ('idle' | 'running' | 'stopped')\n * - `successCount`: Number of task executions that have completed successfully\n *\n * @example\n * ```tsx\n * // Default behavior - no reactive state subscriptions\n * const asyncQueuer = useAsyncQueuer(\n * async (item) => {\n * const result = await processItem(item);\n * return result;\n * },\n * { concurrency: 2, maxSize: 100, started: false }\n * );\n *\n * // Subscribe to state changes deep in component tree using Subscribe HOC\n * <asyncQueuer.Subscribe selector={(state) => ({ size: state.size })}>\n * {({ size }) => (\n * <div>Queue Size: {size}</div>\n * )}\n * </asyncQueuer.Subscribe>\n *\n * // Opt-in to re-render when queue size changes at hook level (optimized for displaying queue length)\n * const asyncQueuer = useAsyncQueuer(\n * async (item) => {\n * const result = await processItem(item);\n * return result;\n * },\n * { concurrency: 2, maxSize: 100, started: false },\n * (state) => ({\n * size: state.size,\n * isEmpty: state.isEmpty,\n * isFull: state.isFull\n * })\n * );\n *\n * // Opt-in to re-render when processing state changes (optimized for loading indicators)\n * const asyncQueuer = useAsyncQueuer(\n * async (item) => {\n * const result = await processItem(item);\n * return result;\n * },\n * { concurrency: 2, maxSize: 100, started: false },\n * (state) => ({\n * isRunning: state.isRunning,\n * isIdle: state.isIdle,\n * status: state.status,\n * activeItems: state.activeItems,\n * pendingTick: state.pendingTick\n * })\n * );\n *\n * // Opt-in to re-render when execution metrics change (optimized for stats display)\n * const asyncQueuer = useAsyncQueuer(\n * async (item) => {\n * const result = await processItem(item);\n * return result;\n * },\n * { concurrency: 2, maxSize: 100, started: false },\n * (state) => ({\n * successCount: state.successCount,\n * errorCount: state.errorCount,\n * settledCount: state.settledCount,\n * expirationCount: state.expirationCount,\n * rejectionCount: state.rejectionCount\n * })\n * );\n *\n * // Opt-in to re-render when results are available (optimized for data display)\n * const asyncQueuer = useAsyncQueuer(\n * async (item) => {\n * const result = await processItem(item);\n * return result;\n * },\n * {\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 * (state) => ({\n * lastResult: state.lastResult,\n * successCount: state.successCount\n * })\n * );\n *\n * // Add items to queue\n * asyncQueuer.addItem(newItem);\n *\n * // Start processing\n * asyncQueuer.start();\n *\n * // Access the selected state (will be empty object {} unless selector provided)\n * const { size, isRunning, activeItems } = asyncQueuer.state;\n * ```\n */\nexport function useAsyncQueuer<TValue, TSelected = {}>(\n fn: (value: TValue) => Promise<any>,\n options: AsyncQueuerOptions<TValue> = {},\n selector: (state: AsyncQueuerState<TValue>) => TSelected = () =>\n ({}) as TSelected,\n): PreactAsyncQueuer<TValue, TSelected> {\n const mergedOptions = {\n ...useDefaultPacerOptions().asyncQueuer,\n ...options,\n } as AsyncQueuerOptions<TValue>\n\n const [asyncQueuer] = useState(() => {\n const queuerInstance = new AsyncQueuer<TValue>(\n fn,\n mergedOptions,\n ) as unknown as PreactAsyncQueuer<TValue, TSelected>\n\n queuerInstance.Subscribe = function Subscribe<TSelected>(props: {\n selector: (state: AsyncQueuerState<TValue>) => TSelected\n children: ((state: TSelected) => ComponentChildren) | ComponentChildren\n }) {\n const selected = useStore(queuerInstance.store, props.selector)\n\n return typeof props.children === 'function'\n ? props.children(selected)\n : props.children\n }\n\n return queuerInstance\n })\n\n asyncQueuer.fn = fn\n asyncQueuer.setOptions(mergedOptions)\n\n const state = useStore(asyncQueuer.store, selector)\n\n return useMemo(\n () =>\n ({\n ...asyncQueuer,\n state,\n }) as PreactAsyncQueuer<TValue, TSelected>, // omit `store` in favor of `state`\n [asyncQueuer, state],\n )\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA4MA,SAAgB,eACd,IACA,UAAsC,EAAE,EACxC,kBACG,EAAE,GACiC;CACtC,MAAM,gBAAgB;EACpB,GAAGA,8CAAwB,CAAC;EAC5B,GAAG;EACJ;CAED,MAAM,CAAC,gDAA8B;EACnC,MAAM,iBAAiB,IAAIC,yCACzB,IACA,cACD;AAED,iBAAe,YAAY,SAAS,UAAqB,OAGtD;GACD,MAAM,gDAAoB,eAAe,OAAO,MAAM,SAAS;AAE/D,UAAO,OAAO,MAAM,aAAa,aAC7B,MAAM,SAAS,SAAS,GACxB,MAAM;;AAGZ,SAAO;GACP;AAEF,aAAY,KAAK;AACjB,aAAY,WAAW,cAAc;CAErC,MAAM,6CAAiB,YAAY,OAAO,SAAS;AAEnD,yCAEK;EACC,GAAG;EACH;EACD,GACH,CAAC,aAAa,MAAM,CACrB"}
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
import { Store } from "@tanstack/preact-store";
|
|
2
|
+
import { ComponentChildren } from "preact";
|
|
3
|
+
import { AsyncQueuer, AsyncQueuerOptions, AsyncQueuerState } from "@tanstack/pacer/async-queuer";
|
|
4
|
+
|
|
5
|
+
//#region src/async-queuer/useAsyncQueuer.d.ts
|
|
6
|
+
interface PreactAsyncQueuer<TValue, TSelected = {}> extends Omit<AsyncQueuer<TValue>, 'store'> {
|
|
7
|
+
/**
|
|
8
|
+
* A Preact HOC (Higher Order Component) that allows you to subscribe to the async queuer state.
|
|
9
|
+
*
|
|
10
|
+
* This is useful for opting into state re-renders for specific parts of the queuer state
|
|
11
|
+
* deep in your component tree without needing to pass a selector to the hook.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* <queuer.Subscribe selector={(state) => ({ size: state.size })}>
|
|
15
|
+
* {({ size }) => (
|
|
16
|
+
* <div>Queue Size: {size}</div>
|
|
17
|
+
* )}
|
|
18
|
+
* </queuer.Subscribe>
|
|
19
|
+
*/
|
|
20
|
+
Subscribe: <TSelected>(props: {
|
|
21
|
+
selector: (state: AsyncQueuerState<TValue>) => TSelected;
|
|
22
|
+
children: ((state: TSelected) => ComponentChildren) | ComponentChildren;
|
|
23
|
+
}) => ComponentChildren;
|
|
24
|
+
/**
|
|
25
|
+
* Reactive state that will be updated and re-rendered when the queuer state changes
|
|
26
|
+
*
|
|
27
|
+
* Use this instead of `queuer.store.state`
|
|
28
|
+
*/
|
|
29
|
+
readonly state: Readonly<TSelected>;
|
|
30
|
+
/**
|
|
31
|
+
* @deprecated Use `queuer.state` instead of `queuer.store.state` if you want to read reactive state.
|
|
32
|
+
* The state on the store object is not reactive, as it has not been wrapped in a `useStore` hook internally.
|
|
33
|
+
* Although, you can make the state reactive by using the `useStore` in your own usage.
|
|
34
|
+
*/
|
|
35
|
+
readonly store: Store<Readonly<AsyncQueuerState<TValue>>>;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* A lower-level Preact hook that creates an `AsyncQueuer` instance for managing an async queue of items.
|
|
39
|
+
*
|
|
40
|
+
* Features:
|
|
41
|
+
* - Priority queue support via getPriority option
|
|
42
|
+
* - Configurable concurrency limit
|
|
43
|
+
* - Task success/error/completion callbacks
|
|
44
|
+
* - FIFO (First In First Out) or LIFO (Last In First Out) queue behavior
|
|
45
|
+
* - Pause/resume task processing
|
|
46
|
+
* - Task cancellation
|
|
47
|
+
* - Item expiration to clear stale items from the queue
|
|
48
|
+
*
|
|
49
|
+
* Tasks are processed concurrently up to the configured concurrency limit. When a task completes,
|
|
50
|
+
* the next pending task is processed if below the concurrency limit.
|
|
51
|
+
*
|
|
52
|
+
* Error Handling:
|
|
53
|
+
* - If an `onError` handler is provided, it will be called with the error and queuer instance
|
|
54
|
+
* - If `throwOnError` is true (default when no onError handler is provided), the error will be thrown
|
|
55
|
+
* - If `throwOnError` is false (default when onError handler is provided), the error will be swallowed
|
|
56
|
+
* - Both onError and throwOnError can be used together - the handler will be called before any error is thrown
|
|
57
|
+
* - The error state can be checked using the underlying AsyncQueuer instance
|
|
58
|
+
*
|
|
59
|
+
* ## State Management and Selector
|
|
60
|
+
*
|
|
61
|
+
* The hook uses TanStack Store for reactive state management. You can subscribe to state changes
|
|
62
|
+
* in two ways:
|
|
63
|
+
*
|
|
64
|
+
* **1. Using `queuer.Subscribe` HOC (Recommended for component tree subscriptions)**
|
|
65
|
+
*
|
|
66
|
+
* Use the `Subscribe` HOC to subscribe to state changes deep in your component tree without
|
|
67
|
+
* needing to pass a selector to the hook. This is ideal when you want to subscribe to state
|
|
68
|
+
* in child components.
|
|
69
|
+
*
|
|
70
|
+
* **2. Using the `selector` parameter (For hook-level subscriptions)**
|
|
71
|
+
*
|
|
72
|
+
* The `selector` parameter allows you to specify which state changes will trigger a re-render
|
|
73
|
+
* at the hook level, optimizing performance by preventing unnecessary re-renders when irrelevant
|
|
74
|
+
* state changes occur.
|
|
75
|
+
*
|
|
76
|
+
* **By default, there will be no reactive state subscriptions** and you must opt-in to state
|
|
77
|
+
* tracking by providing a selector function or using the `Subscribe` HOC. This prevents unnecessary
|
|
78
|
+
* re-renders and gives you full control over when your component updates.
|
|
79
|
+
*
|
|
80
|
+
* Available state properties:
|
|
81
|
+
* - `activeItems`: Items currently being processed by the queuer
|
|
82
|
+
* - `errorCount`: Number of task executions that have resulted in errors
|
|
83
|
+
* - `expirationCount`: Number of items that have been removed due to expiration
|
|
84
|
+
* - `isEmpty`: Whether the queuer has no items to process
|
|
85
|
+
* - `isFull`: Whether the queuer has reached its maximum capacity
|
|
86
|
+
* - `isIdle`: Whether the queuer is not currently processing any items
|
|
87
|
+
* - `isRunning`: Whether the queuer is active and will process items automatically
|
|
88
|
+
* - `items`: Array of items currently waiting to be processed
|
|
89
|
+
* - `itemTimestamps`: Timestamps when items were added for expiration tracking
|
|
90
|
+
* - `lastResult`: The result from the most recent task execution
|
|
91
|
+
* - `pendingTick`: Whether the queuer has a pending timeout for processing the next item
|
|
92
|
+
* - `rejectionCount`: Number of items that have been rejected from being added
|
|
93
|
+
* - `settledCount`: Number of task executions that have completed (success or error)
|
|
94
|
+
* - `size`: Number of items currently in the queue
|
|
95
|
+
* - `status`: Current processing status ('idle' | 'running' | 'stopped')
|
|
96
|
+
* - `successCount`: Number of task executions that have completed successfully
|
|
97
|
+
*
|
|
98
|
+
* @example
|
|
99
|
+
* ```tsx
|
|
100
|
+
* // Default behavior - no reactive state subscriptions
|
|
101
|
+
* const asyncQueuer = useAsyncQueuer(
|
|
102
|
+
* async (item) => {
|
|
103
|
+
* const result = await processItem(item);
|
|
104
|
+
* return result;
|
|
105
|
+
* },
|
|
106
|
+
* { concurrency: 2, maxSize: 100, started: false }
|
|
107
|
+
* );
|
|
108
|
+
*
|
|
109
|
+
* // Subscribe to state changes deep in component tree using Subscribe HOC
|
|
110
|
+
* <asyncQueuer.Subscribe selector={(state) => ({ size: state.size })}>
|
|
111
|
+
* {({ size }) => (
|
|
112
|
+
* <div>Queue Size: {size}</div>
|
|
113
|
+
* )}
|
|
114
|
+
* </asyncQueuer.Subscribe>
|
|
115
|
+
*
|
|
116
|
+
* // Opt-in to re-render when queue size changes at hook level (optimized for displaying queue length)
|
|
117
|
+
* const asyncQueuer = useAsyncQueuer(
|
|
118
|
+
* async (item) => {
|
|
119
|
+
* const result = await processItem(item);
|
|
120
|
+
* return result;
|
|
121
|
+
* },
|
|
122
|
+
* { concurrency: 2, maxSize: 100, started: false },
|
|
123
|
+
* (state) => ({
|
|
124
|
+
* size: state.size,
|
|
125
|
+
* isEmpty: state.isEmpty,
|
|
126
|
+
* isFull: state.isFull
|
|
127
|
+
* })
|
|
128
|
+
* );
|
|
129
|
+
*
|
|
130
|
+
* // Opt-in to re-render when processing state changes (optimized for loading indicators)
|
|
131
|
+
* const asyncQueuer = useAsyncQueuer(
|
|
132
|
+
* async (item) => {
|
|
133
|
+
* const result = await processItem(item);
|
|
134
|
+
* return result;
|
|
135
|
+
* },
|
|
136
|
+
* { concurrency: 2, maxSize: 100, started: false },
|
|
137
|
+
* (state) => ({
|
|
138
|
+
* isRunning: state.isRunning,
|
|
139
|
+
* isIdle: state.isIdle,
|
|
140
|
+
* status: state.status,
|
|
141
|
+
* activeItems: state.activeItems,
|
|
142
|
+
* pendingTick: state.pendingTick
|
|
143
|
+
* })
|
|
144
|
+
* );
|
|
145
|
+
*
|
|
146
|
+
* // Opt-in to re-render when execution metrics change (optimized for stats display)
|
|
147
|
+
* const asyncQueuer = useAsyncQueuer(
|
|
148
|
+
* async (item) => {
|
|
149
|
+
* const result = await processItem(item);
|
|
150
|
+
* return result;
|
|
151
|
+
* },
|
|
152
|
+
* { concurrency: 2, maxSize: 100, started: false },
|
|
153
|
+
* (state) => ({
|
|
154
|
+
* successCount: state.successCount,
|
|
155
|
+
* errorCount: state.errorCount,
|
|
156
|
+
* settledCount: state.settledCount,
|
|
157
|
+
* expirationCount: state.expirationCount,
|
|
158
|
+
* rejectionCount: state.rejectionCount
|
|
159
|
+
* })
|
|
160
|
+
* );
|
|
161
|
+
*
|
|
162
|
+
* // Opt-in to re-render when results are available (optimized for data display)
|
|
163
|
+
* const asyncQueuer = useAsyncQueuer(
|
|
164
|
+
* async (item) => {
|
|
165
|
+
* const result = await processItem(item);
|
|
166
|
+
* return result;
|
|
167
|
+
* },
|
|
168
|
+
* {
|
|
169
|
+
* concurrency: 2,
|
|
170
|
+
* maxSize: 100,
|
|
171
|
+
* started: false,
|
|
172
|
+
* onSuccess: (result) => {
|
|
173
|
+
* console.log('Item processed:', result);
|
|
174
|
+
* },
|
|
175
|
+
* onError: (error) => {
|
|
176
|
+
* console.error('Processing failed:', error);
|
|
177
|
+
* }
|
|
178
|
+
* },
|
|
179
|
+
* (state) => ({
|
|
180
|
+
* lastResult: state.lastResult,
|
|
181
|
+
* successCount: state.successCount
|
|
182
|
+
* })
|
|
183
|
+
* );
|
|
184
|
+
*
|
|
185
|
+
* // Add items to queue
|
|
186
|
+
* asyncQueuer.addItem(newItem);
|
|
187
|
+
*
|
|
188
|
+
* // Start processing
|
|
189
|
+
* asyncQueuer.start();
|
|
190
|
+
*
|
|
191
|
+
* // Access the selected state (will be empty object {} unless selector provided)
|
|
192
|
+
* const { size, isRunning, activeItems } = asyncQueuer.state;
|
|
193
|
+
* ```
|
|
194
|
+
*/
|
|
195
|
+
declare function useAsyncQueuer<TValue, TSelected = {}>(fn: (value: TValue) => Promise<any>, options?: AsyncQueuerOptions<TValue>, selector?: (state: AsyncQueuerState<TValue>) => TSelected): PreactAsyncQueuer<TValue, TSelected>;
|
|
196
|
+
//#endregion
|
|
197
|
+
export { PreactAsyncQueuer, useAsyncQueuer };
|
|
198
|
+
//# sourceMappingURL=useAsyncQueuer.d.cts.map
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
import { ComponentChildren } from "preact";
|
|
2
|
+
import { Store } from "@tanstack/preact-store";
|
|
3
|
+
import { AsyncQueuer, AsyncQueuerOptions, AsyncQueuerState } from "@tanstack/pacer/async-queuer";
|
|
4
|
+
|
|
5
|
+
//#region src/async-queuer/useAsyncQueuer.d.ts
|
|
6
|
+
interface PreactAsyncQueuer<TValue, TSelected = {}> extends Omit<AsyncQueuer<TValue>, 'store'> {
|
|
7
|
+
/**
|
|
8
|
+
* A Preact HOC (Higher Order Component) that allows you to subscribe to the async queuer state.
|
|
9
|
+
*
|
|
10
|
+
* This is useful for opting into state re-renders for specific parts of the queuer state
|
|
11
|
+
* deep in your component tree without needing to pass a selector to the hook.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* <queuer.Subscribe selector={(state) => ({ size: state.size })}>
|
|
15
|
+
* {({ size }) => (
|
|
16
|
+
* <div>Queue Size: {size}</div>
|
|
17
|
+
* )}
|
|
18
|
+
* </queuer.Subscribe>
|
|
19
|
+
*/
|
|
20
|
+
Subscribe: <TSelected>(props: {
|
|
21
|
+
selector: (state: AsyncQueuerState<TValue>) => TSelected;
|
|
22
|
+
children: ((state: TSelected) => ComponentChildren) | ComponentChildren;
|
|
23
|
+
}) => ComponentChildren;
|
|
24
|
+
/**
|
|
25
|
+
* Reactive state that will be updated and re-rendered when the queuer state changes
|
|
26
|
+
*
|
|
27
|
+
* Use this instead of `queuer.store.state`
|
|
28
|
+
*/
|
|
29
|
+
readonly state: Readonly<TSelected>;
|
|
30
|
+
/**
|
|
31
|
+
* @deprecated Use `queuer.state` instead of `queuer.store.state` if you want to read reactive state.
|
|
32
|
+
* The state on the store object is not reactive, as it has not been wrapped in a `useStore` hook internally.
|
|
33
|
+
* Although, you can make the state reactive by using the `useStore` in your own usage.
|
|
34
|
+
*/
|
|
35
|
+
readonly store: Store<Readonly<AsyncQueuerState<TValue>>>;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* A lower-level Preact hook that creates an `AsyncQueuer` instance for managing an async queue of items.
|
|
39
|
+
*
|
|
40
|
+
* Features:
|
|
41
|
+
* - Priority queue support via getPriority option
|
|
42
|
+
* - Configurable concurrency limit
|
|
43
|
+
* - Task success/error/completion callbacks
|
|
44
|
+
* - FIFO (First In First Out) or LIFO (Last In First Out) queue behavior
|
|
45
|
+
* - Pause/resume task processing
|
|
46
|
+
* - Task cancellation
|
|
47
|
+
* - Item expiration to clear stale items from the queue
|
|
48
|
+
*
|
|
49
|
+
* Tasks are processed concurrently up to the configured concurrency limit. When a task completes,
|
|
50
|
+
* the next pending task is processed if below the concurrency limit.
|
|
51
|
+
*
|
|
52
|
+
* Error Handling:
|
|
53
|
+
* - If an `onError` handler is provided, it will be called with the error and queuer instance
|
|
54
|
+
* - If `throwOnError` is true (default when no onError handler is provided), the error will be thrown
|
|
55
|
+
* - If `throwOnError` is false (default when onError handler is provided), the error will be swallowed
|
|
56
|
+
* - Both onError and throwOnError can be used together - the handler will be called before any error is thrown
|
|
57
|
+
* - The error state can be checked using the underlying AsyncQueuer instance
|
|
58
|
+
*
|
|
59
|
+
* ## State Management and Selector
|
|
60
|
+
*
|
|
61
|
+
* The hook uses TanStack Store for reactive state management. You can subscribe to state changes
|
|
62
|
+
* in two ways:
|
|
63
|
+
*
|
|
64
|
+
* **1. Using `queuer.Subscribe` HOC (Recommended for component tree subscriptions)**
|
|
65
|
+
*
|
|
66
|
+
* Use the `Subscribe` HOC to subscribe to state changes deep in your component tree without
|
|
67
|
+
* needing to pass a selector to the hook. This is ideal when you want to subscribe to state
|
|
68
|
+
* in child components.
|
|
69
|
+
*
|
|
70
|
+
* **2. Using the `selector` parameter (For hook-level subscriptions)**
|
|
71
|
+
*
|
|
72
|
+
* The `selector` parameter allows you to specify which state changes will trigger a re-render
|
|
73
|
+
* at the hook level, optimizing performance by preventing unnecessary re-renders when irrelevant
|
|
74
|
+
* state changes occur.
|
|
75
|
+
*
|
|
76
|
+
* **By default, there will be no reactive state subscriptions** and you must opt-in to state
|
|
77
|
+
* tracking by providing a selector function or using the `Subscribe` HOC. This prevents unnecessary
|
|
78
|
+
* re-renders and gives you full control over when your component updates.
|
|
79
|
+
*
|
|
80
|
+
* Available state properties:
|
|
81
|
+
* - `activeItems`: Items currently being processed by the queuer
|
|
82
|
+
* - `errorCount`: Number of task executions that have resulted in errors
|
|
83
|
+
* - `expirationCount`: Number of items that have been removed due to expiration
|
|
84
|
+
* - `isEmpty`: Whether the queuer has no items to process
|
|
85
|
+
* - `isFull`: Whether the queuer has reached its maximum capacity
|
|
86
|
+
* - `isIdle`: Whether the queuer is not currently processing any items
|
|
87
|
+
* - `isRunning`: Whether the queuer is active and will process items automatically
|
|
88
|
+
* - `items`: Array of items currently waiting to be processed
|
|
89
|
+
* - `itemTimestamps`: Timestamps when items were added for expiration tracking
|
|
90
|
+
* - `lastResult`: The result from the most recent task execution
|
|
91
|
+
* - `pendingTick`: Whether the queuer has a pending timeout for processing the next item
|
|
92
|
+
* - `rejectionCount`: Number of items that have been rejected from being added
|
|
93
|
+
* - `settledCount`: Number of task executions that have completed (success or error)
|
|
94
|
+
* - `size`: Number of items currently in the queue
|
|
95
|
+
* - `status`: Current processing status ('idle' | 'running' | 'stopped')
|
|
96
|
+
* - `successCount`: Number of task executions that have completed successfully
|
|
97
|
+
*
|
|
98
|
+
* @example
|
|
99
|
+
* ```tsx
|
|
100
|
+
* // Default behavior - no reactive state subscriptions
|
|
101
|
+
* const asyncQueuer = useAsyncQueuer(
|
|
102
|
+
* async (item) => {
|
|
103
|
+
* const result = await processItem(item);
|
|
104
|
+
* return result;
|
|
105
|
+
* },
|
|
106
|
+
* { concurrency: 2, maxSize: 100, started: false }
|
|
107
|
+
* );
|
|
108
|
+
*
|
|
109
|
+
* // Subscribe to state changes deep in component tree using Subscribe HOC
|
|
110
|
+
* <asyncQueuer.Subscribe selector={(state) => ({ size: state.size })}>
|
|
111
|
+
* {({ size }) => (
|
|
112
|
+
* <div>Queue Size: {size}</div>
|
|
113
|
+
* )}
|
|
114
|
+
* </asyncQueuer.Subscribe>
|
|
115
|
+
*
|
|
116
|
+
* // Opt-in to re-render when queue size changes at hook level (optimized for displaying queue length)
|
|
117
|
+
* const asyncQueuer = useAsyncQueuer(
|
|
118
|
+
* async (item) => {
|
|
119
|
+
* const result = await processItem(item);
|
|
120
|
+
* return result;
|
|
121
|
+
* },
|
|
122
|
+
* { concurrency: 2, maxSize: 100, started: false },
|
|
123
|
+
* (state) => ({
|
|
124
|
+
* size: state.size,
|
|
125
|
+
* isEmpty: state.isEmpty,
|
|
126
|
+
* isFull: state.isFull
|
|
127
|
+
* })
|
|
128
|
+
* );
|
|
129
|
+
*
|
|
130
|
+
* // Opt-in to re-render when processing state changes (optimized for loading indicators)
|
|
131
|
+
* const asyncQueuer = useAsyncQueuer(
|
|
132
|
+
* async (item) => {
|
|
133
|
+
* const result = await processItem(item);
|
|
134
|
+
* return result;
|
|
135
|
+
* },
|
|
136
|
+
* { concurrency: 2, maxSize: 100, started: false },
|
|
137
|
+
* (state) => ({
|
|
138
|
+
* isRunning: state.isRunning,
|
|
139
|
+
* isIdle: state.isIdle,
|
|
140
|
+
* status: state.status,
|
|
141
|
+
* activeItems: state.activeItems,
|
|
142
|
+
* pendingTick: state.pendingTick
|
|
143
|
+
* })
|
|
144
|
+
* );
|
|
145
|
+
*
|
|
146
|
+
* // Opt-in to re-render when execution metrics change (optimized for stats display)
|
|
147
|
+
* const asyncQueuer = useAsyncQueuer(
|
|
148
|
+
* async (item) => {
|
|
149
|
+
* const result = await processItem(item);
|
|
150
|
+
* return result;
|
|
151
|
+
* },
|
|
152
|
+
* { concurrency: 2, maxSize: 100, started: false },
|
|
153
|
+
* (state) => ({
|
|
154
|
+
* successCount: state.successCount,
|
|
155
|
+
* errorCount: state.errorCount,
|
|
156
|
+
* settledCount: state.settledCount,
|
|
157
|
+
* expirationCount: state.expirationCount,
|
|
158
|
+
* rejectionCount: state.rejectionCount
|
|
159
|
+
* })
|
|
160
|
+
* );
|
|
161
|
+
*
|
|
162
|
+
* // Opt-in to re-render when results are available (optimized for data display)
|
|
163
|
+
* const asyncQueuer = useAsyncQueuer(
|
|
164
|
+
* async (item) => {
|
|
165
|
+
* const result = await processItem(item);
|
|
166
|
+
* return result;
|
|
167
|
+
* },
|
|
168
|
+
* {
|
|
169
|
+
* concurrency: 2,
|
|
170
|
+
* maxSize: 100,
|
|
171
|
+
* started: false,
|
|
172
|
+
* onSuccess: (result) => {
|
|
173
|
+
* console.log('Item processed:', result);
|
|
174
|
+
* },
|
|
175
|
+
* onError: (error) => {
|
|
176
|
+
* console.error('Processing failed:', error);
|
|
177
|
+
* }
|
|
178
|
+
* },
|
|
179
|
+
* (state) => ({
|
|
180
|
+
* lastResult: state.lastResult,
|
|
181
|
+
* successCount: state.successCount
|
|
182
|
+
* })
|
|
183
|
+
* );
|
|
184
|
+
*
|
|
185
|
+
* // Add items to queue
|
|
186
|
+
* asyncQueuer.addItem(newItem);
|
|
187
|
+
*
|
|
188
|
+
* // Start processing
|
|
189
|
+
* asyncQueuer.start();
|
|
190
|
+
*
|
|
191
|
+
* // Access the selected state (will be empty object {} unless selector provided)
|
|
192
|
+
* const { size, isRunning, activeItems } = asyncQueuer.state;
|
|
193
|
+
* ```
|
|
194
|
+
*/
|
|
195
|
+
declare function useAsyncQueuer<TValue, TSelected = {}>(fn: (value: TValue) => Promise<any>, options?: AsyncQueuerOptions<TValue>, selector?: (state: AsyncQueuerState<TValue>) => TSelected): PreactAsyncQueuer<TValue, TSelected>;
|
|
196
|
+
//#endregion
|
|
197
|
+
export { PreactAsyncQueuer, useAsyncQueuer };
|
|
198
|
+
//# sourceMappingURL=useAsyncQueuer.d.ts.map
|