|
| 1 | +import React, { PropTypes, Component } from 'react'; |
| 2 | +import PureRenderMixin from 'react-addons-pure-render-mixin'; |
| 3 | +import get from 'lodash/get'; |
| 4 | +import storeShape from '../utils/storeShape'; |
| 5 | + |
| 6 | +const ownProps = Object.getOwnPropertyNames; |
| 7 | +const proto = Object.getPrototypeOf; |
| 8 | + |
| 9 | +export default class Connector extends Component { |
| 10 | + static reduce(namespace, stateToHandlers) { |
| 11 | + this.$namespace = namespace; |
| 12 | + const initialState = this.$state; |
| 13 | + const actionNames = ownProps(stateToHandlers()); |
| 14 | + this.__generateDispatchers(actionNames); |
| 15 | + |
| 16 | + return function(state = initialState, action) { |
| 17 | + const [actionNamespace, actionType] = action.type.split('/'); |
| 18 | + |
| 19 | + if (actionNamespace === namespace) { |
| 20 | + const handler = stateToHandlers(state)[actionType]; |
| 21 | + if (handler) { |
| 22 | + return handler(action.data); |
| 23 | + } |
| 24 | + } |
| 25 | + return state; |
| 26 | + }; |
| 27 | + } |
| 28 | + |
| 29 | + static __generateDispatchers(actionNames) { |
| 30 | + actionNames.forEach(name => { |
| 31 | + const type = this.action(name); |
| 32 | + if (typeof this.prototype[`$${name}`] !== 'function') { |
| 33 | + this.prototype[`$${name}`] = function(data) { |
| 34 | + this.dispatch(type, data); |
| 35 | + }; |
| 36 | + } |
| 37 | + }); |
| 38 | + } |
| 39 | + |
| 40 | + static $namespace = 'global'; |
| 41 | + |
| 42 | + static action(name) { |
| 43 | + return `${this.$namespace}/${name}`; |
| 44 | + } |
| 45 | + |
| 46 | + static get displayName() { |
| 47 | + const viewName = this.$connection.displayName || this.$connection.name || 'Component'; |
| 48 | + |
| 49 | + return `Connector(${viewName})`; |
| 50 | + } |
| 51 | + |
| 52 | + static contextTypes = { |
| 53 | + store: storeShape |
| 54 | + }; |
| 55 | + |
| 56 | + static propTypes = { |
| 57 | + store: storeShape |
| 58 | + }; |
| 59 | + |
| 60 | + static childContextTypes = { |
| 61 | + on: PropTypes.object |
| 62 | + }; |
| 63 | + |
| 64 | + constructor(props, context) { |
| 65 | + super(props, context); |
| 66 | + this.store = props.store || context.store; |
| 67 | + this.state = this.getExposedState(); |
| 68 | + |
| 69 | + this.shouldComponentUpdate = PureRenderMixin.shouldComponentUpdate.bind(this); |
| 70 | + } |
| 71 | + |
| 72 | + getChildContext() { |
| 73 | + return { on: this.getEventHandlers() }; |
| 74 | + } |
| 75 | + |
| 76 | + componentDidMount() { |
| 77 | + this.trySubscribe(); |
| 78 | + } |
| 79 | + |
| 80 | + componentWillUnmount() { |
| 81 | + this.tryUnsubscribe(); |
| 82 | + } |
| 83 | + |
| 84 | + trySubscribe() { |
| 85 | + if (!this.unsubscribe) { |
| 86 | + this.unsubscribe = this.store.subscribe(this.handleChange.bind(this)); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + tryUnsubscribe() { |
| 91 | + if (this.unsubscribe) { |
| 92 | + this.unsubscribe(); |
| 93 | + this.unsubscribe = null; |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + handleChange() { |
| 98 | + if (!this.unsubscribe) return; |
| 99 | + |
| 100 | + const state = this.getExposedState(); |
| 101 | + this.setState(state); |
| 102 | + } |
| 103 | + |
| 104 | + getExposedState() { |
| 105 | + const state = this.store.getState(); |
| 106 | + return this.$expose(get(state, this.constructor.$namespace) || this.constructor.$state, state); |
| 107 | + } |
| 108 | + |
| 109 | + dispatch(type, data) { |
| 110 | + return this.store.dispatch({ type, data }); |
| 111 | + } |
| 112 | + |
| 113 | + $expose($state) { |
| 114 | + return $state || {}; |
| 115 | + } |
| 116 | + |
| 117 | + getEventHandlers() { |
| 118 | + return ownProps(proto(this)).reduce((handlers, key) => { |
| 119 | + if (key[0] === '$' && key[1] != '$' && typeof this[key] === 'function' && key != '$expose') { |
| 120 | + handlers[key] = this[key].bind(this); |
| 121 | + } |
| 122 | + return handlers; |
| 123 | + }, {}); |
| 124 | + } |
| 125 | + |
| 126 | + getConnection() { |
| 127 | + return this.constructor.$connection; |
| 128 | + } |
| 129 | + |
| 130 | + render() { |
| 131 | + return React.createElement(this.getConnection(), { ...this.props, ...this.state }); |
| 132 | + } |
| 133 | +} |
0 commit comments