2022-03-28 20:55:26 +00:00
|
|
|
import { defineAsyncComponent, shallowReactive, h } from 'vue'
|
2020-05-25 13:11:05 +00:00
|
|
|
|
|
|
|
/* By default async components don't have any way to recover, if component is
|
|
|
|
* failed, it is failed forever. This helper tries to remedy that by recreating
|
|
|
|
* async component when retry is requested (by user). You need to emit the
|
|
|
|
* `resetAsyncComponent` event from child to reset the component. Generally,
|
|
|
|
* this should be done from error component but could be done from loading or
|
|
|
|
* actual target component itself if needs to be.
|
|
|
|
*/
|
|
|
|
function getResettableAsyncComponent (asyncComponent, options) {
|
2022-03-28 20:55:26 +00:00
|
|
|
const asyncComponentFactory = () => () => defineAsyncComponent({
|
|
|
|
loader: asyncComponent,
|
2020-05-25 13:11:05 +00:00
|
|
|
...options
|
|
|
|
})
|
|
|
|
|
2022-03-28 20:55:26 +00:00
|
|
|
const observe = shallowReactive({ c: asyncComponentFactory() })
|
2020-05-25 13:11:05 +00:00
|
|
|
|
|
|
|
return {
|
2022-03-28 20:55:26 +00:00
|
|
|
render () {
|
2020-05-25 13:11:05 +00:00
|
|
|
// emit event resetAsyncComponent to reloading
|
2022-03-28 20:55:26 +00:00
|
|
|
return h(observe.c(), {
|
|
|
|
onResetAsyncComponent () {
|
|
|
|
observe.c = asyncComponentFactory()
|
|
|
|
}
|
|
|
|
})
|
2020-05-25 13:11:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default getResettableAsyncComponent
|