SOURCE

export default class Dep{
    static targe?:Watcher;
    subs:Array<Watcher>;

    constructor {
        this.subs = []
    }
    /**
   * 添加订阅者(watcher)
   **/
    addSub (sub: Watcher) {
        this.subs.push(sub)
    }
   /**
   * 移除订阅者(watcher)
   **/
  removeSub (sub: Watcher) {
    remove(this.subs, sub)
  }
   /**
   * 添加订阅(调用订阅者上的 addDep 方法)
   **/
  depend () {
    if (Dep.target) {
      Dep.target.addDep(this)
    }
  }
  /**
   * 遍历通知订阅者更新
   **/
  notify () {
    const subs = this.subs.slice()
    for (let i = 0, l = subs.length; i < l; i++) {
      subs[i].update()
    }
  }  

}

Dep.target = null

export default defineReactive(
    obj:Object,
    key:String,
    val:any
){
    const dep = new Dep()
    Object.defineProperties(obj,key,{
        enumerable:true,
        configurable:true,
        
        get:function reactiveGetter(){
            if(dep.target){
                dep.depend()
            }   
        },
        set:function reactiveSetter(newVal){
            val = newVal
            // 每次更新数据之后广播更新
            dep.notify()
        }
    })
} 
console 命令行工具 X clear

                    
>
console