一个是Observer 类 属性上有个Dep,这里主要是对数组(数组没有get/set不能像对象属性那样)和对象本身进行依赖收集和通知
一个是对属性get/set处理时候的Dep,这个主要是对象的属性进行依赖收集和通知
2.DepDep 是 Observer 与 Watcher 桥梁,也可以认为Dep是服务于Observer的订阅系统。Watcher订阅某个Observer的Dep,当Observer观察的数据发生变化时,通过Dep通知各个已经订阅的Watcher。
export default class Dep { static target: ?Watcher; id: number; subs: Array<Watcher>; constructor () { this.id = uid++ this.subs = [] //Watcher实例 } //接收的参数为Watcher实例,并把Watcher实例存入记录依赖的数组中 addSub (sub: Watcher) { this.subs.push(sub) } //与addSub对应,作用是将Watcher实例从记录依赖的数组中移除 removeSub (sub: Watcher) { remove(this.subs, sub) } //依赖收集 depend () { if (Dep.target) { //存放当前Wather实例 //将当前 Dep 存放到 Watcher(观察者) 中的依赖中 Dep.target.addDep(this) } } //通知依赖数组中所有的watcher进行更新操作 notify () { const subs = this.subs.slice() for (let i = 0, l = subs.length; i < l; i++) { subs[i].update() } } } Dep.target = null const targetStack = [] export function pushTarget (target: ?Watcher) { targetStack.push(target) Dep.target = target } export function popTarget () { targetStack.pop() Dep.target = targetStack[targetStack.length - 1] } 3.Watcher先看 Watcher 的构造函数
constructor( vm: Component, expOrFn: string | Function, cb: Function, options?: ?Object, isRenderWatcher?: boolean) { ... if (typeof expOrFn === 'function') { this.getter = expOrFn } else { this.getter = parsePath(expOrFn) if (!this.getter) { this.getter = noop } } this.value = this.lazy ? undefined : this.get() }expOrFn,对于初始化用来渲染视图的 watcher 来说,就是render方法,对于computed来说就是表达式,对于watch才是key,而getter方法是用来取value的。最后调用了get()方法
get () { //将Dep.target设置为当前watcher实例 pushTarget(this) let value const vm = this.vm try { // 执行一次get 收集依赖 value = this.getter.call(vm, vm) } catch (e) { if (this.user) { handleError(e, vm, `getter for watcher "${this.expression}"`) } else { throw e } } finally { if (this.deep) { traverse(value) } popTarget() this.cleanupDeps() //清楚依赖 } return value }假如当前Watcher实例中 getter 是 render,当render遇到模板中的{{xxx}}表达式的时候,就是去读取 data.xxx,这个时候就触发 data.xxx 的 get方法,这个时候 get 中会执行Dep.depend(),而此时 Dep.target 就是当前 watcher ,然后调用 watcher.addDep()。也就将data.xxx 与 当前watcher 关联起来了
//watcher 的其他方法 //接收参数dep(Dep实例),让当前watcher订阅dep addDep (dep: Dep) { const id = dep.id if (!this.newDepIds.has(id)) { this.newDepIds.add(id) this.newDeps.push(dep) if (!this.depIds.has(id)) { //将watcher实例 也添加到 Dep实例中 dep.addSub(this) } } } //清楚对dep的订阅信息 cleanupDeps () { } //立刻运行watcher或者将watcher加入队列中 update () { if (this.lazy) { this.dirty = true } else if (this.sync) { this.run() } else { queueWatcher(this) } } //运行watcher,调用this.get()求值,然后触发回调 run () { if (this.active) { const value = this.get() if ( value !== this.value || isObject(value) || this.deep ) { const oldValue = this.value this.value = value if (this.user) { try { this.cb.call(this.vm, value, oldValue) } catch (e) { handleError(e, this.vm, `callback for watcher "${this.expression}"`) } } else { this.cb.call(this.vm, value, oldValue) } } } } //调用this.get()求值 evaluate () { this.value = this.get() this.dirty = false } //遍历this.deps,让当前watcher实例订阅所有dep depend () { let i = this.deps.length while (i--) { this.deps[i].depend() } } //去除当前watcher实例所有的订阅 teardown () { if (this.active) { if (!this.vm._isBeingDestroyed) { remove(this.vm._watchers, this) } let i = this.deps.length while (i--) { this.deps[i].removeSub(this) } this.active = false } }