-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
94 lines (76 loc) · 2.38 KB
/
index.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
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
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import registerServiceWorker from './registerServiceWorker';
import { createStore, compose, applyMiddleware, combineReducers } from 'redux';
import { Provider } from 'react-redux';
import createSagaMiddleware from 'redux-saga';
import { put, call, cancelled, fork } from 'redux-saga/effects';
import { createBrowserHistory as createHistory } from 'history';
import {
reducer as routerReducer,
saga as routerSaga,
buildRoutesMap,
route,
navigate,
} from 'redux-saga-first-router';
const history = createHistory();
function* navigateASaga(...args) {
yield call(() => console.log('Navigated to route A'));
yield fork(navigateASubSaga, ...args);
}
function* navigateASubSaga() {
yield call(() => console.log('Enter route A sub-saga'));
try {
yield call(() => new Promise(() => {})); // halt here
} finally {
if (yield cancelled()) {
yield call(() => console.log('Cancelled route A sub-saga'));
}
}
}
function* navigateBSaga(...args) {
yield call(() => console.log('Navigated to route B', ...args));
}
function* navigateCSaga(...args) {
yield call(() => console.log('Navigated to route C', ...args));
}
function* queryCSaga(...args) {
yield call(() => console.log('Query changed in route C', ...args));
yield fork(queryCSubSaga, ...args);
}
function* queryCSubSaga(params, query) {
yield call(() => console.log('Enter route C query sub-saga', query));
try {
yield call(() => new Promise(() => {})); // halt here
} finally {
if (yield cancelled()) {
yield call(() => console.log('Cancelled route C query sub-saga', query));
}
}
}
const routesMap = buildRoutesMap(
route('A', '/a', navigateASaga),
route('B', '/b/:timeStamp', navigateBSaga),
route('C', '/c/:opt?', navigateCSaga, queryCSaga),
route('_ROOT_', '/', function* navigateRoot() {
yield put(navigate('A'));
})
);
const reducer = combineReducers({
routing: routerReducer,
});
const composeEnhancers =
(process.env.NODE_ENV === 'development' && window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__) ||
compose;
const sagaMiddleware = createSagaMiddleware();
const store = createStore(reducer, composeEnhancers(applyMiddleware(sagaMiddleware)));
sagaMiddleware.run(routerSaga, routesMap, history);
ReactDOM.render(
<Provider store={store}>
<App />
</Provider>,
document.getElementById('root')
);
registerServiceWorker();