Home / File/ next-tick.ts — vue Source File

next-tick.ts — vue Source File

Architecture documentation for next-tick.ts, a typescript file in the vue codebase. 5 imports, 0 dependents.

File typescript CompilerSFC ScriptAnalyzer 5 imports 3 functions

Entity Profile

Dependency Diagram

graph LR
  6a3cc423_2c8d_a3ad_7c16_364ff040a3cb["next-tick.ts"]
  34ce4c45_a0c3_08f3_7cbe_679778705432["error.ts"]
  6a3cc423_2c8d_a3ad_7c16_364ff040a3cb --> 34ce4c45_a0c3_08f3_7cbe_679778705432
  4935dbd4_998d_c5cd_41c0_2e16500a201c["handleError"]
  6a3cc423_2c8d_a3ad_7c16_364ff040a3cb --> 4935dbd4_998d_c5cd_41c0_2e16500a201c
  193753e6_b34c_6e61_7eaf_22084e4e0013["env.ts"]
  6a3cc423_2c8d_a3ad_7c16_364ff040a3cb --> 193753e6_b34c_6e61_7eaf_22084e4e0013
  79fc8de3_c290_2d2b_ada1_8908272616fd["isNative"]
  6a3cc423_2c8d_a3ad_7c16_364ff040a3cb --> 79fc8de3_c290_2d2b_ada1_8908272616fd
  156bf2e1_8a13_f22d_5437_54f14bcef8fa["util"]
  6a3cc423_2c8d_a3ad_7c16_364ff040a3cb --> 156bf2e1_8a13_f22d_5437_54f14bcef8fa
  style 6a3cc423_2c8d_a3ad_7c16_364ff040a3cb fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

/* globals MutationObserver */

import { noop } from 'shared/util'
import { handleError } from './error'
import { isIE, isIOS, isNative } from './env'

export let isUsingMicroTask = false

const callbacks: Array<Function> = []
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 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 */
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.
  // Technically 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(): Promise<void>
export function nextTick<T>(this: T, cb: (this: T, ...args: any[]) => any): void
export function nextTick<T>(cb: (this: T, ...args: any[]) => any, ctx: T): void
/**
 * @internal
 */
export function nextTick(cb?: (...args: any[]) => any, ctx?: object) {
  let _resolve
  callbacks.push(() => {
    if (cb) {
      try {
        cb.call(ctx)
      } catch (e: any) {
        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
    })
  }
}

Domain

Subdomains

Frequently Asked Questions

What does next-tick.ts do?
next-tick.ts is a source file in the vue codebase, written in typescript. It belongs to the CompilerSFC domain, ScriptAnalyzer subdomain.
What functions are defined in next-tick.ts?
next-tick.ts defines 3 function(s): flushCallbacks, nextTick, timerFunc.
What does next-tick.ts depend on?
next-tick.ts imports 5 module(s): env.ts, error.ts, handleError, isNative, util.
Where is next-tick.ts in the architecture?
next-tick.ts is located at src/core/util/next-tick.ts (domain: CompilerSFC, subdomain: ScriptAnalyzer, directory: src/core/util).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free