2016-06-18 20:30:45 +05:30
|
|
|
|
import { createStore, applyMiddleware, compose } from 'redux';
|
|
|
|
|
// midleware, который позволяет возвращать из экшенов функции
|
|
|
|
|
// это полезно для работы с асинхронными действиями,
|
|
|
|
|
// а также дает возможность проверить какие-либо условия перед запуском экшена
|
|
|
|
|
// или даже вообще его не запускать в зависимости от условий
|
|
|
|
|
import thunk from 'redux-thunk';
|
2016-10-31 12:21:38 +05:30
|
|
|
|
import persistState from 'redux-localstorage';
|
2016-06-18 20:30:45 +05:30
|
|
|
|
|
2019-12-09 12:37:07 +05:30
|
|
|
|
import reducers, { Store } from 'app/reducers';
|
2016-06-18 20:30:45 +05:30
|
|
|
|
|
2019-12-09 12:37:07 +05:30
|
|
|
|
export default function storeFactory(): Store {
|
2019-11-27 14:33:32 +05:30
|
|
|
|
const middlewares = applyMiddleware(thunk);
|
|
|
|
|
const persistStateEnhancer = persistState(['accounts', 'user'], {
|
|
|
|
|
key: 'redux-storage',
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
let enhancer;
|
2016-06-18 20:30:45 +05:30
|
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
|
if (process.env.NODE_ENV === 'production') {
|
|
|
|
|
enhancer = compose(middlewares, persistStateEnhancer);
|
|
|
|
|
} else {
|
|
|
|
|
const composeEnhancers =
|
2019-12-09 12:37:07 +05:30
|
|
|
|
(window as any).__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
|
2019-11-27 14:33:32 +05:30
|
|
|
|
enhancer = composeEnhancers(middlewares, persistStateEnhancer);
|
|
|
|
|
}
|
2016-06-18 20:30:45 +05:30
|
|
|
|
|
2019-12-09 12:37:07 +05:30
|
|
|
|
const store = createStore(reducers, {}, enhancer) as Store;
|
2016-06-18 20:30:45 +05:30
|
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
|
// Hot reload reducers
|
|
|
|
|
if (module.hot && typeof module.hot.accept === 'function') {
|
2019-12-08 00:32:00 +05:30
|
|
|
|
module.hot.accept('app/reducers', () =>
|
|
|
|
|
store.replaceReducer(require('app/reducers').default),
|
2019-11-27 14:33:32 +05:30
|
|
|
|
);
|
|
|
|
|
}
|
2016-06-18 20:30:45 +05:30
|
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
|
return store;
|
2016-06-18 20:30:45 +05:30
|
|
|
|
}
|