Skip to content

Commit de41ba4

Browse files
committed
Add badly typed support for an "extra" arg on creation
Also use ThunkDispatch as the default type
1 parent 3e6509d commit de41ba4

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

packages/action-listener-middleware/src/index.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
AnyAction,
88
MiddlewareAPI,
99
Action,
10+
ThunkDispatch,
1011
} from '@reduxjs/toolkit'
1112

1213
interface BaseActionCreator<P, T extends string, M = never, E = never> {
@@ -61,6 +62,8 @@ export interface ActionListenerMiddlewareAPI<
6162
> extends MiddlewareAPI<D, S> {
6263
unsubscribe(): void
6364
currentPhase: MiddlewarePhase
65+
// TODO Figure out how to pass this through the other types correctly
66+
extra: unknown
6467
}
6568

6669
/**
@@ -82,6 +85,10 @@ export interface ActionListenerOptions {
8285
when?: When
8386
}
8487

88+
export interface CreateListenerMiddlewareOptions<ExtraArgument = unknown> {
89+
extra?: ExtraArgument
90+
}
91+
8592
export interface AddListenerAction<
8693
A extends AnyAction,
8794
S,
@@ -201,8 +208,10 @@ const actualMiddlewarePhases = ['beforeReducer', 'afterReducer'] as const
201208
*/
202209
export function createActionListenerMiddleware<
203210
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> = {}) {
206215
type ListenerEntry = ActionListenerOptions & {
207216
id: string
208217
listener: ActionListener<any, S, D, any>
@@ -212,6 +221,7 @@ export function createActionListenerMiddleware<
212221
}
213222

214223
const listenerMap = new Map<string, ListenerEntry>()
224+
const { extra } = middlewareOptions
215225

216226
const middleware: Middleware<
217227
{
@@ -253,10 +263,11 @@ export function createActionListenerMiddleware<
253263
entry.listener(action, {
254264
...api,
255265
currentPhase,
266+
extra,
256267
unsubscribe: entry.unsubscribe,
257268
})
258269
} catch (err) {
259-
// ignore
270+
// ignore errors deliberately
260271
}
261272
}
262273
if (currentPhase === 'beforeReducer') {

packages/action-listener-middleware/src/tests/listenerMiddleware.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,30 @@ describe('createActionListenerMiddleware', () => {
4545
})
4646
})
4747

48+
test('Allows passing an extra argument on middleware creation', () => {
49+
const originalExtra = 42
50+
middleware = createActionListenerMiddleware({
51+
extra: originalExtra,
52+
})
53+
reducer = jest.fn(() => ({}))
54+
store = configureStore({
55+
reducer,
56+
middleware: [middleware] as const,
57+
})
58+
59+
let foundExtra = null
60+
61+
middleware.addListener(
62+
(action: AnyAction) => true,
63+
(action, listenerApi) => {
64+
foundExtra = listenerApi.extra
65+
}
66+
)
67+
68+
store.dispatch(testAction1('a'))
69+
expect(foundExtra).toBe(originalExtra)
70+
})
71+
4872
test('directly subscribing', () => {
4973
const listener = jest.fn((_: TestAction1) => {})
5074

0 commit comments

Comments
 (0)