Skip to content

Commit

Permalink
feat(asyncComponent): add onError option for defineAsyncComponent
Browse files Browse the repository at this point in the history
BREAKING CHANGE: `retryWhen` and `maxRetries` options for
`defineAsyncComponent` has been replaced by the more flexible `onError`
option, per vuejs/rfcs#148
  • Loading branch information
yyx990803 committed Apr 7, 2020
1 parent f87d6b5 commit e804463
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 14 deletions.
27 changes: 22 additions & 5 deletions packages/runtime-core/__tests__/apiAsyncComponent.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,13 @@ describe('api: defineAsyncComponent', () => {
reject = _reject
})
},
retryWhen: error => error.message.match(/foo/)
onError(error, retry, fail) {
if (error.message.match(/foo/)) {
retry()
} else {
fail()
}
}
})

const root = nodeOps.createElement('div')
Expand Down Expand Up @@ -526,7 +532,13 @@ describe('api: defineAsyncComponent', () => {
reject = _reject
})
},
retryWhen: error => error.message.match(/bar/)
onError(error, retry, fail) {
if (error.message.match(/bar/)) {
retry()
} else {
fail()
}
}
})

const root = nodeOps.createElement('div')
Expand All @@ -549,7 +561,7 @@ describe('api: defineAsyncComponent', () => {
expect(serializeInner(root)).toBe('<!---->')
})

test('retry (fail w/ maxRetries)', async () => {
test('retry (fail w/ max retry attempts)', async () => {
let loaderCallCount = 0
let reject: (e: Error) => void

Expand All @@ -560,8 +572,13 @@ describe('api: defineAsyncComponent', () => {
reject = _reject
})
},
retryWhen: error => error.message.match(/foo/),
maxRetries: 1
onError(error, retry, fail, attempts) {
if (error.message.match(/foo/) && attempts <= 1) {
retry()
} else {
fail()
}
}
})

const root = nodeOps.createElement('div')
Expand Down
25 changes: 16 additions & 9 deletions packages/runtime-core/src/apiAsyncComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
ComponentInternalInstance,
isInSSRComponentSetup
} from './component'
import { isFunction, isObject, NO } from '@vue/shared'
import { isFunction, isObject } from '@vue/shared'
import { ComponentPublicInstance } from './componentProxy'
import { createVNode } from './vnode'
import { defineComponent } from './apiDefineComponent'
Expand All @@ -27,9 +27,13 @@ export interface AsyncComponentOptions<T = any> {
errorComponent?: PublicAPIComponent
delay?: number
timeout?: number
retryWhen?: (error: Error) => any
maxRetries?: number
suspensible?: boolean
onError?: (
error: Error,
retry: () => void,
fail: () => void,
attempts: number
) => any
}

export function defineAsyncComponent<
Expand All @@ -45,16 +49,15 @@ export function defineAsyncComponent<
errorComponent: errorComponent,
delay = 200,
timeout, // undefined = never times out
retryWhen = NO,
maxRetries = 3,
suspensible = true
suspensible = true,
onError: userOnError
} = source

let pendingRequest: Promise<Component> | null = null
let resolvedComp: Component | undefined

let retries = 0
const retry = (error?: unknown) => {
const retry = () => {
retries++
pendingRequest = null
return load()
Expand All @@ -67,8 +70,12 @@ export function defineAsyncComponent<
(thisRequest = pendingRequest = loader()
.catch(err => {
err = err instanceof Error ? err : new Error(String(err))
if (retryWhen(err) && retries < maxRetries) {
return retry(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
}
Expand Down

0 comments on commit e804463

Please sign in to comment.