Skip to content

Commit

Permalink
fix(reactivity): replaced ref in reactive object should be tracked (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
yangmingshan authored Jun 12, 2020
1 parent d437a01 commit 80e1693
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
15 changes: 15 additions & 0 deletions packages/reactivity/__tests__/reactive.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,21 @@ describe('reactivity/reactive', () => {
expect(typeof obj.b).toBe(`number`)
})

test('should allow setting property from a ref to another ref', () => {
const foo = ref(0)
const bar = ref(1)
const observed = reactive({ a: foo })
const dummy = computed(() => observed.a)
expect(dummy.value).toBe(0)

// @ts-ignore
observed.a = bar
expect(dummy.value).toBe(1)

bar.value++
expect(dummy.value).toBe(2)
})

test('non-observable values', () => {
const assertValue = (value: any) => {
reactive(value)
Expand Down
5 changes: 2 additions & 3 deletions packages/reactivity/src/baseHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,22 +66,21 @@ function createGetter(isReadonly = false, shallow = false) {
return res
}

!isReadonly && track(target, TrackOpTypes.GET, key)

if (shallow) {
!isReadonly && track(target, TrackOpTypes.GET, key)
return res
}

if (isRef(res)) {
if (targetIsArray) {
!isReadonly && track(target, TrackOpTypes.GET, key)
return res
} else {
// ref unwrapping, only for Objects, not for Arrays.
return res.value
}
}

!isReadonly && track(target, TrackOpTypes.GET, key)
return isObject(res)
? isReadonly
? // need to lazy access readonly and reactive here to avoid
Expand Down

0 comments on commit 80e1693

Please sign in to comment.