UNPKG

5.79 kBJavaScriptView Raw
1import thunk from 'redux-thunk'
2import { browserHistory } from 'react-router'
3import { routerMiddleware, routerReducer } from 'react-router-redux'
4
5//
6
7import { ReactApp } from 'super-project/ReactApp'
8import {
9 reducer as realtimeLocationReducer,
10 REALTIME_LOCATION_REDUCER_NAME,
11 actionUpdate
12} from 'super-project/React/realtime-location'
13import {
14 reducerLocaleId as i18nReducerLocaleId,
15 reducerLocales as i18nReducerLocales,
16} from 'super-project/i18n/redux'
17import i18nRegister from 'super-project/i18n/register/isomorphic.client'
18
19//
20
21import { SERVER_REDUCER_NAME, serverReducer } from '../server/redux'
22
23const ROUTER_REDUCDER_NAME = 'routing'
24
25
26
27
28export default ({
29 i18n = JSON.parse(process.env.SUPER_I18N) || false,
30 router,
31 redux,
32 client
33}) => {
34 // ============================================================================
35 // React 初始化
36 // ============================================================================
37
38 const reactApp = new ReactApp({ rootDom: 'root' })
39
40 reactApp.redux.middleware.use(thunk)
41 reactApp.redux.middleware.use(routerMiddleware(browserHistory))
42 if (__CLIENT__) self.routerHistory = browserHistory
43
44
45
46
47 // ============================================================================
48 // Redux/Reducer 初始化
49 // ============================================================================
50
51 const reducers = {
52 // 路由状态扩展
53 [ROUTER_REDUCDER_NAME]: routerReducer,
54 // 目的:新页面请求处理完成后再改变URL
55 [REALTIME_LOCATION_REDUCER_NAME]: realtimeLocationReducer,
56 // 对应服务器生成的store
57 [SERVER_REDUCER_NAME]: serverReducer,
58 }
59 if (i18n) {
60 reducers.localeId = i18nReducerLocaleId
61 reducers.locales = i18nReducerLocales
62 }
63
64 // 兼容配置嵌套
65 if (!redux)
66 redux = client.redux
67
68 const { combineReducers } = redux
69 if (typeof combineReducers === 'object') {
70 for (let key in combineReducers) {
71 reducers[key] = combineReducers[key]
72 }
73 }
74 for (let key in reducers) {
75 reactApp.redux.reducer.use(key, reducers[key])
76 }
77
78
79
80
81 // ============================================================================
82 // 路由初始化
83 // ============================================================================
84 if (typeof router !== 'object') {
85 if (client.router) // 兼容配置嵌套
86 router = client.router
87 else
88 router = {}
89 }
90 reactApp.react.router.use({
91 path: '',
92 // component: App, 可扩展1层component
93 childRoutes: [router]
94 })
95
96
97
98
99 // ============================================================================
100 // 客户端专用初始化流程
101 // ============================================================================
102
103 if (__CLIENT__) {
104 const {
105 before,
106 after,
107 } = client
108 const onRouterUpdate = client.routerUpdate || client.onRouterUpdate
109 const onHistoryUpdate = client.historyUpdate || client.onHistoryUpdate
110
111 reactApp.react.router.ext({
112 onUpdate: (...args) => {
113 if (__DEV__)
114 console.log(
115 `[super/client] ` +
116 `callback: onRouterUpdate`,
117 args
118 )
119 // if (__DEV__) console.log('router onUpdate', self.__LATHPATHNAME__, location.pathname)
120 if (typeof onRouterUpdate === 'function')
121 onRouterUpdate(...args)
122 }
123 })
124
125 if (i18n) i18nRegister(__REDUX_STATE__)
126
127 let beforePromise = before
128 if (__DEV__)
129 console.log(
130 `[super/client] ` +
131 `callback: before`,
132 // args
133 )
134 if (typeof before === 'function') {
135 beforePromise = new Promise(resolve => {
136 before()
137 resolve()
138 })
139 } else if (typeof before !== 'object' || typeof before.then !== 'function') {
140 beforePromise = new Promise(resolve => resolve())
141 }
142
143 beforePromise.then(() =>
144 reactApp.run({
145 browserHistoryOnUpdate: (location, store) => {
146 // 回调: browserHistoryOnUpdate
147 // 正常路由跳转时,URL发生变化后瞬间会触发,顺序在react组件读取、渲染之前
148 if (__DEV__) {
149 console.log('🌏 browserHistory update', location)
150 }
151 // console.log(actionUpdate(location))
152 store.dispatch(actionUpdate(location))
153 // console.log(store.getState())
154
155 if (__DEV__)
156 console.log(
157 `[super/client] ` +
158 `callback: onHistoryUpdate`,
159 [location, store]
160 )
161 if (typeof onHistoryUpdate === 'function')
162 onHistoryUpdate(location, store)
163 }
164 })
165 )
166 .then((appData) => {
167 if (__DEV__)
168 console.log(
169 `[super/client] ` +
170 `callback: after`,
171 [appData]
172 )
173 if (typeof after === 'function') after(appData)
174 })
175 }
176
177
178
179
180 // ============================================================================
181 // 结束
182 // ============================================================================
183 return reactApp
184}