Skip to content

Commit b6314ef

Browse files
committed
Add getOriginalState and verify predicate test behavior
1 parent afc7df3 commit b6314ef

File tree

2 files changed

+51
-6
lines changed

2 files changed

+51
-6
lines changed

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ export interface ActionListenerMiddlewareAPI<
6060
D extends Dispatch<AnyAction>,
6161
O extends ActionListenerOptions
6262
> extends MiddlewareAPI<D, S> {
63+
getOriginalState: () => S
6364
unsubscribe(): void
6465
currentPhase: MiddlewarePhase
6566
// TODO Figure out how to pass this through the other types correctly
@@ -246,12 +247,14 @@ export function createActionListenerMiddleware<
246247
return
247248
}
248249

249-
let stateBefore = api.getState()
250250
if (listenerMap.size === 0) {
251251
return next(action)
252252
}
253253

254254
let result: unknown
255+
const originalState = api.getState()
256+
const getOriginalState = () => originalState
257+
255258
for (const currentPhase of actualMiddlewarePhases) {
256259
let stateNow = api.getState()
257260
for (let entry of listenerMap.values()) {
@@ -265,6 +268,7 @@ export function createActionListenerMiddleware<
265268
try {
266269
entry.listener(action, {
267270
...api,
271+
getOriginalState,
268272
currentPhase,
269273
extra,
270274
unsubscribe: entry.unsubscribe,
@@ -302,20 +306,21 @@ export function createActionListenerMiddleware<
302306
// eslint-disable-next-line no-redeclare
303307
function addListener<
304308
MA extends AnyAction,
305-
M extends MatchFunction<MA>,
309+
M extends ListenerPredicate<MA>,
306310
O extends ActionListenerOptions
307311
>(
308312
matcher: M,
309-
listener: ActionListener<GuardedType<M>, S, D, O>,
313+
listener: ActionListener<AnyAction, S, D, O>,
310314
options?: O
311-
): Unsubscribe // eslint-disable-next-line no-redeclare
315+
): Unsubscribe
316+
// eslint-disable-next-line no-redeclare
312317
function addListener<
313318
MA extends AnyAction,
314-
M extends ListenerPredicate<MA>,
319+
M extends MatchFunction<MA>,
315320
O extends ActionListenerOptions
316321
>(
317322
matcher: M,
318-
listener: ActionListener<AnyAction, S, D, O>,
323+
listener: ActionListener<GuardedType<M>, S, D, O>,
319324
options?: O
320325
): Unsubscribe
321326
// eslint-disable-next-line no-redeclare

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {
22
configureStore,
33
createAction,
4+
createSlice,
45
AnyAction,
56
isAnyOf,
67
} from '@reduxjs/toolkit'
@@ -14,6 +15,8 @@ import {
1415

1516
const middlewareApi = {
1617
getState: expect.any(Function),
18+
getOriginalState: expect.any(Function),
19+
extra: undefined,
1720
dispatch: expect.any(Function),
1821
currentPhase: expect.stringMatching(/beforeReducer|afterReducer/),
1922
unsubscribe: expect.any(Function),
@@ -128,6 +131,43 @@ describe('createActionListenerMiddleware', () => {
128131
])
129132
})
130133

134+
test('Can subscribe with an action predicate function', () => {
135+
const slice = createSlice({
136+
name: 'counter',
137+
initialState: 0,
138+
reducers: {
139+
increment(state) {
140+
return state + 1
141+
},
142+
},
143+
})
144+
const { increment } = slice.actions
145+
146+
store = configureStore({
147+
reducer: slice.reducer,
148+
middleware: [middleware] as const,
149+
})
150+
151+
const listener = jest.fn((_: TestAction1) => {})
152+
153+
let listenerCalls = 0
154+
155+
middleware.addListener(
156+
(action, state) => {
157+
return state > 1
158+
},
159+
(action, listenerApi) => {
160+
listenerCalls++
161+
}
162+
)
163+
164+
store.dispatch(increment())
165+
store.dispatch(increment())
166+
store.dispatch(increment())
167+
168+
expect(listenerCalls).toBe(2)
169+
})
170+
131171
test('subscribing with the same listener will not make it trigger twice (like EventTarget.addEventListener())', () => {
132172
const listener = jest.fn((_: TestAction1) => {})
133173

0 commit comments

Comments
 (0)