| 
									
										
										
										
											2016-01-27 11:34:44 -08:00
										 |  |  | import { Observable } from 'rx'; | 
					
						
							| 
									
										
										
										
											2018-01-05 15:07:41 -08:00
										 |  |  | import createDebugger from 'debug'; | 
					
						
							| 
									
										
										
										
											2016-01-27 11:34:44 -08:00
										 |  |  | import { compose, createStore, applyMiddleware } from 'redux'; | 
					
						
							| 
									
										
										
										
											2017-11-09 17:10:30 -08:00
										 |  |  | import { selectLocationState, connectRoutes } from 'redux-first-router'; | 
					
						
							|  |  |  | import { combineReducers } from 'berkeleys-redux-utils'; | 
					
						
							| 
									
										
										
										
											2016-01-27 11:34:44 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-04 16:46:19 -07:00
										 |  |  | import { createEpic } from 'redux-epic'; | 
					
						
							| 
									
										
										
										
											2017-11-09 17:10:30 -08:00
										 |  |  | import appReducer from './reducer.js'; | 
					
						
							|  |  |  | import routesMap from './routes-map.js'; | 
					
						
							| 
									
										
										
										
											2017-07-31 20:04:01 -07:00
										 |  |  | import epics from './epics'; | 
					
						
							| 
									
										
										
										
											2016-01-27 11:34:44 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | import servicesCreator from '../utils/services-creator'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-05 15:07:41 -08:00
										 |  |  | const debug = createDebugger('fcc:app:createApp'); | 
					
						
							| 
									
										
										
										
											2016-01-27 11:34:44 -08:00
										 |  |  | // createApp(settings: {
 | 
					
						
							|  |  |  | //   history?: History,
 | 
					
						
							| 
									
										
										
										
											2017-11-09 17:10:30 -08:00
										 |  |  | //   defaultState?: Object|Void,
 | 
					
						
							| 
									
										
										
										
											2016-01-27 11:34:44 -08:00
										 |  |  | //   serviceOptions?: Object,
 | 
					
						
							| 
									
										
										
										
											2018-01-17 10:51:44 -08:00
										 |  |  | //   middlewares?: [...Function],
 | 
					
						
							|  |  |  | //   enhancers?: [...Function],
 | 
					
						
							|  |  |  | //   epics?: [...Function],
 | 
					
						
							| 
									
										
										
										
											2016-01-27 11:34:44 -08:00
										 |  |  | // }) => Observable
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Either location or history must be defined
 | 
					
						
							|  |  |  | export default function createApp({ | 
					
						
							|  |  |  |   history, | 
					
						
							| 
									
										
										
										
											2017-11-09 17:10:30 -08:00
										 |  |  |   defaultState, | 
					
						
							| 
									
										
										
										
											2016-01-27 11:34:44 -08:00
										 |  |  |   serviceOptions = {}, | 
					
						
							|  |  |  |   middlewares: sideMiddlewares = [], | 
					
						
							|  |  |  |   enhancers: sideEnhancers = [], | 
					
						
							| 
									
										
										
										
											2017-07-31 20:04:01 -07:00
										 |  |  |   epics: sideEpics = [], | 
					
						
							|  |  |  |   epicOptions: sideEpicOptions = {} | 
					
						
							| 
									
										
										
										
											2016-01-27 11:34:44 -08:00
										 |  |  | }) { | 
					
						
							| 
									
										
										
										
											2017-07-31 20:04:01 -07:00
										 |  |  |   const epicOptions = { | 
					
						
							|  |  |  |     ...sideEpicOptions, | 
					
						
							| 
									
										
										
										
											2016-11-12 17:09:07 +00:00
										 |  |  |     services: servicesCreator(serviceOptions) | 
					
						
							| 
									
										
										
										
											2016-01-27 11:34:44 -08:00
										 |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-31 20:04:01 -07:00
										 |  |  |   const epicMiddleware = createEpic( | 
					
						
							|  |  |  |     epicOptions, | 
					
						
							|  |  |  |     ...epics, | 
					
						
							|  |  |  |     ...sideEpics | 
					
						
							| 
									
										
										
										
											2016-04-24 21:54:48 -07:00
										 |  |  |   ); | 
					
						
							| 
									
										
										
										
											2017-11-09 17:10:30 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   const { | 
					
						
							|  |  |  |     reducer: routesReducer, | 
					
						
							|  |  |  |     middleware: routesMiddleware, | 
					
						
							|  |  |  |     enhancer: routesEnhancer | 
					
						
							| 
									
										
										
										
											2018-05-15 06:12:05 +01:00
										 |  |  |   } = connectRoutes(history, routesMap); | 
					
						
							| 
									
										
										
										
											2017-11-09 17:10:30 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   routesReducer.toString = () => 'location'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-31 20:04:01 -07:00
										 |  |  |   const enhancer = compose( | 
					
						
							| 
									
										
										
										
											2017-11-09 17:10:30 -08:00
										 |  |  |     routesEnhancer, | 
					
						
							| 
									
										
										
										
											2016-01-27 11:34:44 -08:00
										 |  |  |     applyMiddleware( | 
					
						
							| 
									
										
										
										
											2017-11-09 17:10:30 -08:00
										 |  |  |       routesMiddleware, | 
					
						
							| 
									
										
										
										
											2017-07-31 20:04:01 -07:00
										 |  |  |       epicMiddleware, | 
					
						
							|  |  |  |       ...sideMiddlewares | 
					
						
							| 
									
										
										
										
											2016-01-27 11:34:44 -08:00
										 |  |  |     ), | 
					
						
							|  |  |  |     // enhancers must come after middlewares
 | 
					
						
							|  |  |  |     // on client side these are things like Redux DevTools
 | 
					
						
							|  |  |  |     ...sideEnhancers | 
					
						
							| 
									
										
										
										
											2017-07-31 20:04:01 -07:00
										 |  |  |   ); | 
					
						
							| 
									
										
										
										
											2017-11-09 17:10:30 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   const reducer = combineReducers( | 
					
						
							|  |  |  |     appReducer, | 
					
						
							|  |  |  |     routesReducer | 
					
						
							| 
									
										
										
										
											2017-07-31 20:04:01 -07:00
										 |  |  |   ); | 
					
						
							| 
									
										
										
										
											2016-01-27 11:34:44 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // create composed store enhancer
 | 
					
						
							|  |  |  |   // use store enhancer function to enhance `createStore` function
 | 
					
						
							| 
									
										
										
										
											2017-11-09 17:10:30 -08:00
										 |  |  |   // call enhanced createStore function with reducer and defaultState
 | 
					
						
							| 
									
										
										
										
											2016-01-27 11:34:44 -08:00
										 |  |  |   // to create store
 | 
					
						
							| 
									
										
										
										
											2017-11-09 17:10:30 -08:00
										 |  |  |   const store = createStore(reducer, defaultState, enhancer); | 
					
						
							|  |  |  |   const location = selectLocationState(store.getState()); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-05 15:07:41 -08:00
										 |  |  |   // note(berks): should get stripped in production client by webpack
 | 
					
						
							|  |  |  |   // We need to find a way to hoist to top level in production node env
 | 
					
						
							|  |  |  |   // babel plugin, maybe? After a quick search I couldn't find one
 | 
					
						
							|  |  |  |   if (process.env.NODE_ENV === 'development') { | 
					
						
							|  |  |  |     if (module.hot) { | 
					
						
							|  |  |  |       module.hot.accept('./reducer.js', () => { | 
					
						
							|  |  |  |         debug('hot reloading reducers'); | 
					
						
							|  |  |  |         store.replaceReducer(combineReducers( | 
					
						
							|  |  |  |           require('./reducer.js').default, | 
					
						
							|  |  |  |           routesReducer | 
					
						
							|  |  |  |         )); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2017-11-09 17:10:30 -08:00
										 |  |  |   // ({
 | 
					
						
							|  |  |  |   //   redirect,
 | 
					
						
							|  |  |  |   //   props,
 | 
					
						
							|  |  |  |   //   reducer,
 | 
					
						
							|  |  |  |   //   store,
 | 
					
						
							|  |  |  |   //   epic: epicMiddleware
 | 
					
						
							|  |  |  |   // }));
 | 
					
						
							|  |  |  |   return Observable.of({ | 
					
						
							|  |  |  |     store, | 
					
						
							|  |  |  |     epic: epicMiddleware, | 
					
						
							|  |  |  |     location, | 
					
						
							|  |  |  |     notFound: false | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2016-01-27 11:34:44 -08:00
										 |  |  | } |