关于javascript:Vue源码解读四更新策略

1次阅读

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

之前介绍过初始化时 Vue 对数据的响应式解决是利用了 Object.defifineProperty(),通过定义对象属性 getter 办法拦挡对象属性的拜访,进行依赖的收集,依赖收集的作用就是在数据变更的时候能告诉到相干依赖进行更新。

告诉更新

setter

当响应式数据产生变更时,会触发拦挡的 setter 函数,先来看看 setter:

// src/core/observer/index.js
export function defineReactive (
  obj: Object,
  key: string,
  val: any,
  customSetter?: ?Function,
  shallow?: boolean
) {
  // ...
  Object.defineProperty(obj, key, {
    enumerable: true,
    configurable: true,
    // ...
    // 劫持批改操作
    set: function reactiveSetter (newVal) {// 旧的 obj[key] 
      const value = getter ? getter.call(obj) : val
      // 如果新旧值一样,则间接 return,无需更新
      if (newVal === value || (newVal !== newVal && value !== value)) {return}
      /* eslint-enable no-self-compare */
      if (process.env.NODE_ENV !== 'production' && customSetter) {customSetter()
      }
      // setter 不存在阐明该属性是一个只读属性,间接 return
      if (getter && !setter) return
      // 设置新值
      if (setter) {setter.call(obj, newVal)
      } else {val = newVal}
      // 对新值进行察看,让新值也是响应式的
      childOb = !shallow && observe(newVal)
      // 依赖告诉更新
      dep.notify()}
  })
}

dep.notify()

  // src/core/observer/dep.js
  // 告诉更新
  notify () {const subs = this.subs.slice()
    if (process.env.NODE_ENV !== 'production' && !config.async) {subs.sort((a, b) => a.id - b.id)
    }
    for (let i = 0, l = subs.length; i < l; i++) {subs[i].update()}
  }

遍历 dep 中存储的 watcher,执行 watcher.update()

watcher.update()

// src/core/observer/watcher.js
export default class Watcher {
  // ...
  update () {
    /* istanbul ignore else */
    if (this.lazy) {
      // 懒执行时走这里,比方 computed watcher
      // 将 dirty 置为 true,计算属性的求值就会从新计算
      this.dirty = true
    } else if (this.sync) {
      // 同步执行,在应用 vm.$watch 或者 watch 选项时能够传一个 sync 选项,// 当为 true 时在数据更新时该 watcher 就不走异步更新队列,间接执行 this.run 办法进行更新
      // 这个属性在官网文档中没有呈现
      this.run()} else {
      // 更新时个别都这里,将 watcher 放入 watcher 队列
      queueWatcher(this)
    }
  }
}

queueWatcher

// src/core/observer/scheduler.js
const queue: Array<Watcher> = []
let has: {[key: number]: ?true } = {}
let waiting = false
let flushing = false
/**
 * 将 watcher 放入 queue 队列
 */
export function queueWatcher (watcher: Watcher) {
  const id = watcher.id
  // 如果 watcher 曾经存在,则跳过
  if (has[id] == null) {
    // 缓存 watcher.id,用于判断 watcher 是否曾经入队
    has[id] = true
    if (!flushing) {
      // 以后没有处于刷新队列状态,watcher 间接入队
      queue.push(watcher)
    } else {
      // 正在刷新队列,这时用户可能增加新的 watcher, 就会走到这里
      // 从后往前找,找到第一个 watcher.id 比以后队列中 watcher.id 大的地位,而后将本人插入到该地位。放弃队列是有序的。let i = queue.length - 1
      while (i > index && queue[i].id > watcher.id) {i--}
      queue.splice(i + 1, 0, watcher)
    }
    // waiting 保障了 nextTick 的调用只有一次
    if (!waiting) {
      waiting = true
      if (process.env.NODE_ENV !== 'production' && !config.async) {
        // 间接刷新调度队列
        // 个别不会走这儿,Vue 默认是异步执行,如果改为同步执行,性能会大打折扣
        flushSchedulerQueue()
        return
      }
      // nextTick => vm.$nextTick、Vue.nextTick
      nextTick(flushSchedulerQueue)
    }
  }
}

nextTick 等会再看,它的作用次要就是把 flushSchedulerQueue 应用异步工作去执行,先尝试用微工作,不反对的状况再用宏工作去执行。

那么先看看 flushSchedulerQueue 的作用:

flushSchedulerQueue

