From: yangxiuxiu <79584569+yangxiuxiu1115@users.noreply.github.com> Date: Wed, 7 Feb 2024 05:33:44 +0000 (+0800) Subject: fix(runtime-core): support for nested calls to runWithContext (#10261) X-Git-Tag: v3.4.16~3 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=75e02b5099a08166bdf407127916734c48209ee9;p=thirdparty%2Fvuejs%2Fcore.git fix(runtime-core): support for nested calls to runWithContext (#10261) close #10260 --- diff --git a/packages/runtime-core/__tests__/apiCreateApp.spec.ts b/packages/runtime-core/__tests__/apiCreateApp.spec.ts index f8dcd6aee2..05e51e43bb 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 b99d06e01f..c476cdbe70 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 } }, })