-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
125 lines (107 loc) · 3.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import * as React from "react";
import { connect } from "react-redux";
import { createAction, Action } from "redux-actions";
import { Map } from "immutable";
const hoistStatics = require("hoist-non-react-statics");
let idCounter = 0;
function genKey(name) {
let key = (++idCounter).toString(36);
if (__DEV__) {
return `${name}-${key}`;
}
return key;
}
const widgetInstanceType = {};
function mapStateToProps(state, { widgetKey }) {
return { widgetState: state.widgetState.get(widgetKey) };
}
function mapStateToPropsWithSelector(selector) {
return (state, { widgetKey }) => {
return {
widgetState: state.widgetState.get(widgetKey),
...selector(state)
};
};
}
function mapDispatchToProps(actions) {
return function(dispatch, { widgetKey }) {
const ret = { dispatch };
for (const key of Object.keys(actions)) {
const actionCreator = actions[key];
ret[key] = (...args) =>
dispatch({ ...actionCreator(...args), widgetKey });
}
return ret;
};
}
function noop(state) {
return state;
}
export const INIT = "@@Widget/INIT";
export const init = createAction(INIT);
export const UNLOAD = "@@Widget/UNLOAD";
export const unload = createAction(UNLOAD);
// 用来包装一个widget,替代connect,并提供widgetKey和widgetState属性。
// actions 可以包含local actions或全局actions,但发出的action都会有widgetKey字段。
export function widget(reducer = noop, actions = {}, selector) {
return function(Comp) {
const name = Comp.displayName || Comp.name;
if (!name) {
throw new Error("Component must have a name");
}
const ConnectedComp = connect(
selector ? mapStateToPropsWithSelector(selector) : mapStateToProps,
actions && mapDispatchToProps(actions)
)(Comp);
const Wrapped = class WrappedWidget extends React.Component {
widgetKey = genKey(name);
constructor(props) {
super(props);
widgetInstanceType[this.widgetKey] = reducer;
const { dispatch } = this.props;
if (dispatch) {
dispatch({ ...init(), widgetKey: this.widgetKey });
}
}
componentWillUnmount() {
const { dispatch } = this.props;
if (dispatch) {
dispatch({ ...unload(), widgetKey: this.widgetKey });
}
delete widgetInstanceType[this.widgetKey];
}
render() {
const { dispatch, ...others } = this.props;
return <ConnectedComp {...others} widgetKey={this.widgetKey} />;
}
};
const ret = connect()(Wrapped);
hoistStatics(ret, Comp);
return ret;
};
}
// 核心reducer,用来清空移除的widget或分发事件到对应的reducer
export default function reducer(state, action) {
const { widgetKey } = action;
state = state || Map();
if (widgetKey) {
if (widgetKey === "*") {
for (const key of state.keySeq().toArray()) {
const widgetState = state.get(key);
const reducer = widgetInstanceType[key];
const nextState = reducer(widgetState, action);
state = state.set(key, nextState);
}
} else {
const widgetState = state.get(widgetKey);
const reducer = widgetInstanceType[widgetKey];
const nextState = reducer(widgetState, action);
if (action.type === UNLOAD) {
state = state.delete(widgetKey);
} else {
state = state.set(widgetKey, nextState);
}
}
}
return state;
}