-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.js
80 lines (68 loc) · 2.08 KB
/
store.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/**
* Create the store with asynchronously loaded reducers
*/
import { createStore, applyMiddleware, compose } from 'redux';
import { routerMiddleware } from 'react-router-redux';
import createSagaMiddleware from 'redux-saga';
import { createLogger } from "redux-logger";
import { persistState } from 'redux-devtools';
import createReducer from './reducers';
import DevTools from './containers/DevTools';
import { hasReduxDevToolExtension } from './internal/utils';
import rootSaga from './saga';
const sagaMiddleware = createSagaMiddleware();
export default function configureStore(initialState, history) {
// Create the store with two common middlewares
// 1. sagaMiddleware: Makes redux-sagas work
// 2. routerMiddleware: Syncs the location/URL path to the state
const middlewares = [
sagaMiddleware,
routerMiddleware(history),
];
// Add some debug middlewares
if (process.env.NODE_ENV !== "production") {
const loggerMiddleware = createLogger({
level: "info",
collapsed: true,
logErrors: true,
duration: true,
colors: {
title: () => "#3366ff",
prevState: () => "#75b8d4",
nextState: () => "#f6921e",
action: () => "#60bd16",
error: () => "#aa0000",
},
});
middlewares.unshift(
loggerMiddleware,
require("redux-immutable-state-invariant").default(),
);
}
const enhancers = [
applyMiddleware(...middlewares),
];
if (process.env.NODE_ENV !== "production") {
enhancers.push(
hasReduxDevToolExtension ? window.__REDUX_DEVTOOLS_EXTENSION__() : DevTools.instrument(),
persistState(
window.location.href.match(/[?&]debug_session=([^&#]+)\b/)
)
);
}
const store = createStore(
createReducer(),
initialState,
compose(...enhancers),
);
// Extensions
store.runSaga = sagaMiddleware.run(rootSaga);
store.asyncReducers = {};
// Make reducers hot reloadable
if (module.hot) {
module.hot.accept('./reducers', () => {
store.replaceReducer(createReducer(store.injectedReducers));
});
}
return store;
}