1 | import parseName from './parseName'
|
2 | import appFactory from './appFactory'
|
3 |
|
4 | export default (actionInjections, reducerInjections) => (store) => {
|
5 | return next => action => {
|
6 | const {
|
7 | getState,
|
8 | dispatch
|
9 | } = store
|
10 |
|
11 | if (typeof action === 'function') {
|
12 | const {
|
13 | fullName,
|
14 | name,
|
15 | query,
|
16 | params,
|
17 | actionCreator,
|
18 | args,
|
19 | reducer
|
20 | } = action()
|
21 |
|
22 | const reduce = (type, ...args) => {
|
23 | dispatch({
|
24 | type: '@@reduce',
|
25 | payload: {
|
26 | fullName,
|
27 | name,
|
28 | query,
|
29 | type,
|
30 | reducer,
|
31 | payload: args,
|
32 | reducerInjections
|
33 | }
|
34 | })
|
35 | }
|
36 |
|
37 | const getStateByApp = () => getState().get(fullName)
|
38 | const injections = {
|
39 | currentApp: {
|
40 | fullName,
|
41 | name,
|
42 | query,
|
43 | params
|
44 | },
|
45 | store,
|
46 | reduce,
|
47 | getState: getStateByApp,
|
48 | ...actionInjections
|
49 | }
|
50 | const realAction = actionCreator(
|
51 | ...args,
|
52 | injections
|
53 | )
|
54 |
|
55 | if (typeof realAction === 'function') {
|
56 | realAction(injections)
|
57 | }
|
58 |
|
59 | } else if (action.type && action.type == '@@loadApp') {
|
60 | try {
|
61 | const fullName = action.payload.fullName,
|
62 | prevFullName = action.payload.prevFullName,
|
63 | parsedName = parseName(fullName),
|
64 | appInfo = appFactory.getApp(parsedName.name)
|
65 |
|
66 |
|
67 | appInfo.load((component, action, reducer) => {
|
68 | return next({
|
69 | type: '@@loadAppReal',
|
70 | payload: {
|
71 | fullName,
|
72 | appInfo,
|
73 | component,
|
74 | action,
|
75 | reducer,
|
76 | prevFullName
|
77 | }
|
78 | })
|
79 | })
|
80 | }
|
81 | catch (e) {
|
82 | console.error(e)
|
83 | return next(action)
|
84 | }
|
85 |
|
86 | } else {
|
87 | return next(action)
|
88 | }
|
89 | }
|
90 | } |
\ | No newline at end of file |