Skip to content

fix: pause dep collection during immediate watcher invocation #11943

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

Merged
merged 6 commits into from
Mar 30, 2021
Merged
Show file tree
Hide file tree
Changes from 4 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
31 changes: 31 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,37 @@ describe('Instance methods lifecycle', () => {
}
}).$mount()
})

it('Dep.target should be undefined during invocation of child immediate watcher', done => {
let calls = 0, childData
const parentUpdate = jasmine.createSpy()
new Vue({
template: '<div><my-component></my-component></div>',
updated: parentUpdate,
components: {
myComponent: {
template: '<div>{{ a }}</div>',
data() {
return childData = { a: 123 }
},
watch: {
a: {
handler() {
++calls
expect(Dep.target).toBe(undefined)
},
immediate: true
}
}
}
}
}).$mount()
expect(calls).toBe(1)
childData.a++
waitForUpdate(() => {
expect(parentUpdate).not.toHaveBeenCalled()
}).then(done)
})
})

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