-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
95 lines (72 loc) · 2.55 KB
/
index.js
File metadata and controls
95 lines (72 loc) · 2.55 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import { createStore, applyMiddleware, compose, combineReducers } from 'redux'
import createSagaMiddleware from 'redux-saga'
import * as sagaEffects from 'redux-saga/effects'
import errorCatcher from '../helper-sentry'
import sagaHandleCallbacks from './saga.handleCallbacks'
function makeStoreConfigurer(params) {
if (process.env.NODE_ENV !== 'production') {
if (!params.reducers) {
throw new Error('reducers is required for makeStoreConfigurer')
}
if (!params.sagas) {
throw new Error('sagas is required for makeStoreConfigurer')
}
}
if (!params.initialState) {
params.initialState = {}
}
const composeEnhancers =
process.env.NODE_ENV === 'development'
? // eslint-disable-next-line no-underscore-dangle
window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose
: compose
const appReducer = combineReducers(params.reducers)
const shouldCaptureAction = action => !/^@@redux-form/.test(action.type)
const shouldCapturePayload = action =>
!/^Session/.test(action.type) && !/\/(SUCCESS|FAILURE)/i.test(action.type)
const rootReducer = (state, action) => {
const { type, ...actionData } = action
if (shouldCaptureAction(action)) {
errorCatcher.addBreadcrumb({
message: type,
data: shouldCapturePayload(action) ? actionData : {},
category: 'redux.action',
level: 'debug',
})
}
const enhancedAppReducer = params.reducerMiddleware
? params.reducerMiddleware(appReducer)
: appReducer
return enhancedAppReducer(state, action)
}
function* rootSaga(run) {
const task = yield sagaEffects.fork(function* () {
yield sagaEffects.all([
...params.sagas.map(saga => sagaEffects.call(saga)),
sagaEffects.call(sagaHandleCallbacks),
])
})
yield sagaEffects.take(RESTART.type)
yield sagaEffects.cancel(task)
run(rootSaga, run)
}
return (initialState = params.initialState) => {
const sagaMiddleware = createSagaMiddleware({
onError: (error, info) => {
errorCatcher.report(error, info)
console.error(error)
console.error(info.sagaStack)
},
})
const enhancer = composeEnhancers(applyMiddleware(sagaMiddleware))
const store = createStore(rootReducer, initialState, enhancer)
sagaMiddleware.run(rootSaga, sagaMiddleware.run)
return store
}
}
export { useDispatch, useSelector, Provider } from 'react-redux'
export const effects = sagaEffects
export const RESTART = {
type: '@@redux-saga/ROOT_RESTART',
}
export default makeStoreConfigurer