@prefecthq/prefect-ui-library
Version:
This library is the Vue and Typescript component library for [Prefect 2](https://github.com/PrefectHQ/prefect) and [Prefect Cloud 2](https://www.prefect.io/cloud/). _The components and utilities in this project are not meant to be used independently_.
26 lines (17 loc) • 690 B
text/typescript
import { inject as vueInject, InjectionKey, ComponentInternalInstance, getCurrentInstance } from 'vue'
export function inject<T>(key: InjectionKey<T> | string): T {
const value = vueInject(key)
if (value === undefined) {
throw `Failed to inject value with key ${String(key)}`
}
return value
}
type ComponentInstanceWithProvide = ComponentInternalInstance & { provides: Record<string | symbol, unknown> } | null
export function injectFromSelfOrAncestor<T>(key: InjectionKey<T>): T {
const vm = getCurrentInstance() as ComponentInstanceWithProvide
const value = vm?.provides[key as symbol]
if (value !== undefined) {
return value as T
}
return inject(key)
}