fix: prevent withAsyncContext currentInstance leak in edge cases
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user