2016-01-27 19:34:44 +00:00
|
|
|
import { Observable } from 'rx';
|
|
|
|
import { match } from 'react-router';
|
|
|
|
import { compose, createStore, applyMiddleware } from 'redux';
|
|
|
|
|
2016-05-04 23:46:19 +00:00
|
|
|
import { createEpic } from 'redux-epic';
|
2016-01-27 19:34:44 +00:00
|
|
|
import createReducer from './create-reducer';
|
2017-08-01 03:04:01 +00:00
|
|
|
import createRoutes from './create-routes.js';
|
|
|
|
import createPanesMap from './create-panes-map.js';
|
|
|
|
import createPanesAspects from './Panes/redux';
|
|
|
|
import epics from './epics';
|
2016-01-27 19:34:44 +00:00
|
|
|
|
|
|
|
import servicesCreator from '../utils/services-creator';
|
|
|
|
|
|
|
|
const createRouteProps = Observable.fromNodeCallback(match);
|
|
|
|
|
|
|
|
//
|
|
|
|
// createApp(settings: {
|
2016-03-03 03:45:54 +00:00
|
|
|
// location?: Location|String,
|
2016-01-27 19:34:44 +00:00
|
|
|
// history?: History,
|
2016-06-14 16:31:41 +00:00
|
|
|
// syncHistoryWithStore?: ((history, store) => history) = (x) => x,
|
2016-01-27 19:34:44 +00:00
|
|
|
// initialState?: Object|Void,
|
|
|
|
// serviceOptions?: Object,
|
|
|
|
// middlewares?: Function[],
|
|
|
|
// sideReducers?: Object
|
|
|
|
// enhancers?: Function[],
|
2017-08-01 03:04:01 +00:00
|
|
|
// epics?: Function[],
|
2016-01-27 19:34:44 +00:00
|
|
|
// }) => Observable
|
|
|
|
//
|
|
|
|
// Either location or history must be defined
|
|
|
|
export default function createApp({
|
|
|
|
location,
|
|
|
|
history,
|
2016-06-14 16:31:41 +00:00
|
|
|
syncHistoryWithStore = (x) => x,
|
|
|
|
syncOptions = {},
|
2016-01-27 19:34:44 +00:00
|
|
|
initialState,
|
|
|
|
serviceOptions = {},
|
|
|
|
middlewares: sideMiddlewares = [],
|
|
|
|
enhancers: sideEnhancers = [],
|
|
|
|
reducers: sideReducers = {},
|
2017-08-01 03:04:01 +00:00
|
|
|
epics: sideEpics = [],
|
|
|
|
epicOptions: sideEpicOptions = {}
|
2016-01-27 19:34:44 +00:00
|
|
|
}) {
|
2017-08-01 03:04:01 +00:00
|
|
|
const epicOptions = {
|
|
|
|
...sideEpicOptions,
|
2016-11-12 17:09:07 +00:00
|
|
|
services: servicesCreator(serviceOptions)
|
2016-01-27 19:34:44 +00:00
|
|
|
};
|
|
|
|
|
2017-08-01 03:04:01 +00:00
|
|
|
const epicMiddleware = createEpic(
|
|
|
|
epicOptions,
|
|
|
|
...epics,
|
|
|
|
...sideEpics
|
2016-04-25 04:54:48 +00:00
|
|
|
);
|
2017-08-01 03:04:01 +00:00
|
|
|
const {
|
|
|
|
reducer: panesReducer,
|
|
|
|
middleware: panesMiddleware
|
|
|
|
} = createPanesAspects(createPanesMap());
|
|
|
|
const enhancer = compose(
|
2016-01-27 19:34:44 +00:00
|
|
|
applyMiddleware(
|
2017-08-01 03:04:01 +00:00
|
|
|
panesMiddleware,
|
|
|
|
epicMiddleware,
|
|
|
|
...sideMiddlewares
|
2016-01-27 19:34:44 +00:00
|
|
|
),
|
|
|
|
// enhancers must come after middlewares
|
|
|
|
// on client side these are things like Redux DevTools
|
|
|
|
...sideEnhancers
|
2017-08-01 03:04:01 +00:00
|
|
|
);
|
|
|
|
const reducer = createReducer(
|
|
|
|
{
|
|
|
|
[panesReducer]: panesReducer,
|
|
|
|
...sideReducers
|
|
|
|
},
|
|
|
|
);
|
2016-01-27 19:34:44 +00:00
|
|
|
|
|
|
|
// create composed store enhancer
|
|
|
|
// use store enhancer function to enhance `createStore` function
|
|
|
|
// call enhanced createStore function with reducer and initialState
|
|
|
|
// to create store
|
2017-08-01 03:04:01 +00:00
|
|
|
const store = createStore(reducer, initialState, enhancer);
|
2016-06-14 16:31:41 +00:00
|
|
|
// sync history client side with store.
|
|
|
|
// server side this is an identity function and history is undefined
|
|
|
|
history = syncHistoryWithStore(history, store, syncOptions);
|
2017-08-01 03:04:01 +00:00
|
|
|
const routes = createRoutes(store);
|
2016-01-27 19:34:44 +00:00
|
|
|
// createRouteProps({
|
2016-03-03 03:45:54 +00:00
|
|
|
// redirect: LocationDescriptor,
|
2016-01-27 19:34:44 +00:00
|
|
|
// history: History,
|
|
|
|
// routes: Object
|
|
|
|
// }) => Observable
|
|
|
|
return createRouteProps({ routes, location, history })
|
2016-03-03 03:45:54 +00:00
|
|
|
.map(([ redirect, props ]) => ({
|
|
|
|
redirect,
|
2016-01-27 19:34:44 +00:00
|
|
|
props,
|
|
|
|
reducer,
|
2016-04-25 04:54:48 +00:00
|
|
|
store,
|
2017-08-01 03:04:01 +00:00
|
|
|
epic: epicMiddleware
|
2016-01-27 19:34:44 +00:00
|
|
|
}));
|
|
|
|
}
|