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

createRateLimitedSignal.md

75 lines2.6 KB

1---
2id: createRateLimitedSignal
3title: createRateLimitedSignal
4---
5​
6```ts
7function createRateLimitedSignal<TValue, TSelected>(
8 initialValue,
9 options,
10 selector?): [CellValue<TValue>, SetValue<TValue>, SvelteRateLimiter<SetValue<TValue>, TSelected>];
11```
12​
13Defined in: [packages/svelte-pacer/src/rate-limiter/createRateLimitedSignal.ts:41](https://github.com/TanStack/pacer/blob/main/packages/svelte-pacer/src/rate-limiter/createRateLimitedSignal.ts#L41)
14​
15Creates rate-limited state with a scheduled setter.
16​
17Accepts updates while the configured limit has capacity in its fixed or sliding window. Rejected updates are discarded instead of delayed.
18​
19## Return value
20​
21Returns [value, setValue, utility]. The value is an accessor; call value() in the template. Setters accept a value or a functional updater. Updaters run when the utility executes, using the last committed value. Pending updates may be replaced or rejected according to the utility's scheduling rules. To store a function itself, pass an updater that returns that function.
22​
23## State and ownership
24​
25The value updates independently of the utility selector. The default utility selection is {}. Pass a selector to subscribe to fields such as executionCount, isPending, or status where the underlying utility exposes them.
26​
27Call during component initialization. Component destruction removes effects and subscriptions and runs utility cleanup.
28Options accept an object with top-level getters or an options factory. Function-valued core options remain callbacks. Local options override provider defaults without replacing the utility or its pending work.
29onUnmount replaces default cleanup and receives the utility instance. A custom callback must perform every needed cancel, stop, or abort action.
30​
31## Type Parameters
32​
33### TValue
34​
35`TValue`
36​
37### TSelected
38​
39`TSelected` = \{
40\}
41​
42## Parameters
43​
44### initialValue
45​
46`TValue`
47​
48### options
49​
50[`SveltePacerOptions`](../type-aliases/SveltePacerOptions.md)\<[`SvelteRateLimiterOptions`](../interfaces/SvelteRateLimiterOptions.md)\<`SetValue`\<`TValue`\>, `TSelected`\>\>
51​
52### selector?
53​
54(`state`) => `TSelected`
55​
56## Returns
57​
58\[`CellValue`\<`TValue`\>, `SetValue`\<`TValue`\>, [`SvelteRateLimiter`](../interfaces/SvelteRateLimiter.md)\<`SetValue`\<`TValue`\>, `TSelected`\>\]
59​
60## Example
61​
62```ts
63import { createRateLimitedSignal } from '@tanstack/svelte-pacer'
64​
65// During component initialization:
66const [count, setCount, utility] = createRateLimitedSignal(0, { limit: 3, window: 1000 },
67 (state) => ({ executionCount: state.executionCount }),
69setCount((previous) => previous + 1)
70// Bind count and utility.state in the component. Read count() for the committed value.
71```
72​
73## See
74​
75createRateLimiter