7
7
AnyAction ,
8
8
MiddlewareAPI ,
9
9
Action ,
10
+ ThunkDispatch ,
10
11
} from '@reduxjs/toolkit'
11
12
12
13
interface BaseActionCreator < P , T extends string , M = never , E = never > {
@@ -61,6 +62,8 @@ export interface ActionListenerMiddlewareAPI<
61
62
> extends MiddlewareAPI < D , S > {
62
63
unsubscribe ( ) : void
63
64
currentPhase : MiddlewarePhase
65
+ // TODO Figure out how to pass this through the other types correctly
66
+ extra : unknown
64
67
}
65
68
66
69
/**
@@ -82,6 +85,10 @@ export interface ActionListenerOptions {
82
85
when ?: When
83
86
}
84
87
88
+ export interface CreateListenerMiddlewareOptions < ExtraArgument = unknown > {
89
+ extra ?: ExtraArgument
90
+ }
91
+
85
92
export interface AddListenerAction <
86
93
A extends AnyAction ,
87
94
S ,
@@ -201,8 +208,10 @@ const actualMiddlewarePhases = ['beforeReducer', 'afterReducer'] as const
201
208
*/
202
209
export function createActionListenerMiddleware <
203
210
S ,
204
- D extends Dispatch < AnyAction > = Dispatch
205
- > ( ) {
211
+ // TODO Carry through the thunk extra arg somehow?
212
+ D extends Dispatch < AnyAction > = ThunkDispatch < S , unknown , AnyAction > ,
213
+ ExtraArgument = unknown
214
+ > ( middlewareOptions : CreateListenerMiddlewareOptions < ExtraArgument > = { } ) {
206
215
type ListenerEntry = ActionListenerOptions & {
207
216
id : string
208
217
listener : ActionListener < any , S , D , any >
@@ -212,6 +221,7 @@ export function createActionListenerMiddleware<
212
221
}
213
222
214
223
const listenerMap = new Map < string , ListenerEntry > ( )
224
+ const { extra } = middlewareOptions
215
225
216
226
const middleware : Middleware <
217
227
{
@@ -253,10 +263,11 @@ export function createActionListenerMiddleware<
253
263
entry . listener ( action , {
254
264
...api ,
255
265
currentPhase,
266
+ extra,
256
267
unsubscribe : entry . unsubscribe ,
257
268
} )
258
269
} catch ( err ) {
259
- // ignore
270
+ // ignore errors deliberately
260
271
}
261
272
}
262
273
if ( currentPhase === 'beforeReducer' ) {
0 commit comments