Skip to content
Source
Sign inSign up
TanStack/pacer
Open History to see the last commit

createAsyncRateLimiter.md

109 lines3.8 KB

1---
2id: createAsyncRateLimiter
3title: createAsyncRateLimiter
4---
5​
6```ts
7function createAsyncRateLimiter<TFn, TSelected>(
8 host,
9 fn,
10 options,
11selector?): LitAsyncRateLimiter<TFn, TSelected>;
12```
13​
14Defined in: [async-rate-limiter/createAsyncRateLimiter.ts:96](https://github.com/TanStack/pacer/blob/main/packages/lit-pacer/src/async-rate-limiter/createAsyncRateLimiter.ts#L96)
15​
16Creates and retains the AsyncRateLimiter for its Lit owner.
17​
18Accepts at most a configured number of calls in a fixed or sliding window. Calls beyond the limit are rejected rather than queued. Use the state and timing methods to display capacity and retry timing.
19​
20The callback may return a Promise. Core result, error, retry, and abort behavior is preserved.
21Use onSuccess, onError, and onSettled for execution outcomes.
22​
23## State and subscriptions
24​
25Pass a selector to track only the state consumed by the owner. The default selection is {},
26so utility state changes do not update the owner unless it opts in. Selection uses shallow
27comparison. The raw store remains available for additional subscriptions.
28Use utility.subscribe(childHost, selector) for a child subscription. It returns a getter
29and cleans up with the child without canceling the parent utility.
30​
31Available state fields:
32​
33- `errorCount`: Number of function executions that have resulted in errors
34- `executionTimes`: Array of timestamps when executions occurred for rate limiting calculations
35- `isExceeded`: Whether the rate limiter has exceeded the limit
36- `isExecuting`: Whether the rate-limited function is currently executing asynchronously
37- `lastResult`: The result from the most recent successful function execution
38- `rejectionCount`: Number of function executions that have been rejected due to rate limiting
39- `settleCount`: Number of function executions that have completed (either successfully or with errors)
40- `status`: Current execution status - 'disabled' when not active, 'executing' when executing, 'idle' when not executing, 'exceeded' when rate limit is exceeded
41- `successCount`: Number of function executions that have completed successfully
42- `maybeExecuteCount`: Number of times maybeExecute has been called (for reduction calculations)
43​
44## Options and ownership
45​
46Pass an options object with property getters or a factory. Top-level properties are read
47reactively; function-valued core options remain callbacks. Local options override provider
48defaults. Updates retain the utility, its store, counters, and pending work.
49Disconnecting the host calls abort().
50onUnmount replaces default cleanup and receives the same adapter instance. A custom callback
51must perform all required cleanup. Use flush() where supported to finish pending work.
52Reconnecting the host refreshes options and restores subscriptions to the same instance.
53​
54## Type Parameters
55​
56### TFn
57​
58`TFn` *extends* `AnyAsyncFunction`
59​
60### TSelected
61​
62`TSelected` = \{
63\}
64​
65## Parameters
66​
67### host
68​
69`ReactiveControllerHost`
70​
71Owner of option updates, subscriptions, and cleanup.
72​
73### fn
74​
75`TFn`
76​
77Function executed by the utility.
78​
79### options
80​
81[`LitPacerOptions`](../type-aliases/LitPacerOptions.md)\<[`LitAsyncRateLimiterOptions`](../interfaces/LitAsyncRateLimiterOptions.md)\<`TFn`, `TSelected`\>\>
82​
83Core options or a reactive factory, plus an optional onUnmount callback.
84​
85### selector?
86​
87(`state`) => `TSelected`
88​
89Selects state that updates the owner. Omit to leave selected state empty.
90​
91## Returns
92​
93[`LitAsyncRateLimiter`](../interfaces/LitAsyncRateLimiter.md)\<`TFn`, `TSelected`\>
94​
95The retained utility instance with selected state and child subscriptions.
96​
97## Example
98​
99```ts
100import { createAsyncRateLimiter } from '@tanstack/lit-pacer'
101​
102const utility = createAsyncRateLimiter(
103 this, async (value: string) => { console.log(value) },
104 { limit: 5, window: 1000 },
105 (state) => ({ executionCount: state.executionCount }),
107utility.maybeExecute('item')
108// Selected state: utility.state.executionCount
109```