|
| 1 | +import createStoreShape from '../utils/createStoreShape'; |
| 2 | +import getDisplayName from '../utils/getDisplayName'; |
| 3 | +import shallowEqual from '../utils/shallowEqual'; |
| 4 | +import isPlainObject from '../utils/isPlainObject'; |
| 5 | +import wrapActionCreators from '../utils/wrapActionCreators'; |
| 6 | +import invariant from 'invariant'; |
| 7 | + |
| 8 | +const emptySelector = () => ({}); |
| 9 | + |
| 10 | +const emptyBinder = () => ({}); |
| 11 | + |
| 12 | +const identityMerge = (slice, actionsCreators, props) => ({...slice, ...actionsCreators, ...props}); |
| 13 | + |
| 14 | + |
| 15 | +export default function createConnectWrapper(React) { |
| 16 | + const { Component, PropTypes } = React; |
| 17 | + const storeShape = createStoreShape(PropTypes); |
| 18 | + |
| 19 | + return function connect(select, bindActionCreators, merge) { |
| 20 | + |
| 21 | + const subscribing = select ? true : false; |
| 22 | + |
| 23 | + select = select || emptySelector; |
| 24 | + |
| 25 | + bindActionCreators = bindActionCreators || emptyBinder; |
| 26 | + |
| 27 | + if (isPlainObject(bindActionCreators)) { |
| 28 | + bindActionCreators = wrapActionCreators(bindActionCreators); |
| 29 | + } |
| 30 | + |
| 31 | + merge = merge || identityMerge; |
| 32 | + |
| 33 | + return DecoratedComponent => class ConnectWrapper extends Component { |
| 34 | + static displayName = `ConnectWrapper(${getDisplayName(DecoratedComponent)})`; |
| 35 | + static DecoratedComponent = DecoratedComponent; |
| 36 | + |
| 37 | + static contextTypes = { |
| 38 | + store: storeShape.isRequired |
| 39 | + }; |
| 40 | + |
| 41 | + componentWillReceiveProps(nextProps) { |
| 42 | + console.log('recieving props', this.props, nextProps) |
| 43 | + } |
| 44 | + |
| 45 | + shouldComponentUpdate(nextProps, nextState) { |
| 46 | + console.log('shallowEqual of props', shallowEqual(this.props, nextProps), this.props, nextProps) |
| 47 | + return (this.subscribed && !this.isSliceEqual(this.state.slice, nextState.slice)) || |
| 48 | + !shallowEqual(this.props, nextProps); |
| 49 | + } |
| 50 | + |
| 51 | + isSliceEqual(slice, nextSlice) { |
| 52 | + const isRefEqual = slice === nextSlice; |
| 53 | + if (isRefEqual) { |
| 54 | + return true; |
| 55 | + } else if (typeof slice !== 'object' || typeof nextSlice !== 'object') { |
| 56 | + return isRefEqual; |
| 57 | + } |
| 58 | + return shallowEqual(slice, nextSlice); |
| 59 | + } |
| 60 | + |
| 61 | + constructor(props, context) { |
| 62 | + super(props, context); |
| 63 | + this.state = { |
| 64 | + ...this.selectState(props, context), |
| 65 | + ...this.bindActionCreators(context), |
| 66 | + }; |
| 67 | + } |
| 68 | + |
| 69 | + componentWillMount() { |
| 70 | + console.log('will mount', this.props) |
| 71 | + } |
| 72 | + |
| 73 | + componentDidMount() { |
| 74 | + console.log('mounted', this.props) |
| 75 | + if (subscribing) { |
| 76 | + this.subscribed = true; |
| 77 | + this.unsubscribe = this.context.store.subscribe(::this.handleChange); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + componentWillUnmount() { |
| 82 | + if (subscribing) { |
| 83 | + this.unsubscribe(); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + handleChange(props = this.props) { |
| 88 | + const nextState = this.selectState(props, this.context); |
| 89 | + if (!this.isSliceEqual(this.state.slice, nextState.slice)) { |
| 90 | + this.setState(nextState); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + selectState(props = this.props, context = this.context) { |
| 95 | + const state = context.store.getState(); |
| 96 | + const slice = select(state); |
| 97 | + |
| 98 | + invariant( |
| 99 | + isPlainObject(slice), |
| 100 | + 'The return value of `select` prop must be an object. Instead received %s.', |
| 101 | + slice |
| 102 | + ); |
| 103 | + |
| 104 | + return { slice }; |
| 105 | + } |
| 106 | + |
| 107 | + bindActionCreators(context = this.context) { |
| 108 | + const { dispatch } = context.store; |
| 109 | + const actionCreators = bindActionCreators(dispatch); |
| 110 | + |
| 111 | + invariant( |
| 112 | + isPlainObject(actionCreators), |
| 113 | + 'The return value of `bindActionCreators` prop must be an object. Instead received %s.', |
| 114 | + actionCreators |
| 115 | + ); |
| 116 | + |
| 117 | + return { actionCreators }; |
| 118 | + } |
| 119 | + |
| 120 | + merge(props = this.props, state = this.state) { |
| 121 | + const { slice, actionCreators } = state; |
| 122 | + const merged = merge(slice, actionCreators, props); |
| 123 | + |
| 124 | + invariant( |
| 125 | + isPlainObject(merged), |
| 126 | + 'The return value of `merge` prop must be an object. Instead received %s.', |
| 127 | + merged |
| 128 | + ); |
| 129 | + |
| 130 | + console.log('merging with ', merged) |
| 131 | + |
| 132 | + return merged; |
| 133 | + } |
| 134 | + |
| 135 | + render() { |
| 136 | + return <DecoratedComponent {...this.merge()} />; |
| 137 | + } |
| 138 | + }; |
| 139 | + }; |
| 140 | +} |
0 commit comments