pleroma-fe/src/services/resettable_async_component.js

31 lines
1 KiB
JavaScript
Raw Normal View History

import { defineAsyncComponent, shallowReactive, h } from 'vue'
/* 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.
*/
2026-01-06 16:22:52 +02:00
function getResettableAsyncComponent(asyncComponent, options) {
const asyncComponentFactory = () => () =>
defineAsyncComponent({
loader: asyncComponent,
...options,
})
const observe = shallowReactive({ c: asyncComponentFactory() })
return {
2026-01-06 16:22:52 +02:00
render() {
// emit event resetAsyncComponent to reloading
return h(observe.c(), {
2026-01-06 16:22:52 +02:00
onResetAsyncComponent() {
observe.c = asyncComponentFactory()
2026-01-06 16:22:52 +02:00
},
})
2026-01-06 16:22:52 +02:00
},
}
}
export default getResettableAsyncComponent