fix: prevent withAsyncContext currentInstance leak in edge cases

This commit is contained in:
Evan You
2021-06-29 14:21:31 -04:00
parent 0240e82a38
commit 9ee41e14d2
3 changed files with 170 additions and 33 deletions

View File

@@ -231,13 +231,20 @@ export function mergeDefaults(
/**
* 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)
setCurrentInstance(null) // unset after storing instance
if (__DEV__ && !ctx) {
warn(`withAsyncContext() called when there is no active context instance.`)
}
let res: T
try {
res = await awaitable
} finally {
setCurrentInstance(ctx)
}
return res
}

View File

@@ -623,6 +623,11 @@ function setupStatefulComponent(
currentInstance = null
if (isPromise(setupResult)) {
const unsetInstance = () => {
currentInstance = null
}
setupResult.then(unsetInstance, unsetInstance)
if (isSSR) {
// return the promise so server-renderer can wait on it
return setupResult