/
store.js
54 lines (45 loc) · 1.71 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
import { createStore, compose, applyMiddleware } from 'redux';
import { combinedReducer } from 'combinedReducer';
import offline from 'offline';
import thunk from 'redux-thunk';
import createSagaMiddleware from 'redux-saga';
import Reactotron, { trackGlobalErrors, networking, openInEditor, asyncStorage } from 'reactotron-react-native'; // eslint-disable-line
import sagaPlugin from 'reactotron-redux-saga';// eslint-disable-line
import { reactotronRedux } from 'reactotron-redux'; // eslint-disable-line
import { rootSaga } from 'sagas';
if (__DEV__) {
Reactotron.configure()
.use(reactotronRedux())
.use(sagaPlugin())
.use(trackGlobalErrors())
.use(networking())
.use(openInEditor())
.use(asyncStorage())
.connect()
.clear();
window.tron = Reactotron; // eslint-disable-line
}
const sagaMonitor = __DEV__ && Reactotron.createSagaMonitor();
const sagaMiddleware = createSagaMiddleware({ sagaMonitor });
const authMiddleware = ({ getState }) => next => action =>
action && action.type && action.type.endsWith('REQUEST')
? next({ ...action, auth: getState().user.token })
: next(action);
const middlewareList = [thunk, authMiddleware, sagaMiddleware];
function createAppStore(startApp) {
let storeCreator = createStore;
const { middleware: offlineMiddleware, enhanceReducer, enhanceStore } = offline({ persistCallback: startApp });
const middleware = applyMiddleware(...middlewareList, offlineMiddleware);
if (__DEV__) {
storeCreator = Reactotron.createStore;
}
return storeCreator(
enhanceReducer(combinedReducer),
compose(
enhanceStore,
middleware
)
);
}
createAppStore.runSagas = () => sagaMiddleware.run(rootSaga);
export default createAppStore;