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

UseRateLimitedState.md

139 lines3.8 KB

1---
2id: UseRateLimitedState
3title: UseRateLimitedState
4---
5​
6Defined in: [packages/ember-pacer/src/rate-limiter/useRateLimitedState.ts:59](https://github.com/TanStack/pacer/blob/main/packages/ember-pacer/src/rate-limiter/useRateLimitedState.ts#L59)
7​
8Creates rate-limited state with a scheduled setter.
9​
10Accepts updates while the configured limit has capacity in its fixed or sliding window. Rejected updates are discarded instead of delayed.
11​
12## Return value
13​
14Yields an object with value, setValue, and utility. Read value in the template; utility exposes controls and selected state. 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.
15​
16## State and ownership
17​
18The 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.
19​
20Invoke in a Glimmer template. Positional arguments provide the callback or value and optional selector. Named arguments provide options. Removing the invocation runs cleanup.
21Tracked named arguments refresh options after rendering. Local options override provider defaults without replacing the utility or its pending work.
22onUnmount replaces default cleanup and receives the utility instance. A custom callback must perform every needed cancel, stop, or abort action.
23​
24## Example
25​
26```gts
27import { on } from '@ember/modifier'
28import { fn } from '@ember/helper'
29import { useRateLimitedState } from '@tanstack/ember-pacer'
30​
31// Inside a component template:
32<template>
33{{#let (useRateLimitedState 0 limit=3 window=1000) as |result|}}
34 <output>{{result.value}}</output>
35 <button {{on "click" (fn result.setValue 1)}}>Update</button>
36{{/let}}
37</template>
38```
39​
40## See
41​
42useRateLimiter
43​
44## Extends
45​
46- `default`\<\{
47 `Args`: \{
48 `Named`: [`EmberRateLimiterOptions`](../interfaces/EmberRateLimiterOptions.md)\<`SetValue`\<`TValue`\>, `TSelected`\>;
49 `Positional`: \[`TValue`\] \| \[`TValue`, (`state`) => `TSelected`\];
50 \};
51 `Return`: [`EmberRateLimitedState`](../interfaces/EmberRateLimitedState.md)\<`TValue`, `TSelected`\>;
52\}\>
53​
54## Type Parameters
55​
56### TValue
57​
58`TValue`
59​
60### TSelected
61​
62`TSelected` = \{
63\}
64​
65## Constructors
66​
67### Constructor
68​
69```ts
70new UseRateLimitedState<TValue, TSelected>(owner?): UseRateLimitedState;
71```
72​
73Defined in: node\_modules/.pnpm/ember-source@7.3.0\_@glimmer+component@2.1.1\_supports-color@7.2.0\_\_supports-color@7.2.0/node\_modules/ember-source/types/stable/@ember/object/index.d.ts:28
74​
75#### Parameters
76​
77##### owner?
78​
79`Owner`
80​
81#### Returns
82​
83`UseRateLimitedState`
84​
85#### Inherited from
86​
87```ts
88Helper<{
89 Args: {
90 Positional:
91 | [value: TValue]
92 | [value: TValue, selector: (state: RateLimiterState) => TSelected]
93 Named: EmberRateLimiterOptions<SetValue<TValue>, TSelected>
94 }
95 Return: EmberRateLimitedState<TValue, TSelected>
96}>.constructor
97```
98​
99## Methods
100​
101### compute()
102​
103```ts
104compute(positional, options): EmberRateLimitedState<TValue, TSelected>;
105```
106​
107Defined in: [packages/ember-pacer/src/rate-limiter/useRateLimitedState.ts:75](https://github.com/TanStack/pacer/blob/main/packages/ember-pacer/src/rate-limiter/useRateLimitedState.ts#L75)
108​
109Override this function when writing a class-based helper.
110​
111#### Parameters
112​
113##### positional
114​
115\[`TValue`, (`state`) => `TSelected`?\]
116​
117The positional arguments to the helper
118​
119##### options
120​
121[`EmberRateLimiterOptions`](../interfaces/EmberRateLimiterOptions.md)\<`SetValue`\<`TValue`\>, `TSelected`\>
122​
123#### Returns
124​
125[`EmberRateLimitedState`](../interfaces/EmberRateLimitedState.md)\<`TValue`, `TSelected`\>
126​
127#### Method
128​
129compute
130​
131#### Since
132​
1331.13.0
134​
135#### Overrides
136​
137```ts
138Helper.compute
139```