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

createRateLimiter.md

102 lines3.3 KB

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