-
Notifications
You must be signed in to change notification settings - Fork 9
/
configureStore.js
65 lines (55 loc) 路 1.92 KB
/
configureStore.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
55
56
57
58
59
60
61
62
63
64
65
/* global window */
/* eslint no-underscore-dangle: 0 */
import { createStore, applyMiddleware, compose } from 'redux';
import { run } from '@cycle/rxjs-run';
import { Observable } from 'rxjs/Observable';
import { createCycleMiddleware } from 'redux-cycles';
import { makeHTTPDriver } from '@cycle/http';
import { timeDriver } from '@cycle/time/rxjs';
import storageDriver from '@cycle/storage';
import { setAdapt } from '@cycle/run/lib/adapt';
import { routerMiddleware } from 'react-router-redux';
import { reducer, cycle as main } from '../modules';
// TODO: rxjs dependency break the adapter
// https://github.com/cyclejs/cyclejs/blob/master/rxjs-run/package.json#L35
setAdapt(stream => Observable.from(stream));
/**
* Compose with Redux devtool
* Ref: https://github.com/zalmoxisus/redux-devtools-extension/blob/master/npm-package/developmentOnly.js
*/
const composeEnhancers =
process.env.NODE_ENV !== 'production' &&
typeof window === 'object' &&
window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__
? window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__
: compose;
const configureStore = (initialState, history) => {
const cycleMiddleware = createCycleMiddleware();
const { makeActionDriver, makeStateDriver } = cycleMiddleware;
const middlewares = [cycleMiddleware, routerMiddleware(history)];
/**
* Prevents state from being mutated
* Ref: https://github.com/buunguyen/redux-freeze#usage
*/
if (process.env.NODE_ENV !== 'production') {
middlewares.push(require('redux-freeze')); // eslint-disable-line global-require
}
const store = createStore(
reducer,
initialState,
composeEnhancers(applyMiddleware(...middlewares)),
);
/**
* redux-cycles
* Remind: MUST create sotre first.
*/
run(main, {
ACTION: makeActionDriver(),
STATE: makeStateDriver(),
Time: timeDriver,
HTTP: makeHTTPDriver(),
STORAGE: storageDriver,
});
return store;
};
export default configureStore;