feat(sfc): auto restore current instance after await statements in async setup()

This commit is contained in:
Evan You
2021-06-29 09:24:12 -04:00
parent fd7fa6f694
commit 0240e82a38
5 changed files with 106 additions and 11 deletions

View File

@@ -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
}

View File

@@ -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