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