interfaces.ts
88 lines
| 2.4 KiB
| video/mp2t
|
TypeScriptLexer
|
|
r115 | import { isPrimitive, primitive } from "../safe"; | ||
|
|
r49 | import { ActivationContext } from "./ActivationContext"; | ||
| import { Constructor, Factory } from "../interfaces"; | ||||
|
|
r115 | export interface Descriptor<S = any, T = any> { | ||
| activate(context: ActivationContext<S>): T; | ||||
|
|
r49 | } | ||
|
|
r113 | export function isDescriptor(x: any): x is Descriptor { | ||
|
|
r49 | return (!isPrimitive(x)) && | ||
| (x.activate instanceof Function); | ||||
| } | ||||
|
|
r115 | export type ServiceMap<S, S2 extends S = S> = { | ||
| [k in keyof S2]: Descriptor<S, S2[k]>; | ||||
| }; | ||||
| export type PartialServiceMap<S, S2 extends S = S> = Partial<ServiceMap<S, S2>>; | ||||
|
|
r49 | |||
| export enum ActivationType { | ||||
|
|
r65 | Singleton = 1, | ||
|
|
r49 | Container, | ||
| Hierarchy, | ||||
| Context, | ||||
| Call | ||||
| } | ||||
|
|
r113 | export interface RegistrationWithServices<S> { | ||
| services?: ServiceMap<S>; | ||||
|
|
r49 | } | ||
|
|
r113 | export interface ServiceRegistration<T, P, S> extends RegistrationWithServices<S> { | ||
|
|
r49 | |||
| activation?: "singleton" | "container" | "hierarchy" | "context" | "call"; | ||||
|
|
r113 | params?: P; | ||
|
|
r49 | |||
| inject?: object | object[]; | ||||
|
|
r113 | cleanup?: ((instance: T) => void) | string; | ||
|
|
r49 | } | ||
|
|
r113 | export interface TypeRegistration<T, P, S> extends ServiceRegistration<T, P, S> { | ||
| $type: string | Constructor<T>; | ||||
|
|
r49 | } | ||
|
|
r113 | export interface FactoryRegistration<T, P, S> extends ServiceRegistration<T, P, S> { | ||
| $factory: string | Factory<T>; | ||||
|
|
r49 | } | ||
|
|
r113 | export interface ValueRegistration<T> { | ||
| $value: T; | ||||
|
|
r49 | parse?: boolean; | ||
| } | ||||
|
|
r113 | export interface DependencyRegistration<S, K extends keyof S> extends RegistrationWithServices<S> { | ||
| $dependency: K; | ||||
|
|
r49 | lazy?: boolean; | ||
| optional?: boolean; | ||||
|
|
r113 | default?: S[K]; | ||
|
|
r49 | } | ||
|
|
r115 | export type Parse<T> = T extends primitive ? T: | ||
| T extends Descriptor<infer V> ? V : | ||||
| { [K in keyof T]: Parse<T[K]> }; | ||||
| export interface Resolver<S> { | ||||
| resolve<K extends keyof ContainerServices<S>, T extends ContainerServices<S>[K] = ContainerServices<S>[K]>(name: K, def?: T): T; | ||||
| } | ||||
| export type ContainerServices<S> = S & { | ||||
| container: Resolver<S>; | ||||
| }; | ||||
|
|
r113 | export function isTypeRegistration(x: any): x is TypeRegistration<any, any, any> { | ||
|
|
r65 | return (!isPrimitive(x)) && ("$type" in x); | ||
|
|
r49 | } | ||
|
|
r113 | export function isFactoryRegistration(x: any): x is FactoryRegistration<any, any, any> { | ||
|
|
r65 | return (!isPrimitive(x)) && ("$factory" in x); | ||
|
|
r49 | } | ||
|
|
r113 | export function isValueRegistration(x: any): x is ValueRegistration<any> { | ||
|
|
r49 | return (!isPrimitive(x)) && ("$value" in x); | ||
| } | ||||
|
|
r114 | export function isDependencyRegistration<S>(x: any): x is DependencyRegistration<S, keyof S> { | ||
|
|
r49 | return (!isPrimitive(x)) && ("$dependency" in x); | ||
| } | ||||
