Skip to content

Commit f3f043d

Browse files
author
boquanfu
committed
feat: add oldValue for watch
1 parent 6117424 commit f3f043d

File tree

5 files changed

+21
-5
lines changed

5 files changed

+21
-5
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ Component({
8181
sum: 2,
8282
},
8383
watch: {
84-
'a, b': function (a, b) {
84+
'a, b': function (a, b, oldA, oldB) {
8585
this.setData({
8686
sum: a + b,
8787
})

UPDATE.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,7 @@
5050
## 7.0.0
5151

5252
- 新增一些 watch 辅助接口
53+
54+
## 7.1.0
55+
56+
- 为 watch 新增 oldValue

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "miniprogram-computed",
3-
"version": "7.0.0",
3+
"version": "7.1.0",
44
"description": "Computed & watch - wechat miniprogram custom component extend behavior",
55
"main": "dist/index.js",
66
"types": "types/index.d.ts",

src/behavior.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ class ComputedBuilder {
224224
if (changed) {
225225
listener.apply(
226226
this,
227-
originalCurValWithOptions.map(({ val }) => val),
227+
originalCurValWithOptions.map(({ val }) => val).concat(oldVal),
228228
)
229229
}
230230
},

test/index.test.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ const behaviorA = Behavior({
2929
describe('computed behavior', () => {
3030
test('watch basics', () => {
3131
let funcTriggeringCount = 0
32+
let prevA = 1
33+
let prevB = 2
3234

3335
const component = renderComponent(undefined, '<view>{{a}}+{{b}}={{c}}</view>', (Component) => {
3436
Component({
@@ -44,7 +46,11 @@ describe('computed behavior', () => {
4446
c: 3,
4547
},
4648
watch: {
47-
'a, b': function (a, b) {
49+
'a, b': function (a: number, b: number, oldA: number, oldB: number) {
50+
expect(prevA).toBe(oldA)
51+
expect(prevB).toBe(oldB)
52+
prevA = a
53+
prevB = b
4854
funcTriggeringCount++
4955
this.setData({
5056
c: a + b,
@@ -85,7 +91,13 @@ describe('computed behavior', () => {
8591
c: 0,
8692
}))
8793
.init((ctx) => {
88-
watch(ctx, 'a, b', (a: number, b: number) => {
94+
let oldA = ctx.data.a
95+
let oldB = ctx.data.b
96+
watch(ctx, 'a, b', (a: number, b: number, prevA: number, prevB: number) => {
97+
expect(prevA).toBe(oldA)
98+
expect(prevB).toBe(oldB)
99+
oldA = a
100+
oldB = b
89101
ctx.setData({ c: a + b })
90102
})
91103
})

0 commit comments

Comments
 (0)