// src/core/observer/scheduler.js
function flushSchedulerQueue () {currentFlushTimestamp = getNow()
  flushing = true
  let watcher, id
  // 对队列做了从小到大的排序,目标:
  // 1. 组件的更新由父到子, 因为父组件在子组件之前被创立, 所以 watcher 的创立也是先父后子,执行程序也应该放弃先父后子。// 2. 一个组件的用户 watcher 先于渲染 watcher 执行,认为用户 watcher 创立先于渲染 watcher。// 3. 如果一个组件在父组件的 watcher 执行期间被销毁,那么它对应的 watcher 执行都能够被跳过,所以父组件的 watcher 应该先执行。queue.sort((a, b) => a.id - b.id)
  // 在遍历的时候每次都会对 queue.length 求值,因为在 watcher.run() 的时候,很可能用户会再次增加新的 watcher
  for (index = 0; index < queue.length; index++) {watcher = queue[index]
    // 执行 beforeUpdate 生命周期钩子,在 mount 阶段创立 Watcher 时传入
    if (watcher.before) {watcher.before()
    }
    // 将缓存的 watcher 革除
    id = watcher.id
    has[id] = null
    // 执行 watcher.run,最终触发更新函数
    watcher.run()
    // in dev build, check and stop circular updates.
    if (process.env.NODE_ENV !== 'production' && has[id] != null) {circular[id] = (circular[id] || 0) + 1
      if (circular[id] > MAX_UPDATE_COUNT) {
        warn(
          'You may have an infinite update loop' + (
            watcher.user
              ? `in watcher with expression "${watcher.expression}"`
              : `in a component render function.`
          ),
          watcher.vm
        )
        break
      }
    }
  }
  // 在重置状态之前保留队列的正本
  const activatedQueue = activatedChildren.slice()
  const updatedQueue = queue.slice()
  // 重置刷新队列状态
  resetSchedulerState()
  // keep-alive 组件相干
  callActivatedHooks(activatedQueue)
  // 执行 updated 生命周期钩子
  callUpdatedHooks(updatedQueue)
  // devtool hook
  /* istanbul ignore if */
  if (devtools && config.devtools) {devtools.emit('flush')
  }
}

/**
 * 把这些管制流程状态的一些变量复原到初始值,把 watcher 队列清空。*/
function resetSchedulerState () {
  index = queue.length = activatedChildren.length = 0
  has = {}
  if (process.env.NODE_ENV !== 'production') {circular = {}
  }
  waiting = flushing = false
}

/**
 * 由子组件到父组件顺次执行 updated 生命周期钩子
 */
function callUpdatedHooks (queue) {
  let i = queue.length
  while (i--) {const watcher = queue[i]
    const vm = watcher.vm
    if (vm._watcher === watcher && vm._isMounted && !vm._isDestroyed) {callHook(vm, 'updated')
    }
  }
}

下面代码能够看出 flushSchedulerQueue 的作用就是执行更新队列。通过 watcher.run() 触发最终的更新。

watcher.run()

// src/core/observer/watcher.js
export default class Watcher {
  constructor(
    vm: Component,
    expOrFn: string | Function,
    cb: Function,
    options?: ?Object,
    isRenderWatcher?: boolean
  ) {this.cb = cb} 
  run () {if (this.active) {
      // 调用 this.get 办法
      const value = this.get()
      if (
        value !== this.value ||  // 新旧值不相等
        isObject(value) ||   // 新值是对象
        this.deep   // deep 模式
      ) {
        // 更新旧值为新值
        const oldValue = this.value
        this.value = value
        if (this.user) {
          // 如果是用户 watcher
          const info = `callback for watcher "${this.expression}"`
          invokeWithErrorHandling(this.cb, this.vm, [value, oldValue], this.vm, info)
        } else {
          // 渲染 watcher,this.cb = noop,一个空函数
          this.cb.call(this.vm, value, oldValue)
        }
      }
    }
  }
}

这里有两种状况,当 this.usertrue 的时候代表用户 watcher,在之前介绍过也就是 user watcher , 否则执行渲染 watcher 的逻辑。

  • user watcher

invokeWithErrorHandling 接管的第一个参数就是咱们自定义侦听属性的回调函数,在初始化侦听属性 initWatch 办法过程中,实例化 new Watcher(vm, expOrFn, cb, options) 的时候传入。
第三个参数就是 [value, oldValue] (新值和旧值),这也就是为什么在侦听属性的回调函数中能取得新值和旧值。

// src/core/util/error.js
export function invokeWithErrorHandling (
  handler: Function,
  context: any,
  args: null | any[],
  vm: any,
  info: string
) {
  let res
  // 利用 try catch 做一些错误处理
  try {res = args ? handler.apply(context, args) : handler.call(context)
    if (res && !res._isVue && isPromise(res) && !res._handled) {res.catch(e => handleError(e, vm, info + ` (Promise/async)`))
      // issue #9511
      // avoid catch triggering multiple times when nested calls
      res._handled = true
    }
  } catch (e) {handleError(e, vm, info)
  }
  return res
}
  • 渲染 watcher

如果是渲染 watcher 则执行 this.cb.call(this.vm, value, oldValue)。渲染 Wather 的实例化是在挂载时 mountComponent 办法中执行的:

  //  src/core/instance/lifecycle.js
  new Watcher(vm, updateComponent, noop, {before () {if (vm._isMounted && !vm._isDestroyed) {callHook(vm, 'beforeUpdate')
      }
    }
  }, true /* isRenderWatcher */)

export function noop (a?: any, b?: any, c?: any) {} 是一个空函数,所以 this.cb.call(this.vm, value, oldValue),就是在执行一个空函数。

