Skip to content

Commit

Permalink
fix: remove wrong observe toggle, properly disable tracking in setup()
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Jun 20, 2022
1 parent 9d54f8b commit 2d67641
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/v3/apiSetup.ts
@@ -1,6 +1,6 @@
import { Component } from 'types/component'
import { PropOptions } from 'types/options'
import { toggleObserving } from '../core/observer'
import { popTarget, pushTarget } from '../core/observer/dep'
import { def, invokeWithErrorHandling, isReserved, warn } from '../core/util'
import VNode from '../core/vdom/vnode'
import {
Expand Down Expand Up @@ -31,15 +31,15 @@ export function initSetup(vm: Component) {
const ctx = (vm._setupContext = createSetupContext(vm))

setCurrentInstance(vm)
toggleObserving(false)
pushTarget()
const setupResult = invokeWithErrorHandling(
setup,
null,
[vm._props || shallowReactive({}), ctx],
vm,
`setup`
)
toggleObserving(true)
popTarget()
setCurrentInstance()

if (isFunction(setupResult)) {
Expand Down
27 changes: 27 additions & 0 deletions test/unit/features/v3/apiSetup.spec.ts
Expand Up @@ -267,4 +267,31 @@ describe('api: setup context', () => {
}
}).$mount()
})

it('should not track dep accessed in setup', async () => {
const spy = vi.fn()
const msg = ref('hi')

const Child = {
setup: () => {
msg.value
return () => {}
}
}

new Vue({
setup() {
return h => {
spy()
return h(Child)
}
}
}).$mount()

expect(spy).toHaveBeenCalledTimes(1)

msg.value = 'bye'
await nextTick()
expect(spy).toHaveBeenCalledTimes(1)
})
})

0 comments on commit 2d67641

Please sign in to comment.