This repository was archived by the owner on Dec 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathProviderService.ts
44 lines (34 loc) · 1.47 KB
/
ProviderService.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
import {createStore, applyMiddleware} from 'redux';
import rootReducer from '../stores/rootReducer';
import {composeWithDevTools} from 'redux-devtools-extension/developmentOnly';
import createSagaMiddleware, {END, SagaMiddleware} from 'redux-saga';
import rootSaga from '../stores/rootSaga';
import IStore from '../interfaces/stores/IStore';
import ISagaStore from '../interfaces/stores/ISagaStore';
class ProviderService {
public static createProviderStore(initialState: any = {}, isServerSide: boolean = false): ISagaStore<IStore> {
const sagaMiddleware: SagaMiddleware<any> = createSagaMiddleware();
const store = createStore(
rootReducer,
initialState,
composeWithDevTools(applyMiddleware(sagaMiddleware)),
) as ISagaStore<IStore>;
if (isServerSide) {
store.runSaga = sagaMiddleware.run;
store.endSaga = () => store.dispatch(END);
} else {
sagaMiddleware.run(rootSaga as any);
}
ProviderService._setupHotReloading(store);
return store;
}
private static _setupHotReloading(store: ISagaStore<IStore>) {
if ((module as any).hot) {
(module as any).hot.accept('../store/rootReducer', () => {
const nextReducer = require('../store/rootReducer').default; // eslint-disable-line global-require
store.replaceReducer(nextReducer);
});
}
}
}
export default ProviderService;