# HG changeset patch # User cin # Date 2020-07-13 08:19:54 # Node ID b58fedd83580d676d633eee730f296081e3bd064 # Parent 264497557e7950f1ef543a0ac7c4a7802242e9f0 working on container configuration dsl diff --git a/src/main/ts/di/Annotations.ts b/src/main/ts/di/Annotations.ts --- a/src/main/ts/di/Annotations.ts +++ b/src/main/ts/di/Annotations.ts @@ -1,27 +1,17 @@ import { primitive } from "../safe"; -import { TypeRegistration } from "./Configuration"; +import { TypeRegistration, DependencyRegistration, LazyDependencyRegistration, Registration, StrictTypeRegistration } from "./Configuration"; export interface InjectOptions { lazy?: boolean; } -export interface Dependency { - $dependency: K; - - lazy?: boolean; - -} - -export interface Lazy extends Dependency { - lazy: true; -} - type Compatible = T2 extends T1 ? any : never; type ExtractService = K extends keyof S ? S[K] : K; type ExtractDependency = D extends { $dependency: infer K } ? D extends { lazy: true } ? () => ExtractService : ExtractService : + D extends { $type: new (...args: any[]) => infer I } ? I : WalkDependencies; type WalkDependencies = D extends primitive ? D : @@ -62,8 +52,10 @@ export interface LazyDependencyOptions { define(): Builder; - dependency(name: K, opts: LazyDependencyOptions): Lazy; - dependency(name: K, opts?: DependencyOptions): Dependency; + dependency(name: K, opts: LazyDependencyOptions): LazyDependencyRegistration; + dependency(name: K, opts?: DependencyOptions): DependencyRegistration; + + $type

) => any>(target: C, ...params: P): StrictTypeRegistration; configure(): Config; } @@ -76,9 +68,12 @@ type PromiseOrValue = PromiseLike export interface Config { - register(name: K, m: { $from: Promise> } | S[K]): Config>; + register(name: K, m: { $from: Promise> }): Config>; register(name: K, m: { $from: Promise>, service: M }): Config>; - register S[K]>(name: K, d: TypeRegistration): Config>; + + register(name: K, m: Registration): Config>; + registerType( + name: K, $type: new (...args: ExtractDependency) => S[K], ...params: P): Config>; } export declare function declare(): Declaration; diff --git a/src/main/ts/di/Configuration.ts b/src/main/ts/di/Configuration.ts --- a/src/main/ts/di/Configuration.ts +++ b/src/main/ts/di/Configuration.ts @@ -44,12 +44,17 @@ export interface ServiceRegistration void) | string; } -export interface TypeRegistration any, S extends object> extends ServiceRegistration, S> { +export interface TypeRegistration any, S extends object> extends ServiceRegistration, S> { $type: string | C; - params?: ConstructorParameters; + params?: Registration, S>; } -export interface FactoryRegistration any, S extends object> extends ServiceRegistration, S> { +export interface StrictTypeRegistration any, S extends object> extends ServiceRegistration, S> { + $type: C; + params?: Registration, S>; +} + +export interface FactoryRegistration any, S extends object> extends ServiceRegistration, S> { $factory: string | F; } @@ -69,12 +74,14 @@ export interface LazyDependencyRegistrat lazy: true; } +type OfType = Extract<{ [k in K]: T}, S>; + export type Registration = T extends primitive ? T : ( T | { [k in keyof T]: Registration } | - TypeRegistration T, S> | - FactoryRegistration<() => T, S> | + TypeRegistration T, S> | + FactoryRegistration<(...args: any[]) => T, S> | ValueRegistration | DependencyRegistration ); diff --git a/src/test/ts/mock/Bar.ts b/src/test/ts/mock/Bar.ts --- a/src/test/ts/mock/Bar.ts +++ b/src/test/ts/mock/Bar.ts @@ -8,20 +8,20 @@ export const service = define(); nested: { lazy: dependency("foo", { lazy: true }) }, - host: "value" -}) + host: dependency("host") +}, "") export class Bar { barName = "bar"; _v: Foo | undefined; - constructor(_opts?: { + constructor(_opts: { foo?: Foo; nested?: { lazy: () => Foo }, host: string - }) { + }, s: string) { if (_opts && _opts.foo) this._v = _opts.foo; diff --git a/src/test/ts/mock/config.ts b/src/test/ts/mock/config.ts --- a/src/test/ts/mock/config.ts +++ b/src/test/ts/mock/config.ts @@ -1,15 +1,23 @@ -import { configure } from "./services"; +import { configure, dependency, Services, $type } from "./services"; import { Foo } from "./Foo"; import { Bar } from "./Bar"; +import { Box } from "./Box"; + export const config = configure() .register("bar", { $from: import("./Bar"), service: "service" }) - .register("box", { $from: import("./Box") }) + // .register("box", { $from: import("./Box") }) .register("host", "example.com") - .register("foo", { - $type: Foo - }) - .register("bar2", { - $type: Bar, - params: [] - }); + // .registerType("bar2", Bar, [{ foo: dependency("foo"), host: "" }]); + .register("bar2", $type(Bar, + { + foo: $type(Foo) + .override("host", "foo.example.com") + .inject("setName", dependency("host")) + .activate("context"), + host: "" + }, + "") + ) + .registerType("box", Box, dependency("bar")); + diff --git a/src/test/ts/mock/services.ts b/src/test/ts/mock/services.ts --- a/src/test/ts/mock/services.ts +++ b/src/test/ts/mock/services.ts @@ -22,4 +22,4 @@ export interface Services { /** * Экспортируем вспомогательные функции для описания сервисов и кинфогурации */ -export const { define, dependency, configure } = declare(); +export const { define, dependency, configure, $type } = declare();