2018-04-24 10:13:48 +00:00
|
|
|
import {
|
|
|
|
createStore,
|
|
|
|
applyMiddleware,
|
|
|
|
combineReducers,
|
|
|
|
compose,
|
|
|
|
} from 'redux';
|
|
|
|
import createSagaMiddleware from 'redux-saga';
|
|
|
|
import {
|
|
|
|
all,
|
2018-04-24 15:11:09 +00:00
|
|
|
// fork,
|
2018-04-24 10:13:48 +00:00
|
|
|
} from 'redux-saga/effects';
|
|
|
|
|
|
|
|
import * as serverlist from './serverlist';
|
|
|
|
import * as serverlistLoader from './serverlist_loader';
|
|
|
|
import * as ui from './ui';
|
|
|
|
import * as mastersync from './mastersync';
|
|
|
|
|
|
|
|
const reduxModules = Object.entries({
|
|
|
|
serverlist,
|
|
|
|
serverlistLoader,
|
|
|
|
ui,
|
|
|
|
mastersync,
|
|
|
|
});
|
|
|
|
|
2018-04-24 15:11:09 +00:00
|
|
|
// console.debug('all redux modules', reduxModules);
|
2018-04-24 10:13:48 +00:00
|
|
|
|
|
|
|
const reducers =
|
|
|
|
reduxModules
|
|
|
|
.reduce((current, [name, m]) => {
|
|
|
|
if (!m.reducer) { return current; }
|
|
|
|
return {
|
|
|
|
...current,
|
|
|
|
[name]: m.reducer,
|
|
|
|
};
|
|
|
|
}, {});
|
2018-04-24 15:11:09 +00:00
|
|
|
// console.debug('combined reducers:', reducers);
|
2018-04-24 10:13:48 +00:00
|
|
|
|
|
|
|
const reducer = combineReducers(reducers);
|
|
|
|
|
|
|
|
// const initialState =
|
|
|
|
// reduxModules
|
|
|
|
// .reduce((current, [name, m]) => {
|
|
|
|
// if (!m.initialState) { return current; }
|
|
|
|
// return {
|
|
|
|
// ...current,
|
|
|
|
// [name]: m.initialState,
|
|
|
|
// };
|
|
|
|
// }, {});
|
|
|
|
// console.debug('combined initial state:', initialState);
|
|
|
|
|
|
|
|
// create the saga middleware
|
|
|
|
function* rootSaga() {
|
2018-04-24 15:11:09 +00:00
|
|
|
const sagas = Object.values(reduxModules) // TODO - !?
|
|
|
|
.reduce((current, [, m]) => {
|
2018-04-24 10:13:48 +00:00
|
|
|
if (!m.sagas) {
|
2018-04-24 15:11:09 +00:00
|
|
|
// console.debug(`Module ${name} does not have any sagas`);
|
2018-04-24 10:13:48 +00:00
|
|
|
return current;
|
|
|
|
}
|
|
|
|
return [
|
|
|
|
...current,
|
|
|
|
...m.sagas,
|
|
|
|
];
|
|
|
|
}, []);
|
2018-04-24 15:11:09 +00:00
|
|
|
// console.debug('Will run these sagas from root:', sagas);
|
2018-04-24 10:13:48 +00:00
|
|
|
yield all(sagas);
|
2018-04-24 15:11:09 +00:00
|
|
|
// console.debug('Root saga done');
|
2018-04-24 10:13:48 +00:00
|
|
|
}
|
|
|
|
const sagaMiddleware = createSagaMiddleware();
|
|
|
|
|
|
|
|
const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
|
|
|
|
export default createStore(
|
|
|
|
reducer,
|
|
|
|
// initialState,
|
|
|
|
composeEnhancers(
|
|
|
|
applyMiddleware(
|
|
|
|
sagaMiddleware,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
sagaMiddleware.run(rootSaga);
|