##// END OF EJS Templates
sync
cin -
r121:876264d00a17 ioc ts support
parent child
Show More
@@ -9,6 +9,7 export interface Dependency<K extends ke
9 9 $dependency: K;
10 10
11 11 lazy?: boolean;
12
12 13 }
13 14
14 15 export interface Lazy<K extends keyof any> extends Dependency<K> {
@@ -34,11 +35,6 export class Builder<T, S extends object
34 35 }
35 36
36 37 inject<P extends any[]>(...args: P) {
37 // K = "bar"
38 // M = "setValue"
39 // S[K] = Bar
40 // T[M] = (value: string) => void
41 // P[m] = (value: V) => void
42 38 return <X extends { [m in M]: (...args: any) => any }, M extends keyof (T | X)>(
43 39 target: X,
44 40 memberName: M,
@@ -48,17 +44,26 export class Builder<T, S extends object
48 44 };
49 45 }
50 46
51 getDescriptor(): TypeRegistration<T, any, S> {
47 getDescriptor(): TypeRegistration<new () => T, S> {
52 48 throw new Error();
53 49 }
54 50
55 51 }
56 52
53 export interface DependencyOptions<T> {
54 optional?: boolean;
55 default?: T;
56 }
57
58 export interface LazyDependencyOptions<T> extends DependencyOptions<T> {
59 lazy: true;
60 }
61
57 62 interface Declaration<S extends object> {
58 63 define<T>(): Builder<T, S>;
59 64
60 dependency<K extends keyof S>(name: K, opts: { lazy: true }): Lazy<K>;
61 dependency<K extends keyof S>(name: K, opts?: any): Dependency<K>;
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>;
62 67
63 68 config(): Config<S>;
64 69 }
@@ -32,24 +32,23 export interface RegistrationScope<S ext
32 32 /**
33 33 * Базовый интефейс конфигурации сервисов
34 34 */
35 export interface ServiceRegistration<T, P, S extends object> extends RegistrationScope<S> {
35 export interface ServiceRegistration<T, S extends object> extends RegistrationScope<S> {
36 36
37 37 activation?: ActivationType;
38 38
39 params?: P;
39 params?: any;
40 40
41 41 inject?: object | object[];
42 42
43 43 cleanup?: ((instance: T) => void) | string;
44 44 }
45 45
46 export interface TypeRegistration<T, P extends any[], S extends object> extends ServiceRegistration<T, P, S> {
47 $type: string | (new (...params: P) => T);
48
46 export interface TypeRegistration<C extends new () => any, S extends object> extends ServiceRegistration<InstanceType<C>, S> {
47 $type: string | C;
49 48 }
50 49
51 export interface FactoryRegistration<T, P extends any[], S extends object> extends ServiceRegistration<T, P, S> {
52 $factory: string | ((...params: P) => T);
50 export interface FactoryRegistration<F extends () => any, S extends object> extends ServiceRegistration<ReturnType<F>, S> {
51 $factory: string | F;
53 52 }
54 53
55 54 export interface ValueRegistration<T> {
@@ -64,12 +63,16 export interface DependencyRegistration<
64 63 default?: ContainerResolve<S, K>;
65 64 }
66 65
66 export interface LazyDependencyRegistration<S extends object, K extends ContainerKeys<S> = ContainerKeys<S>> extends DependencyRegistration<S, K> {
67 lazy: true;
68 }
69
67 70 export type Registration<T, S extends object> = T extends primitive ? T :
68 71 (
69 72 T |
70 73 { [k in keyof T]: Registration<T[k], S> } |
71 TypeRegistration<T, any, S> |
72 FactoryRegistration<T, any, S> |
74 TypeRegistration<new () => T, S> |
75 FactoryRegistration<() => T, S> |
73 76 ValueRegistration<any> |
74 77 DependencyRegistration<S, keyof S>
75 78 );
@@ -86,11 +89,11 const _activationTypes: { [k in Activati
86 89 call: 5
87 90 };
88 91
89 export function isTypeRegistration(x: any): x is TypeRegistration<any, any, any> {
92 export function isTypeRegistration(x: any): x is TypeRegistration<new () => any, any> {
90 93 return (!isPrimitive(x)) && ("$type" in x);
91 94 }
92 95
93 export function isFactoryRegistration(x: any): x is FactoryRegistration<any, any, any> {
96 export function isFactoryRegistration(x: any): x is FactoryRegistration<() => any, any> {
94 97 return (!isPrimitive(x)) && ("$factory" in x);
95 98 }
96 99
@@ -311,7 +314,7 export class Configuration<S extends obj
311 314 return v;
312 315 }
313 316
314 _makeServiceParams(data: ServiceRegistration<any, any, S>) {
317 _makeServiceParams(data: ServiceRegistration<any, S>) {
315 318 const opts: any = {
316 319 owner: this._container
317 320 };
@@ -365,7 +368,7 export class Configuration<S extends obj
365 368 return d;
366 369 }
367 370
368 async _visitTypeRegistration(data: TypeRegistration<any, any, S>, name: string) {
371 async _visitTypeRegistration(data: TypeRegistration<new () => any, S>, name: string) {
369 372 argumentNotNull(data.$type, "data.$type");
370 373 this._enter(name);
371 374
@@ -386,7 +389,7 export class Configuration<S extends obj
386 389 return d;
387 390 }
388 391
389 async _visitFactoryRegistration(data: FactoryRegistration<any, any, S>, name: string) {
392 async _visitFactoryRegistration(data: FactoryRegistration<() => any, S>, name: string) {
390 393 argumentOfType(data.$factory, Function, "data.$factory");
391 394 this._enter(name);
392 395
@@ -7,7 +7,8 export const service = define<Bar>();
7 7 foo: dependency("foo"),
8 8 nested: {
9 9 lazy: dependency("foo", {lazy: true})
10 }
10 },
11 host: "value"
11 12 })
12 13 export class Bar {
13 14 barName = "bar";
@@ -18,7 +19,8 export class Bar {
18 19 foo?: Foo;
19 20 nested?: {
20 21 lazy: () => Foo
21 }
22 },
23 host: string
22 24 }) {
23 25
24 26 if (_opts && _opts.foo)
General Comments 0
You need to be logged in to leave comments. Login now