2020-07-06 21:59:56 +05:30
|
|
|
|
import { createStore, applyMiddleware, compose, StoreEnhancer } from 'redux';
|
2016-06-18 20:30:45 +05:30
|
|
|
|
// 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
|
|
|
|
|
2020-07-22 15:31:12 +05:30
|
|
|
|
import reducers from 'app/reducers';
|
|
|
|
|
import { Store } from 'app/types';
|
2016-06-18 20:30:45 +05:30
|
|
|
|
|
2020-07-06 21:59:56 +05:30
|
|
|
|
export default function storeFactory(preloadedState = {}): Store {
|
2020-05-24 04:38:24 +05:30
|
|
|
|
const middlewares = applyMiddleware(thunk);
|
|
|
|
|
const persistStateEnhancer = persistState(['accounts', 'user'], {
|
|
|
|
|
key: 'redux-storage',
|
|
|
|
|
});
|
2019-11-27 14:33:32 +05:30
|
|
|
|
|
2020-07-06 21:59:56 +05:30
|
|
|
|
let enhancer: StoreEnhancer;
|
2016-06-18 20:30:45 +05:30
|
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
|
if (process.env.NODE_ENV === 'production') {
|
|
|
|
|
enhancer = compose(middlewares, persistStateEnhancer);
|
|
|
|
|
} else {
|
|
|
|
|
const composeEnhancers = (window as any).__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
|
|
|
|
|
enhancer = composeEnhancers(middlewares, persistStateEnhancer);
|
|
|
|
|
}
|
2016-06-18 20:30:45 +05:30
|
|
|
|
|
2020-07-06 21:59:56 +05:30
|
|
|
|
const store = createStore(reducers, preloadedState, enhancer) as Store;
|
2016-06-18 20:30:45 +05:30
|
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
|
// Hot reload reducers
|
|
|
|
|
if (module.hot && typeof module.hot.accept === 'function') {
|
2020-10-11 23:49:12 +05:30
|
|
|
|
module.hot.accept('app/reducers', () =>
|
|
|
|
|
store.replaceReducer(
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
|
|
|
require('app/reducers').default,
|
|
|
|
|
),
|
|
|
|
|
);
|
2020-05-24 04:38:24 +05:30
|
|
|
|
}
|
2016-06-18 20:30:45 +05:30
|
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
|
return store;
|
2016-06-18 20:30:45 +05:30
|
|
|
|
}
|