Skip to content

Commit d740d9f

Browse files
committed
feat(async-batcher): make result a type parameter
1 parent 8a1f754 commit d740d9f

File tree

20 files changed

+209
-175
lines changed

20 files changed

+209
-175
lines changed

.changeset/blue-lions-bake.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
'@tanstack/pacer-devtools': patch
3+
'@tanstack/react-pacer': patch
4+
'@tanstack/solid-pacer': patch
5+
'@tanstack/pacer': patch
6+
---
7+
8+
feat(async-batcher): make result a type parameter

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

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

1010
```ts
11-
function useAsyncBatchedCallback<TFn>(fn, options): (...args) => Promise<void>
11+
function useAsyncBatchedCallback<TValue, TResult>(fn, options): (item) => Promise<void>
1212
```
1313

14-
Defined in: [react-pacer/src/async-batcher/useAsyncBatchedCallback.ts:43](https://github.com/TanStack/pacer/blob/main/packages/react-pacer/src/async-batcher/useAsyncBatchedCallback.ts#L43)
14+
Defined in: [react-pacer/src/async-batcher/useAsyncBatchedCallback.ts:42](https://github.com/TanStack/pacer/blob/main/packages/react-pacer/src/async-batcher/useAsyncBatchedCallback.ts#L42)
1515

1616
A React hook that creates a batched version of an async callback function.
1717
This hook is a convenient wrapper around the `useAsyncBatcher` hook,
@@ -34,27 +34,29 @@ Consider using the `useAsyncBatcher` hook instead.
3434

3535
## Type Parameters
3636

37-
**TFn** *extends* `AnyAsyncFunction`
37+
**TValue**
38+
39+
**TResult**
3840

3941
## Parameters
4042

4143
### fn
4244

43-
(`items`) => `Promise`\<`any`\>
45+
(`items`) => `Promise`\<`TResult`\>
4446

4547
### options
4648

47-
`AsyncBatcherOptions`\<`Parameters`\<`TFn`\>\[`0`\]\>
49+
`AsyncBatcherOptions`\<`TValue`, `TResult`\>
4850

4951
## Returns
5052

5153
`Function`
5254

5355
### Parameters
5456

55-
#### args
57+
#### item
5658

57-
...`Parameters`\<`TFn`\>
59+
`TValue`
5860

5961
### Returns
6062

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

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ title: useAsyncBatcher
88
# Function: useAsyncBatcher()
99

1010
```ts
11-
function useAsyncBatcher<TValue, TSelected>(
11+
function useAsyncBatcher<TValue, TResult, TSelected>(
1212
fn,
1313
options,
14-
selector): ReactAsyncBatcher<TValue, TSelected>
14+
selector): ReactAsyncBatcher<TValue, TResult, TSelected>
1515
```
1616

1717
Defined in: [react-pacer/src/async-batcher/useAsyncBatcher.ts:167](https://github.com/TanStack/pacer/blob/main/packages/react-pacer/src/async-batcher/useAsyncBatcher.ts#L167)
@@ -75,32 +75,34 @@ Available state properties:
7575

7676
**TValue**
7777

78+
**TResult**
79+
7880
**TSelected** = \{\}
7981

8082
## Parameters
8183

8284
### fn
8385

84-
(`items`) => `Promise`\<`any`\>
86+
(`items`) => `Promise`\<`TResult`\>
8587

8688
### options
8789

88-
`AsyncBatcherOptions`\<`TValue`\> = `{}`
90+
`AsyncBatcherOptions`\<`TValue`, `TResult`\> = `{}`
8991

9092
### selector
9193

9294
(`state`) => `TSelected`
9395

9496
## Returns
9597

96-
[`ReactAsyncBatcher`](../../interfaces/reactasyncbatcher.md)\<`TValue`, `TSelected`\>
98+
[`ReactAsyncBatcher`](../../interfaces/reactasyncbatcher.md)\<`TValue`, `TResult`, `TSelected`\>
9799

98100
## Example
99101

100102
```tsx
101103
// Basic async batcher for API requests - no reactive state subscriptions
102104
const asyncBatcher = useAsyncBatcher(
103-
async (items) => {
105+
async (items: Item[]) => {
104106
const results = await Promise.all(items.map(item => processItem(item)));
105107
return results;
106108
},
@@ -109,7 +111,7 @@ const asyncBatcher = useAsyncBatcher(
109111
110112
// Opt-in to re-render when execution state changes (optimized for loading indicators)
111113
const asyncBatcher = useAsyncBatcher(
112-
async (items) => {
114+
async (items: Item[]) => {
113115
const results = await Promise.all(items.map(item => processItem(item)));
114116
return results;
115117
},
@@ -123,7 +125,7 @@ const asyncBatcher = useAsyncBatcher(
123125
124126
// Opt-in to re-render when results are available (optimized for data display)
125127
const asyncBatcher = useAsyncBatcher(
126-
async (items) => {
128+
async (items: Item[]) => {
127129
const results = await Promise.all(items.map(item => processItem(item)));
128130
return results;
129131
},
@@ -137,7 +139,7 @@ const asyncBatcher = useAsyncBatcher(
137139
138140
// Opt-in to re-render when error state changes (optimized for error handling)
139141
const asyncBatcher = useAsyncBatcher(
140-
async (items) => {
142+
async (items: Item[]) => {
141143
const results = await Promise.all(items.map(item => processItem(item)));
142144
return results;
143145
},
@@ -155,7 +157,7 @@ const asyncBatcher = useAsyncBatcher(
155157
156158
// Complete example with all callbacks
157159
const asyncBatcher = useAsyncBatcher(
158-
async (items) => {
160+
async (items: Item[]) => {
159161
const results = await Promise.all(items.map(item => processItem(item)));
160162
return results;
161163
},

docs/framework/react/reference/interfaces/reactasyncbatcher.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,20 @@ title: ReactAsyncBatcher
55

66
<!-- DO NOT EDIT: this page is autogenerated from the type comments -->
77

8-
# Interface: ReactAsyncBatcher\<TValue, TSelected\>
8+
# Interface: ReactAsyncBatcher\<TValue, TResult, TSelected\>
99

1010
Defined in: [react-pacer/src/async-batcher/useAsyncBatcher.ts:10](https://github.com/TanStack/pacer/blob/main/packages/react-pacer/src/async-batcher/useAsyncBatcher.ts#L10)
1111

1212
## Extends
1313

14-
- `Omit`\<`AsyncBatcher`\<`TValue`\>, `"store"`\>
14+
- `Omit`\<`AsyncBatcher`\<`TValue`, `TResult`\>, `"store"`\>
1515

1616
## Type Parameters
1717

1818
**TValue**
1919

20+
**TResult**
21+
2022
**TSelected** = \{\}
2123

2224
## Properties
@@ -38,7 +40,7 @@ Use this instead of `batcher.store.state`
3840
### ~~store~~
3941

4042
```ts
41-
readonly store: Store<Readonly<AsyncBatcherState<TValue>>>;
43+
readonly store: Store<Readonly<AsyncBatcherState<TValue, TResult>>>;
4244
```
4345

4446
Defined in: [react-pacer/src/async-batcher/useAsyncBatcher.ts:23](https://github.com/TanStack/pacer/blob/main/packages/react-pacer/src/async-batcher/useAsyncBatcher.ts#L23)

docs/framework/solid/reference/functions/createasyncbatcher.md

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ title: createAsyncBatcher
88
# Function: createAsyncBatcher()
99

1010
```ts
11-
function createAsyncBatcher<TValue, TSelected>(
11+
function createAsyncBatcher<TValue, TResult, TSelected>(
1212
fn,
1313
initialOptions,
14-
selector): SolidAsyncBatcher<TValue, TSelected>
14+
selector): SolidAsyncBatcher<TValue, TResult, TSelected>
1515
```
1616

1717
Defined in: [async-batcher/createAsyncBatcher.ts:128](https://github.com/TanStack/pacer/blob/main/packages/solid-pacer/src/async-batcher/createAsyncBatcher.ts#L128)
@@ -71,7 +71,7 @@ Example usage:
7171
```tsx
7272
// Default behavior - no reactive state subscriptions
7373
const asyncBatcher = createAsyncBatcher(
74-
async (items) => {
74+
async (items: Item[]) => {
7575
const results = await Promise.all(items.map(item => processItem(item)));
7676
return results;
7777
},
@@ -89,7 +89,7 @@ const asyncBatcher = createAsyncBatcher(
8989

9090
// Opt-in to re-render when items or isExecuting changes (optimized for UI updates)
9191
const asyncBatcher = createAsyncBatcher(
92-
async (items) => {
92+
async (items: Item[]) => {
9393
const results = await Promise.all(items.map(item => processItem(item)));
9494
return results;
9595
},
@@ -99,7 +99,7 @@ const asyncBatcher = createAsyncBatcher(
9999

100100
// Opt-in to re-render when error state changes (optimized for error handling)
101101
const asyncBatcher = createAsyncBatcher(
102-
async (items) => {
102+
async (items: Item[]) => {
103103
const results = await Promise.all(items.map(item => processItem(item)));
104104
return results;
105105
},
@@ -121,22 +121,24 @@ const { items, isExecuting } = asyncBatcher.state();
121121

122122
**TValue**
123123

124+
**TResult**
125+
124126
**TSelected** = \{\}
125127

126128
## Parameters
127129

128130
### fn
129131

130-
(`items`) => `Promise`\<`any`\>
132+
(`items`) => `Promise`\<`TResult`\>
131133

132134
### initialOptions
133135

134-
`AsyncBatcherOptions`\<`TValue`\> = `{}`
136+
`AsyncBatcherOptions`\<`TValue`, `TResult`\> = `{}`
135137

136138
### selector
137139

138140
(`state`) => `TSelected`
139141

140142
## Returns
141143

142-
[`SolidAsyncBatcher`](../../interfaces/solidasyncbatcher.md)\<`TValue`, `TSelected`\>
144+
[`SolidAsyncBatcher`](../../interfaces/solidasyncbatcher.md)\<`TValue`, `TResult`, `TSelected`\>

docs/framework/solid/reference/interfaces/solidasyncbatcher.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,20 @@ title: SolidAsyncBatcher
55

66
<!-- DO NOT EDIT: this page is autogenerated from the type comments -->
77

8-
# Interface: SolidAsyncBatcher\<TValue, TSelected\>
8+
# Interface: SolidAsyncBatcher\<TValue, TResult, TSelected\>
99

1010
Defined in: [async-batcher/createAsyncBatcher.ts:10](https://github.com/TanStack/pacer/blob/main/packages/solid-pacer/src/async-batcher/createAsyncBatcher.ts#L10)
1111

1212
## Extends
1313

14-
- `Omit`\<`AsyncBatcher`\<`TValue`\>, `"store"`\>
14+
- `Omit`\<`AsyncBatcher`\<`TValue`, `TResult`\>, `"store"`\>
1515

1616
## Type Parameters
1717

1818
**TValue**
1919

20+
**TResult**
21+
2022
**TSelected** = \{\}
2123

2224
## Properties
@@ -38,7 +40,7 @@ Use this instead of `batcher.store.state`
3840
### ~~store~~
3941

4042
```ts
41-
readonly store: Store<Readonly<AsyncBatcherState<TValue>>>;
43+
readonly store: Store<Readonly<AsyncBatcherState<TValue, TResult>>>;
4244
```
4345

4446
Defined in: [async-batcher/createAsyncBatcher.ts:23](https://github.com/TanStack/pacer/blob/main/packages/solid-pacer/src/async-batcher/createAsyncBatcher.ts#L23)

docs/guides/async-batching.md

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ The `asyncBatch` function provides a simple way to create an async batching func
2626
```ts
2727
import { asyncBatch } from '@tanstack/pacer'
2828

29-
const processAsyncBatch = asyncBatch<number>(
30-
async (items) => {
29+
const processAsyncBatch = asyncBatch(
30+
async (items: number[]) => {
3131
// Process the batch asynchronously
3232
const results = await Promise.all(
3333
items.map(item => processApiCall(item))
@@ -65,8 +65,8 @@ For more control over async batch behavior, use the `AsyncBatcher` class directl
6565
```ts
6666
import { AsyncBatcher } from '@tanstack/pacer'
6767

68-
const batcher = new AsyncBatcher<number>(
69-
async (items) => {
68+
const batcher = new AsyncBatcher(
69+
async (items: number[]) => {
7070
// Process the batch asynchronously
7171
const results = await Promise.all(
7272
items.map(item => processApiCall(item))
@@ -109,8 +109,8 @@ batcher.start() // Resume processing
109109
Unlike the synchronous batcher which returns void, the async version allows you to capture and use the return value from your batch function:
110110

111111
```ts
112-
const batcher = new AsyncBatcher<string>(
113-
async (items) => {
112+
const batcher = new AsyncBatcher(
113+
async (items: string[]) => {
114114
const results = await processBatch(items)
115115
return results
116116
},
@@ -130,8 +130,8 @@ const batcher = new AsyncBatcher<string>(
130130
The async batcher provides comprehensive error handling capabilities:
131131

132132
```ts
133-
const batcher = new AsyncBatcher<number>(
134-
async (items) => {
133+
const batcher = new AsyncBatcher(
134+
async (items: number[]) => {
135135
// This might throw an error
136136
const results = await riskyBatchOperation(items)
137137
return results
@@ -164,8 +164,8 @@ const batcher = new AsyncBatcher<number>(
164164
The async batcher tracks when batches are actively executing:
165165

166166
```ts
167-
const batcher = new AsyncBatcher<number>(
168-
async (items) => {
167+
const batcher = new AsyncBatcher(
168+
async (items: number[]) => {
169169
console.log('Starting batch execution...')
170170
const results = await longRunningBatchOperation(items)
171171
console.log('Batch execution completed')
@@ -196,8 +196,8 @@ The `AsyncBatcher` supports these async-specific callbacks:
196196
The async batcher provides flexible error handling through the `throwOnError` option:
197197

198198
```ts
199-
const batcher = new AsyncBatcher<number>(
200-
async (items) => {
199+
const batcher = new AsyncBatcher(
200+
async (items: number[]) => {
201201
// This might throw an error
202202
throw new Error('Batch processing failed')
203203
},
@@ -222,8 +222,8 @@ const batcher = new AsyncBatcher<number>(
222222
Like the synchronous batcher, the async batcher supports dynamic options:
223223

224224
```ts
225-
const batcher = new AsyncBatcher<number>(
226-
async (items) => {
225+
const batcher = new AsyncBatcher(
226+
async (items: number[]) => {
227227
return await processBatch(items)
228228
},
229229
{
@@ -360,8 +360,8 @@ The `AsyncBatcherState` includes:
360360
The async batcher tracks items that failed during batch processing:
361361

362362
```ts
363-
const batcher = new AsyncBatcher<number>(
364-
async (items) => {
363+
const batcher = new AsyncBatcher(
364+
async (items: number[]) => {
365365
// This might fail for some items
366366
if (items.some(item => item < 0)) {
367367
throw new Error('Negative numbers not allowed')

0 commit comments

Comments
 (0)