Kaynağa Gözat

move dep target logic into Dep itself

Evan You 10 yıl önce
ebeveyn
işleme
35d137c536
2 değiştirilmiş dosya ile 13 ekleme ve 18 silme
  1. 10 0
      src/core/observer/dep.js
  2. 3 18
      src/core/observer/watcher.js

+ 10 - 0
src/core/observer/dep.js

@@ -46,3 +46,13 @@ export default class Dep {
 // this is globally unique because there could be only one
 // watcher being evaluated at any time.
 Dep.target = null
+const targetStack = []
+
+export function pushTarget (_target: Watcher) {
+  if (Dep.target) targetStack.push(Dep.target)
+  Dep.target = _target
+}
+
+export function popTarget () {
+  Dep.target = targetStack.pop()
+}

+ 3 - 18
src/core/observer/watcher.js

@@ -1,7 +1,7 @@
 /* @flow */
 
 import config from '../config'
-import Dep from './dep'
+import Dep, { pushTarget, popTarget } from './dep'
 import { queueWatcher } from './scheduler'
 import {
   warn,
@@ -83,7 +83,7 @@ export default class Watcher {
    * Evaluate the getter, and re-collect dependencies.
    */
   get () {
-    this.pushTarget()
+    pushTarget(this)
     let value: any
     try {
       value = this.getter.call(this.vm, this.vm)
@@ -116,26 +116,11 @@ export default class Watcher {
     if (this.deep) {
       traverse(value)
     }
-    this.popTarget()
+    popTarget()
     this.cleanupDeps()
     return value
   }
 
-  /**
-   * Set this watcher as the active dep target
-   */
-  pushTarget () {
-    if (Dep.target) targetStack.push(Dep.target)
-    Dep.target = this
-  }
-
-  /**
-   * Restore previous dep target
-   */
-  popTarget () {
-    Dep.target = targetStack.pop()
-  }
-
   /**
    * Add a dependency to this directive.
    */