-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
95 lines (77 loc) · 2.37 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
95
var { cst, ccContext, getState } = require('concent');
var { createStore, combineReducers } = require('redux');
var { FORCE_UPDATE, SET_STATE, SET_MODULE_STATE, INVOKE, SYNC } = cst;
var pluginName = 'reduxDevTool';
var toExport = module.exports = {};
var reduxStore = null;
function createReducer(module, initState) {
return function (state, action) {
if (state === undefined) state = initState;
if (action.module === module) {
var targetPayload = action.sharedState || action.payload;
return Object.assign(state, targetPayload);
} else {
return state;
}
};
}
function createReducers() {
var modules = Object.keys(ccContext.moduleName2stateKeys);
var reducers = {};
modules.forEach(function (m) {
reducers[m] = createReducer(m, getState(m));
});
return reducers;
}
function injectReduxDevTool() {
reduxStore = createStore(
combineReducers(createReducers()),
window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()
);
reduxStore.subscribe(function () {
if (actionLen === actionPrevLen + 1) {
//来自于concent的监听
actionPrevLen++;
} else {
// 来自于devtool点击jump 或者 skip
if (toolConf.log) console.warn('暂未打通concent与redux dev tool的jump、skip等功能');
}
});
}
var actionLen = 1;
var actionPrevLen = 1;
function dispatchAction(actionForRedux) {
if (reduxStore) {
actionLen++;
reduxStore.dispatch(actionForRedux);
}
}
function getActionType(calledBy, type) {
if ([FORCE_UPDATE, SET_STATE, SET_MODULE_STATE, INVOKE, SYNC].includes(calledBy)) {
return `ccApi/${calledBy}`;
} else {
return `dispatch/${type}`;
}
}
function getPayload(payload) {
const newPayload = Object.assign({}, payload);
newPayload.type = getActionType(payload.calledBy, payload.type);
return newPayload;
}
/** concent启动时会调用一次插件的install接口 */
toExport.install = function (on) {
injectReduxDevTool();
on(cst.SIG_STATE_CHANGED, function (data) {
dispatchAction(getPayload(data.payload));
});
on(cst.SIG_MODULE_CONFIGURED, function () {
reduxStore.replaceReducer(combineReducers(createReducers()));
});
return { name: pluginName }
}
var toolConf = { log: true };
toExport.setConf = function (conf) {
var _conf = conf || {};
if (_conf.log !== undefined) toolConf.log = _conf.log;
}
toExport.default = toExport;