@tanstack/pacer 0.15.4 → 0.16.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/async-batcher.cjs +66 -5
- package/dist/cjs/async-batcher.cjs.map +1 -1
- package/dist/cjs/async-batcher.d.cts +94 -17
- package/dist/cjs/async-debouncer.cjs +53 -19
- package/dist/cjs/async-debouncer.cjs.map +1 -1
- package/dist/cjs/async-debouncer.d.cts +76 -10
- package/dist/cjs/async-queuer.cjs +58 -2
- package/dist/cjs/async-queuer.cjs.map +1 -1
- package/dist/cjs/async-queuer.d.cts +100 -10
- package/dist/cjs/async-rate-limiter.cjs +51 -2
- package/dist/cjs/async-rate-limiter.cjs.map +1 -1
- package/dist/cjs/async-rate-limiter.d.cts +91 -28
- package/dist/cjs/async-retryer.cjs +285 -0
- package/dist/cjs/async-retryer.cjs.map +1 -0
- package/dist/cjs/async-retryer.d.cts +308 -0
- package/dist/cjs/async-throttler.cjs +101 -53
- package/dist/cjs/async-throttler.cjs.map +1 -1
- package/dist/cjs/async-throttler.d.cts +82 -10
- package/dist/cjs/batcher.cjs +5 -1
- package/dist/cjs/batcher.cjs.map +1 -1
- package/dist/cjs/batcher.d.cts +10 -2
- package/dist/cjs/debouncer.cjs +5 -1
- package/dist/cjs/debouncer.cjs.map +1 -1
- package/dist/cjs/debouncer.d.cts +7 -3
- package/dist/cjs/event-client.cjs +3 -1
- package/dist/cjs/event-client.cjs.map +1 -1
- package/dist/cjs/event-client.d.cts +3 -0
- package/dist/cjs/index.cjs +13 -1
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.cts +1 -0
- package/dist/cjs/queuer.cjs +5 -1
- package/dist/cjs/queuer.cjs.map +1 -1
- package/dist/cjs/queuer.d.cts +8 -4
- package/dist/cjs/rate-limiter.cjs +5 -1
- package/dist/cjs/rate-limiter.cjs.map +1 -1
- package/dist/cjs/rate-limiter.d.cts +8 -1
- package/dist/cjs/throttler.cjs +5 -1
- package/dist/cjs/throttler.cjs.map +1 -1
- package/dist/cjs/throttler.d.cts +7 -0
- package/dist/cjs/utils.cjs +0 -10
- package/dist/cjs/utils.cjs.map +1 -1
- package/dist/cjs/utils.d.cts +0 -1
- package/dist/esm/async-batcher.d.ts +94 -17
- package/dist/esm/async-batcher.js +68 -7
- package/dist/esm/async-batcher.js.map +1 -1
- package/dist/esm/async-debouncer.d.ts +76 -10
- package/dist/esm/async-debouncer.js +55 -21
- package/dist/esm/async-debouncer.js.map +1 -1
- package/dist/esm/async-queuer.d.ts +100 -10
- package/dist/esm/async-queuer.js +60 -4
- package/dist/esm/async-queuer.js.map +1 -1
- package/dist/esm/async-rate-limiter.d.ts +91 -28
- package/dist/esm/async-rate-limiter.js +53 -4
- package/dist/esm/async-rate-limiter.js.map +1 -1
- package/dist/esm/async-retryer.d.ts +308 -0
- package/dist/esm/async-retryer.js +285 -0
- package/dist/esm/async-retryer.js.map +1 -0
- package/dist/esm/async-throttler.d.ts +82 -10
- package/dist/esm/async-throttler.js +103 -55
- package/dist/esm/async-throttler.js.map +1 -1
- package/dist/esm/batcher.d.ts +10 -2
- package/dist/esm/batcher.js +6 -2
- package/dist/esm/batcher.js.map +1 -1
- package/dist/esm/debouncer.d.ts +7 -3
- package/dist/esm/debouncer.js +7 -3
- package/dist/esm/debouncer.js.map +1 -1
- package/dist/esm/event-client.d.ts +3 -0
- package/dist/esm/event-client.js +3 -1
- package/dist/esm/event-client.js.map +1 -1
- package/dist/esm/index.d.ts +1 -0
- package/dist/esm/index.js +24 -12
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/queuer.d.ts +8 -4
- package/dist/esm/queuer.js +7 -3
- package/dist/esm/queuer.js.map +1 -1
- package/dist/esm/rate-limiter.d.ts +8 -1
- package/dist/esm/rate-limiter.js +7 -3
- package/dist/esm/rate-limiter.js.map +1 -1
- package/dist/esm/throttler.d.ts +7 -0
- package/dist/esm/throttler.js +7 -3
- package/dist/esm/throttler.js.map +1 -1
- package/dist/esm/utils.d.ts +0 -1
- package/dist/esm/utils.js +0 -10
- package/dist/esm/utils.js.map +1 -1
- package/package.json +13 -3
- package/src/async-batcher.ts +144 -22
- package/src/async-debouncer.ts +118 -33
- package/src/async-queuer.ts +147 -14
- package/src/async-rate-limiter.ts +129 -33
- package/src/async-retryer.ts +664 -0
- package/src/async-throttler.ts +196 -75
- package/src/batcher.ts +16 -4
- package/src/debouncer.ts +17 -5
- package/src/event-client.ts +6 -1
- package/src/index.ts +2 -0
- package/src/queuer.ts +19 -6
- package/src/rate-limiter.ts +18 -3
- package/src/throttler.ts +17 -2
- package/src/utils.ts +0 -15
package/src/rate-limiter.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Store } from '@tanstack/store'
|
|
2
|
-
import {
|
|
2
|
+
import { parseFunctionOrValue } from './utils'
|
|
3
3
|
import { emitChange, pacerEventClient } from './event-client'
|
|
4
4
|
import type { AnyFunction } from './types'
|
|
5
5
|
|
|
@@ -86,6 +86,18 @@ export interface RateLimiterOptions<TFn extends AnyFunction> {
|
|
|
86
86
|
windowType?: 'fixed' | 'sliding'
|
|
87
87
|
}
|
|
88
88
|
|
|
89
|
+
/**
|
|
90
|
+
* Utility function for sharing common `RateLimiterOptions` options between different `RateLimiter` instances.
|
|
91
|
+
*/
|
|
92
|
+
export function rateLimiterOptions<
|
|
93
|
+
TFn extends AnyFunction = AnyFunction,
|
|
94
|
+
TOptions extends Partial<RateLimiterOptions<TFn>> = Partial<
|
|
95
|
+
RateLimiterOptions<TFn>
|
|
96
|
+
>,
|
|
97
|
+
>(options: TOptions): TOptions {
|
|
98
|
+
return options
|
|
99
|
+
}
|
|
100
|
+
|
|
89
101
|
const defaultOptions: Omit<
|
|
90
102
|
Required<RateLimiterOptions<any>>,
|
|
91
103
|
'initialState' | 'onExecute' | 'onReject' | 'key'
|
|
@@ -102,6 +114,7 @@ const defaultOptions: Omit<
|
|
|
102
114
|
* Rate limiting is a simple approach that allows a function to execute up to a limit within a time window,
|
|
103
115
|
* then blocks all subsequent calls until the window passes. This can lead to "bursty" behavior where
|
|
104
116
|
* all executions happen immediately, followed by a complete block.
|
|
117
|
+
* This synchronous version is lighter weight and often all you need - upgrade to AsyncRateLimiter when you need promises, retry support, abort capabilities, or advanced error handling.
|
|
105
118
|
*
|
|
106
119
|
* The rate limiter supports two types of windows:
|
|
107
120
|
* - 'fixed': A strict window that resets after the window period. All executions within the window count
|
|
@@ -143,7 +156,7 @@ const defaultOptions: Omit<
|
|
|
143
156
|
export class RateLimiter<TFn extends AnyFunction> {
|
|
144
157
|
readonly store: Store<Readonly<RateLimiterState>> =
|
|
145
158
|
new Store<RateLimiterState>(getDefaultRateLimiterState())
|
|
146
|
-
key: string
|
|
159
|
+
key: string | undefined
|
|
147
160
|
options: RateLimiterOptions<TFn>
|
|
148
161
|
#timeoutIds: Set<NodeJS.Timeout> = new Set()
|
|
149
162
|
|
|
@@ -151,7 +164,7 @@ export class RateLimiter<TFn extends AnyFunction> {
|
|
|
151
164
|
public fn: TFn,
|
|
152
165
|
initialOptions: RateLimiterOptions<TFn>,
|
|
153
166
|
) {
|
|
154
|
-
this.key =
|
|
167
|
+
this.key = initialOptions.key
|
|
155
168
|
this.options = {
|
|
156
169
|
...defaultOptions,
|
|
157
170
|
...initialOptions,
|
|
@@ -358,6 +371,8 @@ export class RateLimiter<TFn extends AnyFunction> {
|
|
|
358
371
|
/**
|
|
359
372
|
* Creates a rate-limited function that will execute the provided function up to a maximum number of times within a time window.
|
|
360
373
|
*
|
|
374
|
+
* This synchronous version is lighter weight and often all you need - upgrade to asyncRateLimit when you need promises, retry support, abort capabilities, or advanced error handling.
|
|
375
|
+
*
|
|
361
376
|
* Note that rate limiting is a simpler form of execution control compared to throttling or debouncing:
|
|
362
377
|
* - A rate limiter will allow all executions until the limit is reached, then block all subsequent calls until the window resets
|
|
363
378
|
* - A throttler ensures even spacing between executions, which can be better for consistent performance
|
package/src/throttler.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Store } from '@tanstack/store'
|
|
2
|
-
import {
|
|
2
|
+
import { parseFunctionOrValue } from './utils'
|
|
3
3
|
import { emitChange, pacerEventClient } from './event-client'
|
|
4
4
|
import type { AnyFunction } from './types'
|
|
5
5
|
|
|
@@ -89,6 +89,18 @@ export interface ThrottlerOptions<TFn extends AnyFunction> {
|
|
|
89
89
|
wait: number | ((throttler: Throttler<TFn>) => number)
|
|
90
90
|
}
|
|
91
91
|
|
|
92
|
+
/**
|
|
93
|
+
* Utility function for sharing common `ThrottlerOptions` options between different `Throttler` instances.
|
|
94
|
+
*/
|
|
95
|
+
export function throttlerOptions<
|
|
96
|
+
TFn extends AnyFunction = AnyFunction,
|
|
97
|
+
TOptions extends Partial<ThrottlerOptions<TFn>> = Partial<
|
|
98
|
+
ThrottlerOptions<TFn>
|
|
99
|
+
>,
|
|
100
|
+
>(options: TOptions): TOptions {
|
|
101
|
+
return options
|
|
102
|
+
}
|
|
103
|
+
|
|
92
104
|
const defaultOptions: Omit<
|
|
93
105
|
Required<ThrottlerOptions<any>>,
|
|
94
106
|
'initialState' | 'onExecute' | 'key'
|
|
@@ -105,6 +117,7 @@ const defaultOptions: Omit<
|
|
|
105
117
|
* Throttling ensures a function is called at most once within a specified time window.
|
|
106
118
|
* Unlike debouncing which waits for a pause in calls, throttling guarantees consistent
|
|
107
119
|
* execution timing regardless of call frequency.
|
|
120
|
+
* This synchronous version is lighter weight and often all you need - upgrade to AsyncThrottler when you need promises, retry support, abort/cancel capabilities, or advanced error handling.
|
|
108
121
|
*
|
|
109
122
|
* Supports both leading and trailing edge execution:
|
|
110
123
|
* - Leading: Execute immediately on first call (default: true)
|
|
@@ -146,7 +159,7 @@ export class Throttler<TFn extends AnyFunction> {
|
|
|
146
159
|
public fn: TFn,
|
|
147
160
|
initialOptions: ThrottlerOptions<TFn>,
|
|
148
161
|
) {
|
|
149
|
-
this.key =
|
|
162
|
+
this.key = initialOptions.key
|
|
150
163
|
this.options = {
|
|
151
164
|
...defaultOptions,
|
|
152
165
|
...initialOptions,
|
|
@@ -321,6 +334,8 @@ export class Throttler<TFn extends AnyFunction> {
|
|
|
321
334
|
/**
|
|
322
335
|
* Creates a throttled function that limits how often the provided function can execute.
|
|
323
336
|
*
|
|
337
|
+
* This synchronous version is lighter weight and often all you need - upgrade to asyncThrottle when you need promises, retry support, abort/cancel capabilities, or advanced error handling.
|
|
338
|
+
*
|
|
324
339
|
* Throttling ensures a function executes at most once within a specified time window,
|
|
325
340
|
* regardless of how many times it is called. This is useful for rate-limiting
|
|
326
341
|
* expensive operations or UI updates.
|
package/src/utils.ts
CHANGED
|
@@ -10,18 +10,3 @@ export function parseFunctionOrValue<T, TArgs extends Array<any>>(
|
|
|
10
10
|
): T {
|
|
11
11
|
return isFunction(value) ? value(...args) : value
|
|
12
12
|
}
|
|
13
|
-
|
|
14
|
-
export function createKey(key?: string): string {
|
|
15
|
-
if (key) {
|
|
16
|
-
return key
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
if (
|
|
20
|
-
typeof crypto !== 'undefined' &&
|
|
21
|
-
typeof crypto.randomUUID === 'function'
|
|
22
|
-
) {
|
|
23
|
-
return crypto.randomUUID()
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
return ''
|
|
27
|
-
}
|