| 8 | export type InjectionKey<T> = symbol & InjectionConstraint<T> |
| 9 | |
| 10 | export function provide<T, K = InjectionKey<T> | string | number>( |
| 11 | key: K, |
| 12 | value: K extends InjectionKey<infer V> ? V : T, |
| 13 | ): void { |
| 14 | if (__DEV__) { |
| 15 | if (!currentInstance || currentInstance.isMounted) { |
| 16 | warn(`provide() can only be used inside setup().`) |
| 17 | } |
| 18 | } |
| 19 | if (currentInstance) { |
| 20 | let provides = currentInstance.provides |
| 21 | // by default an instance inherits its parent's provides object |
| 22 | // but when it needs to provide values of its own, it creates its |
| 23 | // own provides object using parent provides object as prototype. |
| 24 | // this way in `inject` we can simply look up injections from direct |
| 25 | // parent and let the prototype chain do the work. |
| 26 | const parentProvides = |
| 27 | currentInstance.parent && currentInstance.parent.provides |
| 28 | if (parentProvides === provides) { |
| 29 | provides = currentInstance.provides = Object.create(parentProvides) |
| 30 | } |
| 31 | // TS doesn't allow symbol as index type |
| 32 | provides[key as string] = value |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | export function inject<T>(key: InjectionKey<T> | string): T | undefined |
| 37 | export function inject<T>( |