github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/public/libs/vue-1.0.24/src/observer/dep.js (about)

     1  import { toArray } from '../util/index'
     2  
     3  let uid = 0
     4  
     5  /**
     6   * A dep is an observable that can have multiple
     7   * directives subscribing to it.
     8   *
     9   * @constructor
    10   */
    11  
    12  export default function Dep () {
    13    this.id = uid++
    14    this.subs = []
    15  }
    16  
    17  // the current target watcher being evaluated.
    18  // this is globally unique because there could be only one
    19  // watcher being evaluated at any time.
    20  Dep.target = null
    21  
    22  /**
    23   * Add a directive subscriber.
    24   *
    25   * @param {Directive} sub
    26   */
    27  
    28  Dep.prototype.addSub = function (sub) {
    29    this.subs.push(sub)
    30  }
    31  
    32  /**
    33   * Remove a directive subscriber.
    34   *
    35   * @param {Directive} sub
    36   */
    37  
    38  Dep.prototype.removeSub = function (sub) {
    39    this.subs.$remove(sub)
    40  }
    41  
    42  /**
    43   * Add self as a dependency to the target watcher.
    44   */
    45  
    46  Dep.prototype.depend = function () {
    47    Dep.target.addDep(this)
    48  }
    49  
    50  /**
    51   * Notify all subscribers of a new value.
    52   */
    53  
    54  Dep.prototype.notify = function () {
    55    // stablize the subscriber list first
    56    var subs = toArray(this.subs)
    57    for (var i = 0, l = subs.length; i < l; i++) {
    58      subs[i].update()
    59    }
    60  }