-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstore.js
More file actions
69 lines (62 loc) · 2.06 KB
/
store.js
File metadata and controls
69 lines (62 loc) · 2.06 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
import { createStore, applyMiddleware, compose } from 'redux';
import createSagaMiddleware, { END } from 'redux-saga';
import reducer from '../Reducers';
export default class ConfigureStore {
constructor(
preloadState = {},
sagas = [],
) {
// to load initial sagas
this.promises = [];
const sagaMiddleware = createSagaMiddleware();
const middlewares = [
sagaMiddleware,
];
let composeEnhancers = compose;
// when the store will be instantiated in the server,
// you must be sure that the window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__
// will not crash the application
if (process.env.NODE_ENV === 'development' && typeof window !== 'undefined') {
composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || composeEnhancers;
}
const enhancer = applyMiddleware(...middlewares);
// create store
const store = createStore(
reducer,
preloadState,
composeEnhancers(enhancer),
);
// run the sagas sequentially and map it with the 'this.promises'
this.promises = (!(sagas instanceof Array) ? [sagas] : sagas)
.map(saga => sagaMiddleware.run(saga).done);
this.hmrReducers();
// create new store with redux store and the functions we want to expose
this = {...this, store,
{
runSaga: sagaMiddleware.run,
},
};
}
hmrReducers = () => {
if (module.hot && process.env.NODE_ENV === 'development') {
module.hot.accept('../Reducers', () => {
this.replaceReducer(require('../Reducers')); // eslint-disable-line global-require
});
}
}
// when receive SAGA INIT ACTION (redux action)
// we make sur that all actions are dispatched
// after that we stop the loop with ( END )
// and we resolve all promises
initActions = actions => new Promise((resolve, reject) => {
actions.forEach((action) => {
this.dispatch(action);
});
this.dispatch(END);
Promise.all(this.promises).then(() => {
resolve();
}).catch(() => {
reject();
});
});
}