Skip to content

Commit 113038c

Browse files
committed
Add a basic request ID counter to createAsyncThunk
1 parent 24863a1 commit 113038c

File tree

2 files changed

+37
-24
lines changed

2 files changed

+37
-24
lines changed

src/createAsyncThunk.test.ts

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

1818
const result = 42
1919
const args = 123
20+
const requestId = '1'
2021

2122
const thunkActionCreator = createAsyncThunk(
2223
'testType',
@@ -34,24 +35,25 @@ describe('createAsyncThunk', () => {
3435

3536
expect(dispatch).toHaveBeenNthCalledWith(
3637
1,
37-
thunkActionCreator.pending(args)
38+
thunkActionCreator.pending(args, requestId)
3839
)
3940

4041
expect(dispatch).toHaveBeenNthCalledWith(
4142
2,
42-
thunkActionCreator.fulfilled(result, args)
43+
thunkActionCreator.fulfilled(result, args, requestId)
4344
)
4445

4546
expect(dispatch).toHaveBeenNthCalledWith(
4647
3,
47-
thunkActionCreator.finished(args)
48+
thunkActionCreator.finished(args, requestId)
4849
)
4950
})
5051

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

5455
const args = 123
56+
const requestId = '1'
5557

5658
const error = new Error('Panic!')
5759

@@ -68,17 +70,17 @@ describe('createAsyncThunk', () => {
6870

6971
expect(dispatch).toHaveBeenNthCalledWith(
7072
1,
71-
thunkActionCreator.pending(args)
73+
thunkActionCreator.pending(args, requestId)
7274
)
7375

7476
expect(dispatch).toHaveBeenNthCalledWith(
7577
2,
76-
thunkActionCreator.rejected(error, args)
78+
thunkActionCreator.rejected(error, args, requestId)
7779
)
7880

7981
expect(dispatch).toHaveBeenNthCalledWith(
8082
3,
81-
thunkActionCreator.finished(args)
83+
thunkActionCreator.finished(args, requestId)
8284
)
8385
})
8486
})

src/createAsyncThunk.ts

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,35 +32,43 @@ export function createAsyncThunk<
3232
) {
3333
const fulfilled = createAction(
3434
type + '/fulfilled',
35-
(result: Returned, args: ActionParams) => {
35+
(result: Returned, args: ActionParams, requestId: string) => {
3636
return {
3737
payload: result,
38-
meta: { args }
38+
meta: { args, requestId }
3939
}
4040
}
4141
)
4242

43-
const pending = createAction(type + '/pending', (args: ActionParams) => {
44-
return {
45-
payload: undefined,
46-
meta: { args }
43+
let requestIdCounter = 0
44+
45+
const pending = createAction(
46+
type + '/pending',
47+
(args: ActionParams, requestId: string) => {
48+
return {
49+
payload: undefined,
50+
meta: { args, requestId }
51+
}
4752
}
48-
})
53+
)
4954

50-
const finished = createAction(type + '/finished', (args: ActionParams) => {
51-
return {
52-
payload: undefined,
53-
meta: { args }
55+
const finished = createAction(
56+
type + '/finished',
57+
(args: ActionParams, requestId: string) => {
58+
return {
59+
payload: undefined,
60+
meta: { args, requestId }
61+
}
5462
}
55-
})
63+
)
5664

5765
const rejected = createAction(
5866
type + '/rejected',
59-
(error: Error, args: ActionParams) => {
67+
(error: Error, args: ActionParams, requestId: string) => {
6068
return {
6169
payload: undefined,
6270
error,
63-
meta: { args }
71+
meta: { args, requestId }
6472
}
6573
}
6674
)
@@ -71,8 +79,11 @@ export function createAsyncThunk<
7179
getState: TA['getState'],
7280
extra: TA['extra']
7381
) => {
82+
requestIdCounter++
83+
const requestId = `${requestIdCounter}`
84+
7485
try {
75-
dispatch(pending(args))
86+
dispatch(pending(args, requestId))
7687
// TODO Also ugly types
7788
const result = (await payloadCreator(args, {
7889
dispatch,
@@ -81,13 +92,13 @@ export function createAsyncThunk<
8192
} as TA)) as Returned
8293

8394
// TODO How do we avoid errors in here from hitting the catch clause?
84-
return dispatch(fulfilled(result, args))
95+
return dispatch(fulfilled(result, args, requestId))
8596
} catch (err) {
8697
// TODO Errors aren't serializable
87-
dispatch(rejected(err, args))
98+
dispatch(rejected(err, args, requestId))
8899
} finally {
89100
// TODO IS there really a benefit from a "finished" action?
90-
dispatch(finished(args))
101+
dispatch(finished(args, requestId))
91102
}
92103
}
93104
}

0 commit comments

Comments
 (0)