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(setup): handle updates for directly return a reactive object #488

Merged
merged 2 commits into from
Aug 22, 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
11 changes: 7 additions & 4 deletions src/mixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
SetupFunction,
Data,
} from './component'
import { isRef, isReactive, markRaw, markReactive } from './reactivity'
import { isRef, isReactive, markRaw, markReactive, toRefs } from './reactivity'
import { isPlainObject, assert, proxy, warn, isFunction } from './utils'
import { ref } from './apis'
import vmStateManager from './utils/vmStateManager'
Expand Down Expand Up @@ -94,13 +94,16 @@ export function mixin(Vue: VueConstructor) {
return activateCurrentInstance(vm, () => bindingFunc())
}
return
}
if (isPlainObject(binding)) {
} else if (isPlainObject(binding)) {
if (isReactive(binding)) {
binding = toRefs(binding) as Data
}

const bindingObj = binding
vmStateManager.set(vm, 'rawBindings', binding)

Object.keys(binding).forEach((name) => {
let bindingValue = bindingObj[name]
let bindingValue: any = bindingObj[name]
// only make primitive value reactive
if (!isRef(bindingValue)) {
if (isReactive(bindingValue)) {
Expand Down
4 changes: 4 additions & 0 deletions test/helpers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@ const Vue = require('vue/dist/vue.common.js')
export function nextTick(): Promise<any> {
return Vue.nextTick()
}

export function sleep(ms = 100) {
return new Promise((resolve) => setTimeout(resolve, ms))
}
25 changes: 25 additions & 0 deletions test/setup.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ const {
toRefs,
markRaw,
toRaw,
nextTick,
} = require('../src')
const { sleep } = require('./helpers/utils')

describe('setup', () => {
beforeEach(() => {
Expand Down Expand Up @@ -861,4 +863,27 @@ describe('setup', () => {
const vm = new Vue(Constructor).$mount()
expect(vm.$el.textContent).toBe('Composition-api')
})

// #487
it('should handle updates for directly return a reactive object.', async () => {
const opts = {
template: '<div>{{ count }}</div>',
setup() {
const state = reactive({ count: 1 })

setTimeout(() => {
state.count = 2
}, 1)

return state
},
}
const Constructor = Vue.extend(opts).extend({})

const vm = new Vue(Constructor).$mount()
expect(vm.$el.textContent).toBe('1')
await sleep(10)
await nextTick()
expect(vm.$el.textContent).toBe('2')
})
})