Skip to content

Commit

Permalink
Improves props validation
Browse files Browse the repository at this point in the history
Resolves brave#8557

Auditors: @bridiver

Test Plan:
- create redux component
- pass prop as a simple List
- component should not update if props is the same
  • Loading branch information
NejcZdovc committed Apr 28, 2017
1 parent d9745a5 commit 82a7641
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
8 changes: 8 additions & 0 deletions app/common/state/immutableUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ const api = {
return Immutable.List.isList(obj)
},

isSameHashCode: (first, second) => {
if (first === null && second === null) {
return true
}

return second !== null ? first.hashCode() === second.hashCode() : false
},

makeImmutable: (obj) => {
return api.isImmutable(obj) ? obj : Immutable.fromJS(obj)
}
Expand Down
11 changes: 9 additions & 2 deletions app/renderer/components/reduxComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const ImmutableComponent = require('./immutableComponent')
const React = require('react')
const windowStore = require('../../../js/stores/windowStore')
const debounce = require('../../../js/lib/debounce')
const {isList, isSameHashCode} = require('../../common/state/immutableUtil')

const mergePropsImpl = (stateProps, dispatchProps, ownProps) => {
return Object.assign({}, stateProps, dispatchProps, ownProps)
Expand Down Expand Up @@ -44,9 +45,15 @@ class ReduxComponent extends ImmutableComponent {
this.setState(this.buildProps(nextProps))
}

checkParam (next, old, prop) {
return isList(next[prop])
? !isSameHashCode(next[prop], old[prop])
: next[prop] !== this.state[prop]
}

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

mergeProps (stateProps, dispatchProps, ownProps) {
Expand Down

0 comments on commit 82a7641

Please sign in to comment.