渲染 watcher 在执行 watcher.run 会调用 this.get(),也就会执行 this.getter.call(vm, vm)this.getter 理论就是实例化时传入的第二个参数 updateComponent

//  src/core/instance/lifecycle.js
updateComponent = () => {vm._update(vm._render(), hydrating)
}

所以这就是当咱们去批改组件相干的响应式数据的时候,会触发组件从新渲染的起因,接着就会进入 patch  的过程。

nextTick

后面介绍了 flushSchedulerQueue 的作用就是去执行更新队列,那么咱们看看 queueWatcher 中的这段代码是怎么回事:

nextTick(flushSchedulerQueue)

nextTick

// src/core/util/next-tick.js
const callbacks = []
let pending = false

export function nextTick (cb?: Function, ctx?: Object) {
  let _resolve
  // 用 callbacks 数组存储通过包装的 cb 函数
  callbacks.push(() => {if (cb) {
      // 用 try catch 包装回调函数,便于谬误捕捉
      try {cb.call(ctx)
      } catch (e) {handleError(e, ctx, 'nextTick')
      }
    } else if (_resolve) {_resolve(ctx)
    }
  })
  if (!pending) {
    pending = true
    timerFunc()}
  // $flow-disable-line
  if (!cb && typeof Promise !== 'undefined') {
    return new Promise(resolve => {_resolve = resolve})
  }
}

nextTick 第一个参数是一个回调函数,这里的回调函数对应的就是 flushSchedulerQueue 了。通过 try catch 将回调函数包装,用于谬误捕捉,而后将其放入 callbacks 中。

这里应用 callbacks 而不是间接在 nextTick 中执行回调函数的起因是保障在同一个 tick 内屡次执行 nextTick,不会开启多个异步工作,而把这些异步工作都压成一个同步工作,在下一个 tick 执行结束。

接下来当 pendingfalse 的时候执行 timerFuncpendingtrue,示意正在将工作放入浏览器的工作队列中;pendingfalse,示意工作曾经放入浏览器工作队列中了。

最初,nextTick 在没有传入 cb 回调函数的时候,会返回 promise,提供了一个 .then 的调用。

nextTick().then(() => {})

timerFunc

// src/core/util/next-tick.js
// 能够看到 timerFunc 的作用很简略,就是将 flushCallbacks 函数放入浏览器的异步工作队列中
let timerFunc
if (typeof Promise !== 'undefined' && isNative(Promise)) {const p = Promise.resolve()
  timerFunc = () => {
    // 首选 Promise
    p.then(flushCallbacks)
    /**
     * 在有问题的 UIWebViews 中,Promise.then 不会齐全中断,然而它可能会陷入怪异的状态,* 在这种状态下,回调被推入微工作队列,但队列没有被刷新,直到浏览器须要执行其余工作,例如解决一个计时器。* 因而,咱们能够通过增加空计时器来“强制”刷新微工作队列。*/
    if (isIOS) setTimeout(noop)
  }
  isUsingMicroTask = true
} else if (!isIE && typeof MutationObserver !== 'undefined' && (isNative(MutationObserver) ||
  // PhantomJS and iOS 7.x
  MutationObserver.toString() === '[object MutationObserverConstructor]'
)) {
  // 而后应用 MutationObserver
  let counter = 1
  const observer = new MutationObserver(flushCallbacks)
  const textNode = document.createTextNode(String(counter))
  observer.observe(textNode, {characterData: true})
  timerFunc = () => {counter = (counter + 1) % 2
    textNode.data = String(counter)
  }
  isUsingMicroTask = true
} else if (typeof setImmediate !== 'undefined' && isNative(setImmediate)) {
  // 而后 setImmediate,宏工作
  timerFunc = () => {setImmediate(flushCallbacks)
  }
} else {
  // 最初 setTimeout
  timerFunc = () => {setTimeout(flushCallbacks, 0)
  }
}

flushCallbacks

// src/core/util/next-tick.js
/**
 *   1、将 pending 置为 false
 *   2、清空 callbacks 数组
 *   3、执行 callbacks 数组中的每一个函数(比方 flushSchedulerQueue、用户调用 nextTick 传递的回调函数)*/
function flushCallbacks () {
  pending = false
  const copies = callbacks.slice(0)
  callbacks.length = 0
  for (let i = 0; i < copies.length; i++) {copies[i]()}
}

不论是全局 API Vue.nextTick,还是实例办法 vm.$nextTick,最初都是调用 next-tick.js 中的 nextTick 办法。

相干链接

Vue 源码解读(预):手写一个简易版 Vue

Vue 源码解读(一):筹备工作

Vue 源码解读(二):初始化和挂载

Vue 源码解读(三):响应式原理

Vue 源码解读(四):更新策略

[Vue 源码解读(五):render 和 VNode(待续)]()

[Vue 源码解读(六):update 和 patch(待续)]()

[Vue 源码解读(七):模板编译(待续)]()

如果感觉还对付的话,给个赞吧!!!也能够来我的集体博客逛逛 https://www.mingme.net/

正文完
 0