Skip to content

Commit 9215dd8

Browse files
committed
add display name for Connect
1 parent f8ca549 commit 9215dd8

File tree

1 file changed

+53
-45
lines changed

1 file changed

+53
-45
lines changed

lib/react-most.js

Lines changed: 53 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ const CONTEXT_TYPE = {
1616
}
1717

1818
export function connect(main, initprops={}) {
19-
return function(ReactClass){
20-
if (ReactClass.contextTypes === CONTEXT_TYPE) {
19+
return function(WrappedComponent){
20+
let connectDisplayName = `Connect(${getDisplayName(WrappedComponent)})`
21+
if (WrappedComponent.contextTypes === CONTEXT_TYPE) {
2122
class Connect extends React.PureComponent {
2223
constructor(props, context) {
2324
super(props, context);
@@ -26,54 +27,57 @@ export function connect(main, initprops={}) {
2627
this.actions = mergeAll([actions, props.actions])
2728
}
2829
render(){
29-
return <ReactClass {...this.props} {...initprops} sink$={this.sink$} actions={this.actions}/>
30+
return <WrappedComponent {...this.props} {...initprops} sink$={this.sink$} actions={this.actions}/>
3031
}
3132
}
32-
Connect.contextTypes = CONTEXT_TYPE
33-
return Connect
34-
}
35-
class Connect extends React.PureComponent {
36-
constructor(props, context) {
37-
super(props, context);
38-
if(initprops.history || props.history){
39-
initprops.history = initHistory(context[HISTORY_STREAM])
40-
initprops.history.travel.observe(state=>{
41-
return this.setState(state)
42-
})
43-
}
44-
45-
let [actions, sink$] = actionsAndSinks(main(context[INTENT_STREAM],props), this)
46-
this.sink$ = sink$.concat(props.sink$||[])
47-
this.actions = mergeAll([actions, props.actions])
48-
let defaultKey = keys(ReactClass.defaultProps)
49-
this.state = mergeAll([ReactClass.defaultProps, pick(defaultKey, props)])
50-
}
51-
componentWillReceiveProps(nextProps){
52-
this.setState(state=>pick(keys(state), nextProps))
53-
}
54-
componentDidMount(){
55-
this.context[EACH_FLATMAP](this.sink$, (action)=>{
56-
if(action instanceof Function) {
57-
this.setState((prevState, props)=>{
58-
let newState = action.call(this, prevState,props);
59-
if(initprops.history && newState != prevState){
60-
initprops.history.cursor = -1;
61-
this.context[HISTORY_STREAM].send(prevState);
62-
}
63-
return newState;
64-
});
65-
} else {
66-
/* istanbul ignore next */
67-
console.warn('action', action,'need to be a Function which map from current state to new state');
33+
Connect.contextTypes = CONTEXT_TYPE;
34+
Connect.displayName = connectDisplayName;
35+
return Connect;
36+
}else{
37+
class Connect extends React.PureComponent {
38+
constructor(props, context) {
39+
super(props, context);
40+
if(initprops.history || props.history){
41+
initprops.history = initHistory(context[HISTORY_STREAM])
42+
initprops.history.travel.observe(state=>{
43+
return this.setState(state)
44+
})
6845
}
69-
});
70-
}
71-
render() {
72-
return <ReactClass {...this.props} {...this.state} {...initprops} actions={this.actions} />
46+
47+
let [actions, sink$] = actionsAndSinks(main(context[INTENT_STREAM],props), this)
48+
this.sink$ = sink$.concat(props.sink$||[])
49+
this.actions = mergeAll([actions, props.actions])
50+
let defaultKey = keys(WrappedComponent.defaultProps)
51+
this.state = mergeAll([WrappedComponent.defaultProps, pick(defaultKey, props)])
52+
}
53+
componentWillReceiveProps(nextProps){
54+
this.setState(state=>pick(keys(state), nextProps))
55+
}
56+
componentDidMount(){
57+
this.context[EACH_FLATMAP](this.sink$, (action)=>{
58+
if(action instanceof Function) {
59+
this.setState((prevState, props)=>{
60+
let newState = action.call(this, prevState,props);
61+
if(initprops.history && newState != prevState){
62+
initprops.history.cursor = -1;
63+
this.context[HISTORY_STREAM].send(prevState);
64+
}
65+
return newState;
66+
});
67+
} else {
68+
/* istanbul ignore next */
69+
console.warn('action', action,'need to be a Function which map from current state to new state');
70+
}
71+
});
72+
}
73+
render() {
74+
return <WrappedComponent {...this.props} {...this.state} {...initprops} actions={this.actions} />
75+
}
7376
}
77+
Connect.contextTypes = CONTEXT_TYPE;
78+
Connect.displayName = connectDisplayName;
79+
return Connect;
7480
}
75-
Connect.contextTypes = CONTEXT_TYPE;
76-
return Connect;
7781
}
7882
}
7983

@@ -137,3 +141,7 @@ function actionsAndSinks(sinks, self){
137141
}
138142
return [_actions, _sinks]
139143
}
144+
145+
function getDisplayName(WrappedComponent) {
146+
return WrappedComponent.displayName || WrappedComponent.name || 'Component'
147+
}

0 commit comments

Comments
 (0)