-
Notifications
You must be signed in to change notification settings - Fork 2
/
CreateStore.ts
67 lines (53 loc) · 1.88 KB
/
CreateStore.ts
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
import { createReactNavigationReduxMiddleware } from 'react-navigation-redux-helpers'
import {
AnyAction,
applyMiddleware,
compose,
createStore,
Middleware,
Reducer,
StoreEnhancer
} from 'redux'
import createSagaMiddleware from 'redux-saga'
import ScreenTracking from './ScreenTrackingMiddleware'
import Config from 'appSrc/Config/DebugConfig'
import ReduxPersist from 'appSrc/Config/ReduxPersist'
import Rehydration from 'appSrc/Services/Rehydration'
import StoreState from 'appSrc/Types/StoreState'
// creates the store
export default (rootReducer: Reducer<StoreState, AnyAction>, rootSaga) => {
/* ------------- Redux Configuration ------------- */
const middleware: Middleware[] = []
const enhancers: StoreEnhancer[] = []
/* ------------- Navigation Middleware ------------ */
const navigationMiddleware = createReactNavigationReduxMiddleware(
(state: StoreState) => state.nav
)
middleware.push(navigationMiddleware)
/* ------------- Analytics Middleware ------------- */
middleware.push(ScreenTracking)
/* ------------- Saga Middleware ------------- */
const sagaMonitor = Config.useReactotron
? console.tron.createSagaMonitor()
: null
const sagaMiddleware = createSagaMiddleware({ sagaMonitor })
middleware.push(sagaMiddleware)
/* ------------- Assemble Middleware ------------- */
enhancers.push(applyMiddleware(...middleware))
if (Config.useReactotron) {
enhancers.push(console.tron.createEnhancer())
}
// if Reactotron is enabled (default for __DEV__), we'll create the store through Reactotron
const store = createStore(rootReducer, compose(...enhancers))
// configure persistStore and check reducer version number
if (ReduxPersist.active) {
Rehydration.updateReducers(store)
}
// kick off root saga
const sagasManager: any = sagaMiddleware.run(rootSaga)
return {
store,
sagasManager,
sagaMiddleware
}
}