Skip to content

Commit

Permalink
fix: pause dep collection during immediate watcher invocation (#11943)
Browse files Browse the repository at this point in the history
Co-authored-by: Eduardo San Martin Morote <posva@users.noreply.github.com>
  • Loading branch information
delaneyb and posva committed Mar 30, 2021
1 parent fb16d7b commit 987f322
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/core/instance/state.js
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
32 changes: 32 additions & 0 deletions test/unit/features/instance/methods-lifecycle.spec.js
Expand Up @@ -53,6 +53,38 @@ describe('Instance methods lifecycle', () => {
}
}).$mount()
})

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

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

0 comments on commit 987f322

Please sign in to comment.