2020-03-22 04:01:08 +08:00
|
|
|
import {
|
|
|
|
PublicAPIComponent,
|
|
|
|
Component,
|
|
|
|
currentInstance,
|
2020-03-24 04:14:56 +08:00
|
|
|
ComponentInternalInstance,
|
|
|
|
isInSSRComponentSetup
|
2020-03-22 04:01:08 +08:00
|
|
|
} from './component'
|
2020-04-08 02:34:42 +08:00
|
|
|
import { isFunction, isObject } from '@vue/shared'
|
2020-03-22 04:01:08 +08:00
|
|
|
import { ComponentPublicInstance } from './componentProxy'
|
|
|
|
import { createVNode } from './vnode'
|
|
|
|
import { defineComponent } from './apiDefineComponent'
|
|
|
|
import { warn } from './warning'
|
|
|
|
import { ref } from '@vue/reactivity'
|
|
|
|
import { handleError, ErrorCodes } from './errorHandling'
|
|
|
|
|
|
|
|
export type AsyncComponentResolveResult<T = PublicAPIComponent> =
|
|
|
|
| T
|
|
|
|
| { default: T } // es modules
|
|
|
|
|
|
|
|
export type AsyncComponentLoader<T = any> = () => Promise<
|
|
|
|
AsyncComponentResolveResult<T>
|
|
|
|
>
|
|
|
|
|
|
|
|
export interface AsyncComponentOptions<T = any> {
|
|
|
|
loader: AsyncComponentLoader<T>
|
2020-03-27 08:58:31 +08:00
|
|
|
loadingComponent?: PublicAPIComponent
|
|
|
|
errorComponent?: PublicAPIComponent
|
2020-03-22 04:01:08 +08:00
|
|
|
delay?: number
|
|
|
|
timeout?: number
|
|
|
|
suspensible?: boolean
|
2020-04-08 02:34:42 +08:00
|
|
|
onError?: (
|
|
|
|
error: Error,
|
|
|
|
retry: () => void,
|
|
|
|
fail: () => void,
|
|
|
|
attempts: number
|
|
|
|
) => any
|
2020-03-22 04:01:08 +08:00
|
|
|
}
|
|
|
|
|
2020-03-26 23:59:54 +08:00
|
|
|
export function defineAsyncComponent<
|
2020-03-22 04:01:08 +08:00
|
|
|
T extends PublicAPIComponent = { new (): ComponentPublicInstance }
|
|
|
|
>(source: AsyncComponentLoader<T> | AsyncComponentOptions<T>): T {
|
|
|
|
if (isFunction(source)) {
|
|
|
|
source = { loader: source }
|
|
|
|
}
|
|
|
|
|
|
|
|
const {
|
|
|
|
loader,
|
2020-03-27 08:58:31 +08:00
|
|
|
loadingComponent: loadingComponent,
|
|
|
|
errorComponent: errorComponent,
|
2020-03-22 04:01:08 +08:00
|
|
|
delay = 200,
|
2020-03-27 08:58:31 +08:00
|
|
|
timeout, // undefined = never times out
|
2020-04-08 02:34:42 +08:00
|
|
|
suspensible = true,
|
|
|
|
onError: userOnError
|
2020-03-22 04:01:08 +08:00
|
|
|
} = source
|
|
|
|
|
|
|
|
let pendingRequest: Promise<Component> | null = null
|
|
|
|
let resolvedComp: Component | undefined
|
|
|
|
|
2020-03-27 08:58:31 +08:00
|
|
|
let retries = 0
|
2020-04-08 02:34:42 +08:00
|
|
|
const retry = () => {
|
2020-03-27 08:58:31 +08:00
|
|
|
retries++
|
|
|
|
pendingRequest = null
|
|
|
|
return load()
|
|
|
|
}
|
|
|
|
|
2020-03-22 04:01:08 +08:00
|
|
|
const load = (): Promise<Component> => {
|
2020-03-27 08:58:31 +08:00
|
|
|
let thisRequest: Promise<Component>
|
2020-03-22 04:01:08 +08:00
|
|
|
return (
|
|
|
|
pendingRequest ||
|
2020-03-27 08:58:31 +08:00
|
|
|
(thisRequest = pendingRequest = loader()
|
|
|
|
.catch(err => {
|
|
|
|
err = err instanceof Error ? err : new Error(String(err))
|
2020-04-08 02:34:42 +08:00
|
|
|
if (userOnError) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const userRetry = () => resolve(retry())
|
|
|
|
const userFail = () => reject(err)
|
|
|
|
userOnError(err, userRetry, userFail, retries + 1)
|
|
|
|
})
|
2020-03-27 08:58:31 +08:00
|
|
|
} 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}`)
|
|
|
|
}
|
|
|
|
resolvedComp = comp
|
|
|
|
return comp
|
|
|
|
}))
|
2020-03-22 04:01:08 +08:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return defineComponent({
|
2020-03-24 04:14:56 +08:00
|
|
|
__asyncLoader: load,
|
2020-03-22 04:01:08 +08:00
|
|
|
name: 'AsyncComponentWrapper',
|
|
|
|
setup() {
|
|
|
|
const instance = currentInstance!
|
|
|
|
|
|
|
|
// already resolved
|
|
|
|
if (resolvedComp) {
|
|
|
|
return () => createInnerComp(resolvedComp!, instance)
|
|
|
|
}
|
|
|
|
|
2020-03-24 04:14:56 +08:00
|
|
|
const onError = (err: Error) => {
|
|
|
|
pendingRequest = null
|
|
|
|
handleError(err, instance, ErrorCodes.ASYNC_COMPONENT_LOADER)
|
2020-03-22 04:01:08 +08:00
|
|
|
}
|
|
|
|
|
2020-03-24 04:14:56 +08:00
|
|
|
// suspense-controlled or SSR.
|
|
|
|
if (
|
2020-03-30 23:49:51 +08:00
|
|
|
(__FEATURE_SUSPENSE__ && suspensible && instance.suspense) ||
|
2020-03-24 04:14:56 +08:00
|
|
|
(__NODE_JS__ && isInSSRComponentSetup)
|
|
|
|
) {
|
|
|
|
return load()
|
|
|
|
.then(comp => {
|
|
|
|
return () => createInnerComp(comp, instance)
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
onError(err)
|
|
|
|
return () =>
|
|
|
|
errorComponent
|
|
|
|
? createVNode(errorComponent as Component, { error: err })
|
|
|
|
: null
|
|
|
|
})
|
2020-03-22 04:01:08 +08:00
|
|
|
}
|
2020-03-24 04:14:56 +08:00
|
|
|
|
2020-03-22 04:01:08 +08:00
|
|
|
const loaded = ref(false)
|
|
|
|
const error = ref()
|
|
|
|
const delayed = ref(!!delay)
|
|
|
|
|
|
|
|
if (delay) {
|
|
|
|
setTimeout(() => {
|
|
|
|
delayed.value = false
|
|
|
|
}, delay)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (timeout != null) {
|
|
|
|
setTimeout(() => {
|
|
|
|
if (!loaded.value) {
|
|
|
|
const err = new Error(
|
|
|
|
`Async component timed out after ${timeout}ms.`
|
|
|
|
)
|
2020-03-24 04:14:56 +08:00
|
|
|
onError(err)
|
|
|
|
error.value = err
|
2020-03-22 04:01:08 +08:00
|
|
|
}
|
|
|
|
}, timeout)
|
|
|
|
}
|
|
|
|
|
|
|
|
load()
|
|
|
|
.then(() => {
|
|
|
|
loaded.value = true
|
|
|
|
})
|
|
|
|
.catch(err => {
|
2020-03-24 04:14:56 +08:00
|
|
|
onError(err)
|
|
|
|
error.value = err
|
2020-03-22 04:01:08 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
if (loaded.value && resolvedComp) {
|
|
|
|
return createInnerComp(resolvedComp, instance)
|
|
|
|
} else if (error.value && errorComponent) {
|
|
|
|
return createVNode(errorComponent as Component, {
|
|
|
|
error: error.value
|
|
|
|
})
|
|
|
|
} else if (loadingComponent && !delayed.value) {
|
|
|
|
return createVNode(loadingComponent as Component)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}) as any
|
|
|
|
}
|
|
|
|
|
|
|
|
function createInnerComp(
|
|
|
|
comp: Component,
|
2020-04-07 05:37:47 +08:00
|
|
|
{ vnode: { props, children } }: ComponentInternalInstance
|
2020-03-22 04:01:08 +08:00
|
|
|
) {
|
2020-04-07 05:37:47 +08:00
|
|
|
return createVNode(comp, props, children)
|
2020-03-22 04:01:08 +08:00
|
|
|
}
|