-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdynamicInject.js
90 lines (79 loc) · 2.55 KB
/
dynamicInject.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
import { createStore, applyMiddleware, compose } from 'redux';
import { createLogger } from 'redux-logger';
import createSagaMiddleware from 'redux-saga';
import { combineReducers } from 'redux-immutable';
import rootReducer from 'reducers';
// Redux console logger
const logger = createLogger({ collapsed: true });
// Redux DevTools Extension for Chrome and Firefox
/* eslint-disable no-underscore-dangle */
const reduxDevTool = () =>
typeof window === 'object' && typeof window.__REDUX_DEVTOOLS_EXTENSION__ !== 'undefined'
? window.__REDUX_DEVTOOLS_EXTENSION__()
: f => f;
export default function configureStore(initialState) {
const isDev = process.env.NODE_ENV === 'development';
const sagaMiddleware = createSagaMiddleware();
const initialReducer = combineReducers(rootReducer);
let middlewares = [sagaMiddleware];
let composedStoreEnhancer = null;
if (isDev) {
middlewares = applyMiddleware(...middlewares.concat([logger]));
composedStoreEnhancer = compose(
middlewares,
reduxDevTool()
);
} else {
middlewares = applyMiddleware(...middlewares);
}
const store = composedStoreEnhancer
? composedStoreEnhancer(createStore)(initialReducer, initialState)
: createStore(initialReducer, initialState, middlewares);
if (isDev && module.hot) {
module.hot.accept('../store', () => {
/* eslint-disable-next-line */
const nextRootReducer = require('../store/index');
store.replaceReducer(nextRootReducer);
});
}
// 动态注册 reducer
store.asyncReducers = rootReducer;
return {
store,
runSaga: sagaMiddleware.run
};
}
const configure = configureStore();
const { store, runSaga } = configure;
/**
* 使用 replaceReducer 方法动态注册 reducer
* @param {key} reducer key
* @param {reducer} reducer function
*/
const injectReducer = ({ key, reducer }) => {
const { asyncReducers } = store;
if (asyncReducers && asyncReducers.hasOwnProperty(key)) {
// tslint:disable-next-line:no-console
console.log(`${key} Already exist`);
return;
}
asyncReducers[key] = reducer;
store.replaceReducer(combineReducers(asyncReducers));
};
// 动态注册 saga
// 应避免重复注册,发一次action,导致发两次 request 的问题
function proxyRunSaga() {
const alreadyRun = {};
return (saga) => {
const prevTask = alreadyRun[saga];
if (!prevTask || !prevTask.isRunning()) {
alreadyRun[saga] = runSaga(saga);
}
console.log(saga.name, prevTask.isRunning(), '<<<<<isRunning');
}
}
export default {
store,
runSaga: proxyRunSaga(),
injectReducer
};