Skip to content

Commit 301538c

Browse files
committed
feat: useCallback variants of hooks and turn on react compiler linting
1 parent f3ea15d commit 301538c

File tree

174 files changed

+2665
-1533
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

174 files changed

+2665
-1533
lines changed

docs/config.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@
101101
"label": "useDebouncer",
102102
"to": "framework/react/reference/functions/usedebouncer"
103103
},
104+
{
105+
"label": "useDebouncedCallback",
106+
"to": "framework/react/reference/functions/usedebouncedcallback"
107+
},
104108
{
105109
"label": "useDebouncedState",
106110
"to": "framework/react/reference/functions/usedebouncedstate"
@@ -136,6 +140,10 @@
136140
"label": "useDebouncer",
137141
"to": "framework/react/examples/useDebouncer"
138142
},
143+
{
144+
"label": "useDebouncedCallback",
145+
"to": "framework/react/examples/useDebouncedCallback"
146+
},
139147
{
140148
"label": "useDebouncedState",
141149
"to": "framework/react/examples/useDebouncedState"
@@ -186,6 +194,10 @@
186194
"label": "useThrottler",
187195
"to": "framework/react/reference/functions/usethrottler"
188196
},
197+
{
198+
"label": "useThrottledCallback",
199+
"to": "framework/react/reference/functions/usethrottledcallback"
200+
},
189201
{
190202
"label": "useThrottledState",
191203
"to": "framework/react/reference/functions/usethrottledstate"
@@ -221,6 +233,10 @@
221233
"label": "useThrottler",
222234
"to": "framework/react/examples/useThrottler"
223235
},
236+
{
237+
"label": "useThrottledCallback",
238+
"to": "framework/react/examples/useThrottledCallback"
239+
},
224240
{
225241
"label": "useThrottledState",
226242
"to": "framework/react/examples/useThrottledState"
@@ -275,6 +291,10 @@
275291
"label": "useRateLimiter",
276292
"to": "framework/react/reference/functions/useratelimiter"
277293
},
294+
{
295+
"label": "useRateLimitedCallback",
296+
"to": "framework/react/reference/functions/useratelimitedcallback"
297+
},
278298
{
279299
"label": "useRateLimitedState",
280300
"to": "framework/react/reference/functions/useratelimitedstate"
@@ -310,6 +330,10 @@
310330
"label": "useRateLimiter",
311331
"to": "framework/react/examples/useRateLimiter"
312332
},
333+
{
334+
"label": "useRateLimitedCallback",
335+
"to": "framework/react/examples/useRateLimitedCallback"
336+
},
313337
{
314338
"label": "useRateLimitedState",
315339
"to": "framework/react/examples/useRateLimitedState"

docs/framework/react/reference/functions/useasyncdebouncer.md

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ title: useAsyncDebouncer
88
# Function: useAsyncDebouncer()
99

1010
```ts
11-
function useAsyncDebouncer<TFn, TArgs>(fn, options): AsyncDebouncer<TFn, TArgs>
11+
function useAsyncDebouncer<TFn, TArgs>(fn, options): object
1212
```
1313

1414
Defined in: [react-pacer/src/async-debouncer/useAsyncDebouncer.ts:40](https://github.com/TanStack/pacer/blob/main/packages/react-pacer/src/async-debouncer/useAsyncDebouncer.ts#L40)
@@ -40,7 +40,50 @@ wait for user input to settle before making expensive async calls.
4040

4141
## Returns
4242

43-
`AsyncDebouncer`\<`TFn`, `TArgs`\>
43+
`object`
44+
45+
### cancel()
46+
47+
```ts
48+
readonly cancel: () => void;
49+
```
50+
51+
Cancels any pending execution
52+
53+
#### Returns
54+
55+
`void`
56+
57+
### getExecutionCount()
58+
59+
```ts
60+
readonly getExecutionCount: () => number;
61+
```
62+
63+
Returns the number of times the function has been executed
64+
65+
#### Returns
66+
67+
`number`
68+
69+
### maybeExecute()
70+
71+
```ts
72+
readonly maybeExecute: (...args) => Promise<void>;
73+
```
74+
75+
Attempts to execute the debounced function
76+
If a call is already in progress, it will be queued
77+
78+
#### Parameters
79+
80+
##### args
81+
82+
...`TArgs`
83+
84+
#### Returns
85+
86+
`Promise`\<`void`\>
4487
4588
## Example
4689

docs/framework/react/reference/functions/useasyncqueuer.md

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ The hook returns an object containing methods to:
4848
### addItem()
4949
5050
```ts
51-
addItem: (fn, position?) => Promise<TValue>;
51+
readonly addItem: (fn, position?) => Promise<TValue>;
5252
```
5353
5454
Adds a task to the queuer
@@ -70,7 +70,7 @@ Adds a task to the queuer
7070
### clear()
7171
7272
```ts
73-
clear: () => void;
73+
readonly clear: () => void;
7474
```
7575
7676
Removes all items from the queuer
@@ -82,7 +82,7 @@ Removes all items from the queuer
8282
### getActiveItems()
8383
8484
```ts
85-
getActiveItems: () => () => Promise<TValue>[];
85+
readonly getActiveItems: () => () => Promise<TValue>[];
8686
```
8787
8888
Returns the active items
@@ -94,7 +94,7 @@ Returns the active items
9494
### getAllItems()
9595
9696
```ts
97-
getAllItems: () => () => Promise<TValue>[];
97+
readonly getAllItems: () => () => Promise<TValue>[];
9898
```
9999
100100
Returns a copy of all items in the queuer
@@ -106,7 +106,7 @@ Returns a copy of all items in the queuer
106106
### getExecutionCount()
107107
108108
```ts
109-
getExecutionCount: () => number;
109+
readonly getExecutionCount: () => number;
110110
```
111111
112112
Returns the number of items that have been removed from the queuer
@@ -118,7 +118,7 @@ Returns the number of items that have been removed from the queuer
118118
### getNextItem()
119119
120120
```ts
121-
getNextItem: (position?) => undefined | () => Promise<TValue>;
121+
readonly getNextItem: (position?) => undefined | () => Promise<TValue>;
122122
```
123123
124124
Removes and returns an item from the queuer
@@ -136,7 +136,7 @@ Removes and returns an item from the queuer
136136
### getPendingItems()
137137
138138
```ts
139-
getPendingItems: () => () => Promise<TValue>[];
139+
readonly getPendingItems: () => () => Promise<TValue>[];
140140
```
141141
142142
Returns the pending items
@@ -148,7 +148,7 @@ Returns the pending items
148148
### isEmpty()
149149
150150
```ts
151-
isEmpty: () => boolean;
151+
readonly isEmpty: () => boolean;
152152
```
153153
154154
Returns true if the queuer is empty
@@ -160,7 +160,7 @@ Returns true if the queuer is empty
160160
### isFull()
161161
162162
```ts
163-
isFull: () => boolean;
163+
readonly isFull: () => boolean;
164164
```
165165
166166
Returns true if the queuer is full
@@ -172,7 +172,7 @@ Returns true if the queuer is full
172172
### isIdle()
173173
174174
```ts
175-
isIdle: () => boolean;
175+
readonly isIdle: () => boolean;
176176
```
177177
178178
Returns true if the queuer is running but has no items to process
@@ -184,7 +184,7 @@ Returns true if the queuer is running but has no items to process
184184
### isRunning()
185185
186186
```ts
187-
isRunning: () => boolean;
187+
readonly isRunning: () => boolean;
188188
```
189189
190190
Returns true if the queuer is running
@@ -196,7 +196,7 @@ Returns true if the queuer is running
196196
### onError()
197197
198198
```ts
199-
onError: (cb) => () => void;
199+
readonly onError: (cb) => () => void;
200200
```
201201
202202
Adds a callback to be called when a task errors
@@ -218,7 +218,7 @@ Adds a callback to be called when a task errors
218218
### onSettled()
219219
220220
```ts
221-
onSettled: (cb) => () => void;
221+
readonly onSettled: (cb) => () => void;
222222
```
223223
224224
Adds a callback to be called when a task is settled
@@ -240,7 +240,7 @@ Adds a callback to be called when a task is settled
240240
### onSuccess()
241241
242242
```ts
243-
onSuccess: (cb) => () => void;
243+
readonly onSuccess: (cb) => () => void;
244244
```
245245
246246
Adds a callback to be called when a task succeeds
@@ -262,7 +262,7 @@ Adds a callback to be called when a task succeeds
262262
### peek()
263263
264264
```ts
265-
peek: (position?) => undefined | () => Promise<TValue>;
265+
readonly peek: (position?) => undefined | () => Promise<TValue>;
266266
```
267267
268268
Returns an item without removing it
@@ -280,7 +280,7 @@ Returns an item without removing it
280280
### reset()
281281
282282
```ts
283-
reset: (withInitialItems?) => void;
283+
readonly reset: (withInitialItems?) => void;
284284
```
285285
286286
Resets the queuer to its initial state
@@ -298,7 +298,7 @@ Resets the queuer to its initial state
298298
### size()
299299
300300
```ts
301-
size: () => number;
301+
readonly size: () => number;
302302
```
303303
304304
Returns the current size of the queuer
@@ -310,7 +310,7 @@ Returns the current size of the queuer
310310
### start()
311311
312312
```ts
313-
start: () => Promise<void>;
313+
readonly start: () => Promise<void>;
314314
```
315315
316316
Starts the queuer and processes items
@@ -322,7 +322,7 @@ Starts the queuer and processes items
322322
### stop()
323323
324324
```ts
325-
stop: () => void;
325+
readonly stop: () => void;
326326
```
327327
328328
Stops the queuer from processing items
@@ -334,7 +334,7 @@ Stops the queuer from processing items
334334
### throttle()
335335
336336
```ts
337-
throttle: (n) => void;
337+
readonly throttle: (n) => void;
338338
```
339339
340340
Throttles the number of concurrent items that can run at once

docs/framework/react/reference/functions/useasyncratelimiter.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ title: useAsyncRateLimiter
1111
function useAsyncRateLimiter<TFn, TArgs>(fn, options): object
1212
```
1313

14-
Defined in: [react-pacer/src/async-rate-limiter/useAsyncRateLimiter.ts:42](https://github.com/TanStack/pacer/blob/main/packages/react-pacer/src/async-rate-limiter/useAsyncRateLimiter.ts#L42)
14+
Defined in: [react-pacer/src/async-rate-limiter/useAsyncRateLimiter.ts:41](https://github.com/TanStack/pacer/blob/main/packages/react-pacer/src/async-rate-limiter/useAsyncRateLimiter.ts#L41)
1515

1616
A low-level React hook that creates an `AsyncRateLimiter` instance to limit how many times an async function can execute within a time window.
1717

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
---
2+
id: useDebouncedCallback
3+
title: useDebouncedCallback
4+
---
5+
6+
<!-- DO NOT EDIT: this page is autogenerated from the type comments -->
7+
8+
# Function: useDebouncedCallback()
9+
10+
```ts
11+
function useDebouncedCallback<TFn, TArgs>(fn, options): (...args) => void
12+
```
13+
14+
Defined in: react-pacer/src/debouncer/useDebouncedCallback.ts:41
15+
16+
A React hook that creates a debounced version of a callback function.
17+
This hook is essentially a wrapper around the basic `debounce` function
18+
that is exported from `@tanstack/pacer`,
19+
but optimized for React with reactive options and a stable function reference.
20+
21+
The debounced function will only execute after the specified wait time has elapsed
22+
since its last invocation. If called again before the wait time expires, the timer
23+
resets and starts waiting again.
24+
25+
This hook provides a simpler API compared to `useDebouncer`, making it ideal for basic
26+
debouncing needs. However, it does not expose the underlying Debouncer instance.
27+
28+
For advanced usage requiring features like:
29+
- Manual cancellation
30+
- Access to execution counts
31+
- Custom useCallback dependencies
32+
33+
Consider using the `useDebouncer` hook instead.
34+
35+
## Type Parameters
36+
37+
**TFn** *extends* (...`args`) => `any`
38+
39+
**TArgs** *extends* `any`[]
40+
41+
## Parameters
42+
43+
### fn
44+
45+
`TFn`
46+
47+
### options
48+
49+
`DebouncerOptions`
50+
51+
## Returns
52+
53+
`Function`
54+
55+
### Parameters
56+
57+
#### args
58+
59+
...`TArgs`
60+
61+
### Returns
62+
63+
`void`
64+
65+
## Example
66+
67+
```tsx
68+
// Debounce a search handler
69+
const handleSearch = useDebouncedCallback((query: string) => {
70+
fetchSearchResults(query);
71+
}, {
72+
wait: 500 // Wait 500ms between executions
73+
});
74+
75+
// Use in an input
76+
<input
77+
type="text"
78+
onChange={(e) => handleSearch(e.target.value)}
79+
/>
80+
```

0 commit comments

Comments
 (0)