-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathasyncThunkCreator.ts
192 lines (183 loc) · 4.84 KB
/
asyncThunkCreator.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import type {
AsyncThunk,
AsyncThunkConfig,
AsyncThunkOptions,
AsyncThunkPayloadCreator,
OverrideThunkApiConfigs,
} from './createAsyncThunk'
import { createAsyncThunk } from './createAsyncThunk'
import type { CaseReducer } from './createReducer'
import type {
CreatorCaseReducers,
ReducerCreator,
ReducerDefinition,
} from './createSlice'
import { ReducerType } from './createSlice'
import type { Id } from './tsHelpers'
export type AsyncThunkSliceReducerConfig<
State,
ThunkArg extends any,
Returned = unknown,
ThunkApiConfig extends AsyncThunkConfig = {},
> = {
pending?: CaseReducer<
State,
ReturnType<AsyncThunk<Returned, ThunkArg, ThunkApiConfig>['pending']>
>
rejected?: CaseReducer<
State,
ReturnType<AsyncThunk<Returned, ThunkArg, ThunkApiConfig>['rejected']>
>
fulfilled?: CaseReducer<
State,
ReturnType<AsyncThunk<Returned, ThunkArg, ThunkApiConfig>['fulfilled']>
>
settled?: CaseReducer<
State,
ReturnType<
AsyncThunk<Returned, ThunkArg, ThunkApiConfig>['rejected' | 'fulfilled']
>
>
options?: AsyncThunkOptions<ThunkArg, ThunkApiConfig>
}
export interface AsyncThunkSliceReducerDefinition<
State,
ThunkArg extends any,
Returned = unknown,
ThunkApiConfig extends AsyncThunkConfig = {},
> extends AsyncThunkSliceReducerConfig<
State,
ThunkArg,
Returned,
ThunkApiConfig
>,
ReducerDefinition<ReducerType.asyncThunk> {
payloadCreator: AsyncThunkPayloadCreator<Returned, ThunkArg, ThunkApiConfig>
}
/**
* Providing these as part of the config would cause circular types, so we disallow passing them
*/
type PreventCircular<ThunkApiConfig> = {
[K in keyof ThunkApiConfig]: K extends 'state' | 'dispatch'
? never
: ThunkApiConfig[K]
}
export interface AsyncThunkCreator<
State,
CurriedThunkApiConfig extends
PreventCircular<AsyncThunkConfig> = PreventCircular<AsyncThunkConfig>,
> {
<Returned, ThunkArg = void>(
payloadCreator: AsyncThunkPayloadCreator<
Returned,
ThunkArg,
CurriedThunkApiConfig
>,
config?: AsyncThunkSliceReducerConfig<
State,
ThunkArg,
Returned,
CurriedThunkApiConfig
>,
): AsyncThunkSliceReducerDefinition<
State,
ThunkArg,
Returned,
CurriedThunkApiConfig
>
<
Returned,
ThunkArg,
ThunkApiConfig extends PreventCircular<AsyncThunkConfig> = {},
>(
payloadCreator: AsyncThunkPayloadCreator<
Returned,
ThunkArg,
ThunkApiConfig
>,
config?: AsyncThunkSliceReducerConfig<
State,
ThunkArg,
Returned,
ThunkApiConfig
>,
): AsyncThunkSliceReducerDefinition<State, ThunkArg, Returned, ThunkApiConfig>
withTypes<
ThunkApiConfig extends PreventCircular<AsyncThunkConfig>,
>(): AsyncThunkCreator<
State,
OverrideThunkApiConfigs<CurriedThunkApiConfig, ThunkApiConfig>
>
}
export type AsyncThunkCreatorExposes<
State,
CaseReducers extends CreatorCaseReducers<State>,
> = {
actions: {
[ReducerName in keyof CaseReducers]: CaseReducers[ReducerName] extends AsyncThunkSliceReducerDefinition<
State,
infer ThunkArg,
infer Returned,
infer ThunkApiConfig
>
? AsyncThunk<Returned, ThunkArg, ThunkApiConfig>
: never
}
caseReducers: {
[ReducerName in keyof CaseReducers]: CaseReducers[ReducerName] extends AsyncThunkSliceReducerDefinition<
State,
any,
any,
any
>
? Id<
Pick<
Required<CaseReducers[ReducerName]>,
'fulfilled' | 'rejected' | 'pending' | 'settled'
>
>
: never
}
}
export const asyncThunkCreator: ReducerCreator<ReducerType.asyncThunk> = {
type: ReducerType.asyncThunk,
create: /* @__PURE__ */ (() => {
function asyncThunk(
payloadCreator: AsyncThunkPayloadCreator<any, any>,
config: AsyncThunkSliceReducerConfig<any, any>,
): AsyncThunkSliceReducerDefinition<any, any> {
return {
_reducerDefinitionType: ReducerType.asyncThunk,
payloadCreator,
...config,
}
}
asyncThunk.withTypes = () => asyncThunk
return asyncThunk as AsyncThunkCreator<any>
})(),
handle({ type }, definition, context) {
const { payloadCreator, fulfilled, pending, rejected, settled, options } =
definition
const thunk = createAsyncThunk(type, payloadCreator, options as any)
context.exposeAction(thunk)
if (fulfilled) {
context.addCase(thunk.fulfilled, fulfilled)
}
if (pending) {
context.addCase(thunk.pending, pending)
}
if (rejected) {
context.addCase(thunk.rejected, rejected)
}
if (settled) {
context.addMatcher(thunk.settled, settled)
}
context.exposeCaseReducer({
fulfilled: fulfilled || noop,
pending: pending || noop,
rejected: rejected || noop,
settled: settled || noop,
})
},
}
function noop() {}