|
@@ -12,7 +12,7 @@ import {
|
|
|
} from '../util/index'
|
|
} from '../util/index'
|
|
|
|
|
|
|
|
let uid = 0
|
|
let uid = 0
|
|
|
-let prevTarget
|
|
|
|
|
|
|
+const targetStack = []
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* A watcher parses an expression, collects dependencies,
|
|
* A watcher parses an expression, collects dependencies,
|
|
@@ -83,7 +83,7 @@ export default class Watcher {
|
|
|
* Evaluate the getter, and re-collect dependencies.
|
|
* Evaluate the getter, and re-collect dependencies.
|
|
|
*/
|
|
*/
|
|
|
get () {
|
|
get () {
|
|
|
- this.beforeGet()
|
|
|
|
|
|
|
+ this.pushTarget()
|
|
|
let value: any
|
|
let value: any
|
|
|
try {
|
|
try {
|
|
|
value = this.getter.call(this.vm, this.vm)
|
|
value = this.getter.call(this.vm, this.vm)
|
|
@@ -116,18 +116,26 @@ export default class Watcher {
|
|
|
if (this.deep) {
|
|
if (this.deep) {
|
|
|
traverse(value)
|
|
traverse(value)
|
|
|
}
|
|
}
|
|
|
- this.afterGet()
|
|
|
|
|
|
|
+ this.popTarget()
|
|
|
|
|
+ this.cleanupDeps()
|
|
|
return value
|
|
return value
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
- * Prepare for dependency collection.
|
|
|
|
|
|
|
+ * Set this watcher as the active dep target
|
|
|
*/
|
|
*/
|
|
|
- beforeGet () {
|
|
|
|
|
- prevTarget = Dep.target
|
|
|
|
|
|
|
+ pushTarget () {
|
|
|
|
|
+ if (Dep.target) targetStack.push(Dep.target)
|
|
|
Dep.target = this
|
|
Dep.target = this
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Restore previous dep target
|
|
|
|
|
+ */
|
|
|
|
|
+ popTarget () {
|
|
|
|
|
+ Dep.target = targetStack.pop()
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* Add a dependency to this directive.
|
|
* Add a dependency to this directive.
|
|
|
*/
|
|
*/
|
|
@@ -145,8 +153,7 @@ export default class Watcher {
|
|
|
/**
|
|
/**
|
|
|
* Clean up for dependency collection.
|
|
* Clean up for dependency collection.
|
|
|
*/
|
|
*/
|
|
|
- afterGet () {
|
|
|
|
|
- Dep.target = prevTarget
|
|
|
|
|
|
|
+ cleanupDeps () {
|
|
|
let i = this.deps.length
|
|
let i = this.deps.length
|
|
|
while (i--) {
|
|
while (i--) {
|
|
|
const dep = this.deps[i]
|
|
const dep = this.deps[i]
|
|
@@ -207,12 +214,8 @@ export default class Watcher {
|
|
|
* This only gets called for lazy watchers.
|
|
* This only gets called for lazy watchers.
|
|
|
*/
|
|
*/
|
|
|
evaluate () {
|
|
evaluate () {
|
|
|
- // avoid overwriting another watcher that is being
|
|
|
|
|
- // collected.
|
|
|
|
|
- const current = Dep.target
|
|
|
|
|
this.value = this.get()
|
|
this.value = this.get()
|
|
|
this.dirty = false
|
|
this.dirty = false
|
|
|
- Dep.target = current
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|