代码拉取完成,页面将自动刷新
/**
* Container options.
*
* @deprecated
*/
export interface UseContainerOptions {
/**
* If set to true, then default container will be used in the case if given container haven't returned anything.
*/
fallback?: boolean
/**
* If set to true, then default container will be used in the case if given container thrown an exception.
*/
fallbackOnErrors?: boolean
}
/**
* @deprecated
*/
export type ContainedType<T> = { new (...args: any[]): T } | Function
/**
* @deprecated
*/
export interface ContainerInterface {
get<T>(someClass: ContainedType<T>): T
}
/**
* Container to be used by this library for inversion control. If container was not implicitly set then by default
* container simply creates a new instance of the given class.
*
* @deprecated
*/
const defaultContainer: ContainerInterface =
new (class implements ContainerInterface {
private instances: { type: Function; object: any }[] = []
get<T>(someClass: ContainedType<T>): T {
let instance = this.instances.find((i) => i.type === someClass)
if (!instance) {
instance = {
type: someClass,
object: new (someClass as new () => T)(),
}
this.instances.push(instance)
}
return instance.object
}
})()
let userContainer: ContainerInterface
let userContainerOptions: UseContainerOptions | undefined
/**
* Sets container to be used by this library.
*
* @deprecated
*/
export function useContainer(
iocContainer: ContainerInterface,
options?: UseContainerOptions,
) {
userContainer = iocContainer
userContainerOptions = options
}
/**
* Gets the IOC container used by this library.
*
* @deprecated
*/
export function getFromContainer<T>(someClass: ContainedType<T>): T {
if (userContainer) {
try {
const instance = userContainer.get(someClass)
if (instance) return instance
if (!userContainerOptions || !userContainerOptions.fallback)
return instance
} catch (error) {
if (!userContainerOptions || !userContainerOptions.fallbackOnErrors)
throw error
}
}
return defaultContainer.get<T>(someClass)
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。