Vue源码学习—生命周期图示注释

12次阅读

共计 4650 个字符,预计需要花费 12 分钟才能阅读完成。

这篇文章在于总结前段时间对于 Vue 源码的粗略学习,大致讲述一个简单的 vue 实例从创建到更新再到销毁的过程。涉及 Vue 从 html 结构到 ast 对象;再从 render 函数到 virtual dom;以及 vue 的核心 Observer + dep + Watcher 对象所构成的数据驱动视图更新的逻辑;以及最后更新页面所涉及的 diff 算法等等。Vue 博大精深,小子由衷敬畏和仰慕。能力有限,各方面均是个人肤浅认知。记录一些个人学习理解心得,有不当的地方请指正。
【生命周期图注释 – 占位,后面补上】

Vue 的 Observer + dep + watcher
Observer
Observer 在创建的时候,给传入的对象进行封装,返回一个包含 value,dep(from new Dep()),__ob__等属性的 Ob 实例。Ob 对象对传入的 value 进行深层次的改造,给 value 以及属性中是对象或者数组的项进行改造,使他们可以被观测。对象执行 defineReactive(下文解析);数组改写部分原型方法,使他们可以被观测。
export class Observer {
value: any;
dep: Dep;
vmCount: number; // number of vms that have this object as root $data

constructor (value: any) {
this.value = value
this.dep = new Dep()
this.vmCount = 0
// 定义不可枚举的属性__ob__
def(value, ‘__ob__’, this) // 给 data 添加__ob__属性,返回 observer 对象
// root value 是个对象,这个 if 针对属性值
if (Array.isArray(value)) {
// 原型链绑定到改造后的一些方法,是对象操作可被观测
if (hasProto) {
protoAugment(value, arrayMethods)
} else {
copyAugment(value, arrayMethods, arrayKeys)
}
this.observeArray(value)
} else {
this.walk(value)
}
}

/**
* Walk through all properties and convert them into
* getter/setters. This method should only be called when
* value type is Object.
*/
walk (obj: Object) {
const keys = Object.keys(obj)
for (let i = 0; i < keys.length; i++) {
defineReactive(obj, keys[i])
}
}

/**
* Observe a list of Array items.
*/
observeArray (items: Array<any>) {
for (let i = 0, l = items.length; i < l; i++) {
observe(items[i])
}
}
}
通过 Object.defineProperty 在被观测对象的读取和写入过程中执行一些操作,用来触发视图更新。关键步骤有三点:1. 给每一个被观测的属性或者对象利用闭包创建一个 Dep 对象供 get 或者 set 函数使用。2. 存在观测对象的前提下(vm 的 render,update 过程,以及 compute,watch 过程等等,在读取属性值的时候,执行 dep.depend(大致作用就是将当前 target 的 watcher 对象存入 dep.subs);3. 存在观测对象的前提下, 在写入属性值的时候,执行 dep.notify(), 这个方法的大致作用是在被观测对象发生变化时,取出保存在 dep.subs 里面的 watcher 对象,执行这些对象的 update 方法,去更新依赖。这个地方存在一个个人觉得很优秀的设计,就是视图的更新是调用 nextTick 函数并且队列更新视图。传送门 –Vue 异步更新队列大致作用就是异步队列更新视图,达到性能以及效率上的提升。
defineReactive
/**
* Define a reactive property on an Object.
*/
export function defineReactive (
obj: Object,
key: string,
val: any,
customSetter?: ?Function,
shallow?: boolean
) {
const dep = new Dep()

const property = Object.getOwnPropertyDescriptor(obj, key)
if (property && property.configurable === false) {
return
}

// cater for pre-defined getter/setters
const getter = property && property.get
const setter = property && property.set
if ((!getter || setter) && arguments.length === 2) {
val = obj[key]
}

let childOb = !shallow && observe(val) // 作为 props 的子对象不再 ob, observe 返回 ob 对象
Object.defineProperty(obj, key, {
enumerable: true,
configurable: true,
get: function reactiveGetter () {
const value = getter ? getter.call(obj) : val
if (Dep.target) {
dep.depend()
if (childOb) {
childOb.dep.depend()
if (Array.isArray(value)) {
dependArray(value)
}
}
}
return value
},
set: function reactiveSetter (newVal) {
const value = getter ? getter.call(obj) : val
/* eslint-disable no-self-compare */
if (newVal === value || (newVal !== newVal && value !== value)) {
return
}
/* eslint-enable no-self-compare */
if (process.env.NODE_ENV !== ‘production’ && customSetter) {
customSetter()
}
// #7981: for accessor properties without setter
if (getter && !setter) return
if (setter) {
setter.call(obj, newVal)
} else {
val = newVal
}
childOb = !shallow && observe(newVal)
dep.notify() // 通知更新 this.subs.update
}
})
}
Dep
Dep 对象作为 Obverser 对象与 Watcher 对象的中介者,起到关联两个对象,统一把控变化的作用。1. 在被观测属性被读取的时候,且是需要被观测的(被观测对象的操作,后文如无特殊说明,都在这个前提下, 首先检测当前的 watcher 对象是否在已经在当前 dep 对象的 subs 里面,不在则添加。2.notify,是在写入观测者对象之后,触发视图更新的操作。3.Dep.target, 所有 dep 对象公用一个 target。保证同一时间只有一个目标对象在被计算。
export default class Dep {
static target: ?Watcher;
id: number;
subs: Array<Watcher>;

constructor () {
this.id = uid++
this.subs = []
}

addSub (sub: Watcher) {
this.subs.push(sub) // Watcher
}

removeSub (sub: Watcher) {
remove(this.subs, sub)
}

depend () {
if (Dep.target) {
// target 为 Watcher 对象
Dep.target.addDep(this) // this 当前观测者持有的 dep 对象
}
}

notify () {
// stabilize the subscriber list first
const 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
// order
subs.sort((a, b) => a.id – b.id)
}
for (let i = 0, l = subs.length; i < l; i++) {
subs[i].update() // watcher.update
}
}
}

// The current target watcher being evaluated.
// This is globally unique because only one watcher
// can be evaluated at a time.
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]
}

Wathcer
addDep 函数:addDep 的作用是,保证在向 dep 对象添加 wathcer 的时候,一个 depid 在一轮观测中只被添加一次。这个管控过程,在 watcher 里面使用 set 对象维护。update 函数:用来队列触发视图更新
/**
* 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 {
// 代码过长,只展文中提到的部分
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)) {
dep.addSub(this) // this = > Watcher
}
}
}
/**
* 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)
}
}
}

正文完
 0