Skip to content

Commit bf67629

Browse files
committed
update quick start guide
1 parent c3cd727 commit bf67629

File tree

2 files changed

+252
-6
lines changed

2 files changed

+252
-6
lines changed

docs/framework/react/adapter.md

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,173 @@ Or import a core Pacer class/function that is re-exported from the React Adapter
3434
import { debounce, Debouncer } from '@tanstack/react-pacer' // no need to install the core package separately
3535
```
3636
37+
## Option Helpers
38+
39+
If you want a type-safe way to define common options for pacer utilities, TanStack Pacer provides option helpers for each utility. These helpers can be used with React hooks.
40+
41+
### Debouncer Options
42+
43+
```tsx
44+
import { useDebouncer } from '@tanstack/react-pacer'
45+
import { debouncerOptions } from '@tanstack/pacer'
46+
47+
const commonDebouncerOptions = debouncerOptions({
48+
wait: 1000,
49+
leading: false,
50+
trailing: true,
51+
})
52+
53+
const debouncer = useDebouncer(
54+
(query: string) => fetchSearchResults(query),
55+
{ ...commonDebouncerOptions, key: 'searchDebouncer' }
56+
)
57+
```
58+
59+
### Queuer Options
60+
61+
```tsx
62+
import { useQueuer } from '@tanstack/react-pacer'
63+
import { queuerOptions } from '@tanstack/pacer'
64+
65+
const commonQueuerOptions = queuerOptions({
66+
concurrency: 3,
67+
addItemsTo: 'back',
68+
})
69+
70+
const queuer = useQueuer(
71+
(item: string) => processItem(item),
72+
{ ...commonQueuerOptions, key: 'itemQueuer' }
73+
)
74+
```
75+
76+
### Rate Limiter Options
77+
78+
```tsx
79+
import { useRateLimiter } from '@tanstack/react-pacer'
80+
import { rateLimiterOptions } from '@tanstack/pacer'
81+
82+
const commonRateLimiterOptions = rateLimiterOptions({
83+
limit: 5,
84+
window: 60000,
85+
windowType: 'sliding',
86+
})
87+
88+
const rateLimiter = useRateLimiter(
89+
(data: string) => sendApiRequest(data),
90+
{ ...commonRateLimiterOptions, key: 'apiRateLimiter' }
91+
)
92+
```
93+
94+
## Provider
95+
96+
The React Adapter provides a `PacerProvider` component that you can use to provide default options to all instances of pacer utilities within your component tree.
97+
98+
```tsx
99+
import { PacerProvider } from '@tanstack/react-pacer'
100+
101+
// Set default options for react-pacer instances
102+
<PacerProvider
103+
defaultOptions={{
104+
debouncer: { wait: 1000 },
105+
queuer: { concurrency: 3 },
106+
rateLimiter: { limit: 5, window: 60000 },
107+
}}
108+
>
109+
<App />
110+
</PacerProvider>
111+
```
112+
113+
All hooks within the provider will automatically use these default options, which can be overridden on a per-hook basis.
114+
115+
## Examples
116+
117+
### Debouncer Example
118+
119+
```tsx
120+
import { useDebouncer } from '@tanstack/react-pacer'
121+
122+
function SearchComponent() {
123+
const debouncer = useDebouncer(
124+
(query: string) => {
125+
console.log('Searching for:', query)
126+
// Perform search
127+
},
128+
{ wait: 500 }
129+
)
130+
131+
return (
132+
<input
133+
onChange={(e) => debouncer.maybeExecute(e.target.value)}
134+
placeholder="Search..."
135+
/>
136+
)
137+
}
138+
```
139+
140+
### Queuer Example
141+
142+
```tsx
143+
import { useQueuer } from '@tanstack/react-pacer'
144+
145+
function UploadComponent() {
146+
const queuer = useQueuer(
147+
async (file: File) => {
148+
await uploadFile(file)
149+
},
150+
{ concurrency: 3 }
151+
)
152+
153+
const handleFileSelect = (files: FileList) => {
154+
Array.from(files).forEach((file) => {
155+
queuer.add(file)
156+
})
157+
}
158+
159+
return (
160+
<input
161+
type="file"
162+
multiple
163+
onChange={(e) => {
164+
if (e.target.files) {
165+
handleFileSelect(e.target.files)
166+
}
167+
}}
168+
/>
169+
)
170+
}
171+
```
172+
173+
### Rate Limiter Example
174+
175+
```tsx
176+
import { useRateLimiter } from '@tanstack/react-pacer'
177+
178+
function ApiComponent() {
179+
const rateLimiter = useRateLimiter(
180+
(data: string) => {
181+
return fetch('/api/endpoint', {
182+
method: 'POST',
183+
body: JSON.stringify({ data }),
184+
})
185+
},
186+
{
187+
limit: 5,
188+
window: 60000,
189+
windowType: 'sliding',
190+
onReject: () => {
191+
alert('Rate limit reached. Please try again later.')
192+
},
193+
}
194+
)
195+
196+
const handleSubmit = () => {
197+
const remaining = rateLimiter.getRemainingInWindow()
198+
if (remaining > 0) {
199+
rateLimiter.maybeExecute('some data')
200+
}
201+
}
202+
203+
return <button onClick={handleSubmit}>Submit</button>
204+
}
205+
```
206+

docs/quick-start.md

Lines changed: 82 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,87 @@ title: Quick Start
33
id: quick-start
44
---
55

6-
This page contains links to the the highest-level example for each of Tanstack Pacer's 4 core concepts:
6+
## Installation
77

8-
* [Debouncing](./guides/debouncing.md): [`useDebouncedValue`](../examples/react/useDebouncedValue/)
9-
* [Throttling](./guides/throttling.md): [`useThrottledValue`](../examples/react/useThrottledValue/)
10-
* [Rate-limiting](./guides/rate-limiting.md): [`useRateLimitedValue`](../examples/react/useRateLimitedValue/)
11-
* [Queueing](./guides/queueing.md): [`useQueuerState`](../examples/react/useQueuerState/)
8+
Don't have TanStack Pacer installed yet? See the [Installation](../installation.md) page for instructions.
129

13-
If you're looking lower-level examples or `async` examples, take a look at our catalog of examples.
10+
## Understanding Which Pacer Utility to Use
11+
12+
Still learning what TanStack Pacer is and how it can help your application? See the [Which Pacer Utility Should I Choose?](../guides/which-pacer-utility-should-i-choose.md) guide for help choosing which Pacer utility to use. The TanStack Pacer libraries have 5 core utilities, but also quite a few flexible ways to use each utility. Famarilizing yourself with the above guide will help you choose the right utility for your use case.
13+
14+
## API References
15+
16+
See the [API References](../reference/index.md) page for the full list of API references for each Pacer utility.
17+
18+
## Basic Usage
19+
20+
If you are using vanilla JavaScript, there are core classes and functions that you can use from the core pacer package.
21+
22+
### Class Usage
23+
24+
```ts
25+
import { Debouncer } from '@tanstack/pacer' // class
26+
27+
const debouncer = new Debouncer(fn, options)
28+
29+
debouncer.maybeExecute(args) // execute the debounced function
30+
debouncer.cancel() // cancel the debounced function
31+
debouncer.flush() // flush the debounced function
32+
```
33+
34+
### Function Usage
35+
36+
```ts
37+
import { debounce } from '@tanstack/pacer' // function
38+
39+
const debouncedFn = debounce(fn, options)
40+
41+
debouncedFn(args) // execute the debounced function
42+
```
43+
44+
### Framework Hook Usage (Recommended)
45+
46+
If you are using a framework adapter like React, you can use the `useDebouncer` hook to create a debounced function.
47+
48+
```tsx
49+
import { useDebouncer } from '@tanstack/react-pacer'
50+
51+
const debouncer = useDebouncer(fn, options) // recommended
52+
53+
debouncer.maybeExecute(args) // execute the debounced function
54+
debouncer.cancel() // cancel the debounced function
55+
debouncer.flush() // flush the debounced function
56+
```
57+
58+
### Option Helpers
59+
60+
If want a type-safe way to define common options for pacer utilities, TanStack Pacer provides option helpers for each utility.
61+
62+
```ts
63+
import { debouncerOptions } from '@tanstack/pacer'
64+
65+
const commonDebouncerOptions = debouncerOptions({
66+
wait: 1000,
67+
leading: false,
68+
trailing: true,
69+
})
70+
71+
const debouncer = new Debouncer(fn, { ...commonDebouncerOptions, key: 'myDebouncer' })
72+
```
73+
74+
### Providers
75+
76+
In each framework adapter, there is a provider component that you can use to provide default options to all instances of a pacer utility.
77+
78+
```tsx
79+
import { PacerProvider } from '@tanstack/react-pacer'
80+
81+
// set default options for react-pacer instances
82+
<PacerProvider defaultOptions={{ debouncer: { wait: 1000 } }}>
83+
<App />
84+
</PacerProvider>
85+
```
86+
87+
### Devtools
88+
89+
TanStack Pacer provides an official TanStack Devtools integration for each framework adapter. See the [Devtools](../devtools.md) documentation for more information on how to set up and use the Pacer devtools.

0 commit comments

Comments
 (0)