Vue会把普通对象变成响应式对象,响应式对象getter相关的逻辑就是依赖收集
defineReactive
/*** Define a reactive property on an Object.*/export function defineReactive (obj: Object,key: string,val: any,customSetter?: ?Function,shallow?: boolean) {// 实例化一个Deo的实例const dep = new Dep()// ...let childOb = !shallow && observe(val)Object.defineProperty(obj, key, {enumerable: true,configurable: true,get: function reactiveGetter () {const value = getter ? getter.call(obj) : valif (Dep.target) {dep.depend() // 做依赖收集if (childOb) {childOb.dep.depend()if (Array.isArray(value)) {dependArray(value)}}}return value},set: function reactiveSetter (newVal) {// ...}})}
Dep
Dep是整个getter依赖收集的核心
定义在src/core/observer/dep.js中
/* @flow */import type Watcher from './watcher'import { remove } from '../util/index'import config from '../config'let uid = 0/*** A dep is an observable that can have multiple* directives subscribing to it.*/export default class Dep {// 静态属性,全局唯一Watcherstatic target: ?Watcher; // 在同一时间只能有一个全局的Watcher被计算id: number;subs: Array<Watcher>; // Watcher数组constructor () {this.id = uid++this.subs = []}addSub (sub: Watcher) {this.subs.push(sub)}removeSub (sub: Watcher) {remove(this.subs, sub)}depend () {if (Dep.target) {Dep.target.addDep(this)}}notify () {// stabilize the subscriber list firstconst subs = this.subs.slice()if (process.env.NODE_ENV !== 'production' && !config.async) {// subs aren't sorted in scheduler if not running async// we need to sort them now to make sure they fire in correct// ordersubs.sort((a, b) => a.id - b.id)}for (let i = 0, l = subs.length; i < l; i++) {subs[i].update()}}}// The current target watcher being evaluated.// This is globally unique because only one watcher// can be evaluated at a time.Dep.target = nullconst targetStack = []export function pushTarget (target: ?Watcher) {targetStack.push(target)Dep.target = target}export function popTarget () {targetStack.pop()Dep.target = targetStack[targetStack.length - 1]}
Dep实际上是对Watcher的一种管理,Dep脱离Watcher单独存在是没有意义的
Watcher
Watcher的实现定义在src/core/observer/watcher.js中
let uid = 0/*** A watcher parses an expression, collects dependencies,* and fires callback when the expression value changes.* This is used for both the $watch() api and directives.*/export default class Watcher {vm: Component;expression: string;cb: Function;id: number;deep: boolean;user: boolean;lazy: boolean;sync: boolean;dirty: boolean;active: boolean;deps: Array<Dep>;newDeps: Array<Dep>;depIds: SimpleSet;newDepIds: SimpleSet;before: ?Function;getter: Function;value: any;constructor (vm: Component,expOrFn: string | Function,cb: Function,options?: ?Object,isRenderWatcher?: boolean) {this.vm = vmif (isRenderWatcher) {vm._watcher = this}vm._watchers.push(this)// optionsif (options) {this.deep = !!options.deepthis.user = !!options.userthis.lazy = !!options.lazythis.sync = !!options.syncthis.before = options.before} else {this.deep = this.user = this.lazy = this.sync = false}this.cb = cbthis.id = ++uid // uid for batchingthis.active = truethis.dirty = this.lazy // for lazy watchersthis.deps = [] // 表示Watcher实例特有的Dep实例的数组this.newDeps = [] // 表示Watcher实例特有的Dep实例的数组this.depIds = new Set() // 代表this.deps的idthis.newDepIds = new Set() // 代表this.newDeps的idthis.expression = process.env.NODE_ENV !== 'production'? expOrFn.toString(): ''// parse expression for getterif (typeof expOrFn === 'function') {this.getter = expOrFn} else {this.getter = parsePath(expOrFn)if (!this.getter) {this.getter = noopprocess.env.NODE_ENV !== 'production' && warn(`Failed watching path: "${expOrFn}" ` +'Watcher only accepts simple dot-delimited paths. ' +'For full control, use a function instead.',vm)}}this.value = this.lazy? undefined: this.get()}/*** Evaluate the getter, and re-collect dependencies.*/get () {pushTarget(this)let valueconst vm = this.vmtry {value = this.getter.call(vm, vm)} catch (e) {if (this.user) {handleError(e, vm, `getter for watcher "${this.expression}"`)} else {throw e}} finally {// "touch" every property so they are all tracked as// dependencies for deep watchingif (this.deep) {traverse(value)}popTarget()this.cleanupDeps()}return value}/*** Add a dependency to this directive.*/addDep (dep: Dep) {const id = dep.idif (!this.newDepIds.has(id)) {this.newDepIds.add(id)this.newDeps.push(dep)if (!this.depIds.has(id)) {dep.addSub(this)}}}/*** Clean up for dependency collection.*/cleanupDeps () {let i = this.deps.lengthwhile (i--) {const dep = this.deps[i]if (!this.newDepIds.has(dep.id)) {dep.removeSub(this)}}let tmp = this.depIdsthis.depIds = this.newDepIdsthis.newDepIds = tmpthis.newDepIds.clear()tmp = this.depsthis.deps = this.newDepsthis.newDeps = tmpthis.newDeps.length = 0}/*** Subscriber interface.* Will be called when a dependency changes.*/update () {/* istanbul ignore else */if (this.lazy) {this.dirty = true} else if (this.sync) {this.run()} else {queueWatcher(this)}}/*** Scheduler job interface.* Will be called by the scheduler.*/run () {if (this.active) {const value = this.get()if (value !== this.value ||// Deep watchers and watchers on Object/Arrays should fire even// when the value is the same, because the value may// have mutated.isObject(value) ||this.deep) {// set new valueconst oldValue = this.valuethis.value = valueif (this.user) {const info = `callback for watcher "${this.expression}"`invokeWithErrorHandling(this.cb, this.vm, [value, oldValue], this.vm, info)} else {this.cb.call(this.vm, value, oldValue)}}}}/*** Evaluate the value of the watcher.* This only gets called for lazy watchers.*/evaluate () {this.value = this.get()this.dirty = false}/*** Depend on all deps collected by this watcher.*/depend () {let i = this.deps.lengthwhile (i--) {this.deps[i].depend()}}/*** Remove self from all dependencies' subscriber list.*/teardown () {if (this.active) {// remove self from vm's watcher list// this is a somewhat expensive operation so we skip it// if the vm is being destroyed.if (!this.vm._isBeingDestroyed) {remove(this.vm._watchers, this)}let i = this.deps.lengthwhile (i--) {this.deps[i].removeSub(this)}this.active = false}}}
过程分析
Vue的mount过程是通过mountComponent函数,有如下一段重要的逻辑
updateComponent = () => {vm._update(vm._render(), hydrating)}new Watcher(vm, updateComponent, noop, {before () {if (vm._isMounted) {callHook(vm, 'beforeUpdate')}}}, true /* isRenderWatcher */)
当实例化一个渲染watcher的时候首先进入watcher的构造函数,然后会执行this.get()方法进入get函数,接着执行
pushTarget(this)
pushTarget方法定义在src/core/observer/dep.js中
// 把Dep.target赋值为当前的渲染watcher并压栈(为了恢复用)export function pushTarget (target: ?Watcher) {targetStack.push(target)Dep.target = target}
接着又执行
value = this.getter.call(vm, vm) // this.getter对应就是updateComponent函数
实际上就是在执行
vm._update(vm._render(), hydrating) // 会先执行vm.render()方法,这个方法会生成渲染VNode,并且在这个过程中会对vm上的数据进行访问,这个时候就触发了数据对象的getter
每个对象值的getter都持有一个dep,在触发getter的时候会调用dep.depend()方法,也就会执行Dep.target.addDep(this)
Dep.target已经被赋值为渲染watcher,那么就执行到addDep方法
/*** Add a dependency to this directive.*/addDep (dep: Dep) {const id = dep.idif (!this.newDepIds.has(id)) {this.newDepIds.add(id)this.newDeps.push(dep)if (!this.depIds.has(id)) { // 保证同一数据不会被添加多次dep.addSub(this)}}}
执行 dep.addSub(this) 后就会执行 this.subs.push(sub),也就是说把当前的watcher订阅到这个数据持有的dep的subs中,这个的目的是为后续数据变化时能通知到哪些subs做准备
在vm.render()过程中会触发所有数据的getter,这样实际上已经完成了一个依赖收集的过程
其实并没有,在完成依赖收集后还有内容
// "touch" every property so they are all tracked as// dependencies for deep watching// 递归去访问value,触发它所有子项的getterif (this.deep) {traverse(value)}popTarget()this.cleanupDeps()
popTarget方法定义在src/core/observer/dep.js中
// 把Dep.target恢复成上一个状态,因为当前vm的数据依赖收集已经完成,那么对应的渲染Dep.target也需要改变export function popTarget () {targetStack.pop()Dep.target = targetStack[targetStack.length - 1]}
cleanupDeps依赖清空
Vue 是数据驱动的,所以每次数据变化都会重新 render,那么 vm._render() 方法又会再次执行,并再次触发数据的 getters,所以 Watcher 在构造函数中会初始化 2 个 Dep 实例数组,newDeps 表示新添加的 Dep 实例数组,而 deps 表示上一次添加的 Dep 实例数组
/*** Clean up for dependency collection.*/cleanupDeps () {let i = this.deps.lengthwhile (i--) {const dep = this.deps[i]if (!this.newDepIds.has(dep.id)) {dep.removeSub(this) // 移除对dep.subs数组中Watcher的订阅}}// 把newDepIds和depIds交换let tmp = this.depIdsthis.depIds = this.newDepIdsthis.newDepIds = tmp// 把newDepIds清空this.newDepIds.clear()// 把newDeps和deps交换tmp = this.depsthis.deps = this.newDepsthis.newDeps = tmp// 把newDeps清空this.newDeps.length = 0}
为什么需要做 deps 订阅的移除呢,在添加 deps 的订阅过程,已经能通过 id 去重避免重复订阅了
考虑到一种场景,模板会根据 v-if 去渲染不同子模板 a 和 b,当满足某种条件的时候渲染 a 的时候,会访问到 a 中的数据,这时候对 a 使用的数据添加了 getter,做了依赖收集,那么当去修改 a 的数据的时候,理应通知到这些订阅者。那么如果改变了条件渲染了 b 模板,又会对 b 使用的数据添加了 getter,如果没有依赖移除的过程,那么这时候会去修改 a 模板的数据,会通知 a 数据的订阅的回调,这显然是有浪费的
因此 Vue 设计了在每次添加完新的订阅,会移除掉旧的订阅,这样就保证了在刚才的场景中,如果渲染 b 模板的时候去修改 a 模板的数据,a 数据订阅回调已经被移除了,所以不会有任何浪费
