1 | import { Map } from 'immutable'
|
2 | import wrapMapStateToProps from './wrapMapStateToProps'
|
3 | import wrapMapDispatchToProps from './wrapMapDispatchToProps'
|
4 | import createReduxConnector from './createReduxConnector'
|
5 |
|
6 | export default function (state = Map(), {
|
7 | type,
|
8 | payload
|
9 | }) {
|
10 | switch (type) {
|
11 | case "@@loadAppReal":
|
12 | return loadApp(state, payload)
|
13 | case "@@reduce":
|
14 | return reduce(state, payload)
|
15 | case "@@clearAppState":
|
16 | return clearAppState(state, payload)
|
17 | default:
|
18 | return state
|
19 | }
|
20 | }
|
21 |
|
22 |
|
23 | function loadApp(state, {
|
24 | fullName,
|
25 | prevFullName,
|
26 | appInfo,
|
27 | component = {},
|
28 | action = {},
|
29 | reducer = {}
|
30 | }) {
|
31 | if (appInfo) {
|
32 |
|
33 | if (appInfo.name != 'edfx-app-root' && appInfo.name != null) {
|
34 | _hmt && _hmt.push(['_trackEvent', appInfo.moduleName || '', appInfo.description || '', appInfo.name])
|
35 | }
|
36 | }
|
37 | if (!state.has(fullName)) {
|
38 | state = state.set(fullName, Map())
|
39 | const actionInstance = typeof action == 'function' ? action({ appInfo, fullName }) : action,
|
40 | reducerInstance = typeof reducer == 'function' ? reducer({ appInfo, fullName }) : reducer,
|
41 | container = createReduxConnector(
|
42 | component,
|
43 | wrapMapStateToProps(fullName),
|
44 | wrapMapDispatchToProps(fullName, actionInstance, reducerInstance),
|
45 | null, { withRef: true, pure: true }
|
46 | )
|
47 |
|
48 | state = state.setIn([fullName, '@@require'], Map({
|
49 | fullName,
|
50 | appInfo,
|
51 | component,
|
52 | action: actionInstance,
|
53 | reducer: reducerInstance,
|
54 | container
|
55 | }))
|
56 | }
|
57 |
|
58 | if (prevFullName && prevFullName != fullName) {
|
59 | state = clearAppState(state, { fullName: prevFullName })
|
60 | }
|
61 |
|
62 | return state
|
63 | }
|
64 |
|
65 | function clearAppState(state, {
|
66 | fullName
|
67 | }) {
|
68 |
|
69 | if (!state.has(fullName))
|
70 | return state
|
71 |
|
72 | const ks = []
|
73 | state.get(fullName).mapKeys(k => {
|
74 | if (k != '@@require')
|
75 | ks.push(k)
|
76 | return k
|
77 | })
|
78 |
|
79 | ks.forEach(k => {
|
80 | if (k)
|
81 | state = state.update(fullName, x => x.remove(k))
|
82 | })
|
83 |
|
84 | return state
|
85 | }
|
86 |
|
87 |
|
88 | function reduce(state, {
|
89 | reducer,
|
90 | type,
|
91 | payload,
|
92 | fullName,
|
93 | injectFunsForReducer
|
94 | }) {
|
95 |
|
96 | var startDate = new Date()
|
97 | var oldState = state.get(fullName)
|
98 | var newState = reducer[type].apply(this, [oldState].concat(payload))
|
99 |
|
100 | if (typeof newState === "function") {
|
101 | newState = newState(injectFunsForReducer)
|
102 | }
|
103 |
|
104 | if (window.__mk_record_action__ === true) {
|
105 | window.__mk_actions__ = window.__mk_actions__ || []
|
106 | var endDate = new Date()
|
107 | window.__mk_actions__.unshift({
|
108 | appFullName: fullName,
|
109 | reduceMethod: type,
|
110 | payload,
|
111 | oldState,
|
112 | newState,
|
113 | startTime: startDate.getHours() + ':' + startDate.getMinutes() + ':' + startDate.getSeconds() + '.' + startDate.getMilliseconds(),
|
114 | endTime: endDate.getHours() + ':' + endDate.getMinutes() + ':' + endDate.getSeconds() + '.' + endDate.getMilliseconds(),
|
115 | elapsedTime: Math.abs((startDate.getTime() - endDate.getTime()))
|
116 | })
|
117 | } else {
|
118 | if (window.__mk_actions__)
|
119 | window.__mk_actions__ = undefined
|
120 | }
|
121 |
|
122 | return state.set(fullName, newState)
|
123 | }
|