Skip to content

Commit

Permalink
fix(setup): handle updates for directly return a reactive object (#488)
Browse files Browse the repository at this point in the history
* fix(setup): handle updates for directly return a reactive object, fix #487

* chore: type fix
  • Loading branch information
antfu authored Aug 22, 2020
1 parent 76f1f8f commit a7f2c25
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
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')
})
})

0 comments on commit a7f2c25

Please sign in to comment.