Skip to content

Commit 987f322

Browse files
Ben Delaneyposva
Ben Delaney
andauthoredMar 30, 2021
fix: pause dep collection during immediate watcher invocation (#11943)
Co-authored-by: Eduardo San Martin Morote <[email protected]>
1 parent fb16d7b commit 987f322

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed
 

‎src/core/instance/state.js

+2
Original file line numberDiff line numberDiff line change
@@ -355,11 +355,13 @@ export function stateMixin (Vue: Class<Component>) {
355355
options.user = true
356356
const watcher = new Watcher(vm, expOrFn, cb, options)
357357
if (options.immediate) {
358+
pushTarget()
358359
try {
359360
cb.call(vm, watcher.value)
360361
} catch (error) {
361362
handleError(error, vm, `callback for immediate watcher "${watcher.expression}"`)
362363
}
364+
popTarget()
363365
}
364366
return function unwatchFn () {
365367
watcher.teardown()

‎test/unit/features/instance/methods-lifecycle.spec.js

+32
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,38 @@ describe('Instance methods lifecycle', () => {
5353
}
5454
}).$mount()
5555
})
56+
57+
it('Dep.target should be undefined during invocation of child immediate watcher', done => {
58+
let calls = 0
59+
const childData = { a: 1 }
60+
const parentUpdate = jasmine.createSpy()
61+
new Vue({
62+
template: '<div><my-component></my-component></div>',
63+
updated: parentUpdate,
64+
components: {
65+
myComponent: {
66+
template: '<div>{{ a }}</div>',
67+
data() {
68+
return childData
69+
},
70+
watch: {
71+
anything: {
72+
handler() {
73+
++calls
74+
this.a
75+
},
76+
immediate: true
77+
}
78+
}
79+
}
80+
}
81+
}).$mount()
82+
expect(calls).toBe(1)
83+
childData.a++
84+
waitForUpdate(() => {
85+
expect(parentUpdate).not.toHaveBeenCalled()
86+
}).then(done)
87+
})
5688
})
5789

5890
describe('$destroy', () => {

0 commit comments

Comments
 (0)
Please sign in to comment.