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

createRateLimiter.md

101 lines3.3 KB

1---
2id: createRateLimiter
3title: createRateLimiter
4---
5​
6```ts
7function createRateLimiter<TFn, TSelected>(
8 scope,
9 fn,
10 options,
11selector?): AlpineRateLimiter<TFn, TSelected>;
12```
13​
14Defined in: [rate-limiter/createRateLimiter.ts:87](https://github.com/TanStack/pacer/blob/main/packages/alpine-pacer/src/rate-limiter/createRateLimiter.ts#L87)
15​
16Creates and retains the RateLimiter for its Alpine 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(childScope, 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.
45​
46## Type Parameters
47​
48### TFn
49​
50`TFn` *extends* `AnyFunction`
51​
52### TSelected
53​
54`TSelected` = \{
55\}
56​
57## Parameters
58​
59### scope
60​
61[`PacerScope`](../interfaces/PacerScope.md)
62​
63Owner of option updates, subscriptions, and cleanup.
64​
65### fn
66​
67`TFn`
68​
69Function executed by the utility.
70​
71### options
72​
73[`AlpinePacerOptions`](../type-aliases/AlpinePacerOptions.md)\<[`AlpineRateLimiterOptions`](../interfaces/AlpineRateLimiterOptions.md)\<`TFn`, `TSelected`\>\>
74​
75Core options or a reactive factory, plus an optional onUnmount callback.
76​
77### selector?
78​
79(`state`) => `TSelected`
80​
81Selects state that updates the owner. Omit to leave selected state empty.
82​
83## Returns
84​
85[`AlpineRateLimiter`](../interfaces/AlpineRateLimiter.md)\<`TFn`, `TSelected`\>
86​
87The retained utility instance with selected state and child subscriptions.
88​
89## Example
90​
91```ts
92import { createRateLimiter } from '@tanstack/alpine-pacer'
93​
94const utility = createRateLimiter(
95 scope, (value: string) => { console.log(value) },
96 { limit: 5, window: 1000 },
97 (state) => ({ executionCount: state.executionCount }),
99utility.maybeExecute('item')
100// Selected state: utility.state.executionCount
101```