2020-09-05 00:00:37 +08:00
|
|
|
import { isFunction } from '@vue/shared'
|
2019-06-19 17:31:49 +08:00
|
|
|
import { currentInstance } from './component'
|
2021-03-06 00:10:06 +08:00
|
|
|
import { currentRenderingInstance } from './componentRenderContext'
|
2023-04-05 15:18:13 +08:00
|
|
|
import { currentApp } from './apiCreateApp'
|
2019-08-21 03:51:55 +08:00
|
|
|
import { warn } from './warning'
|
2019-06-19 17:31:49 +08:00
|
|
|
|
2024-08-02 11:11:03 +08:00
|
|
|
interface InjectionConstraint<T> {}
|
|
|
|
|
|
|
|
export type InjectionKey<T> = symbol & InjectionConstraint<T>
|
2019-06-19 17:31:49 +08:00
|
|
|
|
2023-05-02 21:35:28 +08:00
|
|
|
export function provide<T, K = InjectionKey<T> | string | number>(
|
|
|
|
key: K,
|
|
|
|
value: K extends InjectionKey<infer V> ? V : T,
|
2023-05-01 11:26:47 +08:00
|
|
|
) {
|
2019-06-19 17:31:49 +08:00
|
|
|
if (!currentInstance) {
|
2019-08-31 03:05:39 +08:00
|
|
|
if (__DEV__) {
|
2019-09-04 06:11:04 +08:00
|
|
|
warn(`provide() can only be used inside setup().`)
|
2019-08-31 03:05:39 +08:00
|
|
|
}
|
2019-06-19 17:31:49 +08:00
|
|
|
} else {
|
2019-06-19 22:48:22 +08:00
|
|
|
let provides = currentInstance.provides
|
|
|
|
// by default an instance inherits its parent's provides object
|
|
|
|
// but when it needs to provide values of its own, it creates its
|
|
|
|
// own provides object using parent provides object as prototype.
|
|
|
|
// this way in `inject` we can simply look up injections from direct
|
|
|
|
// parent and let the prototype chain do the work.
|
|
|
|
const parentProvides =
|
|
|
|
currentInstance.parent && currentInstance.parent.provides
|
|
|
|
if (parentProvides === provides) {
|
|
|
|
provides = currentInstance.provides = Object.create(parentProvides)
|
|
|
|
}
|
2019-10-09 00:43:13 +08:00
|
|
|
// TS doesn't allow symbol as index type
|
|
|
|
provides[key as string] = value
|
2019-06-19 17:31:49 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-20 02:45:11 +08:00
|
|
|
export function inject<T>(key: InjectionKey<T> | string): T | undefined
|
2020-09-05 00:00:37 +08:00
|
|
|
export function inject<T>(
|
|
|
|
key: InjectionKey<T> | string,
|
|
|
|
defaultValue: T,
|
2020-09-15 09:26:28 +08:00
|
|
|
treatDefaultAsFactory?: false,
|
|
|
|
): T
|
|
|
|
export function inject<T>(
|
|
|
|
key: InjectionKey<T> | string,
|
|
|
|
defaultValue: T | (() => T),
|
|
|
|
treatDefaultAsFactory: true,
|
2020-09-05 00:00:37 +08:00
|
|
|
): T
|
2019-10-22 23:26:48 +08:00
|
|
|
export function inject(
|
|
|
|
key: InjectionKey<any> | string,
|
2020-09-05 00:00:37 +08:00
|
|
|
defaultValue?: unknown,
|
|
|
|
treatDefaultAsFactory = false,
|
2019-10-22 23:26:48 +08:00
|
|
|
) {
|
2019-11-06 11:35:24 +08:00
|
|
|
// fallback to `currentRenderingInstance` so that this can be called in
|
|
|
|
// a functional component
|
|
|
|
const instance = currentInstance || currentRenderingInstance
|
2023-04-05 15:18:13 +08:00
|
|
|
|
|
|
|
// also support looking up from app-level provides w/ `app.runWithContext()`
|
|
|
|
if (instance || currentApp) {
|
2020-10-20 08:45:48 +08:00
|
|
|
// #2400
|
|
|
|
// to support `app.use` plugins,
|
2022-01-21 14:18:34 +08:00
|
|
|
// fallback to appContext's `provides` if the instance is at root
|
2024-08-07 12:02:38 +08:00
|
|
|
// #11488, in a nested createApp, prioritize using the provides from currentApp
|
|
|
|
const provides = currentApp
|
|
|
|
? currentApp._context.provides
|
|
|
|
: instance
|
|
|
|
? instance.parent == null
|
|
|
|
? instance.vnode.appContext && instance.vnode.appContext.provides
|
|
|
|
: instance.parent.provides
|
|
|
|
: undefined
|
2020-10-20 08:45:48 +08:00
|
|
|
|
|
|
|
if (provides && (key as string | symbol) in provides) {
|
2019-10-09 00:43:13 +08:00
|
|
|
// TS doesn't allow symbol as index type
|
|
|
|
return provides[key as string]
|
2020-03-31 03:24:55 +08:00
|
|
|
} else if (arguments.length > 1) {
|
2020-09-05 00:00:37 +08:00
|
|
|
return treatDefaultAsFactory && isFunction(defaultValue)
|
2023-04-05 15:18:13 +08:00
|
|
|
? defaultValue.call(instance && instance.proxy)
|
2020-09-05 00:00:37 +08:00
|
|
|
: defaultValue
|
2019-08-21 03:51:55 +08:00
|
|
|
} else if (__DEV__) {
|
2019-10-26 22:52:29 +08:00
|
|
|
warn(`injection "${String(key)}" not found.`)
|
2019-08-21 03:51:55 +08:00
|
|
|
}
|
2019-09-04 06:11:04 +08:00
|
|
|
} else if (__DEV__) {
|
2019-11-06 11:35:24 +08:00
|
|
|
warn(`inject() can only be used inside setup() or functional components.`)
|
2019-06-19 17:31:49 +08:00
|
|
|
}
|
|
|
|
}
|
2023-04-20 10:12:18 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if `inject()` can be used without warning about being called in the wrong place (e.g. outside of
|
|
|
|
* setup()). This is used by libraries that want to use `inject()` internally without triggering a warning to the end
|
|
|
|
* user. One example is `useRoute()` in `vue-router`.
|
|
|
|
*/
|
|
|
|
export function hasInjectionContext(): boolean {
|
|
|
|
return !!(currentInstance || currentRenderingInstance || currentApp)
|
|
|
|
}
|