Skip to content

Commit

Permalink
Merge pull request Tencent#48 from aeroxy/master
Browse files Browse the repository at this point in the history
将rewritePureUpdate内的update也promisify并且更新使用文档
  • Loading branch information
当耐特 authored Nov 2, 2018
2 parents f8faa2f + 5fad49f commit 059f191
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 18 deletions.
28 changes: 26 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
* 超小的代码尺寸(包括 json diff 共100多行)
* 尊重且顺从小程序的设计(其他转译库相当于反其道行)
* 增强 data 数据绑定,函数属性可直接绑定到 WXML
* this.update 兼容 setData 同样的语法
* this.update setData 语法类似,但返回一个Promise
* this.update 比原生 setData 的性能更优,更加智能
* Westore 专为小程序插件开发[定制了模板](https://github.com/dntzhang/westore/tree/master/packages/westore-plugin)
* Westore 集成了腾讯云开发
Expand Down Expand Up @@ -96,6 +96,25 @@ this.update({
})
```

和小程序的setData不同的是回调的方式,小程序的回调为setData的第二个入参,但是update则直接返回一个Promise,并且返回的数据内有更新的数据内容。例如:

``` js
this.setData({
motto: 'Hello Westore'
}, () => {
console.log('the motto has been set')
})
```

被改进为

``` js
this.store.data.mottto = 'Hello Westore'
this.update().then(diff => {
console.log('the motto has been set', diff)
})
```

这里需要特别强调,虽然 this.update 可以兼容小程序的 this.setData 的方式传参,但是更加智能,this.update 会先 Diff 然后 setData。原理:

![](./asset/update2.jpg)
Expand Down Expand Up @@ -295,6 +314,8 @@ this.setData({
logs: (wx.getStorageSync('logs') || []).map(log => {
return util.formatTime(new Date(log))
})
}, () => {
console.log('setData完成了')
})
```

Expand All @@ -304,7 +325,10 @@ this.setData({
this.store.data.logs = (wx.getStorageSync('logs') || []).map(log => {
return util.formatTime(new Date(log))
})
this.update()
this.update().then(diff => {
console.log('setData完成了')
console.log('更新内容为', diff)
})
```

看似一条语句变成了两条语句,但是 this.update 调用的 setData 是 diff 后的,所以传递的数据更少。
Expand Down
36 changes: 20 additions & 16 deletions utils/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,24 +124,28 @@ function _arrayToPath(data, path, result) {
function rewritePureUpdate(ctx) {
ctx.update = function (patch) {
const store = this.store
//defineFnProp(store.data)
if (patch) {
for (let key in patch) {
updateByPath(store.data, key, patch[key])
const that = this
return new Promise(resolve => {
//defineFnProp(store.data)
if (patch) {
for (let key in patch) {
updateByPath(store.data, key, patch[key])
}
}
}
let diffResult = diff(store.data, store.originData)
if (Object.keys(diffResult)[0] == '') {
diffResult = diffResult['']
}
if (Object.keys(diffResult).length > 0) {
this.setData(diffResult)
store.onChange && store.onChange(diffResult)
for (let key in diffResult) {
updateByPath(store.originData, key, typeof diffResult[key] === 'object' ? JSON.parse(JSON.stringify(diffResult[key])) : diffResult[key])
let diffResult = diff(store.data, store.originData)
let array = []
if (Object.keys(diffResult)[0] == '') {
diffResult = diffResult['']
}
}
return diffResult
if (Object.keys(diffResult).length > 0) {
array.push( new Promise( cb => that.setData(diffResult, cb) ) )
store.onChange && store.onChange(diffResult)
for (let key in diffResult) {
updateByPath(store.originData, key, typeof diffResult[key] === 'object' ? JSON.parse(JSON.stringify(diffResult[key])) : diffResult[key])
}
}
Promise.all(array).then( e => resolve(diffResult) )
})
}
}

Expand Down

0 comments on commit 059f191

Please sign in to comment.