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

Commit

Permalink
only update when props actually change
Browse files Browse the repository at this point in the history
  • Loading branch information
bridiver committed Apr 12, 2017
1 parent 2d6df46 commit f5c60e6
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions app/renderer/components/reduxComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,52 @@ const ImmutableComponent = require('../../../js/components/immutableComponent')
const React = require('react')
const windowStore = require('../../../js/stores/windowStore')

const mergePropsImpl = (stateProps, dispatchProps, ownProps) => {
return Object.assign({}, stateProps, dispatchProps, ownProps)
}

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

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

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

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

componentWillReceiveProps (nextProps) {
this.setState(this.buildProps(nextProps))
}

shouldComponentUpdate (nextProps, nextState) {
return super.shouldComponentUpdate(this.buildProps(nextProps), nextState)
return Object.keys(nextState).some((prop) => nextState[prop] !== this.state[prop]) ||
Object.keys(nextProps).some((prop) => nextProps[prop] !== this.props[prop])
}

mergeProps (stateProps, dispatchProps, ownProps) {
return Object.assign({}, stateProps, dispatchProps, ownProps)
return mergePropsImpl(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)
return buildPropsImpl(props, this.componentType)
}

render () {
Expand Down

1 comment on commit f5c60e6

@bsclifton
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

++

Please sign in to comment.