text: /* @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]()
    }
  }
  
  // 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).
  let microTimerFunc
  let macroTimerFunc
  let useMacroTask = false
  
  // 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)
    }
  }
  
  // Determine microtask defer implementation.
  /* istanbul ignore next, $flow-disable-line */
  if (typeof Promise !== 'undefined' && isNative(Promise)) {
    const p = Promise.resolve()
    microTimerFunc = () => {
      p.then(flushCallbacks) // 加入到微任务队列, 此时可能 watcher 的 update 正在执行第一个 update 以及其内的 queueWatcher
      // 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
  }
  
  /**
   * 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    
      }
    })
  }
  
  export function nextTick (cb?: Function, ctx?: Object) {
    let _resolve
    callbacks.push(() => { // 词法作用域, 函数在被书写的时候即确定了变量所在的位置, 不会在执行的时候再查找变量, 因此虽然现在未执行 callback 中的 cb, 即使后面 cb 变了, callback 执行的也还是当前调用的 cb
      if (cb) {
        try {
          cb.call(ctx) // cb 为 flushSchedulerQueue 函数, 用来处理 queue 队列
        } catch (e) {
          handleError(e, ctx, 'nextTick')
        }
      } else if (_resolve) {
        _resolve(ctx)
      }
    })
    if (!pending) { // 多次调用, 先放到 callback 中, 即使第一次已经调用了 microTimerFunc(), 但由于后者是 异步微任务, 因此晚于同步任务(即多次调用 nextTick) 执行
      pending = true
      if (useMacroTask) {
        macroTimerFunc()
      } else {
        microTimerFunc()
      }
    }
    // $flow-disable-line
    if (!cb && typeof Promise !== 'undefined') { // 这条最好放上面, 不然 _resolve 猛一看 undefined 👎
      return new Promise(resolve => {
        _resolve = resolve
      })
    }
  }
  