Skip to content
This repository has been archived by the owner on Dec 11, 2019. It is now read-only.

Commit

Permalink
add reduxComponent
Browse files Browse the repository at this point in the history
  • Loading branch information
bridiver committed Apr 11, 2017
1 parent 70ff720 commit ac5a069
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions app/renderer/components/reduxComponent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const appStore = require('../../../js/stores/appStoreRenderer')
const ImmutableComponent = require('../../../js/components/immutableComponent')
const React = require('react')
const windowStore = require('../../../js/stores/windowStore')

class ReduxComponent extends ImmutableComponent {
constructor (props) {
super(props)
this.componentType = props.componentType
}

componentDidMount () {
appStore.addChangeListener(() => {
if (this.shouldComponentUpdate(this.buildProps(), this.state)) {
this.forceUpdate()
}
})

windowStore.addChangeListener(() => {
if (this.shouldComponentUpdate(this.buildProps(), this.state)) {
this.forceUpdate()
}
})
}

shouldComponentUpdate (nextProps, nextState) {
return super.shouldComponentUpdate(this.buildProps(nextProps), nextState)
}

mergeProps (stateProps, dispatchProps, ownProps) {
return Object.assign({}, stateProps, dispatchProps, ownProps)
}

buildProps (props = this.props) {
const fn = this.componentType.prototype.mergeProps || this.mergeProps
const state = appStore.state.set('currentWindow', windowStore.state)
return fn(state, {}, props)
}

render () {
return React.createElement(this.componentType, this.buildProps())
}
}

module.exports.connect = (componentType) => {
return (props) => {
const component = React.createElement(ReduxComponent, Object.assign({componentType}, props))
return component
}
}

0 comments on commit ac5a069

Please sign in to comment.