Skip to content

Commit 0d4bbd7

Browse files
committed
Use matchers for cDM and include instanceId in final instance.
1 parent 32e9081 commit 0d4bbd7

File tree

3 files changed

+31
-23
lines changed

3 files changed

+31
-23
lines changed

packages/toolkit/src/dynamicMiddleware/index.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ import type {
22
Middleware,
33
Dispatch as ReduxDispatch,
44
UnknownAction,
5-
MiddlewareAPI,
65
} from 'redux'
76
import { compose } from 'redux'
87
import { createAction, isAction } from '../createAction'
8+
import { isAllOf } from '../matchers'
99
import { nanoid } from '../nanoid'
1010
import { find } from '../utils'
1111
import type {
@@ -27,6 +27,11 @@ const createMiddlewareEntry = <
2727
applied: new Map(),
2828
})
2929

30+
const matchInstance =
31+
(instanceId: string) =>
32+
(action: any): action is { meta: { instanceId: string } } =>
33+
action?.meta?.instanceId === instanceId
34+
3035
export const createDynamicMiddleware = <
3136
State = any,
3237
Dispatch extends ReduxDispatch<UnknownAction> = ReduxDispatch<UnknownAction>
@@ -70,9 +75,7 @@ export const createDynamicMiddleware = <
7075
return addMiddleware as AddMiddleware<State, Dispatch>
7176
})()
7277

73-
const getFinalMiddleware = (
74-
api: MiddlewareAPI<Dispatch, State>
75-
): ReturnType<Middleware<any, State, Dispatch>> => {
78+
const getFinalMiddleware: Middleware<{}, State, Dispatch> = (api) => {
7679
const appliedMiddleware = Array.from(middlewareMap.values()).map(
7780
(entry) => {
7881
let applied = entry.applied.get(api)
@@ -86,13 +89,15 @@ export const createDynamicMiddleware = <
8689
return compose(...appliedMiddleware)
8790
}
8891

92+
const isWithMiddleware = isAllOf(
93+
isAction,
94+
withMiddleware,
95+
matchInstance(instanceId)
96+
)
97+
8998
const middleware: DynamicMiddleware<State, Dispatch> =
9099
(api) => (next) => (action) => {
91-
if (
92-
isAction(action) &&
93-
withMiddleware.match(action) &&
94-
action.meta.instanceId === instanceId
95-
) {
100+
if (isWithMiddleware(action)) {
96101
addMiddleware(...action.payload)
97102
return api.dispatch
98103
}
@@ -103,5 +108,6 @@ export const createDynamicMiddleware = <
103108
middleware,
104109
addMiddleware,
105110
withMiddleware,
111+
instanceId,
106112
}
107113
}

packages/toolkit/src/dynamicMiddleware/tests/index.test.ts

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { configureStore } from '../../configureStore'
44
import type { BaseActionCreator, PayloadAction } from '../../createAction'
55
import { isAction } from '../../createAction'
66
import { createAction } from '../../createAction'
7+
import { isAllOf } from '../../matchers'
78

89
export interface ProbeMiddleware
910
extends BaseActionCreator<number, 'probeableMW/probe'> {
@@ -14,24 +15,24 @@ export const probeMiddleware = createAction<number>(
1415
'probeableMW/probe'
1516
) as ProbeMiddleware
1617

17-
export const makeProbeableMiddleware =
18-
<Id extends number>(
19-
id: Id
20-
): Middleware<{
21-
(action: PayloadAction<Id, 'probeableMW/probe'>): Id
22-
}> =>
23-
(api) =>
24-
(next) =>
25-
(action) => {
26-
if (
27-
isAction(action) &&
28-
probeMiddleware.match(action) &&
29-
action.payload === id
30-
) {
18+
const matchId =
19+
<Id extends number>(id: Id) =>
20+
(action: any): action is PayloadAction<Id> =>
21+
action.payload === id
22+
23+
export const makeProbeableMiddleware = <Id extends number>(
24+
id: Id
25+
): Middleware<{
26+
(action: PayloadAction<Id, 'probeableMW/probe'>): Id
27+
}> => {
28+
const isMiddlewareAction = isAllOf(isAction, probeMiddleware, matchId(id))
29+
return (api) => (next) => (action) => {
30+
if (isMiddlewareAction(action)) {
3131
return id
3232
}
3333
return next(action)
3434
}
35+
}
3536

3637
const staticMiddleware = makeProbeableMiddleware(1)
3738

packages/toolkit/src/dynamicMiddleware/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,5 @@ export type DynamicMiddlewareInstance<
8989
middleware: DynamicMiddleware<State, Dispatch>
9090
addMiddleware: AddMiddleware<State, Dispatch>
9191
withMiddleware: WithMiddleware<State, Dispatch>
92+
instanceId: string
9293
}

0 commit comments

Comments
 (0)