Skip to content

Commit

Permalink
fix(runtime-core): support for nested calls to runWithContext (#10261)
Browse files Browse the repository at this point in the history
close #10260
  • Loading branch information
yangxiuxiu1115 committed Feb 7, 2024
1 parent eb1b911 commit 75e02b5
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
7 changes: 7 additions & 0 deletions packages/runtime-core/__tests__/apiCreateApp.spec.ts
Expand Up @@ -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()
Expand Down
3 changes: 2 additions & 1 deletion packages/runtime-core/src/apiCreateApp.ts
Expand Up @@ -387,11 +387,12 @@ export function createAppAPI<HostElement>(
},

runWithContext(fn) {
const lastApp = currentApp
currentApp = app
try {
return fn()
} finally {
currentApp = null
currentApp = lastApp
}
},
})
Expand Down

0 comments on commit 75e02b5

Please sign in to comment.