Skip to content

Commit 6054a27

Browse files
committed
Include requestId in payload creator args, and use nanoid
1 parent fa3c86b commit 6054a27

File tree

2 files changed

+17
-15
lines changed

2 files changed

+17
-15
lines changed

src/createAsyncThunk.test.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,13 @@ describe('createAsyncThunk', () => {
1717

1818
const result = 42
1919
const args = 123
20-
const requestId = '1'
20+
let generatedRequestId = ''
2121

2222
const thunkActionCreator = createAsyncThunk(
2323
'testType',
24-
async (args: number) => {
24+
async (args: number, { requestId }) => {
2525
passedArgs = args
26+
generatedRequestId = requestId
2627
return result
2728
}
2829
)
@@ -35,31 +36,32 @@ describe('createAsyncThunk', () => {
3536

3637
expect(dispatch).toHaveBeenNthCalledWith(
3738
1,
38-
thunkActionCreator.pending(args, requestId)
39+
thunkActionCreator.pending(args, generatedRequestId)
3940
)
4041

4142
expect(dispatch).toHaveBeenNthCalledWith(
4243
2,
43-
thunkActionCreator.fulfilled(result, args, requestId)
44+
thunkActionCreator.fulfilled(result, args, generatedRequestId)
4445
)
4546

4647
expect(dispatch).toHaveBeenNthCalledWith(
4748
3,
48-
thunkActionCreator.finished(args, requestId)
49+
thunkActionCreator.finished(args, generatedRequestId)
4950
)
5051
})
5152

5253
it('accepts arguments and dispatches the actions on reject', async () => {
5354
const dispatch = jest.fn()
5455

5556
const args = 123
56-
const requestId = '1'
57+
let generatedRequestId = ''
5758

5859
const error = new Error('Panic!')
5960

6061
const thunkActionCreator = createAsyncThunk(
6162
'testType',
62-
async (args: number) => {
63+
async (args: number, { requestId }) => {
64+
generatedRequestId = requestId
6365
throw error
6466
}
6567
)
@@ -70,17 +72,17 @@ describe('createAsyncThunk', () => {
7072

7173
expect(dispatch).toHaveBeenNthCalledWith(
7274
1,
73-
thunkActionCreator.pending(args, requestId)
75+
thunkActionCreator.pending(args, generatedRequestId)
7476
)
7577

7678
expect(dispatch).toHaveBeenNthCalledWith(
7779
2,
78-
thunkActionCreator.rejected(error, args, requestId)
80+
thunkActionCreator.rejected(error, args, generatedRequestId)
7981
)
8082

8183
expect(dispatch).toHaveBeenNthCalledWith(
8284
3,
83-
thunkActionCreator.finished(args, requestId)
85+
thunkActionCreator.finished(args, generatedRequestId)
8486
)
8587
})
8688
})

src/createAsyncThunk.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import { Dispatch } from 'redux'
2+
import nanoid from 'nanoid'
23
import { createAction } from './createAction'
34

45
type AsyncThunksArgs<S, E, D extends Dispatch = Dispatch> = {
56
dispatch: D
67
getState: S
78
extra: E
9+
requestId: string
810
}
911

1012
/**
@@ -40,8 +42,6 @@ export function createAsyncThunk<
4042
}
4143
)
4244

43-
let requestIdCounter = 0
44-
4545
const pending = createAction(
4646
type + '/pending',
4747
(args: ActionParams, requestId: string) => {
@@ -79,16 +79,16 @@ export function createAsyncThunk<
7979
getState: TA['getState'],
8080
extra: TA['extra']
8181
) => {
82-
requestIdCounter++
83-
const requestId = `${requestIdCounter}`
82+
const requestId = nanoid()
8483

8584
try {
8685
dispatch(pending(args, requestId))
8786
// TODO Also ugly types
8887
const result = (await payloadCreator(args, {
8988
dispatch,
9089
getState,
91-
extra
90+
extra,
91+
requestId
9292
} as TA)) as Returned
9393

9494
// TODO How do we avoid errors in here from hitting the catch clause?

0 commit comments

Comments
 (0)