From 75e02b5099a08166bdf407127916734c48209ee9 Mon Sep 17 00:00:00 2001 From: yangxiuxiu <79584569+yangxiuxiu1115@users.noreply.github.com> Date: Wed, 7 Feb 2024 13:33:44 +0800 Subject: [PATCH] fix(runtime-core): support for nested calls to runWithContext (#10261) close #10260 --- packages/runtime-core/__tests__/apiCreateApp.spec.ts | 7 +++++++ packages/runtime-core/src/apiCreateApp.ts | 3 ++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/packages/runtime-core/__tests__/apiCreateApp.spec.ts b/packages/runtime-core/__tests__/apiCreateApp.spec.ts index f8dcd6aee2c..05e51e43bb6 100644 --- a/packages/runtime-core/__tests__/apiCreateApp.spec.ts +++ b/packages/runtime-core/__tests__/apiCreateApp.spec.ts @@ -123,6 +123,13 @@ describe('api: createApp', () => { expect(app.runWithContext(() => inject('foo'))).toBe(1) + expect( + app.runWithContext(() => { + app.runWithContext(() => {}) + return inject('foo') + }), + ).toBe(1) + // ensure the context is restored inject('foo') expect('inject() can only be used inside setup').toHaveBeenWarned() diff --git a/packages/runtime-core/src/apiCreateApp.ts b/packages/runtime-core/src/apiCreateApp.ts index b99d06e01ff..c476cdbe701 100644 --- a/packages/runtime-core/src/apiCreateApp.ts +++ b/packages/runtime-core/src/apiCreateApp.ts @@ -387,11 +387,12 @@ export function createAppAPI( }, runWithContext(fn) { + const lastApp = currentApp currentApp = app try { return fn() } finally { - currentApp = null + currentApp = lastApp } }, })