Skip to content

Commit 2c63e94

Browse files
committed
fix watcher prevTarget state when evaluating computed lazy watcher (fix #3133)
1 parent de0b4b8 commit 2c63e94

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

src/core/observer/watcher.js

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
} from '../util/index'
1313

1414
let uid = 0
15-
let prevTarget
15+
const targetStack = []
1616

1717
/**
1818
* A watcher parses an expression, collects dependencies,
@@ -83,7 +83,7 @@ export default class Watcher {
8383
* Evaluate the getter, and re-collect dependencies.
8484
*/
8585
get () {
86-
this.beforeGet()
86+
this.pushTarget()
8787
let value: any
8888
try {
8989
value = this.getter.call(this.vm, this.vm)
@@ -116,18 +116,26 @@ export default class Watcher {
116116
if (this.deep) {
117117
traverse(value)
118118
}
119-
this.afterGet()
119+
this.popTarget()
120+
this.cleanupDeps()
120121
return value
121122
}
122123

123124
/**
124-
* Prepare for dependency collection.
125+
* Set this watcher as the active dep target
125126
*/
126-
beforeGet () {
127-
prevTarget = Dep.target
127+
pushTarget () {
128+
if (Dep.target) targetStack.push(Dep.target)
128129
Dep.target = this
129130
}
130131

132+
/**
133+
* Restore previous dep target
134+
*/
135+
popTarget () {
136+
Dep.target = targetStack.pop()
137+
}
138+
131139
/**
132140
* Add a dependency to this directive.
133141
*/
@@ -145,8 +153,7 @@ export default class Watcher {
145153
/**
146154
* Clean up for dependency collection.
147155
*/
148-
afterGet () {
149-
Dep.target = prevTarget
156+
cleanupDeps () {
150157
let i = this.deps.length
151158
while (i--) {
152159
const dep = this.deps[i]
@@ -207,12 +214,8 @@ export default class Watcher {
207214
* This only gets called for lazy watchers.
208215
*/
209216
evaluate () {
210-
// avoid overwriting another watcher that is being
211-
// collected.
212-
const current = Dep.target
213217
this.value = this.get()
214218
this.dirty = false
215-
Dep.target = current
216219
}
217220

218221
/**

0 commit comments

Comments
 (0)