| @@ -1,138 +1,138 | |||
|
|
1 | 1 | import { TraceSource } from "../log/TraceSource"; |
|
|
2 |
import { argumentNotNull, argumentNotEmptyString |
|
|
|
3 |
import { Descriptor, |
|
|
|
2 | import { argumentNotNull, argumentNotEmptyString } from "../safe"; | |
|
|
3 | import { Descriptor, ContainerProvided, ContainerServiceMap, ContainerKeys, ContainerResolve } from "./interfaces"; | |
|
|
4 | 4 | import { Container } from "./Container"; |
|
|
5 | 5 | import { MapOf } from "../interfaces"; |
|
|
6 | 6 | |
|
|
7 | 7 | const trace = TraceSource.get("@implab/core/di/ActivationContext"); |
|
|
8 | 8 | |
|
|
9 | export interface ActivationContextInfo<S> { | |
|
|
9 | export interface ActivationContextInfo<S extends object> { | |
|
|
10 | 10 | name: string; |
|
|
11 | 11 | |
|
|
12 | 12 | service: string; |
|
|
13 | 13 | |
|
|
14 |
scope: |
|
|
|
14 | scope: ContainerServiceMap<S>; | |
|
|
15 | 15 | } |
|
|
16 | 16 | |
|
|
17 | export class ActivationContext<S> { | |
|
|
17 | export class ActivationContext<S extends object> { | |
|
|
18 | 18 | _cache: MapOf<any>; |
|
|
19 | 19 | |
|
|
20 |
_services: |
|
|
|
20 | _services: ContainerServiceMap<S>; | |
|
|
21 | 21 | |
|
|
22 | 22 | _stack: ActivationContextInfo<S>[]; |
|
|
23 | 23 | |
|
|
24 | 24 | _visited: MapOf<any>; |
|
|
25 | 25 | |
|
|
26 | 26 | _name: string; |
|
|
27 | 27 | |
|
|
28 | 28 | _localized: boolean = false; |
|
|
29 | 29 | |
|
|
30 | 30 | container: Container<S>; |
|
|
31 | 31 | |
|
|
32 |
constructor(container: Container<S>, services: |
|
|
|
32 | constructor(container: Container<S>, services: ContainerServiceMap<S>, name?: string, cache?: object, visited?: MapOf<any>) { | |
|
|
33 | 33 | argumentNotNull(container, "container"); |
|
|
34 | 34 | argumentNotNull(services, "services"); |
|
|
35 | 35 | |
|
|
36 | 36 | this._name = name || "<unnamed>"; |
|
|
37 | 37 | this._visited = visited || {}; |
|
|
38 | 38 | this._stack = []; |
|
|
39 | 39 | this._cache = cache || {}; |
|
|
40 | 40 | this._services = services; |
|
|
41 | 41 | this.container = container; |
|
|
42 | 42 | } |
|
|
43 | 43 | |
|
|
44 | 44 | getName() { |
|
|
45 | 45 | return this._name; |
|
|
46 | 46 | } |
|
|
47 | 47 | |
|
|
48 |
resolve<K extends |
|
|
|
48 | resolve<K extends ContainerKeys<S>>(name: K, def?: ContainerResolve<S, K>) { | |
|
|
49 | 49 | const d = this._services[name]; |
|
|
50 | 50 | |
|
|
51 | 51 | if (d !== undefined) { |
|
|
52 |
return this.activate(d |
|
|
|
52 | return this.activate(d, name.toString()); | |
|
|
53 | 53 | } else { |
|
|
54 | 54 | if (def !== undefined && def !== null) |
|
|
55 | 55 | return def; |
|
|
56 | 56 | else |
|
|
57 | 57 | throw new Error(`Service ${name} not found`); |
|
|
58 | 58 | } |
|
|
59 | 59 | } |
|
|
60 | 60 | |
|
|
61 | 61 | /** |
|
|
62 | 62 | * registers services local to the the activation context |
|
|
63 | 63 | * |
|
|
64 | 64 | * @name{string} the name of the service |
|
|
65 | 65 | * @service{string} the service descriptor to register |
|
|
66 | 66 | */ |
|
|
67 | 67 | register<K extends keyof S>(name: K, service: Descriptor<S, S[K]>) { |
|
|
68 | 68 | argumentNotEmptyString(name, "name"); |
|
|
69 | 69 | |
|
|
70 | this._services[name] = service; | |
|
|
70 | this._services[name] = service as any; | |
|
|
71 | 71 | } |
|
|
72 | 72 | |
|
|
73 | 73 | clone() { |
|
|
74 | 74 | return new ActivationContext<S>( |
|
|
75 | 75 | this.container, |
|
|
76 | 76 | this._services, |
|
|
77 | 77 | this._name, |
|
|
78 | 78 | this._cache, |
|
|
79 | 79 | this._visited |
|
|
80 | 80 | ); |
|
|
81 | 81 | } |
|
|
82 | 82 | |
|
|
83 | 83 | has(id: string) { |
|
|
84 | 84 | return id in this._cache; |
|
|
85 | 85 | } |
|
|
86 | 86 | |
|
|
87 | 87 | get<T>(id: string) { |
|
|
88 | 88 | return this._cache[id]; |
|
|
89 | 89 | } |
|
|
90 | 90 | |
|
|
91 | 91 | store(id: string, value: any) { |
|
|
92 | 92 | return (this._cache[id] = value); |
|
|
93 | 93 | } |
|
|
94 | 94 | |
|
|
95 | 95 | activate<T>(d: Descriptor<S, T>, name: string) { |
|
|
96 | 96 | if (trace.isLogEnabled()) |
|
|
97 | 97 | trace.log(`enter ${name} ${d}`); |
|
|
98 | 98 | |
|
|
99 | 99 | this.enter(name, d.toString()); |
|
|
100 | 100 | const v = d.activate(this); |
|
|
101 | 101 | this.leave(); |
|
|
102 | 102 | |
|
|
103 | 103 | if (trace.isLogEnabled()) |
|
|
104 | 104 | trace.log(`leave ${name}`); |
|
|
105 | 105 | |
|
|
106 | 106 | return v; |
|
|
107 | 107 | } |
|
|
108 | 108 | |
|
|
109 | 109 | visit(id: string) { |
|
|
110 | 110 | const count = this._visited[id] || 0; |
|
|
111 | 111 | this._visited[id] = count + 1; |
|
|
112 | 112 | return count; |
|
|
113 | 113 | } |
|
|
114 | 114 | |
|
|
115 | 115 | getStack() { |
|
|
116 | 116 | return this._stack.slice().reverse(); |
|
|
117 | 117 | } |
|
|
118 | 118 | |
|
|
119 | 119 | private enter(name: string, service: string) { |
|
|
120 | 120 | this._stack.push({ |
|
|
121 | 121 | name, |
|
|
122 | 122 | service, |
|
|
123 | 123 | scope: this._services |
|
|
124 | 124 | }); |
|
|
125 | 125 | this._name = name; |
|
|
126 | 126 | this._services = Object.create(this._services); |
|
|
127 | 127 | } |
|
|
128 | 128 | |
|
|
129 | 129 | private leave() { |
|
|
130 | 130 | const ctx = this._stack.pop(); |
|
|
131 | 131 | if (ctx) { |
|
|
132 | 132 | this._services = ctx.scope; |
|
|
133 | 133 | this._name = ctx.name; |
|
|
134 | 134 | } else { |
|
|
135 | 135 | trace.error("Trying to leave the last activation scope"); |
|
|
136 | 136 | } |
|
|
137 | 137 | } |
|
|
138 | 138 | } |
| @@ -1,36 +1,36 | |||
|
|
1 | 1 | import { Descriptor } from "./interfaces"; |
|
|
2 | 2 | import { ActivationContext } from "./ActivationContext"; |
|
|
3 | 3 | import { isPrimitive } from "../safe"; |
|
|
4 | 4 | import { isDescriptor } from "./traits"; |
|
|
5 | 5 | |
|
|
6 | export class AggregateDescriptor<S, T> implements Descriptor<S, T> { | |
|
|
6 | export class AggregateDescriptor<S extends object, T> implements Descriptor<S, T> { | |
|
|
7 | 7 | _value: any; |
|
|
8 | 8 | |
|
|
9 | 9 | constructor(value: any) { |
|
|
10 | 10 | this._value = value; |
|
|
11 | 11 | } |
|
|
12 | 12 | |
|
|
13 | 13 | activate(context: ActivationContext<S>): T { |
|
|
14 | 14 | return this._parse(this._value, context, "$value"); |
|
|
15 | 15 | } |
|
|
16 | 16 | |
|
|
17 | 17 | _parse(value: any, context: ActivationContext<S>, path: string): any { |
|
|
18 | 18 | if (isPrimitive(value)) |
|
|
19 | 19 | return value as any; |
|
|
20 | 20 | |
|
|
21 | 21 | if (isDescriptor(value)) |
|
|
22 | 22 | return context.activate(value, path); |
|
|
23 | 23 | |
|
|
24 | 24 | if (value instanceof Array) |
|
|
25 | 25 | return value.map((x, i) => this._parse(x, context, `${path}[${i}]`)) as any; |
|
|
26 | 26 | |
|
|
27 | 27 | const t: any = {}; |
|
|
28 | 28 | for (const p in value) |
|
|
29 | 29 | t[p] = this._parse(value[p], context, `${path}.${p}`); |
|
|
30 | 30 | return t; |
|
|
31 | 31 | } |
|
|
32 | 32 | |
|
|
33 | 33 | toString() { |
|
|
34 | 34 | return "@walk"; |
|
|
35 | 35 | } |
|
|
36 | 36 | } |
| @@ -1,75 +1,77 | |||
|
|
1 | 1 | import { primitive } from "../safe"; |
|
|
2 | 2 | import { TypeRegistration } from "./Configuration"; |
|
|
3 | 3 | |
|
|
4 | 4 | export interface InjectOptions { |
|
|
5 | 5 | lazy?: boolean; |
|
|
6 | 6 | } |
|
|
7 | 7 | |
|
|
8 | 8 | export interface Dependency<K extends keyof any> { |
|
|
9 | 9 | $dependency: K; |
|
|
10 | 10 | |
|
|
11 | 11 | lazy?: boolean; |
|
|
12 | 12 | } |
|
|
13 | 13 | |
|
|
14 | 14 | export interface Lazy<K extends keyof any> extends Dependency<K> { |
|
|
15 | 15 | lazy: true; |
|
|
16 | 16 | } |
|
|
17 | 17 | |
|
|
18 | 18 | type Compatible<T1, T2> = T2 extends T1 ? any : never; |
|
|
19 | 19 | |
|
|
20 | 20 | type ExtractService<K, S> = K extends keyof S ? S[K] : K; |
|
|
21 | 21 | |
|
|
22 | 22 | type ExtractDependency<D, S> = D extends { $dependency: infer K } ? |
|
|
23 | 23 | D extends { lazy: true } ? () => ExtractService<K, S> : ExtractService<K, S> : |
|
|
24 | 24 | WalkDependencies<D, S>; |
|
|
25 | 25 | |
|
|
26 | 26 | type WalkDependencies<D, S> = D extends primitive ? D : |
|
|
27 | 27 | { [K in keyof D]: ExtractDependency<D[K], S> }; |
|
|
28 | 28 | |
|
|
29 | export class Builder<T, S> { | |
|
|
29 | export class Builder<T, S extends object> { | |
|
|
30 | 30 | declare<P extends any[]>(...args: P) { |
|
|
31 | 31 | return <C extends new (...args: ExtractDependency<P, S>) => T>(constructor: C) => { |
|
|
32 | return constructor as C & { service: Builder<T, S> }; | |
|
|
32 | ||
|
|
33 | 33 | }; |
|
|
34 | 34 | } |
|
|
35 | 35 | |
|
|
36 | 36 | inject<P extends any[]>(...args: P) { |
|
|
37 | 37 | // K = "bar" |
|
|
38 | 38 | // M = "setValue" |
|
|
39 | 39 | // S[K] = Bar |
|
|
40 | 40 | // T[M] = (value: string) => void |
|
|
41 | 41 | // P[m] = (value: V) => void |
|
|
42 | 42 | return <X extends { [m in M]: (...args: any) => any }, M extends keyof (T | X)>( |
|
|
43 | 43 | target: X, |
|
|
44 | 44 | memberName: M, |
|
|
45 | 45 | descriptor: TypedPropertyDescriptor<Compatible<(...args: ExtractDependency<P, S>) => any, T[M]>> |
|
|
46 | 46 | ) => { |
|
|
47 | 47 | |
|
|
48 | 48 | }; |
|
|
49 | 49 | } |
|
|
50 | 50 | |
|
|
51 | 51 | getDescriptor(): TypeRegistration<T, any, S> { |
|
|
52 | 52 | throw new Error(); |
|
|
53 | 53 | } |
|
|
54 | 54 | |
|
|
55 | 55 | } |
|
|
56 | 56 | |
|
|
57 | interface Declaration<S> { | |
|
|
57 | interface Declaration<S extends object> { | |
|
|
58 | 58 | define<T>(): Builder<T, S>; |
|
|
59 | 59 | |
|
|
60 | 60 | dependency<K extends keyof S>(name: K, opts: { lazy: true }): Lazy<K>; |
|
|
61 | 61 | dependency<K extends keyof S>(name: K, opts?: any): Dependency<K>; |
|
|
62 | 62 | |
|
|
63 | 63 | config(): Config<S>; |
|
|
64 | 64 | } |
|
|
65 | 65 | |
|
|
66 | interface ServiceModule<T, S> { | |
|
|
67 |
|
|
|
|
68 | } | |
|
|
66 | type ServiceModule<T, S extends object, M extends string = "service"> = { | |
|
|
67 | [m in M]: Builder<T, S>; | |
|
|
68 | }; | |
|
|
69 | 69 | |
|
|
70 | interface Config<S> { | |
|
|
71 |
register<K extends |
|
|
|
72 |
register<K extends |
|
|
|
70 | export interface Config<S extends object, Y extends keyof S = keyof S> { | |
|
|
71 | register<K extends Y>(name: K, builder: Builder<S[K], S>): Config<S, Exclude<Y, K>>; | |
|
|
72 | register<K extends Y>(name: K, m: Promise<ServiceModule<S[K], S>>): Config<S, Exclude<Y, K>>; | |
|
|
73 | register<K extends Y, M extends string>(name: K, m: Promise<ServiceModule<S[K], S, M>>, x: M): Config<S, Exclude<Y, K>>; | |
|
|
74 | ||
|
|
73 | 75 | } |
|
|
74 | 76 | |
|
|
75 | 77 | export declare function declare<S extends object>(): Declaration<S>; |
| @@ -1,390 +1,403 | |||
|
|
1 | 1 | import { |
|
|
2 | 2 | PartialServiceMap, |
|
|
3 | ActivationType | |
|
|
3 | ActivationType, | |
|
|
4 | ContainerKeys, | |
|
|
5 | ContainerResolve | |
|
|
4 | 6 | } from "./interfaces"; |
|
|
5 | 7 | |
|
|
6 | import { argumentNotEmptyString, isPrimitive, isPromise, delegate, argumentOfType, argumentNotNull, get } from "../safe"; | |
|
|
8 | import { argumentNotEmptyString, isPrimitive, isPromise, delegate, argumentOfType, argumentNotNull, get, primitive } from "../safe"; | |
|
|
7 | 9 | import { AggregateDescriptor } from "./AggregateDescriptor"; |
|
|
8 | 10 | import { ValueDescriptor } from "./ValueDescriptor"; |
|
|
9 | 11 | import { Container } from "./Container"; |
|
|
10 | 12 | import { ReferenceDescriptor } from "./ReferenceDescriptor"; |
|
|
11 | 13 | import { TypeServiceDescriptor } from "./TypeServiceDescriptor"; |
|
|
12 | 14 | import { FactoryServiceDescriptor } from "./FactoryServiceDescriptor"; |
|
|
13 | 15 | import { TraceSource } from "../log/TraceSource"; |
|
|
14 | 16 | import { ConfigError } from "./ConfigError"; |
|
|
15 | 17 | import { Cancellation } from "../Cancellation"; |
|
|
16 | 18 | import { makeResolver } from "./ResolverHelper"; |
|
|
17 |
import { ICancellation |
|
|
|
19 | import { ICancellation } from "../interfaces"; | |
|
|
18 | 20 | import { isDescriptor } from "./traits"; |
|
|
19 | 21 | |
|
|
20 | export interface RegistrationScope<S> { | |
|
|
22 | export interface RegistrationScope<S extends object> { | |
|
|
21 | 23 | |
|
|
22 | 24 | /** ΡΠ΅ΡΠ²ΠΈΡΡ, ΠΊΠΎΡΠΎΡΡΠ΅ ΡΠ΅Π³ΠΈΡΡΡΠΈΡΡΡΡΡΡ Π² ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡΠ΅ Π°ΠΊΡΠΈΠ²Π°ΡΠΈΠΈ ΠΈ ΡΠ°ΠΊΠΈΠΌ ΠΎΠ±ΡΠ°Π·ΠΎΠΌ |
|
|
23 | 25 | * ΠΌΠΎΠ³ΡΡ ΠΏΠ΅ΡΠ΅ΠΎΠΏΡΠ΅Π΄Π΅Π»ΡΡΡ ΡΠ°Π½Π΅Π΅ Π·Π°ΡΠ΅Π³ΠΈΡΡΡΠΈΡΠΎΠ²Π°Π½Π½ΡΠ΅ ΡΠ΅ΡΠ²ΠΈΡΡ. Π·Π° ΡΡΠΎ ΡΠ²ΠΎΠΉΡΡΠ²ΠΎ |
|
|
24 | 26 | * Π½ΡΠΆΠ½ΠΎ ΠΏΠ»Π°ΡΠΈΡΡ, ΠΊΡΠΎΠΌΠ΅ ΡΠΎΠ³ΠΎ ΠΏΠΎΡΡΠ΄ΠΎΠΊ Π°ΠΊΡΠΈΠ²Π°ΡΠΈΠΈ Π±ΡΠ΄Π΅Ρ Π²Π»ΠΈΡΡΡ Π½Π° ΡΠ΅Π·ΡΠ»ΡΡΠ°Ρ |
|
|
25 | 27 | * ΡΠ°Π·ΡΠ΅ΡΠ΅Π½ΠΈΡ Π·Π°Π²ΠΈΡΠΈΠΌΠΎΡΡΠ΅ΠΉ. |
|
|
26 | 28 | */ |
|
|
27 |
services?: |
|
|
|
29 | services?: RegistrationMap<S>; | |
|
|
28 | 30 | } |
|
|
29 | 31 | |
|
|
30 | 32 | /** |
|
|
31 | 33 | * ΠΠ°Π·ΠΎΠ²ΡΠΉ ΠΈΠ½ΡΠ΅ΡΠ΅ΠΉΡ ΠΊΠΎΠ½ΡΠΈΠ³ΡΡΠ°ΡΠΈΠΈ ΡΠ΅ΡΠ²ΠΈΡΠΎΠ² |
|
|
32 | 34 | */ |
|
|
33 | export interface ServiceRegistration<T, P, S> extends RegistrationScope<S> { | |
|
|
35 | export interface ServiceRegistration<T, P, S extends object> extends RegistrationScope<S> { | |
|
|
34 | 36 | |
|
|
35 | 37 | activation?: ActivationType; |
|
|
36 | 38 | |
|
|
37 | 39 | params?: P; |
|
|
38 | 40 | |
|
|
39 | 41 | inject?: object | object[]; |
|
|
40 | 42 | |
|
|
41 | 43 | cleanup?: ((instance: T) => void) | string; |
|
|
42 | 44 | } |
|
|
43 | 45 | |
|
|
44 | export interface TypeRegistration<T, P extends any[], S> extends ServiceRegistration<T, P, S> { | |
|
|
46 | export interface TypeRegistration<T, P extends any[], S extends object> extends ServiceRegistration<T, P, S> { | |
|
|
45 | 47 | $type: string | (new (...params: P) => T); |
|
|
46 | 48 | |
|
|
47 | 49 | } |
|
|
48 | 50 | |
|
|
49 | export interface FactoryRegistration<T, P extends any[], S> extends ServiceRegistration<T, P, S> { | |
|
|
50 |
$factory: string | ( |
|
|
|
51 | export interface FactoryRegistration<T, P extends any[], S extends object> extends ServiceRegistration<T, P, S> { | |
|
|
52 | $factory: string | ((...params: P) => T); | |
|
|
51 | 53 | } |
|
|
52 | 54 | |
|
|
53 | 55 | export interface ValueRegistration<T> { |
|
|
54 | 56 | $value: T; |
|
|
55 | 57 | parse?: boolean; |
|
|
56 | 58 | } |
|
|
57 | 59 | |
|
|
58 |
export interface DependencyRegistration<S, K extends |
|
|
|
60 | export interface DependencyRegistration<S extends object, K extends ContainerKeys<S> = ContainerKeys<S>> extends RegistrationScope<S> { | |
|
|
59 | 61 | $dependency: K; |
|
|
60 | 62 | lazy?: boolean; |
|
|
61 | 63 | optional?: boolean; |
|
|
62 |
default?: S |
|
|
|
64 | default?: ContainerResolve<S, K>; | |
|
|
63 | 65 | } |
|
|
64 | 66 | |
|
|
67 | export type Registration<T, S extends object> = T extends primitive ? T : | |
|
|
68 | ( | |
|
|
69 | T | | |
|
|
70 | { [k in keyof T]: Registration<T[k], S> } | | |
|
|
71 | TypeRegistration<T, any, S> | | |
|
|
72 | FactoryRegistration<T, any, S> | | |
|
|
73 | ValueRegistration<any> | | |
|
|
74 | DependencyRegistration<S, keyof S> | |
|
|
75 | ); | |
|
|
76 | ||
|
|
77 | export type RegistrationMap<S extends object> = { | |
|
|
78 | [k in keyof S]?: Registration<S[k], S>; | |
|
|
79 | }; | |
|
|
80 | ||
|
|
65 | 81 | const _activationTypes: { [k in ActivationType]: number; } = { |
|
|
66 | 82 | singleton: 1, |
|
|
67 | 83 | container: 2, |
|
|
68 | 84 | hierarchy: 3, |
|
|
69 | 85 | context: 4, |
|
|
70 | 86 | call: 5 |
|
|
71 | 87 | }; |
|
|
72 | 88 | |
|
|
73 | 89 | export function isTypeRegistration(x: any): x is TypeRegistration<any, any, any> { |
|
|
74 | 90 | return (!isPrimitive(x)) && ("$type" in x); |
|
|
75 | 91 | } |
|
|
76 | 92 | |
|
|
77 | 93 | export function isFactoryRegistration(x: any): x is FactoryRegistration<any, any, any> { |
|
|
78 | 94 | return (!isPrimitive(x)) && ("$factory" in x); |
|
|
79 | 95 | } |
|
|
80 | 96 | |
|
|
81 | 97 | export function isValueRegistration(x: any): x is ValueRegistration<any> { |
|
|
82 | 98 | return (!isPrimitive(x)) && ("$value" in x); |
|
|
83 | 99 | } |
|
|
84 | 100 | |
|
|
85 | export function isDependencyRegistration<S>(x: any): x is DependencyRegistration<S, keyof S> { | |
|
|
101 | export function isDependencyRegistration<S extends object>(x: any): x is DependencyRegistration<S, keyof S> { | |
|
|
86 | 102 | return (!isPrimitive(x)) && ("$dependency" in x); |
|
|
87 | 103 | } |
|
|
88 | 104 | |
|
|
89 | 105 | export function isActivationType(x: string): x is ActivationType { |
|
|
90 | 106 | return typeof x === "string" && x in _activationTypes; |
|
|
91 | 107 | } |
|
|
92 | 108 | |
|
|
93 | 109 | const trace = TraceSource.get("@implab/core/di/Configuration"); |
|
|
94 | 110 | async function mapAll(data: any[], map?: (v: any, k: number) => any): Promise<any[]>; |
|
|
95 | 111 | async function mapAll(data: any, map?: (v: any, k: string) => any): Promise<any>; |
|
|
96 | 112 | async function mapAll(data: any, map?: (v: any, k: any) => any): Promise<any> { |
|
|
97 | 113 | if (data instanceof Array) { |
|
|
98 | 114 | return Promise.all(map ? data.map(map) : data); |
|
|
99 | 115 | } else { |
|
|
100 | 116 | const keys = Object.keys(data); |
|
|
101 | 117 | |
|
|
102 | 118 | const o: any = {}; |
|
|
103 | 119 | |
|
|
104 | 120 | await Promise.all(keys.map(async k => { |
|
|
105 | 121 | const v = map ? map(data[k], k) : data[k]; |
|
|
106 | 122 | o[k] = isPromise(v) ? await v : v; |
|
|
107 | 123 | })); |
|
|
108 | 124 | |
|
|
109 | 125 | return o; |
|
|
110 | 126 | } |
|
|
111 | 127 | } |
|
|
112 | 128 | |
|
|
113 | 129 | export type ModuleResolver = (moduleName: string, ct?: ICancellation) => any; |
|
|
114 | 130 | |
|
|
115 | export class Configuration<S> { | |
|
|
131 | export class Configuration<S extends object> { | |
|
|
116 | 132 | |
|
|
117 | 133 | _hasInnerDescriptors = false; |
|
|
118 | 134 | |
|
|
119 | _container: Container<S>; | |
|
|
135 | readonly _container: Container<S>; | |
|
|
120 | 136 | |
|
|
121 | 137 | _path: Array<string>; |
|
|
122 | 138 | |
|
|
123 | 139 | _configName: string | undefined; |
|
|
124 | 140 | |
|
|
125 | 141 | _require: ModuleResolver | undefined; |
|
|
126 | 142 | |
|
|
127 | 143 | constructor(container: Container<S>) { |
|
|
128 | 144 | argumentNotNull(container, "container"); |
|
|
129 | 145 | this._container = container; |
|
|
130 | 146 | this._path = []; |
|
|
131 | 147 | } |
|
|
132 | 148 | |
|
|
133 | 149 | async loadConfiguration(moduleName: string, contextRequire?: any, ct = Cancellation.none) { |
|
|
134 | 150 | argumentNotEmptyString(moduleName, "moduleName"); |
|
|
135 | 151 | |
|
|
136 | 152 | trace.log( |
|
|
137 | 153 | "loadConfiguration moduleName={0}, contextRequire={1}", |
|
|
138 | 154 | moduleName, |
|
|
139 | 155 | contextRequire ? typeof (contextRequire) : "<nil>" |
|
|
140 | 156 | ); |
|
|
141 | 157 | |
|
|
142 | 158 | this._configName = moduleName; |
|
|
143 | 159 | |
|
|
144 | 160 | const r = await makeResolver(undefined, contextRequire); |
|
|
145 | 161 | |
|
|
146 | 162 | const config = await r(moduleName, ct); |
|
|
147 | 163 | |
|
|
148 | 164 | await this._applyConfiguration( |
|
|
149 | 165 | config, |
|
|
150 | 166 | await makeResolver(moduleName, contextRequire), |
|
|
151 | 167 | ct |
|
|
152 | 168 | ); |
|
|
153 | 169 | } |
|
|
154 | 170 | |
|
|
155 |
async applyConfiguration(data: |
|
|
|
171 | async applyConfiguration(data: RegistrationMap<S>, contextRequire?: any, ct = Cancellation.none) { | |
|
|
156 | 172 | argumentNotNull(data, "data"); |
|
|
157 | 173 | |
|
|
158 | 174 | await this._applyConfiguration(data, await makeResolver(void (0), contextRequire), ct); |
|
|
159 | 175 | } |
|
|
160 | 176 | |
|
|
161 |
async _applyConfiguration(data: |
|
|
|
177 | async _applyConfiguration(data: RegistrationMap<S>, resolver?: ModuleResolver, ct = Cancellation.none) { | |
|
|
162 | 178 | trace.log("applyConfiguration"); |
|
|
163 | 179 | |
|
|
164 | 180 | this._configName = "$"; |
|
|
165 | 181 | |
|
|
166 | 182 | if (resolver) |
|
|
167 | 183 | this._require = resolver; |
|
|
168 | 184 | |
|
|
169 | 185 | let services: PartialServiceMap<S>; |
|
|
170 | 186 | |
|
|
171 | 187 | try { |
|
|
172 | 188 | services = await this._visitRegistrations(data, "$"); |
|
|
173 | 189 | } catch (e) { |
|
|
174 | 190 | throw this._makeError(e); |
|
|
175 | 191 | } |
|
|
176 | 192 | |
|
|
177 | 193 | this._container.register(services); |
|
|
178 | 194 | } |
|
|
179 | 195 | |
|
|
180 | 196 | _makeError(inner: any) { |
|
|
181 | 197 | const e = new ConfigError("Failed to load configuration", inner); |
|
|
182 | 198 | e.configName = this._configName || "<inline>"; |
|
|
183 | 199 | e.path = this._makePath(); |
|
|
184 | 200 | return e; |
|
|
185 | 201 | } |
|
|
186 | 202 | |
|
|
187 | 203 | _makePath() { |
|
|
188 | 204 | return this._path |
|
|
189 | 205 | .reduce( |
|
|
190 | 206 | (prev, cur) => typeof cur === "number" ? |
|
|
191 | 207 | `${prev}[${cur}]` : |
|
|
192 | 208 | `${prev}.${cur}` |
|
|
193 | 209 | ) |
|
|
194 | 210 | .toString(); |
|
|
195 | 211 | } |
|
|
196 | 212 | |
|
|
197 | 213 | async _resolveType(moduleName: string, localName: string) { |
|
|
198 | 214 | trace.log("resolveType moduleName={0}, localName={1}", moduleName, localName); |
|
|
199 | 215 | try { |
|
|
200 | 216 | const m = await this._loadModule(moduleName); |
|
|
201 | 217 | return localName ? get(localName, m) : m; |
|
|
202 | 218 | } catch (e) { |
|
|
203 | 219 | trace.error("Failed to resolve type moduleName={0}, localName={1}", moduleName, localName); |
|
|
204 | 220 | throw e; |
|
|
205 | 221 | } |
|
|
206 | 222 | } |
|
|
207 | 223 | |
|
|
208 | 224 | _loadModule(moduleName: string) { |
|
|
209 | 225 | trace.debug("loadModule {0}", moduleName); |
|
|
210 | 226 | if (!this._require) |
|
|
211 | 227 | throw new Error("Module loader isn't specified"); |
|
|
212 | 228 | |
|
|
213 | 229 | return this._require(moduleName); |
|
|
214 | 230 | } |
|
|
215 | 231 | |
|
|
216 |
async _visitRegistrations(data: |
|
|
|
232 | async _visitRegistrations(data: RegistrationMap<S>, name: string) { | |
|
|
217 | 233 | this._enter(name); |
|
|
218 | 234 | |
|
|
219 | 235 | if (data.constructor && |
|
|
220 | 236 | data.constructor.prototype !== Object.prototype) |
|
|
221 | 237 | throw new Error("Configuration must be a simple object"); |
|
|
222 | 238 | |
|
|
223 | const o: PartialServiceMap<S> = {}; | |
|
|
224 | const keys = Object.keys(data); | |
|
|
225 | ||
|
|
226 | 239 | const services = await mapAll(data, async (v, k) => { |
|
|
227 | 240 | const d = await this._visit(v, k.toString()); |
|
|
228 | 241 | return isDescriptor(d) ? d : new AggregateDescriptor(d); |
|
|
229 | 242 | }) as PartialServiceMap<S>; |
|
|
230 | 243 | |
|
|
231 | 244 | this._leave(); |
|
|
232 | 245 | |
|
|
233 | 246 | return services; |
|
|
234 | 247 | } |
|
|
235 | 248 | |
|
|
236 | 249 | _enter(name: string) { |
|
|
237 | 250 | this._path.push(name.toString()); |
|
|
238 | 251 | trace.debug(">{0}", name); |
|
|
239 | 252 | } |
|
|
240 | 253 | |
|
|
241 | 254 | _leave() { |
|
|
242 | 255 | const name = this._path.pop(); |
|
|
243 | 256 | trace.debug("<{0}", name); |
|
|
244 | 257 | } |
|
|
245 | 258 | |
|
|
246 | 259 | async _visit(data: any, name: string): Promise<any> { |
|
|
247 | 260 | if (isPrimitive(data) || isDescriptor(data)) |
|
|
248 | 261 | return data; |
|
|
249 | 262 | |
|
|
250 | 263 | if (isDependencyRegistration<S>(data)) { |
|
|
251 | 264 | return this._visitDependencyRegistration(data, name); |
|
|
252 | 265 | } else if (isValueRegistration(data)) { |
|
|
253 | 266 | return this._visitValueRegistration(data, name); |
|
|
254 | 267 | } else if (isTypeRegistration(data)) { |
|
|
255 | 268 | return this._visitTypeRegistration(data, name); |
|
|
256 | 269 | } else if (isFactoryRegistration(data)) { |
|
|
257 | 270 | return this._visitFactoryRegistration(data, name); |
|
|
258 | 271 | } else if (data instanceof Array) { |
|
|
259 | 272 | return this._visitArray(data, name); |
|
|
260 | 273 | } |
|
|
261 | 274 | |
|
|
262 | 275 | return this._visitObject(data, name); |
|
|
263 | 276 | } |
|
|
264 | 277 | |
|
|
265 | 278 | async _visitObject(data: any, name: string) { |
|
|
266 | 279 | if (data.constructor && |
|
|
267 | 280 | data.constructor.prototype !== Object.prototype) |
|
|
268 | 281 | return new ValueDescriptor(data); |
|
|
269 | 282 | |
|
|
270 | 283 | this._enter(name); |
|
|
271 | 284 | |
|
|
272 | 285 | const v = await mapAll(data, delegate(this, "_visit")); |
|
|
273 | 286 | |
|
|
274 | 287 | // TODO: handle inline descriptors properly |
|
|
275 | 288 | // const ex = { |
|
|
276 | 289 | // activate(ctx) { |
|
|
277 | 290 | // const value = ctx.activate(this.prop, "prop"); |
|
|
278 | 291 | // // some code |
|
|
279 | 292 | // }, |
|
|
280 | 293 | // // will be turned to ReferenceDescriptor |
|
|
281 | 294 | // prop: { $dependency: "depName" } |
|
|
282 | 295 | // }; |
|
|
283 | 296 | |
|
|
284 | 297 | this._leave(); |
|
|
285 | 298 | return v; |
|
|
286 | 299 | } |
|
|
287 | 300 | |
|
|
288 | 301 | async _visitArray(data: any[], name: string) { |
|
|
289 | 302 | if (data.constructor && |
|
|
290 | 303 | data.constructor.prototype !== Array.prototype) |
|
|
291 | 304 | return new ValueDescriptor(data); |
|
|
292 | 305 | |
|
|
293 | 306 | this._enter(name); |
|
|
294 | 307 | |
|
|
295 | 308 | const v = await mapAll(data, delegate(this, "_visit")); |
|
|
296 | 309 | this._leave(); |
|
|
297 | 310 | |
|
|
298 | 311 | return v; |
|
|
299 | 312 | } |
|
|
300 | 313 | |
|
|
301 |
_makeServiceParams |
|
|
|
314 | _makeServiceParams(data: ServiceRegistration<any, any, S>) { | |
|
|
302 | 315 | const opts: any = { |
|
|
303 | 316 | owner: this._container |
|
|
304 | 317 | }; |
|
|
305 | 318 | if (data.services) |
|
|
306 | 319 | opts.services = this._visitRegistrations(data.services, "services"); |
|
|
307 | 320 | |
|
|
308 | 321 | if (data.inject) { |
|
|
309 | 322 | this._enter("inject"); |
|
|
310 | 323 | opts.inject = mapAll( |
|
|
311 | 324 | data.inject instanceof Array ? |
|
|
312 | 325 | data.inject : |
|
|
313 | 326 | [data.inject], |
|
|
314 | 327 | delegate(this, "_visitObject") |
|
|
315 | 328 | ); |
|
|
316 | 329 | this._leave(); |
|
|
317 | 330 | } |
|
|
318 | 331 | |
|
|
319 | 332 | if ("params" in data) |
|
|
320 | 333 | opts.params = data.params instanceof Array ? |
|
|
321 | 334 | this._visitArray(data.params, "params") : |
|
|
322 | 335 | this._visit(data.params, "params"); |
|
|
323 | 336 | |
|
|
324 | 337 | if (data.activation) { |
|
|
325 | 338 | opts.activation = data.activation; |
|
|
326 | 339 | } |
|
|
327 | 340 | |
|
|
328 | 341 | if (data.cleanup) |
|
|
329 | 342 | opts.cleanup = data.cleanup; |
|
|
330 | 343 | |
|
|
331 | 344 | return opts; |
|
|
332 | 345 | } |
|
|
333 | 346 | |
|
|
334 | 347 | async _visitValueRegistration<T>(data: ValueRegistration<T>, name: string) { |
|
|
335 | 348 | this._enter(name); |
|
|
336 | 349 | const d = data.parse ? new AggregateDescriptor(data.$value) : new ValueDescriptor(data.$value); |
|
|
337 | 350 | this._leave(); |
|
|
338 | 351 | return d; |
|
|
339 | 352 | } |
|
|
340 | 353 | |
|
|
341 | 354 | async _visitDependencyRegistration<K extends keyof S>(data: DependencyRegistration<S, K>, name: string) { |
|
|
342 | 355 | argumentNotEmptyString(data && data.$dependency, "data.$dependency"); |
|
|
343 | 356 | this._enter(name); |
|
|
344 | 357 | const d = new ReferenceDescriptor<S, K>({ |
|
|
345 | 358 | name: data.$dependency, |
|
|
346 | 359 | lazy: data.lazy, |
|
|
347 | 360 | optional: data.optional, |
|
|
348 | 361 | default: data.default, |
|
|
349 | 362 | services: data.services && await this._visitRegistrations(data.services, "services") |
|
|
350 | 363 | }); |
|
|
351 | 364 | this._leave(); |
|
|
352 | 365 | return d; |
|
|
353 | 366 | } |
|
|
354 | 367 | |
|
|
355 | 368 | async _visitTypeRegistration(data: TypeRegistration<any, any, S>, name: string) { |
|
|
356 | 369 | argumentNotNull(data.$type, "data.$type"); |
|
|
357 | 370 | this._enter(name); |
|
|
358 | 371 | |
|
|
359 | 372 | const opts = this._makeServiceParams(data); |
|
|
360 | 373 | if (data.$type instanceof Function) { |
|
|
361 | 374 | opts.type = data.$type; |
|
|
362 | 375 | } else { |
|
|
363 | 376 | const [moduleName, typeName] = data.$type.split(":", 2); |
|
|
364 | 377 | opts.type = this._resolveType(moduleName, typeName); |
|
|
365 | 378 | } |
|
|
366 | 379 | |
|
|
367 | 380 | const d = new TypeServiceDescriptor<S, any, any[]>( |
|
|
368 | 381 | await mapAll(opts) |
|
|
369 | 382 | ); |
|
|
370 | 383 | |
|
|
371 | 384 | this._leave(); |
|
|
372 | 385 | |
|
|
373 | 386 | return d; |
|
|
374 | 387 | } |
|
|
375 | 388 | |
|
|
376 | 389 | async _visitFactoryRegistration(data: FactoryRegistration<any, any, S>, name: string) { |
|
|
377 | 390 | argumentOfType(data.$factory, Function, "data.$factory"); |
|
|
378 | 391 | this._enter(name); |
|
|
379 | 392 | |
|
|
380 | 393 | const opts = this._makeServiceParams(data); |
|
|
381 | 394 | opts.factory = data.$factory; |
|
|
382 | 395 | |
|
|
383 | 396 | const d = new FactoryServiceDescriptor<S, any, any[]>( |
|
|
384 | 397 | await mapAll(opts) |
|
|
385 | 398 | ); |
|
|
386 | 399 | |
|
|
387 | 400 | this._leave(); |
|
|
388 | 401 | return d; |
|
|
389 | 402 | } |
|
|
390 | 403 | } |
| @@ -1,140 +1,140 | |||
|
|
1 | 1 | import { ActivationContext } from "./ActivationContext"; |
|
|
2 | 2 | import { ValueDescriptor } from "./ValueDescriptor"; |
|
|
3 | 3 | import { ActivationError } from "./ActivationError"; |
|
|
4 |
import { ServiceMap, Descriptor, PartialServiceMap, Container |
|
|
|
4 | import { ServiceMap, Descriptor, PartialServiceMap, ContainerProvided, Resolver, ContainerServiceMap, ContainerKeys, ContainerResolve } from "./interfaces"; | |
|
|
5 | 5 | import { TraceSource } from "../log/TraceSource"; |
|
|
6 | import { Configuration } from "./Configuration"; | |
|
|
6 | import { Configuration, RegistrationMap } from "./Configuration"; | |
|
|
7 | 7 | import { Cancellation } from "../Cancellation"; |
|
|
8 | 8 | import { MapOf } from "../interfaces"; |
|
|
9 | 9 | import { isDescriptor } from "./traits"; |
|
|
10 | 10 | |
|
|
11 | 11 | const trace = TraceSource.get("@implab/core/di/ActivationContext"); |
|
|
12 | 12 | |
|
|
13 | export class Container<S = any> implements Resolver<S> { | |
|
|
14 |
readonly _services: |
|
|
|
13 | export class Container<S extends object = any> implements Resolver<S> { | |
|
|
14 | readonly _services: ContainerServiceMap<S>; | |
|
|
15 | 15 | |
|
|
16 | 16 | readonly _cache: MapOf<any>; |
|
|
17 | 17 | |
|
|
18 | 18 | readonly _cleanup: (() => void)[]; |
|
|
19 | 19 | |
|
|
20 | 20 | readonly _root: Container<S>; |
|
|
21 | 21 | |
|
|
22 | 22 | readonly _parent?: Container<S>; |
|
|
23 | 23 | |
|
|
24 | 24 | _disposed: boolean; |
|
|
25 | 25 | |
|
|
26 | 26 | constructor(parent?: Container<S>) { |
|
|
27 | 27 | this._parent = parent; |
|
|
28 | 28 | this._services = parent ? Object.create(parent._services) : {}; |
|
|
29 | 29 | this._cache = {}; |
|
|
30 | 30 | this._cleanup = []; |
|
|
31 | 31 | this._root = parent ? parent.getRootContainer() : this; |
|
|
32 | 32 | this._services.container = new ValueDescriptor(this) as any; |
|
|
33 | 33 | this._disposed = false; |
|
|
34 | 34 | } |
|
|
35 | 35 | |
|
|
36 | 36 | getRootContainer() { |
|
|
37 | 37 | return this._root; |
|
|
38 | 38 | } |
|
|
39 | 39 | |
|
|
40 | 40 | getParent() { |
|
|
41 | 41 | return this._parent; |
|
|
42 | 42 | } |
|
|
43 | 43 | |
|
|
44 | resolve<K extends keyof ContainerServices<S>, T extends ContainerServices<S>[K] = ContainerServices<S>[K]>(name: K, def?: T): T { | |
|
|
44 | resolve<K extends ContainerKeys<S>>(name: K, def?: ContainerResolve<S, K>): ContainerResolve<S, K> { | |
|
|
45 | 45 | trace.debug("resolve {0}", name); |
|
|
46 | 46 | const d = this._services[name]; |
|
|
47 | 47 | if (d === undefined) { |
|
|
48 | 48 | if (def !== undefined) |
|
|
49 | 49 | return def; |
|
|
50 | 50 | else |
|
|
51 | 51 | throw new Error("Service '" + name + "' isn't found"); |
|
|
52 | 52 | } else { |
|
|
53 | 53 | |
|
|
54 | 54 | const context = new ActivationContext<S>(this, this._services); |
|
|
55 | 55 | try { |
|
|
56 |
return context.activate(d |
|
|
|
56 | return context.activate(d, name.toString()); | |
|
|
57 | 57 | } catch (error) { |
|
|
58 | 58 | throw new ActivationError(name.toString(), context.getStack(), error); |
|
|
59 | 59 | } |
|
|
60 | 60 | } |
|
|
61 | 61 | } |
|
|
62 | 62 | |
|
|
63 | 63 | /** |
|
|
64 | 64 | * @deprecated use resolve() method |
|
|
65 | 65 | */ |
|
|
66 | getService<K extends keyof S, T extends ContainerServices<S>[K] = ContainerServices<S>[K]>(name: K, def?: T) { | |
|
|
66 | getService<K extends ContainerKeys<S>>(name: K, def?: ContainerResolve<S, K>) { | |
|
|
67 | 67 | return this.resolve(name, def); |
|
|
68 | 68 | } |
|
|
69 | 69 | |
|
|
70 | 70 | register<K extends keyof S>(name: K, service: Descriptor<S, S[K]>): this; |
|
|
71 | 71 | register(services: PartialServiceMap<S>): this; |
|
|
72 | 72 | register<K extends keyof S>(nameOrCollection: K | ServiceMap<S>, service?: Descriptor<S, S[K]>) { |
|
|
73 | 73 | if (arguments.length === 1) { |
|
|
74 | 74 | const data = nameOrCollection as ServiceMap<S>; |
|
|
75 | 75 | |
|
|
76 | 76 | for (const name in data) { |
|
|
77 | 77 | if (Object.prototype.hasOwnProperty.call(data, name)) { |
|
|
78 | 78 | this.register(name, data[name] as Descriptor<S, S[keyof S]>); |
|
|
79 | 79 | } |
|
|
80 | 80 | } |
|
|
81 | 81 | } else { |
|
|
82 | 82 | if (!isDescriptor(service)) |
|
|
83 | 83 | throw new Error("The service parameter must be a descriptor"); |
|
|
84 | 84 | |
|
|
85 | 85 | this._services[nameOrCollection as K] = service as any; |
|
|
86 | 86 | } |
|
|
87 | 87 | return this; |
|
|
88 | 88 | } |
|
|
89 | 89 | |
|
|
90 | 90 | onDispose(callback: () => void) { |
|
|
91 | 91 | if (!(callback instanceof Function)) |
|
|
92 | 92 | throw new Error("The callback must be a function"); |
|
|
93 | 93 | this._cleanup.push(callback); |
|
|
94 | 94 | } |
|
|
95 | 95 | |
|
|
96 | 96 | dispose() { |
|
|
97 | 97 | if (this._disposed) |
|
|
98 | 98 | return; |
|
|
99 | 99 | this._disposed = true; |
|
|
100 | 100 | for (const f of this._cleanup) |
|
|
101 | 101 | f(); |
|
|
102 | 102 | } |
|
|
103 | 103 | |
|
|
104 | 104 | /** |
|
|
105 | 105 | * @param{String|Object} config |
|
|
106 | 106 | * The configuration of the contaier. Can be either a string or an object, |
|
|
107 | 107 | * if the configuration is an object it's treated as a collection of |
|
|
108 | 108 | * services which will be registed in the contaier. |
|
|
109 | 109 | * |
|
|
110 | 110 | * @param{Function} opts.contextRequire |
|
|
111 | 111 | * The function which will be used to load a configuration or types for services. |
|
|
112 | 112 | * |
|
|
113 | 113 | */ |
|
|
114 |
async configure(config: string | |
|
|
|
114 | async configure(config: string | RegistrationMap<S>, opts?: any, ct = Cancellation.none) { | |
|
|
115 | 115 | const c = new Configuration<S>(this); |
|
|
116 | 116 | |
|
|
117 | 117 | if (typeof (config) === "string") { |
|
|
118 | 118 | return c.loadConfiguration(config, opts && opts.contextRequire, ct); |
|
|
119 | 119 | } else { |
|
|
120 | 120 | return c.applyConfiguration(config, opts && opts.contextRequire, ct); |
|
|
121 | 121 | } |
|
|
122 | 122 | } |
|
|
123 | 123 | |
|
|
124 |
createChildContainer<S2 extends |
|
|
|
124 | createChildContainer<S2 extends object = S>(): Container<S & S2> { | |
|
|
125 | 125 | return new Container<S & S2>(this as any); |
|
|
126 | 126 | } |
|
|
127 | 127 | |
|
|
128 | 128 | has(id: string | number) { |
|
|
129 | 129 | return id in this._cache; |
|
|
130 | 130 | } |
|
|
131 | 131 | |
|
|
132 | 132 | get(id: string | number) { |
|
|
133 | 133 | return this._cache[id]; |
|
|
134 | 134 | } |
|
|
135 | 135 | |
|
|
136 | 136 | store(id: string | number, value: any) { |
|
|
137 | 137 | return (this._cache[id] = value); |
|
|
138 | 138 | } |
|
|
139 | 139 | |
|
|
140 | 140 | } |
| @@ -1,21 +1,21 | |||
|
|
1 | 1 | import { ServiceDescriptor, ServiceDescriptorParams } from "./ServiceDescriptor"; |
|
|
2 | 2 | import { argumentNotNull, oid } from "../safe"; |
|
|
3 | 3 | |
|
|
4 | export interface FactoryServiceDescriptorParams<S, T, P extends any[]> extends ServiceDescriptorParams<S, T, P> { | |
|
|
4 | export interface FactoryServiceDescriptorParams<S extends object, T, P extends any[]> extends ServiceDescriptorParams<S, T, P> { | |
|
|
5 | 5 | factory: (...args: P) => T; |
|
|
6 | 6 | } |
|
|
7 | 7 | |
|
|
8 | export class FactoryServiceDescriptor<S, T, P extends any[]> extends ServiceDescriptor<S, T, P> { | |
|
|
8 | export class FactoryServiceDescriptor<S extends object, T, P extends any[]> extends ServiceDescriptor<S, T, P> { | |
|
|
9 | 9 | constructor(opts: FactoryServiceDescriptorParams<S, T, P>) { |
|
|
10 | 10 | super(opts); |
|
|
11 | 11 | |
|
|
12 | 12 | argumentNotNull(opts && opts.factory, "opts.factory"); |
|
|
13 | 13 | |
|
|
14 | 14 | // bind to null |
|
|
15 | 15 | this._factory = (...args) => opts.factory.apply(null, args as any); |
|
|
16 | 16 | |
|
|
17 | 17 | if (opts.activation === "singleton") { |
|
|
18 | 18 | this._cacheId = oid(opts.factory); |
|
|
19 | 19 | } |
|
|
20 | 20 | } |
|
|
21 | 21 | } |
| @@ -1,99 +1,95 | |||
|
|
1 |
import { |
|
|
|
1 | import { argumentNotEmptyString, each } from "../safe"; | |
|
|
2 | 2 | import { ActivationContext } from "./ActivationContext"; |
|
|
3 |
import { |
|
|
|
3 | import { Descriptor, PartialServiceMap, ContainerResolve, ContainerKeys } from "./interfaces"; | |
|
|
4 | 4 | import { ActivationError } from "./ActivationError"; |
|
|
5 | 5 | |
|
|
6 |
export interface ReferenceDescriptorParams<S, K extends |
|
|
|
6 | export interface ReferenceDescriptorParams<S extends object, K extends ContainerKeys<S>> { | |
|
|
7 | 7 | name: K; |
|
|
8 | 8 | lazy?: boolean; |
|
|
9 | 9 | optional?: boolean; |
|
|
10 |
default?: S |
|
|
|
10 | default?: ContainerResolve<S, K>; | |
|
|
11 | 11 | services?: PartialServiceMap<S>; |
|
|
12 | 12 | } |
|
|
13 | 13 | |
|
|
14 | function defined<T>(v: T | undefined) { | |
|
|
15 | if (v === undefined) | |
|
|
16 | throw Error(); | |
|
|
17 | return v; | |
|
|
18 | } | |
|
|
14 | export class ReferenceDescriptor<S extends object = any, K extends ContainerKeys<S> = ContainerKeys<S>> | |
|
|
15 | implements Descriptor<S, ContainerResolve<S, K> | ((args?: PartialServiceMap<S>) => ContainerResolve<S, K>)> { | |
|
|
19 | 16 | |
|
|
20 | export class ReferenceDescriptor<S = any, K extends keyof S = keyof S> implements Descriptor<S, S[K] | ((args?: PartialServiceMap<S> ) => S[K])> { | |
|
|
21 | 17 | _name: K; |
|
|
22 | 18 | |
|
|
23 | 19 | _lazy = false; |
|
|
24 | 20 | |
|
|
25 | 21 | _optional = false; |
|
|
26 | 22 | |
|
|
27 |
_default: S |
|
|
|
23 | _default: ContainerResolve<S, K> | undefined; | |
|
|
28 | 24 | |
|
|
29 | 25 | _services: PartialServiceMap<S>; |
|
|
30 | 26 | |
|
|
31 | 27 | constructor(opts: ReferenceDescriptorParams<S, K>) { |
|
|
32 | 28 | argumentNotEmptyString(opts && opts.name, "opts.name"); |
|
|
33 | 29 | this._name = opts.name; |
|
|
34 | 30 | this._lazy = !!opts.lazy; |
|
|
35 | 31 | this._optional = !!opts.optional; |
|
|
36 | 32 | this._default = opts.default; |
|
|
37 | 33 | |
|
|
38 | this._services = (opts.services || {}) as ServiceMap<S>; | |
|
|
34 | this._services = (opts.services || {}) as PartialServiceMap<S>; | |
|
|
39 | 35 | } |
|
|
40 | 36 | |
|
|
41 | 37 | activate(context: ActivationContext<S>) { |
|
|
42 | 38 | // Π΄ΠΎΠ±Π°Π²Π»ΡΠ΅ΠΌ ΡΠ΅ΡΠ²ΠΈΡΡ |
|
|
43 | 39 | if (this._services) { |
|
|
44 | 40 | each(this._services, (v, k) => context.register(k, v)); |
|
|
45 | 41 | } |
|
|
46 | 42 | |
|
|
47 | 43 | if (this._lazy) { |
|
|
48 | 44 | const saved = context.clone(); |
|
|
49 | 45 | |
|
|
50 | 46 | return (cfg?: PartialServiceMap<S>) => { |
|
|
51 | 47 | // Π·Π°ΡΠΈΡΠ°Π΅ΠΌ ΠΊΠΎΠ½ΡΠ΅ΠΊΡΡ Π½Π° ΡΠ»ΡΡΠ°ΠΉ ΠΈΡΠΊΠ»ΡΡΠ΅Π½ΠΈΡ Π² ΠΏΡΠΎΡΠ΅ΡΡΠ΅ |
|
|
52 | 48 | // Π°ΠΊΡΠΈΠ²Π°ΡΠΈΠΈ |
|
|
53 | 49 | const ct = saved.clone(); |
|
|
54 | 50 | try { |
|
|
55 | 51 | if (cfg) { |
|
|
56 | 52 | each(cfg, (v, k) => ct.register(k, v)); |
|
|
57 | 53 | } |
|
|
58 | 54 | |
|
|
59 | 55 | return this._optional ? ct.resolve(this._name, this._default) : ct |
|
|
60 | 56 | .resolve(this._name); |
|
|
61 | 57 | } catch (error) { |
|
|
62 | 58 | throw new ActivationError(this._name.toString(), ct.getStack(), error); |
|
|
63 | 59 | } |
|
|
64 | 60 | }; |
|
|
65 | 61 | } else { |
|
|
66 | 62 | const v = this._optional ? |
|
|
67 | 63 | context.resolve(this._name, this._default) : |
|
|
68 | 64 | context.resolve(this._name); |
|
|
69 | 65 | |
|
|
70 | 66 | return v; |
|
|
71 | 67 | } |
|
|
72 | 68 | } |
|
|
73 | 69 | |
|
|
74 | 70 | toString() { |
|
|
75 | 71 | const opts = []; |
|
|
76 | 72 | if (this._optional) |
|
|
77 | 73 | opts.push("optional"); |
|
|
78 | 74 | if (this._lazy) |
|
|
79 | 75 | opts.push("lazy"); |
|
|
80 | 76 | |
|
|
81 | 77 | const parts = [ |
|
|
82 | 78 | "@ref " |
|
|
83 | 79 | ]; |
|
|
84 | 80 | if (opts.length) { |
|
|
85 | 81 | parts.push("{"); |
|
|
86 | 82 | parts.push(opts.join()); |
|
|
87 | 83 | parts.push("} "); |
|
|
88 | 84 | } |
|
|
89 | 85 | |
|
|
90 | 86 | parts.push(this._name.toString()); |
|
|
91 | 87 | |
|
|
92 | 88 | if (this._default !== undefined && this._default !== null) { |
|
|
93 | 89 | parts.push(" = "); |
|
|
94 | 90 | parts.push(String(this._default)); |
|
|
95 | 91 | } |
|
|
96 | 92 | |
|
|
97 | 93 | return parts.join(""); |
|
|
98 | 94 | } |
|
|
99 | 95 | } |
| @@ -1,239 +1,239 | |||
|
|
1 | 1 | import { ActivationContext } from "./ActivationContext"; |
|
|
2 | 2 | import { Descriptor, ServiceMap, PartialServiceMap, ActivationType } from "./interfaces"; |
|
|
3 | 3 | import { Container } from "./Container"; |
|
|
4 | 4 | import { argumentNotNull, isPrimitive, keys, isNull } from "../safe"; |
|
|
5 | 5 | import { TraceSource } from "../log/TraceSource"; |
|
|
6 | 6 | import { isDescriptor } from "./traits"; |
|
|
7 | 7 | |
|
|
8 | 8 | let cacheId = 0; |
|
|
9 | 9 | |
|
|
10 | 10 | const trace = TraceSource.get("@implab/core/di/ActivationContext"); |
|
|
11 | 11 | |
|
|
12 | function injectMethod<T, M extends keyof T, S, A>(target: T, method: M, context: ActivationContext<S>, args: A) { | |
|
|
12 | function injectMethod<T, M extends keyof T, S extends object, A>(target: T, method: M, context: ActivationContext<S>, args: A) { | |
|
|
13 | 13 | |
|
|
14 | 14 | const m = target[method]; |
|
|
15 | 15 | if (!m || typeof m !== "function") |
|
|
16 | 16 | throw new Error("Method '" + method + "' not found"); |
|
|
17 | 17 | |
|
|
18 | 18 | if (args instanceof Array) |
|
|
19 | 19 | return m.apply(target, _parse(args, context, "." + method)); |
|
|
20 | 20 | else |
|
|
21 | 21 | return m.call(target, _parse(args, context, "." + method)); |
|
|
22 | 22 | } |
|
|
23 | 23 | |
|
|
24 | 24 | function makeClenupCallback<T>(target: T, method: Cleaner<T>): () => void; |
|
|
25 | 25 | function makeClenupCallback(target: any, method: any) { |
|
|
26 | 26 | if (typeof (method) === "string") { |
|
|
27 | 27 | return () => { |
|
|
28 | 28 | target[method](); |
|
|
29 | 29 | }; |
|
|
30 | 30 | } else { |
|
|
31 | 31 | return () => { |
|
|
32 | 32 | method(target); |
|
|
33 | 33 | }; |
|
|
34 | 34 | } |
|
|
35 | 35 | } |
|
|
36 | 36 | |
|
|
37 | 37 | function _parse(value: any, context: ActivationContext<any>, path: string): any { |
|
|
38 | 38 | if (isPrimitive(value)) |
|
|
39 | 39 | return value as any; |
|
|
40 | 40 | |
|
|
41 | 41 | trace.debug("parse {0}", path); |
|
|
42 | 42 | |
|
|
43 | 43 | if (isDescriptor(value)) |
|
|
44 | 44 | return context.activate(value, path); |
|
|
45 | 45 | |
|
|
46 | 46 | if (value instanceof Array) |
|
|
47 | 47 | return value.map((x, i) => _parse(x, context, `${path}[${i}]`)) as any; |
|
|
48 | 48 | |
|
|
49 | 49 | const t: any = {}; |
|
|
50 | 50 | |
|
|
51 | 51 | keys(value).forEach(p => t[p] = _parse(value[p], context, `${path}.${p}`)); |
|
|
52 | 52 | |
|
|
53 | 53 | return t; |
|
|
54 | 54 | } |
|
|
55 | 55 | |
|
|
56 | 56 | export type Cleaner<T> = ((x: T) => void) | keyof Extract<T, { [M in keyof T]: () => void }>; |
|
|
57 | 57 | |
|
|
58 | 58 | export type InjectionSpec<T> = { |
|
|
59 | 59 | [m in keyof T]?: any; |
|
|
60 | 60 | }; |
|
|
61 | 61 | |
|
|
62 | export interface ServiceDescriptorParams<S, T, P extends any[]> { | |
|
|
62 | export interface ServiceDescriptorParams<S extends object, T, P extends any[]> { | |
|
|
63 | 63 | activation?: ActivationType; |
|
|
64 | 64 | |
|
|
65 | 65 | owner: Container<S>; |
|
|
66 | 66 | |
|
|
67 | 67 | params?: P; |
|
|
68 | 68 | |
|
|
69 | 69 | inject?: InjectionSpec<T>[]; |
|
|
70 | 70 | |
|
|
71 | 71 | services?: PartialServiceMap<S>; |
|
|
72 | 72 | |
|
|
73 | 73 | cleanup?: Cleaner<T>; |
|
|
74 | 74 | } |
|
|
75 | 75 | |
|
|
76 | export class ServiceDescriptor<S, T, P extends any[]> implements Descriptor<S, T> { | |
|
|
76 | export class ServiceDescriptor<S extends object, T, P extends any[]> implements Descriptor<S, T> { | |
|
|
77 | 77 | _instance: T | undefined; |
|
|
78 | 78 | |
|
|
79 | 79 | _hasInstance = false; |
|
|
80 | 80 | |
|
|
81 | 81 | _activationType: ActivationType = "call"; |
|
|
82 | 82 | |
|
|
83 | 83 | _services: ServiceMap<S>; |
|
|
84 | 84 | |
|
|
85 | 85 | _params: P | undefined; |
|
|
86 | 86 | |
|
|
87 | 87 | _inject: InjectionSpec<T>[]; |
|
|
88 | 88 | |
|
|
89 | 89 | _cleanup: Cleaner<T> | undefined; |
|
|
90 | 90 | |
|
|
91 | 91 | _cacheId: any; |
|
|
92 | 92 | |
|
|
93 | 93 | _owner: Container<S>; |
|
|
94 | 94 | |
|
|
95 | 95 | constructor(opts: ServiceDescriptorParams<S, T, P>) { |
|
|
96 | 96 | argumentNotNull(opts, "opts"); |
|
|
97 | 97 | argumentNotNull(opts.owner, "owner"); |
|
|
98 | 98 | |
|
|
99 | 99 | this._owner = opts.owner; |
|
|
100 | 100 | |
|
|
101 | 101 | if (!isNull(opts.activation)) |
|
|
102 | 102 | this._activationType = opts.activation; |
|
|
103 | 103 | |
|
|
104 | 104 | if (!isNull(opts.params)) |
|
|
105 | 105 | this._params = opts.params; |
|
|
106 | 106 | |
|
|
107 | 107 | this._inject = opts.inject || []; |
|
|
108 | 108 | |
|
|
109 | 109 | this._services = (opts.services || {}) as ServiceMap<S>; |
|
|
110 | 110 | |
|
|
111 | 111 | if (opts.cleanup) { |
|
|
112 | 112 | if (!(typeof (opts.cleanup) === "string" || opts.cleanup instanceof Function)) |
|
|
113 | 113 | throw new Error( |
|
|
114 | 114 | "The cleanup parameter must be either a function or a function name"); |
|
|
115 | 115 | |
|
|
116 | 116 | this._cleanup = opts.cleanup; |
|
|
117 | 117 | } |
|
|
118 | 118 | } |
|
|
119 | 119 | |
|
|
120 | 120 | activate(context: ActivationContext<S>) { |
|
|
121 | 121 | // if we have a local service records, register them first |
|
|
122 | 122 | let instance: T; |
|
|
123 | 123 | |
|
|
124 | 124 | // ensure we have a cache id |
|
|
125 | 125 | if (!this._cacheId) |
|
|
126 | 126 | this._cacheId = ++cacheId; |
|
|
127 | 127 | |
|
|
128 | 128 | switch (this._activationType) { |
|
|
129 | 129 | case "singleton": // SINGLETON |
|
|
130 | 130 | // if the value is cached return it |
|
|
131 | 131 | if (this._hasInstance) |
|
|
132 | 132 | return this._instance; |
|
|
133 | 133 | |
|
|
134 | 134 | // singletons are bound to the root container |
|
|
135 | 135 | const container = context.container.getRootContainer(); |
|
|
136 | 136 | |
|
|
137 | 137 | if (container.has(this._cacheId)) { |
|
|
138 | 138 | instance = container.get(this._cacheId); |
|
|
139 | 139 | } else { |
|
|
140 | 140 | instance = this._create(context); |
|
|
141 | 141 | container.store(this._cacheId, instance); |
|
|
142 | 142 | if (this._cleanup) |
|
|
143 | 143 | container.onDispose( |
|
|
144 | 144 | makeClenupCallback(instance, this._cleanup)); |
|
|
145 | 145 | } |
|
|
146 | 146 | |
|
|
147 | 147 | this._hasInstance = true; |
|
|
148 | 148 | return (this._instance = instance); |
|
|
149 | 149 | |
|
|
150 | 150 | case "container": // CONTAINER |
|
|
151 | 151 | // return a cached value |
|
|
152 | 152 | |
|
|
153 | 153 | if (this._hasInstance) |
|
|
154 | 154 | return this._instance; |
|
|
155 | 155 | |
|
|
156 | 156 | // create an instance |
|
|
157 | 157 | instance = this._create(context); |
|
|
158 | 158 | |
|
|
159 | 159 | // the instance is bound to the container |
|
|
160 | 160 | if (this._cleanup) |
|
|
161 | 161 | this._owner.onDispose( |
|
|
162 | 162 | makeClenupCallback(instance, this._cleanup)); |
|
|
163 | 163 | |
|
|
164 | 164 | // cache and return the instance |
|
|
165 | 165 | this._hasInstance = true; |
|
|
166 | 166 | return (this._instance = instance); |
|
|
167 | 167 | case "context": // CONTEXT |
|
|
168 | 168 | // return a cached value if one exists |
|
|
169 | 169 | |
|
|
170 | 170 | if (context.has(this._cacheId)) |
|
|
171 | 171 | return context.get(this._cacheId); |
|
|
172 | 172 | // context context activated instances are controlled by callers |
|
|
173 | 173 | return context.store(this._cacheId, this._create(context)); |
|
|
174 | 174 | case "call": // CALL |
|
|
175 | 175 | // per-call created instances are controlled by callers |
|
|
176 | 176 | return this._create(context); |
|
|
177 | 177 | case "hierarchy": // HIERARCHY |
|
|
178 | 178 | // hierarchy activated instances are behave much like container activated |
|
|
179 | 179 | // except they are created and bound to the child container |
|
|
180 | 180 | |
|
|
181 | 181 | // return a cached value |
|
|
182 | 182 | if (context.container.has(this._cacheId)) |
|
|
183 | 183 | return context.container.get(this._cacheId); |
|
|
184 | 184 | |
|
|
185 | 185 | instance = this._create(context); |
|
|
186 | 186 | |
|
|
187 | 187 | if (this._cleanup) |
|
|
188 | 188 | context.container.onDispose(makeClenupCallback( |
|
|
189 | 189 | instance, |
|
|
190 | 190 | this._cleanup)); |
|
|
191 | 191 | |
|
|
192 | 192 | return context.container.store(this._cacheId, instance); |
|
|
193 | 193 | default: |
|
|
194 | 194 | throw new Error("Invalid activation type: " + this._activationType); |
|
|
195 | 195 | } |
|
|
196 | 196 | } |
|
|
197 | 197 | |
|
|
198 | 198 | isInstanceCreated() { |
|
|
199 | 199 | return this._hasInstance; |
|
|
200 | 200 | } |
|
|
201 | 201 | |
|
|
202 | 202 | getInstance() { |
|
|
203 | 203 | return this._instance; |
|
|
204 | 204 | } |
|
|
205 | 205 | |
|
|
206 | 206 | _factory(...params: any[]): T { |
|
|
207 | 207 | throw Error("Not implemented"); |
|
|
208 | 208 | } |
|
|
209 | 209 | |
|
|
210 | 210 | _create(context: ActivationContext<S>) { |
|
|
211 | 211 | trace.debug(`constructing ${context._name}`); |
|
|
212 | 212 | |
|
|
213 | 213 | if (this._activationType !== "call" && |
|
|
214 | 214 | context.visit(this._cacheId) > 0) |
|
|
215 | 215 | throw new Error("Recursion detected"); |
|
|
216 | 216 | |
|
|
217 | 217 | if (this._services) { |
|
|
218 | 218 | keys(this._services).forEach(p => context.register(p, this._services[p])); |
|
|
219 | 219 | } |
|
|
220 | 220 | |
|
|
221 | 221 | let instance: T; |
|
|
222 | 222 | |
|
|
223 | 223 | if (this._params === undefined) { |
|
|
224 | 224 | instance = this._factory(); |
|
|
225 | 225 | } else if (this._params instanceof Array) { |
|
|
226 | 226 | instance = this._factory.apply(this, _parse(this._params, context, "args")); |
|
|
227 | 227 | } else { |
|
|
228 | 228 | instance = this._factory(_parse(this._params, context, "args")); |
|
|
229 | 229 | } |
|
|
230 | 230 | |
|
|
231 | 231 | if (this._inject) { |
|
|
232 | 232 | this._inject.forEach(spec => { |
|
|
233 | 233 | for (const m in spec) |
|
|
234 | 234 | injectMethod(instance, m, context, spec[m]); |
|
|
235 | 235 | }); |
|
|
236 | 236 | } |
|
|
237 | 237 | return instance; |
|
|
238 | 238 | } |
|
|
239 | 239 | } |
| @@ -1,42 +1,42 | |||
|
|
1 | 1 | import { ServiceDescriptor, ServiceDescriptorParams } from "./ServiceDescriptor"; |
|
|
2 | 2 | import { Constructor, Factory } from "../interfaces"; |
|
|
3 | 3 | import { argumentNotNull, isPrimitive } from "../safe"; |
|
|
4 | 4 | |
|
|
5 | export interface TypeServiceDescriptorParams<S, T extends object, P extends any[]> extends ServiceDescriptorParams<S, T, P> { | |
|
|
5 | export interface TypeServiceDescriptorParams<S extends object, T extends object, P extends any[]> extends ServiceDescriptorParams<S, T, P> { | |
|
|
6 | 6 | type: Constructor<T>; |
|
|
7 | 7 | } |
|
|
8 | 8 | |
|
|
9 | export class TypeServiceDescriptor<S, T extends object, P extends any[]> extends ServiceDescriptor<S, T, P> { | |
|
|
9 | export class TypeServiceDescriptor<S extends object, T extends object, P extends any[]> extends ServiceDescriptor<S, T, P> { | |
|
|
10 | 10 | _type: Constructor; |
|
|
11 | 11 | |
|
|
12 | 12 | constructor(opts: TypeServiceDescriptorParams<S, T, P>) { |
|
|
13 | 13 | super(opts); |
|
|
14 | 14 | argumentNotNull(opts && opts.type, "opts.type"); |
|
|
15 | 15 | |
|
|
16 | 16 | const ctor = this._type = opts.type; |
|
|
17 | 17 | |
|
|
18 | 18 | if (this._params) { |
|
|
19 | 19 | if (this._params.length) { |
|
|
20 | 20 | this._factory = (...args) => { |
|
|
21 | 21 | const t = Object.create(ctor.prototype); |
|
|
22 | 22 | const inst = ctor.apply(t, args); |
|
|
23 | 23 | return isPrimitive(inst) ? t : inst; |
|
|
24 | 24 | }; |
|
|
25 | 25 | } else { |
|
|
26 | 26 | this._factory = arg => { |
|
|
27 | 27 | return new ctor(arg); |
|
|
28 | 28 | }; |
|
|
29 | 29 | } |
|
|
30 | 30 | } else { |
|
|
31 | 31 | this._factory = () => { |
|
|
32 | 32 | return new ctor(); |
|
|
33 | 33 | }; |
|
|
34 | 34 | } |
|
|
35 | 35 | |
|
|
36 | 36 | } |
|
|
37 | 37 | |
|
|
38 | 38 | toString() { |
|
|
39 | 39 | // @constructor {singleton} foo/bar/Baz |
|
|
40 | 40 | return ``; |
|
|
41 | 41 | } |
|
|
42 | 42 | } |
| @@ -1,21 +1,38 | |||
|
|
1 | 1 | import { ActivationContext } from "./ActivationContext"; |
|
|
2 | 2 | |
|
|
3 | export interface Descriptor<S = any, T = any> { | |
|
|
3 | export interface Descriptor<S extends object = any, T = any> { | |
|
|
4 | 4 | activate(context: ActivationContext<S>): T; |
|
|
5 | 5 | } |
|
|
6 | 6 | |
|
|
7 | export type ServiceMap<S> = { | |
|
|
7 | export type ServiceMap<S extends object> = { | |
|
|
8 | 8 | [k in keyof S]: Descriptor<S, S[k]>; |
|
|
9 | 9 | }; |
|
|
10 | 10 | |
|
|
11 | export type PartialServiceMap<S> = { | |
|
|
11 | export type ContainerKeys<S extends object> = keyof S | keyof ContainerProvided<S>; | |
|
|
12 | ||
|
|
13 | export type ContainerResolve<S extends object, K> = | |
|
|
14 | K extends keyof ContainerProvided<S> ? ContainerProvided<S>[K] : | |
|
|
15 | K extends keyof S ? S[K] : never; | |
|
|
16 | ||
|
|
17 | export type ContainerServiceMap<S extends object> = { | |
|
|
18 | [K in ContainerKeys<S>]: Descriptor<S, ContainerResolve<S, K>>; | |
|
|
19 | }; | |
|
|
20 | ||
|
|
21 | export type PartialServiceMap<S extends object> = { | |
|
|
12 | 22 | [k in keyof S]?: Descriptor<S, S[k]>; |
|
|
13 | 23 | }; |
|
|
14 | 24 | |
|
|
15 | export interface Resolver<S> { | |
|
|
16 | resolve<K extends keyof ContainerServices<S>, T extends ContainerServices<S>[K] = ContainerServices<S>[K]>(name: K, def?: T): T; | |
|
|
25 | export interface Resolver<S extends object> { | |
|
|
26 | resolve<K extends ContainerKeys<S>>(name: K, def?: ContainerResolve<S, K>): ContainerResolve<S, K>; | |
|
|
27 | } | |
|
|
28 | ||
|
|
29 | export interface ContainerProvided<S extends object> { | |
|
|
30 | container: Resolver<S>; | |
|
|
17 | 31 | } |
|
|
18 | export type ContainerServices<S> = S & { | |
|
|
19 | container: Resolver<S>; | |
|
|
20 | }; | |
|
|
32 | ||
|
|
33 | export type ContainerRegistered<S extends object> = /*{ | |
|
|
34 | [K in Exclude<keyof S, keyof ContainerProvided<S>>]: S[K]; | |
|
|
35 | };*/ | |
|
|
36 | Exclude<S, ContainerProvided<S>>; | |
|
|
37 | ||
|
|
21 | 38 | export type ActivationType = "singleton" | "container" | "hierarchy" | "context" | "call"; |
| @@ -1,7 +1,7 | |||
|
|
1 | 1 | import { isPrimitive } from "../safe"; |
|
|
2 | 2 | import { Descriptor } from "./interfaces"; |
|
|
3 | 3 | |
|
|
4 | 4 | export function isDescriptor(x: any): x is Descriptor { |
|
|
5 | 5 | return (!isPrimitive(x)) && |
|
|
6 | 6 | (x.activate instanceof Function); |
|
|
7 |
} |
|
|
|
7 | } No newline at end of file | |
| @@ -1,6 +1,6 | |||
|
|
1 | 1 | import { config } from "./services"; |
|
|
2 | 2 | |
|
|
3 | 3 | config() |
|
|
4 | 4 | .register("bar", import("./Bar")) |
|
|
5 | .register("box", import("./Box")) | |
|
|
6 | .register("foo", import("./Foo"), "Foo"); | |
|
|
5 | .register("box", import("./Box"), "service"); | |
|
|
6 | //.register("foo", import("./Foo"), "Foo"); | |
| @@ -1,95 +1,106 | |||
|
|
1 | 1 | import { test } from "./TestTraits"; |
|
|
2 | 2 | import { Container } from "../di/Container"; |
|
|
3 | 3 | import { ReferenceDescriptor } from "../di/ReferenceDescriptor"; |
|
|
4 | 4 | import { AggregateDescriptor } from "../di/AggregateDescriptor"; |
|
|
5 | 5 | import { ValueDescriptor } from "../di/ValueDescriptor"; |
|
|
6 | 6 | import { Foo } from "../mock/Foo"; |
|
|
7 | 7 | import { Bar } from "../mock/Bar"; |
|
|
8 | 8 | import { isNull } from "../safe"; |
|
|
9 | import { Descriptor } from "../di/interfaces"; | |
|
|
9 | import { Box } from "ts/mock/Box"; | |
|
|
10 | 10 | |
|
|
11 | 11 | test("Container register/resolve tests", async t => { |
|
|
12 |
const container = new Container |
|
|
|
12 | const container = new Container<{ | |
|
|
13 | "bla-bla": string; | |
|
|
14 | "connection": string; | |
|
|
15 | "dbParams": { | |
|
|
16 | timeout: number; | |
|
|
17 | connection: string; | |
|
|
18 | } | |
|
|
19 | }>(); | |
|
|
13 | 20 | |
|
|
14 | 21 | const connection1 = "db://localhost"; |
|
|
15 | 22 | |
|
|
16 | 23 | t.throws( |
|
|
17 | 24 | () => container.register("bla-bla", "bla-bla" as any), |
|
|
18 | 25 | "Do not allow to register anything other than descriptors" |
|
|
19 | 26 | ); |
|
|
20 | 27 | |
|
|
21 | 28 | t.doesNotThrow( |
|
|
22 | 29 | () => container.register("connection", new ValueDescriptor(connection1)), |
|
|
23 | 30 | "register ValueDescriptor" |
|
|
24 | 31 | ); |
|
|
25 | 32 | |
|
|
26 | 33 | t.equals(container.resolve("connection"), connection1, "resolve string value"); |
|
|
27 | 34 | |
|
|
28 | 35 | t.doesNotThrow( |
|
|
29 | 36 | () => container.register( |
|
|
30 | 37 | "dbParams", |
|
|
31 | 38 | new AggregateDescriptor({ |
|
|
32 | 39 | timeout: 10, |
|
|
33 | 40 | connection: new ReferenceDescriptor({ name: "connection" }) |
|
|
34 | 41 | }) |
|
|
35 | 42 | ), |
|
|
36 | 43 | "register AggregateDescriptor" |
|
|
37 | 44 | ); |
|
|
38 | 45 | |
|
|
39 | 46 | const dbParams = container.resolve("dbParams"); |
|
|
40 | 47 | t.equals(dbParams.connection, connection1, "should get string value 'dbParams.connection'"); |
|
|
41 | 48 | }); |
|
|
42 | 49 | |
|
|
43 | 50 | test("Container configure/resolve tests", async t => { |
|
|
44 | 51 | |
|
|
45 |
const container = new Container |
|
|
|
52 | const container = new Container<{ | |
|
|
53 | foo: Foo; | |
|
|
54 | box: Bar; | |
|
|
55 | bar: Bar; | |
|
|
56 | }>(); | |
|
|
46 | 57 | |
|
|
47 | 58 | await container.configure({ |
|
|
48 | 59 | foo: { |
|
|
49 | 60 | $type: Foo |
|
|
50 | 61 | }, |
|
|
51 | 62 | |
|
|
52 | 63 | box: { |
|
|
53 | 64 | $type: Bar, |
|
|
54 | 65 | params: { |
|
|
55 | 66 | $dependency: "foo" |
|
|
56 | 67 | } |
|
|
57 | 68 | }, |
|
|
58 | 69 | |
|
|
59 | 70 | bar: { |
|
|
60 | 71 | $type: Bar, |
|
|
61 | 72 | params: { |
|
|
62 | 73 | db: { |
|
|
63 | 74 | provider: { |
|
|
64 | 75 | $dependency: "db" |
|
|
65 | 76 | } |
|
|
66 | 77 | } |
|
|
67 | 78 | } |
|
|
68 | 79 | } |
|
|
69 | 80 | }); |
|
|
70 | 81 | t.pass("should configure from js object"); |
|
|
71 | 82 | |
|
|
72 | 83 | const f1 = container.resolve("foo"); |
|
|
73 | 84 | |
|
|
74 | 85 | t.assert(!isNull(f1), "foo should be not null"); |
|
|
75 | 86 | |
|
|
76 | 87 | t.throws(() => container.resolve("bar"), "should not resolve dependency 'db'"); |
|
|
77 | 88 | |
|
|
78 | 89 | }); |
|
|
79 | 90 | |
|
|
80 | 91 | test("Load configuration from module", async t => { |
|
|
81 | 92 | const container = new Container(); |
|
|
82 | 93 | |
|
|
83 | 94 | await container.configure("../mock/config1", { contextRequire: require }); |
|
|
84 | 95 | t.pass("The configuration should load"); |
|
|
85 | 96 | |
|
|
86 | 97 | const f1 = container.resolve("foo"); |
|
|
87 | 98 | |
|
|
88 | 99 | t.assert(!isNull(f1), "foo should be not null"); |
|
|
89 | 100 | |
|
|
90 | 101 | const b1 = container.resolve("bar") as Bar; |
|
|
91 | 102 | |
|
|
92 | 103 | t.assert(!isNull(b1), "bar should not be null"); |
|
|
93 | 104 | t.assert(!isNull(b1._v), "bar.foo should not be null"); |
|
|
94 | 105 | |
|
|
95 | 106 | }); |
General Comments 0
You need to be logged in to leave comments.
Login now
