| 527 | * @internal |
| 528 | */ |
| 529 | export 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 | } |