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: pause dep collection during immediate watcher invocation #11943

Merged
merged 6 commits into from
Mar 30, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 2 additions & 0 deletions src/core/instance/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -355,11 +355,13 @@ export function stateMixin (Vue: Class<Component>) {
options.user = true
const watcher = new Watcher(vm, expOrFn, cb, options)
if (options.immediate) {
pushTarget()
try {
cb.call(vm, watcher.value)
} catch (error) {
handleError(error, vm, `callback for immediate watcher "${watcher.expression}"`)
}
popTarget()
}
return function unwatchFn () {
watcher.teardown()
Expand Down
25 changes: 25 additions & 0 deletions test/unit/features/instance/methods-lifecycle.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,31 @@ describe('Instance methods lifecycle', () => {
}
}).$mount()
})

it('Dep.target should be undefined during invocation of child immediate watcher', () => {
let calls = 0;
new Vue({
template: '<div><my-component></my-component></div>',
components: {
myComponent: {
template: '<div>hi</div>',
data() {
return { a: 123 }
},
watch: {
a: {
delaneyb marked this conversation as resolved.
Show resolved Hide resolved
handler() {
posva marked this conversation as resolved.
Show resolved Hide resolved
++calls
expect(Dep.target).toBe(undefined)
delaneyb marked this conversation as resolved.
Show resolved Hide resolved
},
immediate: true
}
}
}
}
}).$mount()
expect(calls).toBe(1)
})
})

describe('$destroy', () => {
Expand Down