@tanstack/solid-pacer 0.22.0 → 0.23.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/README.md +4 -4
- package/dist/async-batcher/createAsyncBatcher.d.ts +5 -9
- package/dist/async-batcher/createAsyncBatcher.js +2 -5
- package/dist/async-debouncer/createAsyncDebouncer.d.ts +8 -11
- package/dist/async-debouncer/createAsyncDebouncer.js +5 -7
- package/dist/async-queuer/createAsyncQueuer.d.ts +9 -11
- package/dist/async-queuer/createAsyncQueuer.js +4 -5
- package/dist/async-rate-limiter/createAsyncRateLimiter.d.ts +10 -12
- package/dist/async-rate-limiter/createAsyncRateLimiter.js +7 -8
- package/dist/async-throttler/createAsyncThrottler.d.ts +8 -13
- package/dist/async-throttler/createAsyncThrottler.js +5 -9
- package/dist/batcher/createBatcher.d.ts +11 -14
- package/dist/batcher/createBatcher.js +6 -8
- package/dist/debouncer/createDebouncedSignal.d.ts +2 -4
- package/dist/debouncer/createDebouncedSignal.js +1 -2
- package/dist/debouncer/createDebouncedValue.d.ts +2 -4
- package/dist/debouncer/createDebouncedValue.js +1 -2
- package/dist/debouncer/createDebouncer.d.ts +4 -6
- package/dist/debouncer/createDebouncer.js +1 -2
- package/dist/provider/PacerProvider.d.ts +6 -8
- package/dist/provider/PacerProvider.js +1 -2
- package/dist/queuer/createQueuedSignal.d.ts +2 -4
- package/dist/queuer/createQueuedSignal.js +1 -2
- package/dist/queuer/createQueuer.d.ts +4 -6
- package/dist/queuer/createQueuer.js +1 -2
- package/dist/rate-limiter/createRateLimitedSignal.d.ts +12 -13
- package/dist/rate-limiter/createRateLimitedSignal.js +11 -11
- package/dist/rate-limiter/createRateLimitedValue.d.ts +2 -4
- package/dist/rate-limiter/createRateLimitedValue.js +1 -2
- package/dist/rate-limiter/createRateLimiter.d.ts +8 -9
- package/dist/rate-limiter/createRateLimiter.js +5 -5
- package/dist/throttler/createThrottledSignal.d.ts +2 -6
- package/dist/throttler/createThrottledSignal.js +1 -4
- package/dist/throttler/createThrottledValue.d.ts +2 -6
- package/dist/throttler/createThrottledValue.js +1 -4
- package/dist/throttler/createThrottler.d.ts +4 -8
- package/dist/throttler/createThrottler.js +1 -4
- package/package.json +23 -67
- package/dist/async-batcher/createAsyncBatcher.cjs +0 -166
- package/dist/async-batcher/createAsyncBatcher.cjs.map +0 -1
- package/dist/async-batcher/createAsyncBatcher.d.cts +0 -177
- package/dist/async-batcher/createAsyncBatcher.js.map +0 -1
- package/dist/async-batcher/index.cjs +0 -11
- package/dist/async-batcher/index.d.cts +0 -3
- package/dist/async-debouncer/createAsyncDebouncer.cjs +0 -151
- package/dist/async-debouncer/createAsyncDebouncer.cjs.map +0 -1
- package/dist/async-debouncer/createAsyncDebouncer.d.cts +0 -163
- package/dist/async-debouncer/createAsyncDebouncer.js.map +0 -1
- package/dist/async-debouncer/index.cjs +0 -11
- package/dist/async-debouncer/index.d.cts +0 -3
- package/dist/async-queuer/createAsyncQueuer.cjs +0 -158
- package/dist/async-queuer/createAsyncQueuer.cjs.map +0 -1
- package/dist/async-queuer/createAsyncQueuer.d.cts +0 -169
- package/dist/async-queuer/createAsyncQueuer.js.map +0 -1
- package/dist/async-queuer/index.cjs +0 -11
- package/dist/async-queuer/index.d.cts +0 -3
- package/dist/async-rate-limiter/createAsyncRateLimiter.cjs +0 -193
- package/dist/async-rate-limiter/createAsyncRateLimiter.cjs.map +0 -1
- package/dist/async-rate-limiter/createAsyncRateLimiter.d.cts +0 -208
- package/dist/async-rate-limiter/createAsyncRateLimiter.js.map +0 -1
- package/dist/async-rate-limiter/index.cjs +0 -11
- package/dist/async-rate-limiter/index.d.cts +0 -3
- package/dist/async-throttler/createAsyncThrottler.cjs +0 -151
- package/dist/async-throttler/createAsyncThrottler.cjs.map +0 -1
- package/dist/async-throttler/createAsyncThrottler.d.cts +0 -163
- package/dist/async-throttler/createAsyncThrottler.js.map +0 -1
- package/dist/async-throttler/index.cjs +0 -11
- package/dist/async-throttler/index.d.cts +0 -3
- package/dist/batcher/createBatcher.cjs +0 -132
- package/dist/batcher/createBatcher.cjs.map +0 -1
- package/dist/batcher/createBatcher.d.cts +0 -146
- package/dist/batcher/createBatcher.js.map +0 -1
- package/dist/batcher/index.cjs +0 -11
- package/dist/batcher/index.d.cts +0 -3
- package/dist/debouncer/createDebouncedSignal.cjs +0 -88
- package/dist/debouncer/createDebouncedSignal.cjs.map +0 -1
- package/dist/debouncer/createDebouncedSignal.d.cts +0 -79
- package/dist/debouncer/createDebouncedSignal.js.map +0 -1
- package/dist/debouncer/createDebouncedValue.cjs +0 -77
- package/dist/debouncer/createDebouncedValue.cjs.map +0 -1
- package/dist/debouncer/createDebouncedValue.d.cts +0 -70
- package/dist/debouncer/createDebouncedValue.js.map +0 -1
- package/dist/debouncer/createDebouncer.cjs +0 -129
- package/dist/debouncer/createDebouncer.cjs.map +0 -1
- package/dist/debouncer/createDebouncer.d.cts +0 -144
- package/dist/debouncer/createDebouncer.js.map +0 -1
- package/dist/debouncer/index.cjs +0 -15
- package/dist/debouncer/index.d.cts +0 -5
- package/dist/index.cjs +0 -47
- package/dist/index.d.cts +0 -20
- package/dist/provider/PacerProvider.cjs +0 -27
- package/dist/provider/PacerProvider.cjs.map +0 -1
- package/dist/provider/PacerProvider.d.cts +0 -28
- package/dist/provider/PacerProvider.js.map +0 -1
- package/dist/provider/index.cjs +0 -6
- package/dist/provider/index.d.cts +0 -2
- package/dist/queuer/createQueuedSignal.cjs +0 -129
- package/dist/queuer/createQueuedSignal.cjs.map +0 -1
- package/dist/queuer/createQueuedSignal.d.cts +0 -121
- package/dist/queuer/createQueuedSignal.js.map +0 -1
- package/dist/queuer/createQueuer.cjs +0 -133
- package/dist/queuer/createQueuer.cjs.map +0 -1
- package/dist/queuer/createQueuer.d.cts +0 -147
- package/dist/queuer/createQueuer.js.map +0 -1
- package/dist/queuer/index.cjs +0 -13
- package/dist/queuer/index.d.cts +0 -4
- package/dist/rate-limiter/createRateLimitedSignal.cjs +0 -102
- package/dist/rate-limiter/createRateLimitedSignal.cjs.map +0 -1
- package/dist/rate-limiter/createRateLimitedSignal.d.cts +0 -93
- package/dist/rate-limiter/createRateLimitedSignal.js.map +0 -1
- package/dist/rate-limiter/createRateLimitedValue.cjs +0 -102
- package/dist/rate-limiter/createRateLimitedValue.cjs.map +0 -1
- package/dist/rate-limiter/createRateLimitedValue.d.cts +0 -95
- package/dist/rate-limiter/createRateLimitedValue.js.map +0 -1
- package/dist/rate-limiter/createRateLimiter.cjs +0 -157
- package/dist/rate-limiter/createRateLimiter.cjs.map +0 -1
- package/dist/rate-limiter/createRateLimiter.d.cts +0 -173
- package/dist/rate-limiter/createRateLimiter.js.map +0 -1
- package/dist/rate-limiter/index.cjs +0 -15
- package/dist/rate-limiter/index.d.cts +0 -5
- package/dist/throttler/createThrottledSignal.cjs +0 -79
- package/dist/throttler/createThrottledSignal.cjs.map +0 -1
- package/dist/throttler/createThrottledSignal.d.cts +0 -70
- package/dist/throttler/createThrottledSignal.js.map +0 -1
- package/dist/throttler/createThrottledValue.cjs +0 -74
- package/dist/throttler/createThrottledValue.cjs.map +0 -1
- package/dist/throttler/createThrottledValue.d.cts +0 -67
- package/dist/throttler/createThrottledValue.js.map +0 -1
- package/dist/throttler/createThrottler.cjs +0 -132
- package/dist/throttler/createThrottler.cjs.map +0 -1
- package/dist/throttler/createThrottler.d.cts +0 -147
- package/dist/throttler/createThrottler.js.map +0 -1
- package/dist/throttler/index.cjs +0 -15
- package/dist/throttler/index.d.cts +0 -5
- package/dist/types/index.cjs +0 -8
- package/dist/types/index.d.cts +0 -1
- package/dist/utils/index.cjs +0 -8
- package/dist/utils/index.d.cts +0 -1
- package/src/async-batcher/createAsyncBatcher.ts +0 -234
- package/src/async-batcher/index.ts +0 -3
- package/src/async-debouncer/createAsyncDebouncer.ts +0 -225
- package/src/async-debouncer/index.ts +0 -4
- package/src/async-queuer/createAsyncQueuer.ts +0 -226
- package/src/async-queuer/index.ts +0 -3
- package/src/async-rate-limiter/createAsyncRateLimiter.ts +0 -269
- package/src/async-rate-limiter/index.ts +0 -4
- package/src/async-throttler/createAsyncThrottler.ts +0 -225
- package/src/async-throttler/index.ts +0 -4
- package/src/batcher/createBatcher.ts +0 -199
- package/src/batcher/index.ts +0 -3
- package/src/debouncer/createDebouncedSignal.ts +0 -92
- package/src/debouncer/createDebouncedValue.ts +0 -85
- package/src/debouncer/createDebouncer.ts +0 -201
- package/src/debouncer/index.ts +0 -6
- package/src/index.ts +0 -46
- package/src/provider/PacerProvider.tsx +0 -63
- package/src/provider/index.ts +0 -1
- package/src/queuer/createQueuedSignal.ts +0 -138
- package/src/queuer/createQueuer.ts +0 -199
- package/src/queuer/index.ts +0 -4
- package/src/rate-limiter/createRateLimitedSignal.ts +0 -117
- package/src/rate-limiter/createRateLimitedValue.ts +0 -110
- package/src/rate-limiter/createRateLimiter.ts +0 -226
- package/src/rate-limiter/index.ts +0 -5
- package/src/throttler/createThrottledSignal.ts +0 -81
- package/src/throttler/createThrottledValue.ts +0 -82
- package/src/throttler/createThrottler.ts +0 -204
- package/src/throttler/index.ts +0 -6
- package/src/types/index.ts +0 -1
- package/src/utils/index.ts +0 -1
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
import { createContext, useContext } from 'solid-js'
|
|
2
|
-
import type { JSX } from 'solid-js'
|
|
3
|
-
import type {
|
|
4
|
-
AnyAsyncFunction,
|
|
5
|
-
AnyFunction,
|
|
6
|
-
AsyncBatcherOptions,
|
|
7
|
-
AsyncDebouncerOptions,
|
|
8
|
-
AsyncQueuerOptions,
|
|
9
|
-
AsyncRateLimiterOptions,
|
|
10
|
-
AsyncThrottlerOptions,
|
|
11
|
-
BatcherOptions,
|
|
12
|
-
DebouncerOptions,
|
|
13
|
-
QueuerOptions,
|
|
14
|
-
RateLimiterOptions,
|
|
15
|
-
ThrottlerOptions,
|
|
16
|
-
} from '@tanstack/pacer'
|
|
17
|
-
|
|
18
|
-
export interface PacerProviderOptions {
|
|
19
|
-
asyncBatcher?: Partial<AsyncBatcherOptions<any>>
|
|
20
|
-
asyncDebouncer?: Partial<AsyncDebouncerOptions<AnyAsyncFunction>>
|
|
21
|
-
asyncQueuer?: Partial<AsyncQueuerOptions<any>>
|
|
22
|
-
asyncRateLimiter?: Partial<AsyncRateLimiterOptions<AnyAsyncFunction>>
|
|
23
|
-
asyncThrottler?: Partial<AsyncThrottlerOptions<AnyAsyncFunction>>
|
|
24
|
-
batcher?: Partial<BatcherOptions<any>>
|
|
25
|
-
debouncer?: Partial<DebouncerOptions<AnyFunction>>
|
|
26
|
-
queuer?: Partial<QueuerOptions<any>>
|
|
27
|
-
rateLimiter?: Partial<RateLimiterOptions<AnyFunction>>
|
|
28
|
-
throttler?: Partial<ThrottlerOptions<AnyFunction>>
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
interface PacerContextValue {
|
|
32
|
-
defaultOptions: PacerProviderOptions
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
const PacerContext = createContext<PacerContextValue | null>(null)
|
|
36
|
-
|
|
37
|
-
export interface PacerProviderProps {
|
|
38
|
-
children: JSX.Element
|
|
39
|
-
defaultOptions?: PacerProviderOptions
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
const DEFAULT_OPTIONS: PacerProviderOptions = {}
|
|
43
|
-
|
|
44
|
-
export function PacerProvider(props: PacerProviderProps) {
|
|
45
|
-
const contextValue: PacerContextValue = {
|
|
46
|
-
defaultOptions: props.defaultOptions ?? DEFAULT_OPTIONS,
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
return (
|
|
50
|
-
<PacerContext.Provider value={contextValue}>
|
|
51
|
-
{props.children}
|
|
52
|
-
</PacerContext.Provider>
|
|
53
|
-
)
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
export function usePacerContext() {
|
|
57
|
-
return useContext(PacerContext)
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
export function useDefaultPacerOptions() {
|
|
61
|
-
const context = useContext(PacerContext)
|
|
62
|
-
return context?.defaultOptions ?? {}
|
|
63
|
-
}
|
package/src/provider/index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './PacerProvider'
|
|
@@ -1,138 +0,0 @@
|
|
|
1
|
-
import { createQueuer } from './createQueuer'
|
|
2
|
-
import type { SolidQueuer, SolidQueuerOptions } from './createQueuer'
|
|
3
|
-
import type { QueuerState } from '@tanstack/pacer/queuer'
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* A Solid primitive that creates a queuer with managed state, combining Solid's signals with queuing functionality.
|
|
7
|
-
* This primitive provides both the current queue state and queue control methods.
|
|
8
|
-
*
|
|
9
|
-
* The queue state is automatically updated whenever items are added, removed, or reordered in the queue.
|
|
10
|
-
* All queue operations are reflected in the state array returned by the primitive.
|
|
11
|
-
*
|
|
12
|
-
* The queue can be started and stopped to automatically process items at a specified interval,
|
|
13
|
-
* making it useful as a scheduler. When started, it will process one item per tick, with an
|
|
14
|
-
* optional wait time between ticks.
|
|
15
|
-
*
|
|
16
|
-
* The primitive returns a tuple containing:
|
|
17
|
-
* - The current queue state as an array
|
|
18
|
-
* - The queue instance with methods for queue manipulation
|
|
19
|
-
*
|
|
20
|
-
* ## State Management and Selector
|
|
21
|
-
*
|
|
22
|
-
* The primitive uses Solid's reactive state management via the underlying queuer instance.
|
|
23
|
-
* The `selector` parameter allows you to specify which queuer state changes will trigger reactive updates,
|
|
24
|
-
* optimizing performance by preventing unnecessary updates when irrelevant state changes occur.
|
|
25
|
-
*
|
|
26
|
-
* **By default, there will be no reactive state subscriptions** and you must opt-in to state
|
|
27
|
-
* tracking by providing a selector function. This prevents unnecessary updates and gives you
|
|
28
|
-
* full control over when your component tracks state changes. Only when you provide a selector will the
|
|
29
|
-
* component track changes to the selected state values.
|
|
30
|
-
*
|
|
31
|
-
* Available queuer state properties:
|
|
32
|
-
* - `executionCount`: Number of items that have been processed by the queuer
|
|
33
|
-
* - `expirationCount`: Number of items that have been removed due to expiration
|
|
34
|
-
* - `isEmpty`: Whether the queuer has no items to process
|
|
35
|
-
* - `isFull`: Whether the queuer has reached its maximum capacity
|
|
36
|
-
* - `isIdle`: Whether the queuer is not currently processing any items
|
|
37
|
-
* - `isRunning`: Whether the queuer is active and will process items automatically
|
|
38
|
-
* - `items`: Array of items currently waiting to be processed
|
|
39
|
-
* - `itemTimestamps`: Timestamps when items were added for expiration tracking
|
|
40
|
-
* - `pendingTick`: Whether the queuer has a pending timeout for processing the next item
|
|
41
|
-
* - `rejectionCount`: Number of items that have been rejected from being added
|
|
42
|
-
* - `size`: Number of items currently in the queue
|
|
43
|
-
* - `status`: Current processing status ('idle' | 'running' | 'stopped')
|
|
44
|
-
*
|
|
45
|
-
* @example
|
|
46
|
-
* ```tsx
|
|
47
|
-
* // Default behavior - no reactive state subscriptions
|
|
48
|
-
* const [items, addItem, queue] = createQueuedSignal(
|
|
49
|
-
* (item) => console.log('Processing:', item),
|
|
50
|
-
* {
|
|
51
|
-
* initialItems: ['item1', 'item2'],
|
|
52
|
-
* started: true,
|
|
53
|
-
* wait: 1000,
|
|
54
|
-
* getPriority: (item) => item.priority
|
|
55
|
-
* }
|
|
56
|
-
* );
|
|
57
|
-
*
|
|
58
|
-
* // Opt-in to track queue contents changes (optimized for displaying queue items)
|
|
59
|
-
* const [items, addItem, queue] = createQueuedSignal(
|
|
60
|
-
* (item) => console.log('Processing:', item),
|
|
61
|
-
* { started: true, wait: 1000 },
|
|
62
|
-
* (state) => ({
|
|
63
|
-
* items: state.items,
|
|
64
|
-
* size: state.size,
|
|
65
|
-
* isEmpty: state.isEmpty
|
|
66
|
-
* })
|
|
67
|
-
* );
|
|
68
|
-
*
|
|
69
|
-
* // Opt-in to track processing state changes (optimized for loading indicators)
|
|
70
|
-
* const [items, addItem, queue] = createQueuedSignal(
|
|
71
|
-
* (item) => console.log('Processing:', item),
|
|
72
|
-
* { started: true, wait: 1000 },
|
|
73
|
-
* (state) => ({
|
|
74
|
-
* isRunning: state.isRunning,
|
|
75
|
-
* isIdle: state.isIdle,
|
|
76
|
-
* status: state.status,
|
|
77
|
-
* pendingTick: state.pendingTick
|
|
78
|
-
* })
|
|
79
|
-
* );
|
|
80
|
-
*
|
|
81
|
-
* // Opt-in to track execution metrics changes (optimized for stats display)
|
|
82
|
-
* const [items, addItem, queue] = createQueuedSignal(
|
|
83
|
-
* (item) => console.log('Processing:', item),
|
|
84
|
-
* { started: true, wait: 1000 },
|
|
85
|
-
* (state) => ({
|
|
86
|
-
* executionCount: state.executionCount,
|
|
87
|
-
* expirationCount: state.expirationCount,
|
|
88
|
-
* rejectionCount: state.rejectionCount
|
|
89
|
-
* })
|
|
90
|
-
* );
|
|
91
|
-
*
|
|
92
|
-
* // Add items to queue
|
|
93
|
-
* const handleAdd = (item) => {
|
|
94
|
-
* addItem(item);
|
|
95
|
-
* };
|
|
96
|
-
*
|
|
97
|
-
* // Start automatic processing
|
|
98
|
-
* const startProcessing = () => {
|
|
99
|
-
* queue.start();
|
|
100
|
-
* };
|
|
101
|
-
*
|
|
102
|
-
* // Stop automatic processing
|
|
103
|
-
* const stopProcessing = () => {
|
|
104
|
-
* queue.stop();
|
|
105
|
-
* };
|
|
106
|
-
*
|
|
107
|
-
* // Manual processing still available
|
|
108
|
-
* const handleProcess = () => {
|
|
109
|
-
* const nextItem = queue.getNextItem();
|
|
110
|
-
* if (nextItem) {
|
|
111
|
-
* processItem(nextItem);
|
|
112
|
-
* }
|
|
113
|
-
* };
|
|
114
|
-
*
|
|
115
|
-
* // Access the selected queuer state (will be empty object {} unless selector provided)
|
|
116
|
-
* const { size, isRunning, executionCount } = queue.state;
|
|
117
|
-
* ```
|
|
118
|
-
*/
|
|
119
|
-
export function createQueuedSignal<
|
|
120
|
-
TValue,
|
|
121
|
-
TSelected extends Pick<QueuerState<TValue>, 'items'> = Pick<
|
|
122
|
-
QueuerState<TValue>,
|
|
123
|
-
'items'
|
|
124
|
-
>,
|
|
125
|
-
>(
|
|
126
|
-
fn: (item: TValue) => void,
|
|
127
|
-
options: SolidQueuerOptions<TValue, TSelected> = {},
|
|
128
|
-
selector: (state: QueuerState<TValue>) => TSelected = (state) =>
|
|
129
|
-
({ items: state.items }) as TSelected,
|
|
130
|
-
): [
|
|
131
|
-
() => Array<TValue>,
|
|
132
|
-
SolidQueuer<TValue, TSelected>['addItem'],
|
|
133
|
-
SolidQueuer<TValue, TSelected>,
|
|
134
|
-
] {
|
|
135
|
-
const queue = createQueuer(fn, options, selector)
|
|
136
|
-
|
|
137
|
-
return [() => queue.state().items, queue.addItem, queue]
|
|
138
|
-
}
|
|
@@ -1,199 +0,0 @@
|
|
|
1
|
-
import { Queuer } from '@tanstack/pacer/queuer'
|
|
2
|
-
import { shallow, useSelector } from '@tanstack/solid-store'
|
|
3
|
-
import { createEffect, onCleanup } from 'solid-js'
|
|
4
|
-
import { useDefaultPacerOptions } from '../provider/PacerProvider'
|
|
5
|
-
import type { Store } from '@tanstack/solid-store'
|
|
6
|
-
import type { Accessor, JSX } from 'solid-js'
|
|
7
|
-
import type { QueuerOptions, QueuerState } from '@tanstack/pacer/queuer'
|
|
8
|
-
|
|
9
|
-
export interface SolidQueuerOptions<
|
|
10
|
-
TValue,
|
|
11
|
-
TSelected = {},
|
|
12
|
-
> extends QueuerOptions<TValue> {
|
|
13
|
-
/**
|
|
14
|
-
* Optional callback invoked when the owning component unmounts. Receives the queuer instance.
|
|
15
|
-
* When provided, replaces the default cleanup (stop); use it to call flush(), flushAsBatch(), stop(), add logging, etc.
|
|
16
|
-
*/
|
|
17
|
-
onUnmount?: (queuer: SolidQueuer<TValue, TSelected>) => void
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export interface SolidQueuer<TValue, TSelected = {}> extends Omit<
|
|
21
|
-
Queuer<TValue>,
|
|
22
|
-
'store'
|
|
23
|
-
> {
|
|
24
|
-
/**
|
|
25
|
-
* A Solid component that allows you to subscribe to the queuer state.
|
|
26
|
-
*
|
|
27
|
-
* This is useful for tracking specific parts of the queuer state
|
|
28
|
-
* deep in your component tree without needing to pass a selector to the hook.
|
|
29
|
-
*
|
|
30
|
-
* @example
|
|
31
|
-
* <queuer.Subscribe selector={(state) => ({ size: state.size, isRunning: state.isRunning })}>
|
|
32
|
-
* {(state) => (
|
|
33
|
-
* <div>Queue: {state().size} items, {state().isRunning ? 'Processing' : 'Idle'}</div>
|
|
34
|
-
* )}
|
|
35
|
-
* </queuer.Subscribe>
|
|
36
|
-
*/
|
|
37
|
-
Subscribe: <TSelected>(props: {
|
|
38
|
-
selector: (state: QueuerState<TValue>) => TSelected
|
|
39
|
-
children: ((state: Accessor<TSelected>) => JSX.Element) | JSX.Element
|
|
40
|
-
}) => JSX.Element
|
|
41
|
-
/**
|
|
42
|
-
* Reactive state that will be updated when the queuer state changes
|
|
43
|
-
*
|
|
44
|
-
* Use this instead of `queuer.store.state`
|
|
45
|
-
*/
|
|
46
|
-
readonly state: Accessor<Readonly<TSelected>>
|
|
47
|
-
/**
|
|
48
|
-
* @deprecated Use `queuer.state` instead of `queuer.store.state` if you want to read reactive state.
|
|
49
|
-
* The state on the store object is not reactive, as it has not been wrapped in a `useSelector` hook internally.
|
|
50
|
-
* Although, you can make the state reactive by using the `useSelector` in your own usage.
|
|
51
|
-
*/
|
|
52
|
-
readonly store: Store<Readonly<QueuerState<TValue>>>
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* Creates a Solid-compatible Queuer instance for managing a synchronous queue of items, exposing Solid signals for all stateful properties.
|
|
57
|
-
*
|
|
58
|
-
* Features:
|
|
59
|
-
* - Synchronous processing of items using the provided `fn` function
|
|
60
|
-
* - FIFO (First In First Out) or LIFO (Last In First Out) queue behavior
|
|
61
|
-
* - Priority queueing via `getPriority` or item `priority` property
|
|
62
|
-
* - Item expiration and removal of stale items
|
|
63
|
-
* - Configurable wait time between processing items
|
|
64
|
-
* - Pause/resume processing
|
|
65
|
-
* - Callbacks for queue state changes, execution, rejection, and expiration
|
|
66
|
-
* - All stateful properties (items, counts, etc.) are exposed as Solid signals for reactivity
|
|
67
|
-
*
|
|
68
|
-
* 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()`.
|
|
69
|
-
*
|
|
70
|
-
* 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.
|
|
71
|
-
*
|
|
72
|
-
* ## State Management and Selector
|
|
73
|
-
*
|
|
74
|
-
* The hook uses TanStack Store for reactive state management. You can subscribe to state changes
|
|
75
|
-
* in two ways:
|
|
76
|
-
*
|
|
77
|
-
* **1. Using `queuer.Subscribe` component (Recommended for component tree subscriptions)**
|
|
78
|
-
*
|
|
79
|
-
* Use the `Subscribe` component to subscribe to state changes deep in your component tree without
|
|
80
|
-
* needing to pass a selector to the hook. This is ideal when you want to subscribe to state
|
|
81
|
-
* in child components.
|
|
82
|
-
*
|
|
83
|
-
* **2. Using the `selector` parameter (For hook-level subscriptions)**
|
|
84
|
-
*
|
|
85
|
-
* The `selector` parameter allows you to specify which state changes will trigger reactive updates
|
|
86
|
-
* at the hook level, optimizing performance by preventing unnecessary updates when irrelevant
|
|
87
|
-
* state changes occur.
|
|
88
|
-
*
|
|
89
|
-
* **By default, there will be no reactive state subscriptions** and you must opt-in to state
|
|
90
|
-
* tracking by providing a selector function or using the `Subscribe` component. This prevents unnecessary
|
|
91
|
-
* updates and gives you full control over when your component tracks state changes.
|
|
92
|
-
*
|
|
93
|
-
* Available state properties:
|
|
94
|
-
* - `executionCount`: Number of items that have been processed
|
|
95
|
-
* - `isRunning`: Whether the queuer is currently running (not stopped)
|
|
96
|
-
* - `items`: Array of items currently queued for processing
|
|
97
|
-
* - `rejectionCount`: Number of items that were rejected (expired or failed validation)
|
|
98
|
-
*
|
|
99
|
-
* ## Unmount behavior
|
|
100
|
-
*
|
|
101
|
-
* By default, the primitive stops the queuer when the owning component unmounts.
|
|
102
|
-
* Use the `onUnmount` option to customize this. For example, to flush pending items instead:
|
|
103
|
-
*
|
|
104
|
-
* ```tsx
|
|
105
|
-
* const queue = createQueuer(fn, {
|
|
106
|
-
* started: true,
|
|
107
|
-
* wait: 1000,
|
|
108
|
-
* onUnmount: (q) => q.flush()
|
|
109
|
-
* });
|
|
110
|
-
* ```
|
|
111
|
-
*
|
|
112
|
-
* Example usage:
|
|
113
|
-
* ```tsx
|
|
114
|
-
* // Default behavior - no reactive state subscriptions
|
|
115
|
-
* const queue = createQueuer(
|
|
116
|
-
* (item) => {
|
|
117
|
-
* // process item synchronously
|
|
118
|
-
* console.log('Processing', item);
|
|
119
|
-
* },
|
|
120
|
-
* {
|
|
121
|
-
* started: true, // Start processing immediately
|
|
122
|
-
* wait: 1000, // Process one item every second
|
|
123
|
-
* getPriority: (item) => item.priority // Process higher priority items first
|
|
124
|
-
* }
|
|
125
|
-
* );
|
|
126
|
-
*
|
|
127
|
-
* // Opt-in to track items or isRunning changes (optimized for UI updates)
|
|
128
|
-
* const queue = createQueuer(
|
|
129
|
-
* (item) => console.log('Processing', item),
|
|
130
|
-
* { started: true, wait: 1000 },
|
|
131
|
-
* (state) => ({ items: state.items, isRunning: state.isRunning })
|
|
132
|
-
* );
|
|
133
|
-
*
|
|
134
|
-
* // Opt-in to track execution metrics changes (optimized for tracking progress)
|
|
135
|
-
* const queue = createQueuer(
|
|
136
|
-
* (item) => console.log('Processing', item),
|
|
137
|
-
* { started: true, wait: 1000 },
|
|
138
|
-
* (state) => ({
|
|
139
|
-
* executionCount: state.executionCount,
|
|
140
|
-
* rejectionCount: state.rejectionCount
|
|
141
|
-
* })
|
|
142
|
-
* );
|
|
143
|
-
*
|
|
144
|
-
* // Add items to process - they'll be handled automatically
|
|
145
|
-
* queue.addItem('task1');
|
|
146
|
-
* queue.addItem('task2');
|
|
147
|
-
*
|
|
148
|
-
* // Control the scheduler
|
|
149
|
-
* queue.stop(); // Pause processing
|
|
150
|
-
* queue.start(); // Resume processing
|
|
151
|
-
*
|
|
152
|
-
* // Access the selected state (will be empty object {} unless selector provided)
|
|
153
|
-
* const { items, isRunning } = queue.state();
|
|
154
|
-
* ```
|
|
155
|
-
*/
|
|
156
|
-
export function createQueuer<TValue, TSelected = {}>(
|
|
157
|
-
fn: (item: TValue) => void,
|
|
158
|
-
options: SolidQueuerOptions<TValue, TSelected> = {},
|
|
159
|
-
selector: (state: QueuerState<TValue>) => TSelected = () => ({}) as TSelected,
|
|
160
|
-
): SolidQueuer<TValue, TSelected> {
|
|
161
|
-
const mergedOptions = {
|
|
162
|
-
...useDefaultPacerOptions().queuer,
|
|
163
|
-
...options,
|
|
164
|
-
} as SolidQueuerOptions<TValue, TSelected>
|
|
165
|
-
const queuer = new Queuer(fn, mergedOptions) as unknown as SolidQueuer<
|
|
166
|
-
TValue,
|
|
167
|
-
TSelected
|
|
168
|
-
>
|
|
169
|
-
|
|
170
|
-
queuer.Subscribe = function Subscribe<TSelected>(props: {
|
|
171
|
-
selector: (state: QueuerState<TValue>) => TSelected
|
|
172
|
-
children: ((state: Accessor<TSelected>) => JSX.Element) | JSX.Element
|
|
173
|
-
}) {
|
|
174
|
-
const selected = useSelector(queuer.store, props.selector, {
|
|
175
|
-
compare: shallow,
|
|
176
|
-
})
|
|
177
|
-
|
|
178
|
-
return typeof props.children === 'function'
|
|
179
|
-
? props.children(selected)
|
|
180
|
-
: props.children
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
const state = useSelector(queuer.store, selector, { compare: shallow })
|
|
184
|
-
|
|
185
|
-
createEffect(() => {
|
|
186
|
-
onCleanup(() => {
|
|
187
|
-
if (mergedOptions.onUnmount) {
|
|
188
|
-
mergedOptions.onUnmount(queuer)
|
|
189
|
-
} else {
|
|
190
|
-
queuer.stop()
|
|
191
|
-
}
|
|
192
|
-
})
|
|
193
|
-
})
|
|
194
|
-
|
|
195
|
-
return {
|
|
196
|
-
...queuer,
|
|
197
|
-
state,
|
|
198
|
-
} as SolidQueuer<TValue, TSelected> // omit `store` in favor of `state`
|
|
199
|
-
}
|
package/src/queuer/index.ts
DELETED
|
@@ -1,117 +0,0 @@
|
|
|
1
|
-
import { createSignal } from 'solid-js'
|
|
2
|
-
import { createRateLimiter } from './createRateLimiter'
|
|
3
|
-
import type {
|
|
4
|
-
SolidRateLimiter,
|
|
5
|
-
SolidRateLimiterOptions,
|
|
6
|
-
} from './createRateLimiter'
|
|
7
|
-
import type { Accessor, Setter } from 'solid-js'
|
|
8
|
-
import type { RateLimiterState } from '@tanstack/pacer/rate-limiter'
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* A Solid hook that creates a rate-limited state value that enforces a hard limit on state updates within a time window.
|
|
12
|
-
* This hook combines Solid's createSignal with rate limiting functionality to provide controlled state updates.
|
|
13
|
-
*
|
|
14
|
-
* Rate limiting is a simple "hard limit" approach - it allows all updates until the limit is reached, then blocks
|
|
15
|
-
* subsequent updates until the window resets. Unlike throttling or debouncing, it does not attempt to space out
|
|
16
|
-
* or intelligently collapse updates. This can lead to bursts of rapid updates followed by periods of no updates.
|
|
17
|
-
*
|
|
18
|
-
* The rate limiter supports two types of windows:
|
|
19
|
-
* - 'fixed': A strict window that resets after the window period. All updates within the window count
|
|
20
|
-
* towards the limit, and the window resets completely after the period.
|
|
21
|
-
* - 'sliding': A rolling window that allows updates as old ones expire. This provides a more
|
|
22
|
-
* consistent rate of updates over time.
|
|
23
|
-
*
|
|
24
|
-
* For smoother update patterns, consider:
|
|
25
|
-
* - createThrottledSignal: When you want consistent spacing between updates (e.g. UI changes)
|
|
26
|
-
* - createDebouncedSignal: When you want to collapse rapid updates into a single update (e.g. search input)
|
|
27
|
-
*
|
|
28
|
-
* Rate limiting should primarily be used when you need to enforce strict limits, like API rate limits.
|
|
29
|
-
*
|
|
30
|
-
* The hook returns a tuple containing:
|
|
31
|
-
* - The rate-limited state value accessor
|
|
32
|
-
* - A rate-limited setter function that respects the configured limits
|
|
33
|
-
* - The rateLimiter instance for additional control
|
|
34
|
-
*
|
|
35
|
-
* For more direct control over rate limiting without state management,
|
|
36
|
-
* consider using the lower-level createRateLimiter hook instead.
|
|
37
|
-
*
|
|
38
|
-
* ## State Management and Selector
|
|
39
|
-
*
|
|
40
|
-
* The hook uses TanStack Store for reactive state management via the underlying rate limiter instance.
|
|
41
|
-
* The `selector` parameter allows you to specify which rate limiter state changes will trigger reactive updates,
|
|
42
|
-
* optimizing performance by preventing unnecessary subscriptions when irrelevant state changes occur.
|
|
43
|
-
*
|
|
44
|
-
* **By default, there will be no reactive state subscriptions** and you must opt-in to state
|
|
45
|
-
* tracking by providing a selector function. This prevents unnecessary reactive updates and gives you
|
|
46
|
-
* full control over when your component subscribes to state changes. Only when you provide a selector will
|
|
47
|
-
* the reactive system track the selected state values.
|
|
48
|
-
*
|
|
49
|
-
* Available rate limiter state properties:
|
|
50
|
-
* - `callsInWindow`: Number of calls made in the current window
|
|
51
|
-
* - `remainingInWindow`: Number of calls remaining in the current window
|
|
52
|
-
* - `windowStart`: Unix timestamp when the current window started
|
|
53
|
-
* - `nextWindowStart`: Unix timestamp when the next window will start
|
|
54
|
-
* - `msUntilNextWindow`: Milliseconds until the next window starts
|
|
55
|
-
* - `isAtLimit`: Whether the call limit for the current window has been reached
|
|
56
|
-
* - `status`: Current status ('disabled' | 'idle' | 'at-limit')
|
|
57
|
-
*
|
|
58
|
-
* @example
|
|
59
|
-
* ```tsx
|
|
60
|
-
* // Default behavior - no reactive state subscriptions
|
|
61
|
-
* const [value, setValue, rateLimiter] = createRateLimitedSignal(0, {
|
|
62
|
-
* limit: 5,
|
|
63
|
-
* window: 60000,
|
|
64
|
-
* windowType: 'sliding'
|
|
65
|
-
* });
|
|
66
|
-
*
|
|
67
|
-
* // Opt-in to reactive updates when limit state changes (optimized for UI feedback)
|
|
68
|
-
* const [value, setValue, rateLimiter] = createRateLimitedSignal(
|
|
69
|
-
* 0,
|
|
70
|
-
* { limit: 5, window: 60000 },
|
|
71
|
-
* (state) => ({ isAtLimit: state.isAtLimit, remainingInWindow: state.remainingInWindow })
|
|
72
|
-
* );
|
|
73
|
-
*
|
|
74
|
-
* // With rejection callback and fixed window
|
|
75
|
-
* const [value, setValue] = createRateLimitedSignal(0, {
|
|
76
|
-
* limit: 3,
|
|
77
|
-
* window: 5000,
|
|
78
|
-
* windowType: 'fixed',
|
|
79
|
-
* onReject: (rateLimiter) => {
|
|
80
|
-
* alert(`Rate limit reached. Try again in ${rateLimiter.getMsUntilNextWindow()}ms`);
|
|
81
|
-
* }
|
|
82
|
-
* });
|
|
83
|
-
*
|
|
84
|
-
* // Access rateLimiter state via signals
|
|
85
|
-
* const handleSubmit = () => {
|
|
86
|
-
* const remaining = rateLimiter.state().remainingInWindow;
|
|
87
|
-
* if (remaining > 0) {
|
|
88
|
-
* setValue(newValue);
|
|
89
|
-
* } else {
|
|
90
|
-
* showRateLimitWarning();
|
|
91
|
-
* }
|
|
92
|
-
* };
|
|
93
|
-
* ```
|
|
94
|
-
*/
|
|
95
|
-
export function createRateLimitedSignal<TValue, TSelected = {}>(
|
|
96
|
-
value: TValue,
|
|
97
|
-
initialOptions: SolidRateLimiterOptions<Setter<TValue>, TSelected>,
|
|
98
|
-
selector?: (state: RateLimiterState) => TSelected,
|
|
99
|
-
): [
|
|
100
|
-
Accessor<TValue>,
|
|
101
|
-
Setter<TValue>,
|
|
102
|
-
SolidRateLimiter<Setter<TValue>, TSelected>,
|
|
103
|
-
] {
|
|
104
|
-
const [rateLimitedValue, setRateLimitedValue] = createSignal<TValue>(value)
|
|
105
|
-
|
|
106
|
-
const rateLimiter = createRateLimiter(
|
|
107
|
-
setRateLimitedValue,
|
|
108
|
-
initialOptions,
|
|
109
|
-
selector,
|
|
110
|
-
)
|
|
111
|
-
|
|
112
|
-
return [
|
|
113
|
-
rateLimitedValue,
|
|
114
|
-
rateLimiter.maybeExecute as Setter<TValue>,
|
|
115
|
-
rateLimiter,
|
|
116
|
-
]
|
|
117
|
-
}
|
|
@@ -1,110 +0,0 @@
|
|
|
1
|
-
import { createEffect } from 'solid-js'
|
|
2
|
-
import { createRateLimitedSignal } from './createRateLimitedSignal'
|
|
3
|
-
import type {
|
|
4
|
-
SolidRateLimiter,
|
|
5
|
-
SolidRateLimiterOptions,
|
|
6
|
-
} from './createRateLimiter'
|
|
7
|
-
import type { Accessor, Setter } from 'solid-js'
|
|
8
|
-
import type { RateLimiterState } from '@tanstack/pacer/rate-limiter'
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* A high-level Solid hook that creates a rate-limited version of a value that updates at most a certain number of times within a time window.
|
|
12
|
-
* This hook uses Solid's createSignal internally to manage the rate-limited state.
|
|
13
|
-
*
|
|
14
|
-
* Rate limiting is a simple "hard limit" approach - it allows all updates until the limit is reached, then blocks
|
|
15
|
-
* subsequent updates until the window resets. Unlike throttling or debouncing, it does not attempt to space out
|
|
16
|
-
* or intelligently collapse updates. This can lead to bursts of rapid updates followed by periods of no updates.
|
|
17
|
-
*
|
|
18
|
-
* The rate limiter supports two types of windows:
|
|
19
|
-
* - 'fixed': A strict window that resets after the window period. All updates within the window count
|
|
20
|
-
* towards the limit, and the window resets completely after the period.
|
|
21
|
-
* - 'sliding': A rolling window that allows updates as old ones expire. This provides a more
|
|
22
|
-
* consistent rate of updates over time.
|
|
23
|
-
*
|
|
24
|
-
* For smoother update patterns, consider:
|
|
25
|
-
* - createThrottledValue: When you want consistent spacing between updates (e.g. UI changes)
|
|
26
|
-
* - createDebouncedValue: When you want to collapse rapid updates into a single update (e.g. search input)
|
|
27
|
-
*
|
|
28
|
-
* Rate limiting should primarily be used when you need to enforce strict limits, like API rate limits.
|
|
29
|
-
*
|
|
30
|
-
* The hook returns a tuple containing:
|
|
31
|
-
* - An accessor function that provides the rate-limited value
|
|
32
|
-
* - The rate limiter instance with control methods
|
|
33
|
-
*
|
|
34
|
-
* For more direct control over rate limiting behavior without Solid state management,
|
|
35
|
-
* consider using the lower-level createRateLimiter hook instead.
|
|
36
|
-
*
|
|
37
|
-
* ## State Management and Selector
|
|
38
|
-
*
|
|
39
|
-
* The hook uses TanStack Store for reactive state management via the underlying rate limiter instance.
|
|
40
|
-
* The `selector` parameter allows you to specify which rate limiter state changes will trigger reactive updates,
|
|
41
|
-
* optimizing performance by preventing unnecessary subscriptions when irrelevant state changes occur.
|
|
42
|
-
*
|
|
43
|
-
* **By default, there will be no reactive state subscriptions** and you must opt-in to state
|
|
44
|
-
* tracking by providing a selector function. This prevents unnecessary reactive updates and gives you
|
|
45
|
-
* full control over when your component subscribes to state changes. Only when you provide a selector will
|
|
46
|
-
* the reactive system track the selected state values.
|
|
47
|
-
*
|
|
48
|
-
* Available rate limiter state properties:
|
|
49
|
-
* - `executionCount`: Number of function executions that have been completed
|
|
50
|
-
* - `executionTimes`: Array of timestamps when executions occurred for rate limiting calculations
|
|
51
|
-
* - `rejectionCount`: Number of function executions that have been rejected due to rate limiting
|
|
52
|
-
*
|
|
53
|
-
* @example
|
|
54
|
-
* ```tsx
|
|
55
|
-
* // Default behavior - no reactive state subscriptions
|
|
56
|
-
* const [rateLimitedValue, rateLimiter] = createRateLimitedValue(rawValue, {
|
|
57
|
-
* limit: 5,
|
|
58
|
-
* window: 60000,
|
|
59
|
-
* windowType: 'sliding'
|
|
60
|
-
* });
|
|
61
|
-
*
|
|
62
|
-
* // Opt-in to reactive updates when execution count changes (optimized for tracking successful updates)
|
|
63
|
-
* const [rateLimitedValue, rateLimiter] = createRateLimitedValue(
|
|
64
|
-
* rawValue,
|
|
65
|
-
* { limit: 5, window: 60000, windowType: 'sliding' },
|
|
66
|
-
* (state) => ({ executionCount: state.executionCount })
|
|
67
|
-
* );
|
|
68
|
-
*
|
|
69
|
-
* // Opt-in to reactive updates when rejection count changes (optimized for tracking rate limit violations)
|
|
70
|
-
* const [rateLimitedValue, rateLimiter] = createRateLimitedValue(
|
|
71
|
-
* rawValue,
|
|
72
|
-
* { limit: 5, window: 60000, windowType: 'sliding' },
|
|
73
|
-
* (state) => ({ rejectionCount: state.rejectionCount })
|
|
74
|
-
* );
|
|
75
|
-
*
|
|
76
|
-
* // Opt-in to reactive updates when execution times change (optimized for window calculations)
|
|
77
|
-
* const [rateLimitedValue, rateLimiter] = createRateLimitedValue(
|
|
78
|
-
* rawValue,
|
|
79
|
-
* { limit: 5, window: 60000, windowType: 'sliding' },
|
|
80
|
-
* (state) => ({ executionTimes: state.executionTimes })
|
|
81
|
-
* );
|
|
82
|
-
*
|
|
83
|
-
* // With rejection callback and fixed window
|
|
84
|
-
* const [rateLimitedValue, rateLimiter] = createRateLimitedValue(rawValue, {
|
|
85
|
-
* limit: 3,
|
|
86
|
-
* window: 5000,
|
|
87
|
-
* windowType: 'fixed',
|
|
88
|
-
* onReject: (rateLimiter) => {
|
|
89
|
-
* console.log(`Update rejected. Try again in ${rateLimiter.getMsUntilNextWindow()}ms`);
|
|
90
|
-
* }
|
|
91
|
-
* });
|
|
92
|
-
*
|
|
93
|
-
* // Access the selected rate limiter state (will be empty object {} unless selector provided)
|
|
94
|
-
* const { executionCount, rejectionCount } = rateLimiter.state;
|
|
95
|
-
* ```
|
|
96
|
-
*/
|
|
97
|
-
export function createRateLimitedValue<TValue, TSelected = {}>(
|
|
98
|
-
value: Accessor<TValue>,
|
|
99
|
-
initialOptions: SolidRateLimiterOptions<Setter<TValue>, TSelected>,
|
|
100
|
-
selector?: (state: RateLimiterState) => TSelected,
|
|
101
|
-
): [Accessor<TValue>, SolidRateLimiter<Setter<TValue>, TSelected>] {
|
|
102
|
-
const [rateLimitedValue, setRateLimitedValue, rateLimiter] =
|
|
103
|
-
createRateLimitedSignal(value(), initialOptions, selector)
|
|
104
|
-
|
|
105
|
-
createEffect(() => {
|
|
106
|
-
setRateLimitedValue(value() as any)
|
|
107
|
-
})
|
|
108
|
-
|
|
109
|
-
return [rateLimitedValue, rateLimiter]
|
|
110
|
-
}
|