Skip to content

Commit 1e3d045

Browse files
authored
Merge pull request #3763 from EskiMojo14/cdm-tweaks
2 parents 1016dd9 + d8b7e4a commit 1e3d045

File tree

4 files changed

+47
-48
lines changed

4 files changed

+47
-48
lines changed

packages/toolkit/src/dynamicMiddleware/index.ts

Lines changed: 25 additions & 26 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,33 +27,32 @@ 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>
3338
>(): DynamicMiddlewareInstance<State, Dispatch> => {
3439
const instanceId = nanoid()
3540
const middlewareMap = new Map<string, MiddlewareEntry<State, Dispatch>>()
3641

37-
const insertEntry = (entry: MiddlewareEntry<State, Dispatch>) => {
38-
middlewareMap.set(entry.id, entry)
39-
}
40-
41-
const withMiddleware = (() => {
42-
const withMiddleware = createAction(
42+
const withMiddleware = Object.assign(
43+
createAction(
4344
'dynamicMiddleware/add',
4445
(...middlewares: Middleware<any, State, Dispatch>[]) => ({
4546
payload: middlewares,
4647
meta: {
4748
instanceId,
4849
},
4950
})
50-
)
51-
// @ts-ignore
52-
withMiddleware.withTypes = () => withMiddleware
53-
return withMiddleware as WithMiddleware<State, Dispatch>
54-
})()
51+
),
52+
{ withTypes: () => withMiddleware }
53+
) as WithMiddleware<State, Dispatch>
5554

56-
const addMiddleware = (() => {
55+
const addMiddleware = Object.assign(
5756
function addMiddleware(...middlewares: Middleware<any, State, Dispatch>[]) {
5857
middlewares.forEach((middleware) => {
5958
let entry = find(
@@ -63,16 +62,13 @@ export const createDynamicMiddleware = <
6362
if (!entry) {
6463
entry = createMiddlewareEntry(middleware)
6564
}
66-
insertEntry(entry)
65+
middlewareMap.set(entry.id, entry)
6766
})
68-
}
69-
addMiddleware.withTypes = () => addMiddleware
70-
return addMiddleware as AddMiddleware<State, Dispatch>
71-
})()
67+
},
68+
{ withTypes: () => addMiddleware }
69+
) as AddMiddleware<State, Dispatch>
7270

73-
const getFinalMiddleware = (
74-
api: MiddlewareAPI<Dispatch, State>
75-
): ReturnType<Middleware<any, State, Dispatch>> => {
71+
const getFinalMiddleware: Middleware<{}, State, Dispatch> = (api) => {
7672
const appliedMiddleware = Array.from(middlewareMap.values()).map(
7773
(entry) => {
7874
let applied = entry.applied.get(api)
@@ -86,13 +82,15 @@ export const createDynamicMiddleware = <
8682
return compose(...appliedMiddleware)
8783
}
8884

85+
const isWithMiddleware = isAllOf(
86+
isAction,
87+
withMiddleware,
88+
matchInstance(instanceId)
89+
)
90+
8991
const middleware: DynamicMiddleware<State, Dispatch> =
9092
(api) => (next) => (action) => {
91-
if (
92-
isAction(action) &&
93-
withMiddleware.match(action) &&
94-
action.meta.instanceId === instanceId
95-
) {
93+
if (isWithMiddleware(action)) {
9694
addMiddleware(...action.payload)
9795
return api.dispatch
9896
}
@@ -103,5 +101,6 @@ export const createDynamicMiddleware = <
103101
middleware,
104102
addMiddleware,
105103
withMiddleware,
104+
instanceId,
106105
}
107106
}

packages/toolkit/src/dynamicMiddleware/react/index.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,7 @@ export const createDynamicMiddleware = <
8484
Middlewares extends Middleware<any, State, Dispatch>[]
8585
>(...middlewares: Middlewares) {
8686
instance.addMiddleware(...middlewares)
87-
return function useDispatchWithMiddleware() {
88-
return useDispatch()
89-
}
87+
return useDispatch
9088
}
9189
createDispatchWithMiddlewareHook.withTypes = () =>
9290
createDispatchWithMiddlewareHook

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

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,35 @@ 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'
8+
9+
const probeType = 'probeableMW/probe'
710

811
export interface ProbeMiddleware
9-
extends BaseActionCreator<number, 'probeableMW/probe'> {
10-
<Id extends number>(id: Id): PayloadAction<Id, 'probeableMW/probe'>
12+
extends BaseActionCreator<number, typeof probeType> {
13+
<Id extends number>(id: Id): PayloadAction<Id, typeof probeType>
1114
}
1215

13-
export const probeMiddleware = createAction<number>(
14-
'probeableMW/probe'
15-
) as ProbeMiddleware
16+
export const probeMiddleware = createAction(probeType) as ProbeMiddleware
17+
18+
const matchId =
19+
<Id extends number>(id: Id) =>
20+
(action: any): action is PayloadAction<Id> =>
21+
action.payload === id
1622

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-
) {
23+
export const makeProbeableMiddleware = <Id extends number>(
24+
id: Id
25+
): Middleware<{
26+
(action: PayloadAction<Id, typeof probeType>): 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)