Home / File/ apiAsyncComponent.ts — vue Source File

apiAsyncComponent.ts — vue Source File

Architecture documentation for apiAsyncComponent.ts, a typescript file in the vue codebase. 1 imports, 0 dependents.

File typescript VueCore Instance 1 imports 3 functions

Entity Profile

Dependency Diagram

graph LR
  3c165a4c_f9eb_ba3a_77d3_78acf2fd83e0["apiAsyncComponent.ts"]
  6d8f8976_7066_720b_0d45_45fe42921eaf["util"]
  3c165a4c_f9eb_ba3a_77d3_78acf2fd83e0 --> 6d8f8976_7066_720b_0d45_45fe42921eaf
  style 3c165a4c_f9eb_ba3a_77d3_78acf2fd83e0 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import { warn, isFunction, isObject } from 'core/util'

interface AsyncComponentOptions {
  loader: Function
  loadingComponent?: any
  errorComponent?: any
  delay?: number
  timeout?: number
  suspensible?: boolean
  onError?: (
    error: Error,
    retry: () => void,
    fail: () => void,
    attempts: number
  ) => any
}

type AsyncComponentFactory = () => {
  component: Promise<any>
  loading?: any
  error?: any
  delay?: number
  timeout?: number
}

/**
 * v3-compatible async component API.
 * @internal the type is manually declared in <root>/types/v3-define-async-component.d.ts
 * because it relies on existing manual types
 */
export function defineAsyncComponent(
  source: (() => any) | AsyncComponentOptions
): AsyncComponentFactory {
  if (isFunction(source)) {
    source = { loader: source } as AsyncComponentOptions
  }

  const {
    loader,
    loadingComponent,
    errorComponent,
    delay = 200,
    timeout, // undefined = never times out
    suspensible = false, // in Vue 3 default is true
    onError: userOnError
  } = source

  if (__DEV__ && suspensible) {
    warn(
      `The suspensible option for async components is not supported in Vue2. It is ignored.`
    )
  }

  let pendingRequest: Promise<any> | null = null

  let retries = 0
  const retry = () => {
    retries++
    pendingRequest = null
    return load()
  }

  const load = (): Promise<any> => {
    let thisRequest: Promise<any>
    return (
      pendingRequest ||
      (thisRequest = pendingRequest =
        loader()
          .catch(err => {
            err = err instanceof Error ? err : new Error(String(err))
            if (userOnError) {
              return new Promise((resolve, reject) => {
                const userRetry = () => resolve(retry())
                const userFail = () => reject(err)
                userOnError(err, userRetry, userFail, retries + 1)
              })
            } else {
              throw err
            }
          })
          .then((comp: any) => {
            if (thisRequest !== pendingRequest && pendingRequest) {
              return pendingRequest
            }
            if (__DEV__ && !comp) {
              warn(
                `Async component loader resolved to undefined. ` +
                  `If you are using retry(), make sure to return its return value.`
              )
            }
            // interop module default
            if (
              comp &&
              (comp.__esModule || comp[Symbol.toStringTag] === 'Module')
            ) {
              comp = comp.default
            }
            if (__DEV__ && comp && !isObject(comp) && !isFunction(comp)) {
              throw new Error(`Invalid async component load result: ${comp}`)
            }
            return comp
          }))
    )
  }

  return () => {
    const component = load()

    return {
      component,
      delay,
      timeout,
      error: errorComponent,
      loading: loadingComponent
    }
  }
}

Domain

Subdomains

Dependencies

  • util

Frequently Asked Questions

What does apiAsyncComponent.ts do?
apiAsyncComponent.ts is a source file in the vue codebase, written in typescript. It belongs to the VueCore domain, Instance subdomain.
What functions are defined in apiAsyncComponent.ts?
apiAsyncComponent.ts defines 3 function(s): Promise, defineAsyncComponent, error.
What does apiAsyncComponent.ts depend on?
apiAsyncComponent.ts imports 1 module(s): util.
Where is apiAsyncComponent.ts in the architecture?
apiAsyncComponent.ts is located at src/v3/apiAsyncComponent.ts (domain: VueCore, subdomain: Instance, directory: src/v3).

Analyze Your Own Codebase

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

Try Supermodel Free