-
Notifications
You must be signed in to change notification settings - Fork 7
/
createStore.js
53 lines (45 loc) · 1.49 KB
/
createStore.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
require('rxjs') // require before redux-observable to ensure prototype methods added
const { createStore: Store, applyMiddleware } = require('redux')
const { createEpicMiddleware, combineEpics } = require('redux-observable')
const { routerReducer, routerMiddleware } = require('react-router-redux')
const { reducer: formReducer } = require('redux-form')
const { concat: concatUpdaters, updateStateAt } = require('redux-fp')
const { createLogger } = require('redux-logger')
const { composeWithDevTools } = require('redux-devtools-extension')
module.exports = createStore
function createStore (options) {
const {
state,
updater: appUpdater,
epic,
middlewares = [],
enhancers = [],
history,
client
} = options
const enhancer = composeWithDevTools(
applyMiddleware(...[
createEpicMiddleware(epic, { dependencies: { feathers: client } }),
routerMiddleware(history),
...middlewares,
createLogger()
]),
...enhancers
)
const routerUpdater = updateStateAt('router', reducerToUpdater(routerReducer))
const formUpdater = updateStateAt('form', reducerToUpdater(formReducer))
const updater = concatUpdaters(
appUpdater,
routerUpdater,
formUpdater
)
const reducer = updaterToReducer(updater)
const store = Store(reducer, state, enhancer)
return store
}
function reducerToUpdater (reducer) {
return action => state => reducer(state, action)
}
function updaterToReducer (updater) {
return (state, action) => updater(action)(state)
}