Skip to content

Commit afed3c3

Browse files
committed
chore: refactor
1 parent 2b3e6c6 commit afed3c3

File tree

2 files changed

+11
-13
lines changed

2 files changed

+11
-13
lines changed

packages/runtime-core/src/apiCreateApp.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ export function createAppAPI<HostElement>(
381381
},
382382

383383
runWithContext(fn) {
384-
currentApp = this
384+
currentApp = app
385385
try {
386386
return fn()
387387
} finally {
@@ -399,6 +399,7 @@ export function createAppAPI<HostElement>(
399399
}
400400

401401
/**
402-
* @internal Used to identify the current app when using `inject()` within `app.runWithContext()`.
402+
* @internal Used to identify the current app when using `inject()` within
403+
* `app.runWithContext()`.
403404
*/
404405
export let currentApp: App<unknown> | null = null

packages/runtime-core/src/apiInject.ts

+8-11
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { currentInstance } from './component'
33
import { currentRenderingInstance } from './componentRenderContext'
44
import { currentApp } from './apiCreateApp'
55
import { warn } from './warning'
6-
import { ComponentPublicInstance } from './componentPublicInstance'
76

87
export interface InjectionKey<T> extends Symbol {}
98

@@ -46,28 +45,26 @@ export function inject(
4645
treatDefaultAsFactory = false
4746
) {
4847
// fallback to `currentRenderingInstance` so that this can be called in
49-
// a functional component and to currentApp so it can be called within `app.runWithContext()`
50-
const instance = currentInstance || currentRenderingInstance || currentApp
48+
// a functional component
49+
const instance = currentInstance || currentRenderingInstance
5150

52-
if (instance) {
51+
// also support looking up from app-level provides w/ `app.runWithContext()`
52+
if (instance || currentApp) {
5353
// #2400
5454
// to support `app.use` plugins,
5555
// fallback to appContext's `provides` if the instance is at root
56-
const provides =
57-
'mount' in instance // checks if instance is an App
58-
? instance._context.provides
59-
: instance.parent == null
56+
const provides = instance
57+
? instance.parent == null
6058
? instance.vnode.appContext && instance.vnode.appContext.provides
6159
: instance.parent.provides
60+
: currentApp!._context.provides
6261

6362
if (provides && (key as string | symbol) in provides) {
6463
// TS doesn't allow symbol as index type
6564
return provides[key as string]
6665
} else if (arguments.length > 1) {
6766
return treatDefaultAsFactory && isFunction(defaultValue)
68-
? defaultValue.call(
69-
(instance as { proxy?: ComponentPublicInstance | null }).proxy
70-
)
67+
? defaultValue.call(instance && instance.proxy)
7168
: defaultValue
7269
} else if (__DEV__) {
7370
warn(`injection "${String(key)}" not found.`)

0 commit comments

Comments
 (0)