-
Notifications
You must be signed in to change notification settings - Fork 344
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add defineAsyncComponent API #644
Merged
Merged
Changes from 4 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
b1f582e
Create defineAsyncComponent.ts
mathe42 caae1e3
Update index.ts
mathe42 f2f0b21
better types, typeTests
mathe42 f5e424f
fix typeo
mathe42 87167ef
remove comments
mathe42 6aa7de3
add tests
mathe42 f006cc9
component type
mathe42 e527ea7
testfix
mathe42 a8833e4
rewrite tests to vue 2
mathe42 e0dabf6
fix tests
mathe42 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
import { isFunction, isObject, warn } from '../utils' | ||
import { VueProxy } from './componentProxy' | ||
import { AsyncComponent } from 'vue' | ||
|
||
import { | ||
ComponentOptionsWithoutProps, | ||
ComponentOptionsWithArrayProps, | ||
ComponentOptionsWithProps, | ||
} from './componentOptions' | ||
|
||
type ComponentOptions = | ||
| ComponentOptionsWithoutProps | ||
| ComponentOptionsWithArrayProps | ||
| ComponentOptionsWithProps | ||
|
||
type Component = VueProxy<any, any, any, any, any> | ||
|
||
type ComponentOrComponentOptions = ComponentOptions | Component | ||
|
||
export type AsyncComponentResolveResult<T = ComponentOrComponentOptions> = | ||
| T | ||
| { default: T } // es modules | ||
|
||
export type AsyncComponentLoader = () => Promise<AsyncComponentResolveResult> | ||
|
||
export interface AsyncComponentOptions { | ||
loader: AsyncComponentLoader | ||
loadingComponent?: ComponentOrComponentOptions | ||
errorComponent?: ComponentOrComponentOptions | ||
delay?: number | ||
timeout?: number | ||
suspensible?: boolean | ||
onError?: ( | ||
error: Error, | ||
retry: () => void, | ||
fail: () => void, | ||
attempts: number | ||
) => any | ||
} | ||
|
||
export function defineAsyncComponent( | ||
source: AsyncComponentLoader | AsyncComponentOptions | ||
): AsyncComponent { | ||
if (isFunction(source)) { | ||
source = { loader: source } | ||
} | ||
|
||
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 suspensiblbe option for async components is not supported in Vue2. It is ignored.` | ||
) | ||
} | ||
|
||
let pendingRequest: Promise<Component> | null = null | ||
|
||
let retries = 0 | ||
const retry = () => { | ||
retries++ | ||
pendingRequest = null | ||
return load() | ||
} | ||
|
||
const load = (): Promise<ComponentOrComponentOptions> => { | ||
let thisRequest: Promise<ComponentOrComponentOptions> | ||
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: component as any, // there is a type missmatch between vue2 type and the docs | ||
delay, | ||
timeout, | ||
error: errorComponent, | ||
loading: loadingComponent, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { AsyncComponent } from 'vue' | ||
import { defineAsyncComponent, defineComponent, expectType } from './index' | ||
|
||
function asyncComponent1() { | ||
return Promise.resolve().then(() => { | ||
return defineComponent({}) | ||
}) | ||
} | ||
|
||
function asyncComponent2() { | ||
return Promise.resolve().then(() => { | ||
return { | ||
template: 'ASYNC', | ||
} | ||
}) | ||
} | ||
|
||
const syncComponent1 = defineComponent({ | ||
template: '', | ||
}) | ||
|
||
const syncComponent2 = { | ||
template: '', | ||
} | ||
|
||
defineAsyncComponent(asyncComponent1) | ||
defineAsyncComponent(asyncComponent2) | ||
|
||
defineAsyncComponent({ | ||
loader: asyncComponent1, | ||
delay: 200, | ||
timeout: 3000, | ||
errorComponent: syncComponent1, | ||
loadingComponent: syncComponent1, | ||
}) | ||
|
||
defineAsyncComponent({ | ||
loader: asyncComponent2, | ||
delay: 200, | ||
timeout: 3000, | ||
errorComponent: syncComponent2, | ||
loadingComponent: syncComponent2, | ||
}) | ||
|
||
defineAsyncComponent( | ||
() => | ||
new Promise((resolve, reject) => { | ||
resolve(syncComponent1) | ||
}) | ||
) | ||
|
||
defineAsyncComponent( | ||
() => | ||
new Promise((resolve, reject) => { | ||
resolve(syncComponent2) | ||
}) | ||
) | ||
|
||
const component = defineAsyncComponent({ | ||
// The factory function | ||
loader: asyncComponent1, | ||
// A component to use while the async component is loading | ||
loadingComponent: defineComponent({}), | ||
// A component to use if the load fails | ||
errorComponent: defineComponent({}), | ||
// Delay before showing the loading component. Default: 200ms. | ||
delay: 200, | ||
// The error component will be displayed if a timeout is | ||
// provided and exceeded. Default: Infinity. | ||
timeout: 3000, | ||
// Defining if component is suspensible. Default: true. | ||
suspensible: false, | ||
/** | ||
* | ||
* @param {*} error Error message object | ||
* @param {*} retry A function that indicating whether the async component should retry when the loader promise rejects | ||
* @param {*} fail End of failure | ||
* @param {*} attempts Maximum allowed retries number | ||
*/ | ||
onError(error, retry, fail, attempts) { | ||
expectType<() => void>(retry) | ||
expectType<() => void>(fail) | ||
expectType<number>(attempts) | ||
expectType<Error>(error) | ||
}, | ||
}) | ||
|
||
expectType<AsyncComponent>(component) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Types of Vue2 are not correct in that place I created a PR for that vuejs/vue#11906