From 278f2f428616fb923f2b17c03f1b92a9c3c09cec Mon Sep 17 00:00:00 2001 From: RicardoErii <‘1974364190@qq.com’> Date: Sat, 3 Feb 2024 02:07:41 +0800 Subject: [PATCH] fix(runtime-core): support for nested calls to runWithContext --- 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 } }, })