##// END OF EJS Templates
working on container configuration dsl
cin -
r124:b58fedd83580 ioc ts support
parent child
Show More
@@ -1,27 +1,17
1 1 import { primitive } from "../safe";
2 import { TypeRegistration } from "./Configuration";
2 import { TypeRegistration, DependencyRegistration, LazyDependencyRegistration, Registration, StrictTypeRegistration } from "./Configuration";
3 3
4 4 export interface InjectOptions {
5 5 lazy?: boolean;
6 6 }
7 7
8 export interface Dependency<K extends keyof any> {
9 $dependency: K;
10
11 lazy?: boolean;
12
13 }
14
15 export interface Lazy<K extends keyof any> extends Dependency<K> {
16 lazy: true;
17 }
18
19 8 type Compatible<T1, T2> = T2 extends T1 ? any : never;
20 9
21 10 type ExtractService<K, S> = K extends keyof S ? S[K] : K;
22 11
23 12 type ExtractDependency<D, S> = D extends { $dependency: infer K } ?
24 13 D extends { lazy: true } ? () => ExtractService<K, S> : ExtractService<K, S> :
14 D extends { $type: new (...args: any[]) => infer I } ? I :
25 15 WalkDependencies<D, S>;
26 16
27 17 type WalkDependencies<D, S> = D extends primitive ? D :
@@ -62,8 +52,10 export interface LazyDependencyOptions<T
62 52 interface Declaration<S extends object> {
63 53 define<T>(): Builder<T, S>;
64 54
65 dependency<K extends keyof S>(name: K, opts: LazyDependencyOptions<S[K]>): Lazy<K>;
66 dependency<K extends keyof S>(name: K, opts?: DependencyOptions<S[K]>): Dependency<K>;
55 dependency<K extends keyof S>(name: K, opts: LazyDependencyOptions<S[K]>): LazyDependencyRegistration<S, K>;
56 dependency<K extends keyof S>(name: K, opts?: DependencyOptions<S[K]>): DependencyRegistration<S, K>;
57
58 $type<P extends any[], C extends new (...args: ExtractDependency<P, S>) => any>(target: C, ...params: P): StrictTypeRegistration<C, S>;
67 59
68 60 configure(): Config<S>;
69 61 }
@@ -76,9 +68,12 type PromiseOrValue<T> = PromiseLike<T>
76 68
77 69
78 70 export interface Config<S extends object, Y extends keyof S = keyof S> {
79 register<K extends Y>(name: K, m: { $from: Promise<ServiceModule<S[K], S>> } | S[K]): Config<S, Exclude<Y, K>>;
71 register<K extends Y>(name: K, m: { $from: Promise<ServiceModule<S[K], S>> }): Config<S, Exclude<Y, K>>;
80 72 register<K extends Y, M extends string>(name: K, m: { $from: Promise<ServiceModule<S[K], S, M>>, service: M }): Config<S, Exclude<Y, K>>;
81 register<K extends Y, C extends new (...args: any) => S[K]>(name: K, d: TypeRegistration<C, S>): Config<S, Exclude<Y, K>>;
73
74 register<K extends Y>(name: K, m: Registration<S[K], S>): Config<S, Exclude<Y, K>>;
75 registerType<K extends Y, P extends any[]>(
76 name: K, $type: new (...args: ExtractDependency<P, S>) => S[K], ...params: P): Config<S, Exclude<Y, K>>;
82 77 }
83 78
84 79 export declare function declare<S extends object>(): Declaration<S>;
@@ -44,12 +44,17 export interface ServiceRegistration<T,
44 44 cleanup?: ((instance: T) => void) | string;
45 45 }
46 46
47 export interface TypeRegistration<C extends new () => any, S extends object> extends ServiceRegistration<InstanceType<C>, S> {
47 export interface TypeRegistration<C extends new (...args: any[]) => any, S extends object> extends ServiceRegistration<InstanceType<C>, S> {
48 48 $type: string | C;
49 params?: ConstructorParameters<C>;
49 params?: Registration<ConstructorParameters<C>, S>;
50 50 }
51 51
52 export interface FactoryRegistration<F extends () => any, S extends object> extends ServiceRegistration<ReturnType<F>, S> {
52 export interface StrictTypeRegistration<C extends new (...args: any[]) => any, S extends object> extends ServiceRegistration<InstanceType<C>, S> {
53 $type: C;
54 params?: Registration<ConstructorParameters<C>, S>;
55 }
56
57 export interface FactoryRegistration<F extends (...args: any[]) => any, S extends object> extends ServiceRegistration<ReturnType<F>, S> {
53 58 $factory: string | F;
54 59 }
55 60
@@ -69,12 +74,14 export interface LazyDependencyRegistrat
69 74 lazy: true;
70 75 }
71 76
77 type OfType<K extends keyof S, S, T> = Extract<{ [k in K]: T}, S>;
78
72 79 export type Registration<T, S extends object> = T extends primitive ? T :
73 80 (
74 81 T |
75 82 { [k in keyof T]: Registration<T[k], S> } |
76 TypeRegistration<new () => T, S> |
77 FactoryRegistration<() => T, S> |
83 TypeRegistration<new (...args: any[]) => T, S> |
84 FactoryRegistration<(...args: any[]) => T, S> |
78 85 ValueRegistration<any> |
79 86 DependencyRegistration<S, keyof S>
80 87 );
@@ -8,20 +8,20 export const service = define<Bar>();
8 8 nested: {
9 9 lazy: dependency("foo", { lazy: true })
10 10 },
11 host: "value"
12 })
11 host: dependency("host")
12 }, "")
13 13 export class Bar {
14 14 barName = "bar";
15 15
16 16 _v: Foo | undefined;
17 17
18 constructor(_opts?: {
18 constructor(_opts: {
19 19 foo?: Foo;
20 20 nested?: {
21 21 lazy: () => Foo
22 22 },
23 23 host: string
24 }) {
24 }, s: string) {
25 25
26 26 if (_opts && _opts.foo)
27 27 this._v = _opts.foo;
@@ -1,15 +1,23
1 import { configure } from "./services";
1 import { configure, dependency, Services, $type } from "./services";
2 2 import { Foo } from "./Foo";
3 3 import { Bar } from "./Bar";
4 import { Box } from "./Box";
5
4 6
5 7 export const config = configure()
6 8 .register("bar", { $from: import("./Bar"), service: "service" })
7 .register("box", { $from: import("./Box") })
9 // .register("box", { $from: import("./Box") })
8 10 .register("host", "example.com")
9 .register("foo", {
10 $type: Foo
11 })
12 .register("bar2", {
13 $type: Bar,
14 params: []
15 });
11 // .registerType("bar2", Bar, [{ foo: dependency("foo"), host: "" }]);
12 .register("bar2", $type(Bar,
13 {
14 foo: $type(Foo)
15 .override("host", "foo.example.com")
16 .inject("setName", dependency("host"))
17 .activate("context"),
18 host: ""
19 },
20 "")
21 )
22 .registerType("box", Box, dependency("bar"));
23
@@ -22,4 +22,4 export interface Services {
22 22 /**
23 23 * Экспортируем вспомогательные функции для описания сервисов и кинфогурации
24 24 */
25 export const { define, dependency, configure } = declare<Services>();
25 export const { define, dependency, configure, $type } = declare<Services>();
General Comments 0
You need to be logged in to leave comments. Login now