-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstore.ts
More file actions
61 lines (51 loc) · 1.88 KB
/
store.ts
File metadata and controls
61 lines (51 loc) · 1.88 KB
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
import { routerMiddleware } from 'connected-react-router';
import { compose, createStore, applyMiddleware } from 'redux';
import { createEpicMiddleware } from 'redux-observable';
import reduxThunk from 'redux-thunk';
import { BehaviorSubject } from 'rxjs';
import { switchMap } from 'rxjs/operators';
import { rootEpic } from './epics';
import { createRootReducer } from './reducers';
import { history } from './sbHistory';
const epicMiddleware = createEpicMiddleware();
const { BUILD_MODE } = process.env;
const configureStore = (initialState) => {
if (BUILD_MODE === 'production') {
const store = createStore(
createRootReducer(history),
initialState,
compose(applyMiddleware(reduxThunk, routerMiddleware(history), epicMiddleware)),
);
epicMiddleware.run(rootEpic);
return store;
}
// const reduxLogger = createLogger({
// diff: true,
// });
const store = createStore(
createRootReducer(history),
initialState,
compose(
applyMiddleware(reduxThunk, routerMiddleware(history), epicMiddleware),
window['__REDUX_DEVTOOLS_EXTENSION__'] ? window['__REDUX_DEVTOOLS_EXTENSION__']() : (f) => f,
),
);
const epic$ = new BehaviorSubject(rootEpic);
// Every time a new epic is given to epic$ it
// will unsubscribe from the previous one then
// call and subscribe to the new one because of
// how switchMap works
const hotReloadingEpic = (...args: any[]) => epic$.pipe(switchMap((epic) => epic.apply(this, args)));
epicMiddleware.run(hotReloadingEpic as any);
if ((module as any).hot) {
(module as any).hot.accept('./epics', () => {
const nextRootEpic = require('./epics').default;
epic$.next(nextRootEpic);
});
(module as any).hot.accept('./reducers', () => {
store.replaceReducer(require('./reducers').default);
});
}
return store;
};
export const initializeStore = configureStore({});