MCPcopy
hub / github.com/vuejs/core / withAsyncContext

Function withAsyncContext

packages/runtime-core/src/apiSetupHelpers.ts:529–580  ·  view source on GitHub ↗
(getAwaitable: () => any)

Source from the content-addressed store, hash-verified

527 * @internal
528 */
529export function withAsyncContext(getAwaitable: () => any): [any, () => void] {
530 const ctx = getCurrentInstance()!
531 const inSSRSetup = isInSSRComponentSetup
532 if (__DEV__ && !ctx) {
533 warn(
534 `withAsyncContext called without active current instance. ` +
535 `This is likely a bug.`,
536 )
537 }
538 let awaitable = getAwaitable()
539 unsetCurrentInstance()
540 if (inSSRSetup) {
541 setInSSRSetupState(false)
542 }
543
544 const restore = () => {
545 setCurrentInstance(ctx)
546 if (inSSRSetup) {
547 setInSSRSetupState(true)
548 }
549 }
550
551 // Never restore a captured "prev" instance here: in concurrent async setup
552 // continuations it may belong to a sibling component and cause leaks.
553 // We only need to balance ctx.scope.on() from setCurrentInstance(ctx),
554 // then clear global currentInstance for user microtasks.
555 const cleanup = () => {
556 if (getCurrentInstance() !== ctx) ctx.scope.off()
557 unsetCurrentInstance()
558 if (inSSRSetup) {
559 setInSSRSetupState(false)
560 }
561 }
562
563 if (isPromise(awaitable)) {
564 awaitable = awaitable.catch(e => {
565 restore()
566 // Defer cleanup so the async function's catch continuation
567 // still runs with the restored instance.
568 Promise.resolve().then(() => Promise.resolve().then(cleanup))
569 throw e
570 })
571 }
572 return [
573 awaitable,
574 () => {
575 restore()
576 // Keep instance for the current continuation, then cleanup.
577 Promise.resolve().then(cleanup)
578 },
579 ]
580}

Callers 3

setupFunction · 0.90
setupFunction · 0.85
setupFunction · 0.85

Calls 6

getCurrentInstanceFunction · 0.90
warnFunction · 0.90
unsetCurrentInstanceFunction · 0.90
isPromiseFunction · 0.90
restoreFunction · 0.85
resolveMethod · 0.65

Tested by 3

setupFunction · 0.72
setupFunction · 0.68
setupFunction · 0.68