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

fix(reactivity): allow setting property from a ref to another ref #1058

Merged
merged 1 commit into from
Jun 12, 2020
Merged
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
15 changes: 15 additions & 0 deletions packages/reactivity/__tests__/reactive.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,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 @@ -45,22 +45,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