• NextTick原理


    Node.js中的process.nextTick

    Node.js中有一个nextTick函数和Vue中的nextTick命名一致,很容易让人联想到一起(Node.js的Event Loop和浏览器的Event Loop有差异)。重点讲解一下Node.js中的nextTick的执行机制,简单的举个栗子:

    setTimeout(function() {
      console.log('timeout')
    })
    
    process.nextTick(function(){
      console.log('nextTick 1')
    })
    
    new Promise(function(resolve){
      console.log('Promise 1')
      resolve();
      console.log('Promise 2')
    }).then(function(){
      console.log('Promise Resolve')
    })
    
    process.nextTick(function(){
      console.log('nextTick 2')
    })
    

    在Node环境(10.3.0版本)中打印的顺序: Promise 1 > Promise 2 > nextTick 1 > nextTick 2 > Promise Resolve > timeout

    在Node.js的v10.x版本中对于process.nextTick的说明如下:

    The process.nextTick() method adds the callback to the "next tick queue". Once the current turn of the event loop turn runs to completion, all callbacks currently in the next tick queue will be called. This is not a simple alias to setTimeout(fn, 0). It is much more efficient. It runs before any additional I/O events (including timers) fire in subsequent ticks of the event loop.

    Vue的API命名nextTick

    Vue官方对nextTick这个API的描述:

    在下次 DOM 更新循环结束之后执行延迟回调。在修改数据之后立即使用这个方法,获取更新后的 DOM。

    // 修改数据
    vm.msg = 'Hello'
    // DOM 还没有更新
    Vue.nextTick(function () {
      // DOM 更新了
    })
    
    // 作为一个 Promise 使用 (2.1.0 起新增,详见接下来的提示)
    Vue.nextTick()
     .then(function () {
      // DOM 更新了
    })
    复制代码
    

    2.1.0 起新增:如果没有提供回调且在支持 Promise 的环境中,则返回一个 Promise。请注意 Vue 不自带 Promise 的 polyfill,所以如果你的目标浏览器不原生支持 Promise (IE:你们都看我干嘛),你得自己提供 polyfill。 0

    可能你还没有注意到,Vue 异步执行 DOM 更新。只要观察到数据变化,Vue 将开启一个队列,并缓冲在同一事件循环中发生的所有数据改变。如果同一个 watcher 被多次触发,只会被推入到队列中一次。这种在缓冲时去除重复数据对于避免不必要的计算和 DOM 操作上非常重要。然后,在下一个的事件循环“tick”中,Vue 刷新队列并执行实际 (已去重的) 工作。Vue 在内部尝试对异步队列使用原生的 Promise.then 和 MessageChannel,如果执行环境不支持,会采用 setTimeout(fn, 0) 代替。

    例如,当你设置 vm.someData = 'new value' ,该组件不会立即重新渲染。当刷新队列时,组件会在事件循环队列清空时的下一个“tick”更新。多数情况我们不需要关心这个过程,但是如果你想在 DOM 状态更新后做点什么,这就可能会有些棘手。虽然 Vue.js 通常鼓励开发人员沿着“数据驱动”的方式思考,避免直接接触 DOM,但是有时我们确实要这么做。为了在数据变化之后等待 Vue 完成更新 DOM ,可以在数据变化之后立即使用 Vue.nextTick(callback) 。这样回调函数在 DOM 更新完成后就会调用。

    Vue对于这个API的感情是曲折的,在2.4版本、2.5版本和2.6版本中对于nextTick进行反复变动,原因是浏览器对于微任务的不兼容性影响、微任务宏任务各自优缺点的权衡。

    img

    看以上流程图,如果Vue使用setTimeout宏任务函数,那么势必要等待UI渲染完成后的下一个宏任务执行,而如果Vue使用微任务函数,无需等待UI渲染完成才进行nextTick的回调函数操作,可以想象在JS引擎线程GUI渲染线程之间来回切换,以及等待GUI渲染线程的过程中,浏览器势必要消耗性能,这是一个严谨的框架完全需要考虑的事情。

    当然这里所说的只是nextTick执行用户回调之后的性能情况考虑,这中间当然不能忽略flushBatcherQueue更新Dom的操作,使用异步函数的另外一个作用当然是要确保同步代码执行完毕Dom更新性能优化(例如同步操作对响应式数据使用for循环更新一千次,那么这里只有一次DOM更新而不是一千次)。

    到了这里,对于Vue中nextTick函数的命名应该是了然于心了,当然这个命名不知道和Node.js的process.nextTick还有没有什么必然联系。

    Vue中NextTick源码(这里加了一些简单的注释说明)

    2.5版本

    /* @flow */
    /* globals MessageChannel */
    
    import { noop } from 'shared/util'
    import { handleError } from './error'
    import { isIOS, isNative } from './env'
    
    const callbacks = []
    let pending = false
    
    function flushCallbacks () {
      pending = false
      const copies = callbacks.slice(0)
      callbacks.length = 0
      for (let i = 0; i < copies.length; i++) {
        copies[i]()
      }
    }
    
    // 在2.4中使用了microtasks ,但是还是存在问题,
    // 在2.5版本中组合使用macrotasks和microtasks,组合使用的方式是对外暴露withMacroTask函数
    // Here we have async deferring wrappers using both microtasks and (macro) tasks.
    // In < 2.4 we used microtasks everywhere, but there are some scenarios where
    // microtasks have too high a priority and fire in between supposedly
    // sequential events (e.g. #4521, #6690) or even between bubbling of the same
    // event (#6566). However, using (macro) tasks everywhere also has subtle problems
    // when state is changed right before repaint (e.g. #6813, out-in transitions).
    // Here we use microtask by default, but expose a way to force (macro) task when
    // needed (e.g. in event handlers attached by v-on).
    
    // 2.5版本在nextTick中对于调用microtask(微任务)还是macrotask(宏任务)声明了两个不同的变量
    let microTimerFunc
    let macroTimerFunc
    
    // 默认使用microtask(微任务)
    let useMacroTask = false
    
    
    // 这里主要定义macrotask(宏任务)函数
    // macrotask(宏任务)的执行优先级
    // setImmediate -> MessageChannel -> setTimeout
    // setImmediate是最理想的选择
    // 最Low的状况是降级执行setTimeout
    
    // Determine (macro) task defer implementation.
    // Technically setImmediate should be the ideal choice, but it's only available
    // in IE. The only polyfill that consistently queues the callback after all DOM
    // events triggered in the same loop is by using MessageChannel.
    /* istanbul ignore if */
    if (typeof setImmediate !== 'undefined' && isNative(setImmediate)) {
      macroTimerFunc = () => {
        setImmediate(flushCallbacks)
      }
    } else if (typeof MessageChannel !== 'undefined' && (
      isNative(MessageChannel) ||
      // PhantomJS
      MessageChannel.toString() === '[object MessageChannelConstructor]'
    )) {
      const channel = new MessageChannel()
      const port = channel.port2
      channel.port1.onmessage = flushCallbacks
      macroTimerFunc = () => {
        port.postMessage(1)
      }
    } else {
      /* istanbul ignore next */
      macroTimerFunc = () => {
        setTimeout(flushCallbacks, 0)
      }
    }
    
    
    // 这里主要定义microtask(微任务)函数
    // microtask(微任务)的执行优先级
    // Promise -> macroTimerFunc
    // 如果原生不支持Promise,那么执行macrotask(宏任务)函数
    
    // Determine microtask defer implementation.
    /* istanbul ignore next, $flow-disable-line */
    if (typeof Promise !== 'undefined' && isNative(Promise)) {
      const p = Promise.resolve()
      microTimerFunc = () => {
        p.then(flushCallbacks)
        // in problematic UIWebViews, Promise.then doesn't completely break, but
        // it can get stuck in a weird state where callbacks are pushed into the
        // microtask queue but the queue isn't being flushed, until the browser
        // needs to do some other work, e.g. handle a timer. Therefore we can
        // "force" the microtask queue to be flushed by adding an empty timer.
        if (isIOS) setTimeout(noop)
      }
    } else {
      // fallback to macro
      microTimerFunc = macroTimerFunc
    }
    
    
    // 对外暴露withMacroTask 函数
    // 触发变化执行nextTick时强制执行macrotask(宏任务)函数
    
    /**
     * Wrap a function so that if any code inside triggers state change,
     * the changes are queued using a (macro) task instead of a microtask.
     */
    export function withMacroTask (fn: Function): Function {
      return fn._withTask || (fn._withTask = function () {
        useMacroTask = true
        try {
          return fn.apply(null, arguments)
        } finally {
          useMacroTask = false    
        }
      })
    }
    
    // 这里需要注意pending
    export function nextTick (cb?: Function, ctx?: Object) {
      let _resolve
      callbacks.push(() => {
        if (cb) {
          try {
            cb.call(ctx)
          } catch (e) {
            handleError(e, ctx, 'nextTick')
          }
        } else if (_resolve) {
          _resolve(ctx)
        }
      })
      if (!pending) {
        pending = true
        if (useMacroTask) {
          macroTimerFunc()
        } else {
          microTimerFunc()
        }
      }
      // $flow-disable-line
      if (!cb && typeof Promise !== 'undefined') {
        return new Promise(resolve => {
          _resolve = resolve
        })
      }
    }
    
    

    2.6版本

    /* @flow */
    /* globals MutationObserver */
    
    import { noop } from 'shared/util'
    import { handleError } from './error'
    import { isIE, isIOS, isNative } from './env'
    
    export let isUsingMicroTask = false
    
    const callbacks = []
    let pending = false
    
    function flushCallbacks () {
      pending = false
      const copies = callbacks.slice(0)
      callbacks.length = 0
      for (let i = 0; i < copies.length; i++) {
        copies[i]()
      }
    }
    
    // 在2.5版本中组合使用microtasks 和macrotasks,但是重绘的时候还是存在一些小问题,而且使用macrotasks在任务队列中会有几个特别奇怪的行为没办法避免,So又回到了之前的状态,在任何地方优先使用microtasks 。
    // Here we have async deferring wrappers using microtasks.
    // In 2.5 we used (macro) tasks (in combination with microtasks).
    // However, it has subtle problems when state is changed right before repaint
    // (e.g. #6813, out-in transitions).
    // Also, using (macro) tasks in event handler would cause some weird behaviors
    // that cannot be circumvented (e.g. #7109, #7153, #7546, #7834, #8109).
    // So we now use microtasks everywhere, again.
    // A major drawback of this tradeoff is that there are some scenarios
    // where microtasks have too high a priority and fire in between supposedly
    // sequential events (e.g. #4521, #6690, which have workarounds)
    // or even between bubbling of the same event (#6566).
    let timerFunc
    
    // The nextTick behavior leverages the microtask queue, which can be accessed
    // via either native Promise.then or MutationObserver.
    // MutationObserver has wider support, however it is seriously bugged in
    // UIWebView in iOS >= 9.3.3 when triggered in touch event handlers. It
    // completely stops working after triggering a few times... so, if native
    // Promise is available, we will use it:
    /* istanbul ignore next, $flow-disable-line */
    
    
    // task的执行优先级
    // Promise -> MutationObserver -> setImmediate -> setTimeout
    
    if (typeof Promise !== 'undefined' && isNative(Promise)) {
      const p = Promise.resolve()
      timerFunc = () => {
        p.then(flushCallbacks)
        // In problematic UIWebViews, Promise.then doesn't completely break, but
        // it can get stuck in a weird state where callbacks are pushed into the
        // microtask queue but the queue isn't being flushed, until the browser
        // needs to do some other work, e.g. handle a timer. Therefore we can
        // "force" the microtask queue to be flushed by adding an empty timer.
        if (isIOS) setTimeout(noop)
      }
      isUsingMicroTask = true
    } else if (!isIE && typeof MutationObserver !== 'undefined' && (
      isNative(MutationObserver) ||
      // PhantomJS and iOS 7.x
      MutationObserver.toString() === '[object MutationObserverConstructor]'
    )) {
      // Use MutationObserver where native Promise is not available,
      // e.g. PhantomJS, iOS7, Android 4.4
      // (#6466 MutationObserver is unreliable in IE11)
      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)) {
      // Fallback to setImmediate.
      // Techinically it leverages the (macro) task queue,
      // but it is still a better choice than setTimeout.
      timerFunc = () => {
        setImmediate(flushCallbacks)
      }
    } else {
      // Fallback to setTimeout.
      timerFunc = () => {
        setTimeout(flushCallbacks, 0)
      }
    }
    
    export function nextTick (cb?: Function, ctx?: Object) {
      let _resolve
      callbacks.push(() => {
        if (cb) {
          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么 https://juejin.cn/post/6844903843197616136

  • 相关阅读:
    百度地图地址解析/逆地址解析
    Oracle表空间创建要点
    dubbo——providers
    dubbo——常用标签属性
    dubbo——spring初始化
    dubbo——RPC
    mybatis——datasource
    redis——再补充
    mybatis——缓存
    mybatis——Executor
  • 原文地址:https://www.cnblogs.com/Scooby/p/16248673.html
Copyright © 2020-2023  润新知