|
| 1 | +const appStore = require('../../../js/stores/appStoreRenderer') |
| 2 | +const ImmutableComponent = require('../../../js/components/immutableComponent') |
| 3 | +const React = require('react') |
| 4 | +const windowStore = require('../../../js/stores/windowStore') |
| 5 | + |
| 6 | +class ReduxComponent extends ImmutableComponent { |
| 7 | + constructor (props) { |
| 8 | + super(props) |
| 9 | + this.componentType = props.componentType |
| 10 | + } |
| 11 | + |
| 12 | + componentDidMount () { |
| 13 | + appStore.addChangeListener(() => { |
| 14 | + if (this.shouldComponentUpdate(this.buildProps(), this.state)) { |
| 15 | + this.forceUpdate() |
| 16 | + } |
| 17 | + }) |
| 18 | + |
| 19 | + windowStore.addChangeListener(() => { |
| 20 | + if (this.shouldComponentUpdate(this.buildProps(), this.state)) { |
| 21 | + this.forceUpdate() |
| 22 | + } |
| 23 | + }) |
| 24 | + } |
| 25 | + |
| 26 | + shouldComponentUpdate (nextProps, nextState) { |
| 27 | + return super.shouldComponentUpdate(this.buildProps(nextProps), nextState) |
| 28 | + } |
| 29 | + |
| 30 | + mergeProps (stateProps, dispatchProps, ownProps) { |
| 31 | + return Object.assign({}, stateProps, dispatchProps, ownProps) |
| 32 | + } |
| 33 | + |
| 34 | + buildProps (props = this.props) { |
| 35 | + const fn = this.componentType.prototype.mergeProps || this.mergeProps |
| 36 | + const state = appStore.state.set('currentWindow', windowStore.state) |
| 37 | + return fn(state, {}, props) |
| 38 | + } |
| 39 | + |
| 40 | + render () { |
| 41 | + return React.createElement(this.componentType, this.buildProps()) |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +module.exports.connect = (componentType) => { |
| 46 | + return (props) => { |
| 47 | + const component = React.createElement(ReduxComponent, Object.assign({componentType}, props)) |
| 48 | + return component |
| 49 | + } |
| 50 | +} |
0 commit comments