82 lines
1.8 KiB
JavaScript
82 lines
1.8 KiB
JavaScript
|
import {
|
||
|
createStore,
|
||
|
applyMiddleware,
|
||
|
combineReducers,
|
||
|
compose,
|
||
|
} from 'redux';
|
||
|
import createSagaMiddleware from 'redux-saga';
|
||
|
import {
|
||
|
all,
|
||
|
fork,
|
||
|
} 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,
|
||
|
});
|
||
|
|
||
|
console.debug('all redux modules', reduxModules);
|
||
|
|
||
|
const reducers =
|
||
|
reduxModules
|
||
|
.reduce((current, [name, m]) => {
|
||
|
if (!m.reducer) { return current; }
|
||
|
return {
|
||
|
...current,
|
||
|
[name]: m.reducer,
|
||
|
};
|
||
|
}, {});
|
||
|
console.debug('combined reducers:', reducers);
|
||
|
|
||
|
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() {
|
||
|
const sagas = Object.values(reduxModules)
|
||
|
.reduce((current, [name, m]) => {
|
||
|
if (!m.sagas) {
|
||
|
console.debug(`Module ${name} does not have any sagas`);
|
||
|
return current;
|
||
|
}
|
||
|
return [
|
||
|
...current,
|
||
|
...m.sagas,
|
||
|
];
|
||
|
}, []);
|
||
|
console.debug('Will run these sagas from root:', sagas);
|
||
|
yield all(sagas);
|
||
|
console.debug('Root saga done');
|
||
|
}
|
||
|
const sagaMiddleware = createSagaMiddleware();
|
||
|
|
||
|
const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
|
||
|
export default createStore(
|
||
|
reducer,
|
||
|
// initialState,
|
||
|
composeEnhancers(
|
||
|
applyMiddleware(
|
||
|
sagaMiddleware,
|
||
|
),
|
||
|
),
|
||
|
);
|
||
|
|
||
|
sagaMiddleware.run(rootSaga);
|