feat(sfc): auto restore current instance after await statements in async setup()
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
import {
|
||||
getCurrentInstance,
|
||||
SetupContext,
|
||||
createSetupContext
|
||||
createSetupContext,
|
||||
setCurrentInstance
|
||||
} from './component'
|
||||
import { EmitFn, EmitsOptions } from './componentEmits'
|
||||
import {
|
||||
@@ -226,3 +227,17 @@ export function mergeDefaults(
|
||||
}
|
||||
return props
|
||||
}
|
||||
|
||||
/**
|
||||
* Runtime helper for storing and resuming current instance context in
|
||||
* async setup().
|
||||
* @internal
|
||||
*/
|
||||
export async function withAsyncContext<T>(
|
||||
awaitable: T | Promise<T>
|
||||
): Promise<T> {
|
||||
const ctx = getCurrentInstance()
|
||||
const res = await awaitable
|
||||
setCurrentInstance(ctx)
|
||||
return res
|
||||
}
|
||||
|
||||
@@ -48,12 +48,14 @@ export { defineAsyncComponent } from './apiAsyncComponent'
|
||||
// <script setup> API ----------------------------------------------------------
|
||||
|
||||
export {
|
||||
// macros runtime, for warnings only
|
||||
defineProps,
|
||||
defineEmits,
|
||||
defineExpose,
|
||||
withDefaults,
|
||||
// internal
|
||||
mergeDefaults,
|
||||
withAsyncContext,
|
||||
// deprecated
|
||||
defineEmit,
|
||||
useContext
|
||||
|
||||
Reference in New Issue
Block a user