Skip to content

Commit

Permalink
fix(taro-weapp): 小程序 diff 在新对象缺失旧对象的某些属性时,不再递归 diff,而是直接赋值。fix #1058
Browse files Browse the repository at this point in the history
  • Loading branch information
Chen-jj committed Nov 19, 2018
1 parent f0e526a commit adbc78d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
7 changes: 4 additions & 3 deletions packages/taro-weapp/src/lifecycle.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,11 @@ function doUpdate (component, prevProps, prevState) {
return
}
if (typeof val === 'object') {
if (isEmptyObject(val)) return safeSet(_data, key, val)

val = shakeFnFromObject(val)
if (!isEmptyObject(val)) {
safeSet(_data, key, val)
}
// 避免筛选完 Fn 后产生了空对象还去渲染
if (!isEmptyObject(val)) safeSet(_data, key, val)
} else {
safeSet(_data, key, val)
}
Expand Down
34 changes: 27 additions & 7 deletions packages/taro-weapp/src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,18 @@ function diffArrToPath (to, from, res = {}, keyPrev = '') {
res[targetKey] = toItem
} else {
// 对象
diffObjToPath(toItem, fromItem, res, `${targetKey}.`)
let shouldDiffObject = true
Object.keys(fromItem).some(key => {
if (typeof toItem[key] === 'undefined') {
shouldDiffObject = false
return true
}
})
if (shouldDiffObject) {
diffObjToPath(toItem, fromItem, res, `${targetKey}.`)
} else {
res[targetKey] = toItem
}
}
}
}
Expand All @@ -142,11 +153,9 @@ export function diffObjToPath (to, from, res = {}, keyPrev = '') {
const targetKey = `${keyPrev}${key}`
if (toItem === fromItem) {
continue
} else
if (!hasProp.call(from, key)) {
} else if (!hasProp.call(from, key)) {
res[targetKey] = toItem
} else
if (typeof toItem !== typeof fromItem) {
} else if (typeof toItem !== typeof fromItem) {
res[targetKey] = toItem
} else {
if (typeof toItem !== 'object') {
Expand All @@ -167,8 +176,19 @@ export function diffObjToPath (to, from, res = {}, keyPrev = '') {
if (!toItem || !fromItem) {
res[targetKey] = toItem
} else {
// 对象
diffObjToPath(toItem, fromItem, res, `${targetKey}.`)
// 对象
let shouldDiffObject = true
Object.keys(fromItem).some(key => {
if (typeof toItem[key] === 'undefined') {
shouldDiffObject = false
return true
}
})
if (shouldDiffObject) {
diffObjToPath(toItem, fromItem, res, `${targetKey}.`)
} else {
res[targetKey] = toItem
}
}
}
}
Expand Down

0 comments on commit adbc78d

Please sign in to comment.