Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

不需要给 .setData() 传入整个 state,只传入变化的值就行了 #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 29 additions & 13 deletions src/connect.js
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import shallowEqual from './shallowEqual.js'
import stateDiff from './stateDiff.js'
import warning from './warning.js'
import wrapActionCreators from './wrapActionCreators.js'

const defaultMapStateToProps = state => ({}) // eslint-disable-line no-unused-vars
const defaultMapDispatchToProps = dispatch => ({dispatch})
const defaultMapDispatchToProps = dispatch => ({
dispatch
})

function connect(mapStateToProps, mapDispatchToProps) {
const shouldSubscribe = Boolean(mapStateToProps)
const mapState = mapStateToProps || defaultMapStateToProps
const app = getApp();
const app = getApp()

let mapDispatch
if (typeof mapDispatchToProps === 'function') {
Expand All @@ -27,11 +29,17 @@ function connect(mapStateToProps, mapDispatchToProps) {
}

const state = this.store.getState()
const mappedState = mapState(state, options);
if (!this.data || shallowEqual(this.data, mappedState)) {
return;
const mappedState = mapState(state, options)
const {
__state
} = this
const patch = stateDiff(mappedState, __state)
if (!patch) {
return
}
this.setData(mappedState)
this.__state = mappedState
// only pass in updated data to .setData()
this.setData(patch)
}

const {
Expand All @@ -44,24 +52,32 @@ function connect(mapStateToProps, mapDispatchToProps) {
if (!this.store) {
warning("Store对象不存在!")
}
if(shouldSubscribe){
this.unsubscribe = this.store.subscribe(handleChange.bind(this, options))
handleChange.apply(this)
if (shouldSubscribe) {
this.__state = {}
this.unsubscribe = this.store.subscribe(() => {
handleChange.call(this, options)
})
handleChange.call(this)
}
if (typeof _onLoad === 'function') {
_onLoad.call(this, options)
}
}

function onUnload() {
typeof this.unsubscribe === 'function' && this.unsubscribe()
// should no long receive state changes after .onUnload()
this.unsubscribe = null
if (typeof _onUnload === 'function') {
_onUnload.call(this)
}
typeof this.unsubscribe === 'function' && this.unsubscribe()
}

return Object.assign({}, pageConfig, mapDispatch(app.store.dispatch), {onLoad, onUnload})
return Object.assign({}, pageConfig, mapDispatch(app.store.dispatch), {
onLoad,
onUnload
})
}
}

module.exports = connect
module.exports = connect
25 changes: 0 additions & 25 deletions src/shallowEqual.js

This file was deleted.

29 changes: 29 additions & 0 deletions src/stateDiff.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const hasOwn = Object.prototype.hasOwnProperty

function stateDiff(nextState, state) {
if (nextState === state) {
return null
}

const patch = {}

const keys = Object.keys(nextState)
const length = keys.length
let hasChanged = false
for (let i = 0; i < length; ++i) {
const key = keys[i]
const val = nextState[key]
if (!hasOwn.call(state, key) || val !== state[key]) {
patch[key] = val
hasChanged = true
}
}

if (!hasChanged) {
return null
}

return patch
}

module.exports = stateDiff