| 
									
										
										
										
											2016-01-27 11:34:44 -08:00
										 |  |  | import { Observable } from 'rx'; | 
					
						
							|  |  |  | import { match } from 'react-router'; | 
					
						
							|  |  |  | import { compose, createStore, applyMiddleware } from 'redux'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // main app
 | 
					
						
							|  |  |  | import App from './App.jsx'; | 
					
						
							|  |  |  | // app routes
 | 
					
						
							| 
									
										
										
										
											2016-08-12 15:28:05 -07:00
										 |  |  | import createChildRoute from './routes'; | 
					
						
							| 
									
										
										
										
											2016-01-27 11:34:44 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | // redux
 | 
					
						
							| 
									
										
										
										
											2016-05-04 16:46:19 -07:00
										 |  |  | import { createEpic } from 'redux-epic'; | 
					
						
							| 
									
										
										
										
											2016-01-27 11:34:44 -08:00
										 |  |  | import createReducer from './create-reducer'; | 
					
						
							|  |  |  | import sagas from './sagas'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // general utils
 | 
					
						
							|  |  |  | import servicesCreator from '../utils/services-creator'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const createRouteProps = Observable.fromNodeCallback(match); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // createApp(settings: {
 | 
					
						
							| 
									
										
										
										
											2016-03-02 19:45:54 -08:00
										 |  |  | //   location?: Location|String,
 | 
					
						
							| 
									
										
										
										
											2016-01-27 11:34:44 -08:00
										 |  |  | //   history?: History,
 | 
					
						
							| 
									
										
										
										
											2016-06-14 09:31:41 -07:00
										 |  |  | //   syncHistoryWithStore?: ((history, store) => history) = (x) => x,
 | 
					
						
							| 
									
										
										
										
											2016-01-27 11:34:44 -08:00
										 |  |  | //   initialState?: Object|Void,
 | 
					
						
							|  |  |  | //   serviceOptions?: Object,
 | 
					
						
							|  |  |  | //   middlewares?: Function[],
 | 
					
						
							|  |  |  | //   sideReducers?: Object
 | 
					
						
							|  |  |  | //   enhancers?: Function[],
 | 
					
						
							|  |  |  | //   sagas?: Function[],
 | 
					
						
							|  |  |  | // }) => Observable
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Either location or history must be defined
 | 
					
						
							|  |  |  | export default function createApp({ | 
					
						
							|  |  |  |   location, | 
					
						
							|  |  |  |   history, | 
					
						
							| 
									
										
										
										
											2016-06-14 09:31:41 -07:00
										 |  |  |   syncHistoryWithStore = (x) => x, | 
					
						
							|  |  |  |   syncOptions = {}, | 
					
						
							| 
									
										
										
										
											2016-01-27 11:34:44 -08:00
										 |  |  |   initialState, | 
					
						
							|  |  |  |   serviceOptions = {}, | 
					
						
							|  |  |  |   middlewares: sideMiddlewares = [], | 
					
						
							|  |  |  |   enhancers: sideEnhancers = [], | 
					
						
							|  |  |  |   reducers: sideReducers = {}, | 
					
						
							| 
									
										
										
										
											2016-04-24 21:54:48 -07:00
										 |  |  |   sagas: sideSagas = [], | 
					
						
							|  |  |  |   sagaOptions: sideSagaOptions = {} | 
					
						
							| 
									
										
										
										
											2016-01-27 11:34:44 -08:00
										 |  |  | }) { | 
					
						
							|  |  |  |   const sagaOptions = { | 
					
						
							| 
									
										
										
										
											2016-04-24 21:54:48 -07:00
										 |  |  |     ...sideSagaOptions, | 
					
						
							| 
									
										
										
										
											2016-11-12 17:09:07 +00:00
										 |  |  |     services: servicesCreator(serviceOptions) | 
					
						
							| 
									
										
										
										
											2016-01-27 11:34:44 -08:00
										 |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-24 21:54:48 -07:00
										 |  |  |   const sagaMiddleware = createEpic( | 
					
						
							|  |  |  |     sagaOptions, | 
					
						
							|  |  |  |     ...sagas, | 
					
						
							|  |  |  |     ...sideSagas | 
					
						
							|  |  |  |   ); | 
					
						
							| 
									
										
										
										
											2016-01-27 11:34:44 -08:00
										 |  |  |   const enhancers = [ | 
					
						
							|  |  |  |     applyMiddleware( | 
					
						
							|  |  |  |       ...sideMiddlewares, | 
					
						
							| 
									
										
										
										
											2016-04-24 21:54:48 -07:00
										 |  |  |       sagaMiddleware | 
					
						
							| 
									
										
										
										
											2016-01-27 11:34:44 -08:00
										 |  |  |     ), | 
					
						
							|  |  |  |     // enhancers must come after middlewares
 | 
					
						
							|  |  |  |     // on client side these are things like Redux DevTools
 | 
					
						
							|  |  |  |     ...sideEnhancers | 
					
						
							|  |  |  |   ]; | 
					
						
							|  |  |  |   const reducer = createReducer(sideReducers); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // create composed store enhancer
 | 
					
						
							|  |  |  |   // use store enhancer function to enhance `createStore` function
 | 
					
						
							|  |  |  |   // call enhanced createStore function with reducer and initialState
 | 
					
						
							|  |  |  |   // to create store
 | 
					
						
							|  |  |  |   const store = compose(...enhancers)(createStore)(reducer, initialState); | 
					
						
							| 
									
										
										
										
											2016-06-14 09:31:41 -07:00
										 |  |  |   // sync history client side with store.
 | 
					
						
							|  |  |  |   // server side this is an identity function and history is undefined
 | 
					
						
							|  |  |  |   history = syncHistoryWithStore(history, store, syncOptions); | 
					
						
							| 
									
										
										
										
											2016-08-12 15:28:05 -07:00
										 |  |  |   const routes = { | 
					
						
							|  |  |  |     components: App, | 
					
						
							|  |  |  |     ...createChildRoute({ | 
					
						
							|  |  |  |       getState() { return store.getState(); } | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  |   }; | 
					
						
							| 
									
										
										
										
											2016-01-27 11:34:44 -08:00
										 |  |  |   // createRouteProps({
 | 
					
						
							| 
									
										
										
										
											2016-03-02 19:45:54 -08:00
										 |  |  |   //   redirect: LocationDescriptor,
 | 
					
						
							| 
									
										
										
										
											2016-01-27 11:34:44 -08:00
										 |  |  |   //   history: History,
 | 
					
						
							|  |  |  |   //   routes: Object
 | 
					
						
							|  |  |  |   // }) => Observable
 | 
					
						
							|  |  |  |   return createRouteProps({ routes, location, history }) | 
					
						
							| 
									
										
										
										
											2016-03-02 19:45:54 -08:00
										 |  |  |     .map(([ redirect, props ]) => ({ | 
					
						
							|  |  |  |       redirect, | 
					
						
							| 
									
										
										
										
											2016-01-27 11:34:44 -08:00
										 |  |  |       props, | 
					
						
							|  |  |  |       reducer, | 
					
						
							| 
									
										
										
										
											2016-04-24 21:54:48 -07:00
										 |  |  |       store, | 
					
						
							|  |  |  |       epic: sagaMiddleware | 
					
						
							| 
									
										
										
										
											2016-01-27 11:34:44 -08:00
										 |  |  |     })); | 
					
						
							|  |  |  | } |