| @@ -2,37 +2,38 import { TraceSource } from "../log/Trac | |||
|
|
2 | 2 | import { argumentNotNull, argumentNotEmptyString, isPrimitive, each, isNull } from "../safe"; |
|
|
3 | 3 | import { Descriptor, ServiceMap } from "./interfaces"; |
|
|
4 | 4 | import { Container } from "./Container"; |
|
|
5 | import { MapOf } from "../interfaces"; | |
|
|
5 | 6 | |
|
|
6 | 7 | const trace = TraceSource.get("@implab/core/di/ActivationContext"); |
|
|
7 | 8 | |
|
|
8 | export interface ActivationContextInfo { | |
|
|
9 | export interface ActivationContextInfo<S> { | |
|
|
9 | 10 | name: string; |
|
|
10 | 11 | |
|
|
11 | 12 | service: string; |
|
|
12 | 13 | |
|
|
13 | scope: ServiceMap; | |
|
|
14 | scope: ServiceMap<S>; | |
|
|
14 | 15 | } |
|
|
15 | 16 | |
|
|
16 | export class ActivationContext { | |
|
|
17 |
_cache: |
|
|
|
17 | export class ActivationContext<S> { | |
|
|
18 | _cache: MapOf<any>; | |
|
|
18 | 19 | |
|
|
19 | _services: ServiceMap; | |
|
|
20 | _services: ServiceMap<S>; | |
|
|
20 | 21 | |
|
|
21 | _stack: ActivationContextInfo[]; | |
|
|
22 | _stack: ActivationContextInfo<S>[]; | |
|
|
22 | 23 | |
|
|
23 |
_visited: |
|
|
|
24 | _visited: MapOf<any>; | |
|
|
24 | 25 | |
|
|
25 | 26 | _name: string; |
|
|
26 | 27 | |
|
|
27 | _localized: boolean; | |
|
|
28 | _localized: boolean = false; | |
|
|
28 | 29 | |
|
|
29 | container: Container; | |
|
|
30 | container: Container<S>; | |
|
|
30 | 31 | |
|
|
31 | constructor(container: Container, services: ServiceMap, name?: string, cache?: object, visited?) { | |
|
|
32 | constructor(container: Container<S>, services: ServiceMap<S>, name?: string, cache?: object, visited?: MapOf<any>) { | |
|
|
32 | 33 | argumentNotNull(container, "container"); |
|
|
33 | 34 | argumentNotNull(services, "services"); |
|
|
34 | 35 | |
|
|
35 | this._name = name; | |
|
|
36 | this._name = name || "<unnamed>"; | |
|
|
36 | 37 | this._visited = visited || {}; |
|
|
37 | 38 | this._stack = []; |
|
|
38 | 39 | this._cache = cache || {}; |
| @@ -44,16 +45,17 export class ActivationContext { | |||
|
|
44 | 45 | return this._name; |
|
|
45 | 46 | } |
|
|
46 | 47 | |
|
|
47 | resolve(name, def?): any { | |
|
|
48 | resolve<K extends keyof S, T extends S[K]>(name: K, def?: T) { | |
|
|
48 | 49 | const d = this._services[name]; |
|
|
49 | 50 | |
|
|
50 |
if ( |
|
|
|
51 | if (arguments.length > 1) | |
|
|
51 | if (d !== undefined) { | |
|
|
52 | return this.activate(d as Descriptor<T>, name.toString()); | |
|
|
53 | } else { | |
|
|
54 | if (def !== undefined && def !== null) | |
|
|
52 | 55 | return def; |
|
|
53 | 56 | else |
|
|
54 | 57 | throw new Error(`Service ${name} not found`); |
|
|
55 | ||
|
|
56 | return this.activate(d, name); | |
|
|
58 | } | |
|
|
57 | 59 | } |
|
|
58 | 60 | |
|
|
59 | 61 | /** |
| @@ -62,7 +64,7 export class ActivationContext { | |||
|
|
62 | 64 | * @name{string} the name of the service |
|
|
63 | 65 | * @service{string} the service descriptor to register |
|
|
64 | 66 | */ |
|
|
65 |
register(name: |
|
|
|
67 | register<K extends keyof S>(name: K, service: Descriptor<S[K]>) { | |
|
|
66 | 68 | argumentNotEmptyString(name, "name"); |
|
|
67 | 69 | |
|
|
68 | 70 | this._services[name] = service; |
| @@ -82,20 +84,20 export class ActivationContext { | |||
|
|
82 | 84 | return id in this._cache; |
|
|
83 | 85 | } |
|
|
84 | 86 | |
|
|
85 | get(id: string) { | |
|
|
87 | get<T>(id: string) { | |
|
|
86 | 88 | return this._cache[id]; |
|
|
87 | 89 | } |
|
|
88 | 90 | |
|
|
89 | store(id: string, value) { | |
|
|
91 | store(id: string, value: any) { | |
|
|
90 | 92 | return (this._cache[id] = value); |
|
|
91 | 93 | } |
|
|
92 | 94 | |
|
|
93 | activate(d: Descriptor, name: string) { | |
|
|
95 | activate<T>(d: Descriptor<T>, name: string) { | |
|
|
94 | 96 | if (trace.isLogEnabled()) |
|
|
95 | 97 | trace.log(`enter ${name} ${d}`); |
|
|
96 | 98 | |
|
|
97 | 99 | this.enter(name, d.toString()); |
|
|
98 | const v = d.activate(this); | |
|
|
100 | const v = d.activate<S>(this); | |
|
|
99 | 101 | this.leave(); |
|
|
100 | 102 | |
|
|
101 | 103 | if (trace.isLogEnabled()) |
| @@ -126,7 +128,11 export class ActivationContext { | |||
|
|
126 | 128 | |
|
|
127 | 129 | private leave() { |
|
|
128 | 130 | const ctx = this._stack.pop(); |
|
|
129 | this._services = ctx.scope; | |
|
|
130 |
this._ |
|
|
|
131 | if (ctx) { | |
|
|
132 | this._services = ctx.scope; | |
|
|
133 | this._name = ctx.name; | |
|
|
134 | } else { | |
|
|
135 | trace.error("Trying to leave the last activation scope"); | |
|
|
136 | } | |
|
|
131 | 137 | } |
|
|
132 | 138 | } |
| @@ -1,7 +1,10 | |||
|
|
1 | import { ActivationContextInfo } from "./ActivationContext"; | |
|
|
1 | export interface ActivationItem { | |
|
|
2 | name: string; | |
|
|
3 | service: string; | |
|
|
4 | } | |
|
|
2 | 5 | |
|
|
3 | 6 | export class ActivationError { |
|
|
4 |
activationStack: Activation |
|
|
|
7 | activationStack: ActivationItem[]; | |
|
|
5 | 8 | |
|
|
6 | 9 | service: string; |
|
|
7 | 10 | |
| @@ -9,7 +12,7 export class ActivationError { | |||
|
|
9 | 12 | |
|
|
10 | 13 | message: string; |
|
|
11 | 14 | |
|
|
12 |
constructor(service: string, activationStack: Activation |
|
|
|
15 | constructor(service: string, activationStack: ActivationItem[], innerException: any) { | |
|
|
13 | 16 | this.message = "Failed to activate the service"; |
|
|
14 | 17 | this.activationStack = activationStack; |
|
|
15 | 18 | this.service = service; |
| @@ -2,33 +2,35 import { Descriptor, isDescriptor } from | |||
|
|
2 | 2 | import { ActivationContext } from "./ActivationContext"; |
|
|
3 | 3 | import { isPrimitive } from "../safe"; |
|
|
4 | 4 | |
|
|
5 | export class AggregateDescriptor<T> implements Descriptor<T> { | |
|
|
5 | type Parse<T> = T extends Descriptor<infer V> ? V : | |
|
|
6 | T extends {} ? { [K in keyof T]: Parse<T[K]> } : | |
|
|
7 | T; | |
|
|
8 | ||
|
|
9 | export class AggregateDescriptor<T> implements Descriptor<Parse<T>> { | |
|
|
6 | 10 | _value: T; |
|
|
7 | 11 | |
|
|
8 | 12 | constructor(value: T) { |
|
|
9 | 13 | this._value = value; |
|
|
10 | 14 | } |
|
|
11 | 15 | |
|
|
12 | activate(context: ActivationContext) { | |
|
|
16 | activate<S>(context: ActivationContext<S>) { | |
|
|
13 | 17 | return this._parse(this._value, context, "$value"); |
|
|
14 | 18 | } |
|
|
15 | 19 | |
|
|
16 | // TODO: make async | |
|
|
17 | _parse(value: T, context: ActivationContext, path: string) { | |
|
|
20 | _parse<S, V>(value: V, context: ActivationContext<S>, path: string): Parse<V> { | |
|
|
18 | 21 | if (isPrimitive(value)) |
|
|
19 | return value; | |
|
|
22 | return value as any; | |
|
|
20 | 23 | |
|
|
21 | 24 | if (isDescriptor(value)) |
|
|
22 | 25 | return context.activate(value, path); |
|
|
23 | 26 | |
|
|
24 | 27 | if (value instanceof Array) |
|
|
25 | return value.map((x, i) => this._parse(x, context, `${path}[${i}]`)); | |
|
|
28 | return value.map((x, i) => this._parse(x, context, `${path}[${i}]`)) as any; | |
|
|
26 | 29 | |
|
|
27 | const t = {}; | |
|
|
28 |
for (const p |
|
|
|
30 | const t: any = {}; | |
|
|
31 | for (const p in value) | |
|
|
29 | 32 | t[p] = this._parse(value[p], context, `${path}.${p}`); |
|
|
30 | 33 | return t; |
|
|
31 | ||
|
|
32 | 34 | } |
|
|
33 | 35 | |
|
|
34 | 36 | toString() { |
| @@ -4,22 +4,40 export interface InjectOptions { | |||
|
|
4 | 4 | lazy?: boolean; |
|
|
5 | 5 | } |
|
|
6 | 6 | |
|
|
7 | interface Dependency<K extends keyof any> { | |
|
|
8 | $dependency: K; | |
|
|
9 | ||
|
|
10 | lazy?: boolean; | |
|
|
11 | } | |
|
|
12 | ||
|
|
13 | interface Lazy<K extends keyof any> extends Dependency<K> { | |
|
|
14 | lazy: true; | |
|
|
15 | } | |
|
|
16 | ||
|
|
7 | 17 | type Setter<T = any> = (v: T) => void; |
|
|
8 | 18 | |
|
|
9 | 19 | type Compatible<T1, T2> = T1 extends T2 ? any : never; |
|
|
10 | 20 | |
|
|
11 | type SetterType<T> = T extends (v: infer V) => void ? V : never; | |
|
|
12 | ||
|
|
13 | 21 | type ExtractService<K, S> = K extends keyof S ? S[K] : K; |
|
|
14 | 22 | |
|
|
15 | 23 | type ExtractDependency<D, S> = D extends { $dependency: infer K } ? D extends { lazy: true } ? () => ExtractService<K, S> : ExtractService<K, S> : VisitDependency<D, S>; |
|
|
16 | 24 | |
|
|
17 | 25 | type VisitDependency<D, S> = D extends {} ? { [K in keyof D]: ExtractDependency<D[K], S> } : D; |
|
|
18 | 26 | |
|
|
27 | interface Config<S> { | |
|
|
28 | dependency<K extends keyof S>(name: K): Dependency<K>; | |
|
|
29 | ||
|
|
30 | lazy<K extends keyof S>(name: K): Lazy<K>; | |
|
|
31 | ||
|
|
32 | build<T>(): Builder<T, S>; | |
|
|
33 | } | |
|
|
34 | ||
|
|
35 | export declare function services<S extends object>(): Config<S>; | |
|
|
36 | ||
|
|
19 | 37 | export class Builder<T, S> { |
|
|
20 | 38 | consume<P extends any[]>(...args: P) { |
|
|
21 | 39 | return <C extends new (...args: ExtractDependency<P, S>) => T>(constructor: C) => { |
|
|
22 | return constructor as typeof constructor & { service: () => T }; | |
|
|
40 | return constructor; | |
|
|
23 | 41 | }; |
|
|
24 | 42 | } |
|
|
25 | 43 | |
| @@ -39,3 +57,5 export class Builder<T, S> { | |||
|
|
39 | 57 | } |
|
|
40 | 58 | |
|
|
41 | 59 | } |
|
|
60 | ||
|
|
61 | ||
| @@ -1,11 +1,11 | |||
|
|
1 | 1 | export class ConfigError extends Error { |
|
|
2 |
inner |
|
|
|
2 | inner?: {}; | |
|
|
3 | 3 | |
|
|
4 | path: string; | |
|
|
4 | path?: string; | |
|
|
5 | 5 | |
|
|
6 | configName: string; | |
|
|
6 | configName?: string; | |
|
|
7 | 7 | |
|
|
8 |
constructor(message: string, inner?: |
|
|
|
8 | constructor(message: string, inner?: {}) { | |
|
|
9 | 9 | super(message); |
|
|
10 | 10 | this.inner = inner; |
|
|
11 | 11 | } |
| @@ -28,7 +28,7 import { ICancellation } from "../interf | |||
|
|
28 | 28 | |
|
|
29 | 29 | const trace = TraceSource.get("@implab/core/di/Configuration"); |
|
|
30 | 30 | |
|
|
31 |
async function mapAll(data: any | any[], map?: (v: any, k: |
|
|
|
31 | async function mapAll(data: any | any[], map?: (v: any, k: number | string) => any): Promise<any> { | |
|
|
32 | 32 | if (data instanceof Array) { |
|
|
33 | 33 | return Promise.all(map ? data.map(map) : data); |
|
|
34 | 34 | } else { |
| @@ -47,21 +47,19 async function mapAll(data: any | any[], | |||
|
|
47 | 47 | |
|
|
48 | 48 | export type ModuleResolver = (moduleName: string, ct?: ICancellation) => any; |
|
|
49 | 49 | |
|
|
50 | type _key = string | number; | |
|
|
51 | ||
|
|
52 | export class Configuration { | |
|
|
50 | export class Configuration<S> { | |
|
|
53 | 51 | |
|
|
54 | 52 | _hasInnerDescriptors = false; |
|
|
55 | 53 | |
|
|
56 | _container: Container; | |
|
|
54 | _container: Container<S>; | |
|
|
57 | 55 | |
|
|
58 |
_path: Array< |
|
|
|
56 | _path: Array<string>; | |
|
|
59 | 57 | |
|
|
60 | 58 | _configName: string | undefined; |
|
|
61 | 59 | |
|
|
62 | 60 | _require: ModuleResolver | undefined; |
|
|
63 | 61 | |
|
|
64 | constructor(container: Container) { | |
|
|
62 | constructor(container: Container<S>) { | |
|
|
65 | 63 | argumentNotNull(container, "container"); |
|
|
66 | 64 | this._container = container; |
|
|
67 | 65 | this._path = []; |
| @@ -150,7 +148,7 export class Configuration { | |||
|
|
150 | 148 | return this._require(moduleName); |
|
|
151 | 149 | } |
|
|
152 | 150 | |
|
|
153 |
async _visitRegistrations(data: any, name: |
|
|
|
151 | async _visitRegistrations(data: any, name: string) { | |
|
|
154 | 152 | this._enter(name); |
|
|
155 | 153 | |
|
|
156 | 154 | if (data.constructor && |
| @@ -161,7 +159,7 export class Configuration { | |||
|
|
161 | 159 | const keys = Object.keys(data); |
|
|
162 | 160 | |
|
|
163 | 161 | const services = await mapAll(data, async (v, k) => { |
|
|
164 | const d = await this._visit(v, k); | |
|
|
162 | const d = await this._visit(v, k.toString()); | |
|
|
165 | 163 | return isDescriptor(d) ? d : new AggregateDescriptor(d); |
|
|
166 | 164 | }) as ServiceMap; |
|
|
167 | 165 | |
| @@ -180,11 +178,11 export class Configuration { | |||
|
|
180 | 178 | trace.debug("<{0}", name); |
|
|
181 | 179 | } |
|
|
182 | 180 | |
|
|
183 |
async _visit<T |
|
|
|
181 | async _visit<T>(data: T, name: string) { | |
|
|
184 | 182 | if (isPrimitive(data) || isDescriptor(data)) |
|
|
185 | 183 | return data; |
|
|
186 | 184 | |
|
|
187 | if (isDependencyRegistration(data)) { | |
|
|
185 | if (isDependencyRegistration<S>(data)) { | |
|
|
188 | 186 | return this._visitDependencyRegistration(data, name); |
|
|
189 | 187 | } else if (isValueRegistration(data)) { |
|
|
190 | 188 | return this._visitValueRegistration(data, name); |
| @@ -196,10 +194,10 export class Configuration { | |||
|
|
196 | 194 | return this._visitArray(data, name); |
|
|
197 | 195 | } |
|
|
198 | 196 | |
|
|
199 | return this._visitObject(data, name); | |
|
|
197 | return this._visitObject(data as T & object, name); | |
|
|
200 | 198 | } |
|
|
201 | 199 | |
|
|
202 |
async _visitObject(data: |
|
|
|
200 | async _visitObject<T extends object>(data: T, name: string) { | |
|
|
203 | 201 | if (data.constructor && |
|
|
204 | 202 | data.constructor.prototype !== Object.prototype) |
|
|
205 | 203 | return new ValueDescriptor(data); |
| @@ -222,7 +220,7 export class Configuration { | |||
|
|
222 | 220 | return v; |
|
|
223 | 221 | } |
|
|
224 | 222 | |
|
|
225 |
async _visitArray(data: any[], name: |
|
|
|
223 | async _visitArray(data: any[], name: string) { | |
|
|
226 | 224 | if (data.constructor && |
|
|
227 | 225 | data.constructor.prototype !== Array.prototype) |
|
|
228 | 226 | return new ValueDescriptor(data); |
| @@ -235,7 +233,7 export class Configuration { | |||
|
|
235 | 233 | return v; |
|
|
236 | 234 | } |
|
|
237 | 235 | |
|
|
238 |
_makeServiceParams<T, P |
|
|
|
236 | _makeServiceParams<T, P>(data: ServiceRegistration<T, P, S>) { | |
|
|
239 | 237 | const opts: any = { |
|
|
240 | 238 | owner: this._container |
|
|
241 | 239 | }; |
| @@ -291,14 +289,14 export class Configuration { | |||
|
|
291 | 289 | return opts; |
|
|
292 | 290 | } |
|
|
293 | 291 | |
|
|
294 |
async _visitValueRegistration<T>(data: ValueRegistration<T>, name: |
|
|
|
292 | async _visitValueRegistration<T>(data: ValueRegistration<T>, name: string) { | |
|
|
295 | 293 | this._enter(name); |
|
|
296 | 294 | const d = data.parse ? new AggregateDescriptor(data.$value) : new ValueDescriptor(data.$value); |
|
|
297 | 295 | this._leave(); |
|
|
298 | 296 | return d; |
|
|
299 | 297 | } |
|
|
300 | 298 | |
|
|
301 |
async _visitDependencyRegistration< |
|
|
|
299 | async _visitDependencyRegistration<K extends keyof S>(data: DependencyRegistration<S, K>, name: string) { | |
|
|
302 | 300 | argumentNotEmptyString(data && data.$dependency, "data.$dependency"); |
|
|
303 | 301 | this._enter(name); |
|
|
304 | 302 | const d = new ReferenceDescriptor<S, K>({ |
| @@ -312,7 +310,7 export class Configuration { | |||
|
|
312 | 310 | return d; |
|
|
313 | 311 | } |
|
|
314 | 312 | |
|
|
315 |
async _visitTypeRegistration<T, P |
|
|
|
313 | async _visitTypeRegistration<T, P>(data: TypeRegistration<T, P, S>, name: string) { | |
|
|
316 | 314 | argumentNotNull(data.$type, "data.$type"); |
|
|
317 | 315 | this._enter(name); |
|
|
318 | 316 | |
| @@ -333,7 +331,7 export class Configuration { | |||
|
|
333 | 331 | return d; |
|
|
334 | 332 | } |
|
|
335 | 333 | |
|
|
336 |
async _visitFactoryRegistration<T, P |
|
|
|
334 | async _visitFactoryRegistration<T, P>(data: FactoryRegistration<T, P, S>, name: string) { | |
|
|
337 | 335 | argumentOfType(data.$factory, Function, "data.$factory"); |
|
|
338 | 336 | this._enter(name); |
|
|
339 | 337 | |
| @@ -1,31 +1,35 | |||
|
|
1 | 1 | import { ActivationContext } from "./ActivationContext"; |
|
|
2 | 2 | import { ValueDescriptor } from "./ValueDescriptor"; |
|
|
3 | 3 | import { ActivationError } from "./ActivationError"; |
|
|
4 | import { isDescriptor, ServiceMap } from "./interfaces"; | |
|
|
4 | import { isDescriptor, ServiceMap, Descriptor } from "./interfaces"; | |
|
|
5 | 5 | import { TraceSource } from "../log/TraceSource"; |
|
|
6 | 6 | import { Configuration } from "./Configuration"; |
|
|
7 | 7 | import { Cancellation } from "../Cancellation"; |
|
|
8 | import { MapOf } from "../interfaces"; | |
|
|
8 | 9 | |
|
|
9 | 10 | const trace = TraceSource.get("@implab/core/di/ActivationContext"); |
|
|
10 | 11 | |
|
|
11 | export class Container { | |
|
|
12 | _services: ServiceMap; | |
|
|
12 | export class Container<S extends { container?: Container<S> }> { | |
|
|
13 | readonly _services: ServiceMap<S>; | |
|
|
13 | 14 | |
|
|
14 | _cache: object; | |
|
|
15 | readonly _cache: MapOf<any>; | |
|
|
16 | ||
|
|
17 | readonly _cleanup: (() => void)[]; | |
|
|
15 | 18 | |
|
|
16 | _cleanup: (() => void)[]; | |
|
|
19 | readonly _root: Container<S>; | |
|
|
17 | 20 | |
|
|
18 |
|
|
|
|
21 | readonly _parent?: Container<S>; | |
|
|
19 | 22 | |
|
|
20 | _parent: Container; | |
|
|
23 | _disposed: boolean; | |
|
|
21 | 24 | |
|
|
22 | constructor(parent?: Container) { | |
|
|
25 | constructor(parent?: Container<S>) { | |
|
|
23 | 26 | this._parent = parent; |
|
|
24 | 27 | this._services = parent ? Object.create(parent._services) : {}; |
|
|
25 | 28 | this._cache = {}; |
|
|
26 | 29 | this._cleanup = []; |
|
|
27 | 30 | this._root = parent ? parent.getRootContainer() : this; |
|
|
28 | 31 | this._services.container = new ValueDescriptor(this); |
|
|
32 | this._disposed = false; | |
|
|
29 | 33 | } |
|
|
30 | 34 | |
|
|
31 | 35 | getRootContainer() { |
| @@ -36,57 +40,63 export class Container { | |||
|
|
36 | 40 | return this._parent; |
|
|
37 | 41 | } |
|
|
38 | 42 | |
|
|
39 | resolve(name: string, def?) { | |
|
|
43 | resolve<K extends keyof S, T extends S[K] = S[K]>(name: K, def?: T) { | |
|
|
40 | 44 | trace.debug("resolve {0}", name); |
|
|
41 | 45 | const d = this._services[name]; |
|
|
42 | 46 | if (d === undefined) { |
|
|
43 | if (arguments.length > 1) | |
|
|
47 | if (def !== undefined) | |
|
|
44 | 48 | return def; |
|
|
45 | 49 | else |
|
|
46 | 50 | throw new Error("Service '" + name + "' isn't found"); |
|
|
47 | } | |
|
|
51 | } else { | |
|
|
48 | 52 | |
|
|
49 | const context = new ActivationContext(this, this._services); | |
|
|
50 | try { | |
|
|
51 | return context.activate(d, name); | |
|
|
52 | } catch (error) { | |
|
|
53 | throw new ActivationError(name, context.getStack(), error); | |
|
|
53 | const context = new ActivationContext<S>(this, this._services); | |
|
|
54 | try { | |
|
|
55 | return context.activate(d as Descriptor<T>, name.toString()); | |
|
|
56 | } catch (error) { | |
|
|
57 | throw new ActivationError(name.toString(), context.getStack(), error); | |
|
|
58 | } | |
|
|
54 | 59 | } |
|
|
55 | 60 | } |
|
|
56 | 61 | |
|
|
57 | 62 | /** |
|
|
58 | 63 | * @deprecated use resolve() method |
|
|
59 | 64 | */ |
|
|
60 | getService() { | |
|
|
61 |
return this.resolve |
|
|
|
65 | getService<K extends keyof S>(name: K, def?: S[K]) { | |
|
|
66 | return this.resolve(name, def); | |
|
|
62 | 67 | } |
|
|
63 | 68 | |
|
|
64 | register(nameOrCollection, service?) { | |
|
|
69 | register<K extends keyof S>(name: K, service: Descriptor<S[K]>): this; | |
|
|
70 | register(services: ServiceMap<S>): this; | |
|
|
71 | register<K extends keyof S>(nameOrCollection: K | ServiceMap<S>, service?: Descriptor<S[K]>) { | |
|
|
65 | 72 | if (arguments.length === 1) { |
|
|
66 | const data = nameOrCollection; | |
|
|
67 | for (const name in data) | |
|
|
68 | this.register(name, data[name]); | |
|
|
73 | const data = nameOrCollection as ServiceMap<S>; | |
|
|
74 | for (const name in data) { | |
|
|
75 | if (Object.prototype.hasOwnProperty.call(data, name)) { | |
|
|
76 | this.register(name, data[name] as Descriptor<S[keyof S]>); | |
|
|
77 | } | |
|
|
78 | } | |
|
|
69 | 79 | } else { |
|
|
70 | 80 | if (!isDescriptor(service)) |
|
|
71 | 81 | throw new Error("The service parameter must be a descriptor"); |
|
|
72 | 82 | |
|
|
73 | this._services[nameOrCollection] = service; | |
|
|
83 | this._services[nameOrCollection as K] = service; | |
|
|
74 | 84 | } |
|
|
75 | 85 | return this; |
|
|
76 | 86 | } |
|
|
77 | 87 | |
|
|
78 | onDispose(callback) { | |
|
|
88 | onDispose(callback: () => void) { | |
|
|
79 | 89 | if (!(callback instanceof Function)) |
|
|
80 | 90 | throw new Error("The callback must be a function"); |
|
|
81 | 91 | this._cleanup.push(callback); |
|
|
82 | 92 | } |
|
|
83 | 93 | |
|
|
84 | 94 | dispose() { |
|
|
85 |
if (this._ |
|
|
|
86 | for (const f of this._cleanup) | |
|
|
87 | f(); | |
|
|
88 |
this._cleanup |
|
|
|
89 |
|
|
|
|
95 | if (this._disposed) | |
|
|
96 | return; | |
|
|
97 | this._disposed = true; | |
|
|
98 | for (const f of this._cleanup) | |
|
|
99 | f(); | |
|
|
90 | 100 | } |
|
|
91 | 101 | |
|
|
92 | 102 | /** |
| @@ -109,8 +119,8 export class Container { | |||
|
|
109 | 119 | } |
|
|
110 | 120 | } |
|
|
111 | 121 | |
|
|
112 | createChildContainer() { | |
|
|
113 | return new Container(this); | |
|
|
122 | createChildContainer<S2 extends { container?: Container<S & S2> } = S>(): Container<S & S2> { | |
|
|
123 | return new Container<S & S2>(this as any); | |
|
|
114 | 124 | } |
|
|
115 | 125 | |
|
|
116 | 126 | has(id: string | number) { |
| @@ -1,4 +1,4 | |||
|
|
1 | import { isNull, argumentNotEmptyString, each } from "../safe"; | |
|
|
1 | import { isNull, argumentNotEmptyString, each, keys } from "../safe"; | |
|
|
2 | 2 | import { ActivationContext } from "./ActivationContext"; |
|
|
3 | 3 | import { ServiceMap, Descriptor } from "./interfaces"; |
|
|
4 | 4 | import { ActivationError } from "./ActivationError"; |
| @@ -11,6 +11,12 export interface ReferenceDescriptorPara | |||
|
|
11 | 11 | services?: ServiceMap; |
|
|
12 | 12 | } |
|
|
13 | 13 | |
|
|
14 | function def<T>(v: T) { | |
|
|
15 | if (v === undefined) | |
|
|
16 | throw Error(); | |
|
|
17 | return v; | |
|
|
18 | } | |
|
|
19 | ||
|
|
14 | 20 | export class ReferenceDescriptor<S, K extends keyof S> implements Descriptor<S[K]> { |
|
|
15 | 21 | _name: K; |
|
|
16 | 22 | |
| @@ -20,7 +26,7 export class ReferenceDescriptor<S, K ex | |||
|
|
20 | 26 | |
|
|
21 | 27 | _default: any; |
|
|
22 | 28 | |
|
|
23 | _services: ServiceMap; | |
|
|
29 | _services: ServiceMap<S>; | |
|
|
24 | 30 | |
|
|
25 | 31 | constructor(opts: ReferenceDescriptorParams<S, K>) { |
|
|
26 | 32 | argumentNotEmptyString(opts && opts.name, "opts.name"); |
| @@ -32,24 +38,22 export class ReferenceDescriptor<S, K ex | |||
|
|
32 | 38 | this._services = opts.services || {}; |
|
|
33 | 39 | } |
|
|
34 | 40 | |
|
|
35 |
activate(context: ActivationContext |
|
|
|
41 | activate(context: ActivationContext<S>) { | |
|
|
36 | 42 | // добавляем сервисы |
|
|
37 | 43 | if (this._services) { |
|
|
38 | for (const p of Object.keys(this._services)) | |
|
|
39 | context.register(p, this._services[p]); | |
|
|
44 | each(this._services, (v, k) => context.register(k, def(v))); | |
|
|
40 | 45 | } |
|
|
41 | 46 | |
|
|
42 | 47 | if (this._lazy) { |
|
|
43 | 48 | const saved = context.clone(); |
|
|
44 | 49 | |
|
|
45 | return (cfg: ServiceMap) => { | |
|
|
50 | return (cfg: Partial<ServiceMap<S>>) => { | |
|
|
46 | 51 | // защищаем контекст на случай исключения в процессе |
|
|
47 | 52 | // активации |
|
|
48 | 53 | const ct = saved.clone(); |
|
|
49 | 54 | try { |
|
|
50 | 55 | if (cfg) { |
|
|
51 | for (const k in cfg) | |
|
|
52 | ct.register(k, cfg[k]); | |
|
|
56 | each(cfg, (v, k) => ct.register(k, v || {})); | |
|
|
53 | 57 | } |
|
|
54 | 58 | |
|
|
55 | 59 | return this._optional ? ct.resolve(this._name, this._default) : ct |
| @@ -59,12 +63,6 export class ReferenceDescriptor<S, K ex | |||
|
|
59 | 63 | } |
|
|
60 | 64 | }; |
|
|
61 | 65 | } else { |
|
|
62 | // добавляем сервисы | |
|
|
63 | if (this._services) { | |
|
|
64 | for (const p of Object.keys(this._services)) | |
|
|
65 | context.register(p, this._services[p]); | |
|
|
66 | } | |
|
|
67 | ||
|
|
68 | 66 | const v = this._optional ? |
|
|
69 | 67 | context.resolve(this._name, this._default) : |
|
|
70 | 68 | context.resolve(this._name); |
| @@ -3,7 +3,7 import { ActivationContext } from "./Act | |||
|
|
3 | 3 | import { Constructor, Factory } from "../interfaces"; |
|
|
4 | 4 | |
|
|
5 | 5 | export interface Descriptor<T = any> { |
|
|
6 |
activate(context: ActivationContext |
|
|
|
6 | activate<S>(context: ActivationContext<S>): T; | |
|
|
7 | 7 | } |
|
|
8 | 8 | |
|
|
9 | 9 | export function isDescriptor(x: any): x is Descriptor { |
| @@ -70,6 +70,6 export function isValueRegistration(x: a | |||
|
|
70 | 70 | return (!isPrimitive(x)) && ("$value" in x); |
|
|
71 | 71 | } |
|
|
72 | 72 | |
|
|
73 |
export function isDependencyRegistration(x: any): x is DependencyRegistration< |
|
|
|
73 | export function isDependencyRegistration<S>(x: any): x is DependencyRegistration<S, keyof S> { | |
|
|
74 | 74 | return (!isPrimitive(x)) && ("$dependency" in x); |
|
|
75 | 75 | } |
| @@ -6,9 +6,9 const _oid = typeof Symbol === "function | |||
|
|
6 | 6 | Symbol("__implab__oid__") : |
|
|
7 | 7 | "__implab__oid__"; |
|
|
8 | 8 | |
|
|
9 |
export function oid(instance: |
|
|
|
9 | export function oid(instance: any): string | undefined { | |
|
|
10 | 10 | if (isNull(instance)) |
|
|
11 |
return |
|
|
|
11 | return undefined; | |
|
|
12 | 12 | |
|
|
13 | 13 | if (_oid in instance) |
|
|
14 | 14 | return instance[_oid]; |
| @@ -16,6 +16,10 export function oid(instance: object): s | |||
|
|
16 | 16 | return (instance[_oid] = "oid_" + (++_nextOid)); |
|
|
17 | 17 | } |
|
|
18 | 18 | |
|
|
19 | export function keys<T>(arg: T): (Extract<keyof T, string>)[] { | |
|
|
20 | return isObject(arg) && arg ? Object.keys(arg) as (Extract<keyof T, string>)[] : []; | |
|
|
21 | } | |
|
|
22 | ||
|
|
19 | 23 | export function argumentNotNull(arg: any, name: string) { |
|
|
20 | 24 | if (arg === null || arg === undefined) |
|
|
21 | 25 | throw new Error("The argument " + name + " can't be null or undefined"); |
| @@ -36,6 +40,10 export function argumentOfType(arg: any, | |||
|
|
36 | 40 | throw new Error("The argument '" + name + "' type doesn't match"); |
|
|
37 | 41 | } |
|
|
38 | 42 | |
|
|
43 | export function isObject(val: any): val is object { | |
|
|
44 | return typeof val === "object"; | |
|
|
45 | } | |
|
|
46 | ||
|
|
39 | 47 | export function isNull(val: any) { |
|
|
40 | 48 | return (val === null || val === undefined); |
|
|
41 | 49 | } |
| @@ -65,21 +73,20 export function isCancellable(val: any): | |||
|
|
65 | 73 | return val && typeof val.cancel === "function"; |
|
|
66 | 74 | } |
|
|
67 | 75 | |
|
|
68 | export function isNullOrEmptyString(val: any): val is string | null | undefined { | |
|
|
69 |
|
|
|
|
76 | export function isNullOrEmptyString(val: any): val is (string | null | undefined) { | |
|
|
77 | return (val === null || val === undefined || | |
|
|
70 | 78 | ((typeof (val) === "string" || val instanceof String) && val.length === 0)) |
|
|
71 | return true; | |
|
|
72 | 79 | } |
|
|
73 | 80 | |
|
|
74 | 81 | export function isNotEmptyArray(arg: any): arg is Array<any> { |
|
|
75 | 82 | return (arg instanceof Array && arg.length > 0); |
|
|
76 | 83 | } |
|
|
77 | 84 | |
|
|
78 | function _isStrictMode() { | |
|
|
85 | function _isStrictMode(this: any) { | |
|
|
79 | 86 | return !this; |
|
|
80 | 87 | } |
|
|
81 | 88 | |
|
|
82 | function _getNonStrictGlobal() { | |
|
|
89 | function _getNonStrictGlobal(this: any) { | |
|
|
83 | 90 | return this; |
|
|
84 | 91 | } |
|
|
85 | 92 | |
| @@ -119,7 +126,9 export function get(member: string, cont | |||
|
|
119 | 126 | * @returns Результат вызова функции <c>cb</c>, либо <c>undefined</c> |
|
|
120 | 127 | * если достигнут конец массива. |
|
|
121 | 128 | */ |
|
|
122 | export function each(obj, cb, thisArg?) { | |
|
|
129 | export function each<T>(obj: T, cb: (v: T[keyof T], k: keyof T) => void): void; | |
|
|
130 | export function each(obj: any, cb: any, thisArg?: any): any; | |
|
|
131 | export function each(obj: any, cb: any, thisArg?: any) { | |
|
|
123 | 132 | argumentNotNull(cb, "cb"); |
|
|
124 | 133 | if (obj instanceof Array) { |
|
|
125 | 134 | for (let i = 0; i < obj.length; i++) { |
| @@ -128,8 +137,8 export function each(obj, cb, thisArg?) | |||
|
|
128 | 137 | return x; |
|
|
129 | 138 | } |
|
|
130 | 139 | } else { |
|
|
131 | const keys = Object.keys(obj); | |
|
|
132 | for (const k of keys) { | |
|
|
140 | const _keys = Object.keys(obj); | |
|
|
141 | for (const k of _keys) { | |
|
|
133 | 142 | const x = cb.call(thisArg, obj[k], k); |
|
|
134 | 143 | if (x !== undefined) |
|
|
135 | 144 | return x; |
| @@ -166,8 +175,8 export function mixin<T extends object, | |||
|
|
166 | 175 | _res[p] = source[p]; |
|
|
167 | 176 | } |
|
|
168 | 177 | } else if (template) { |
|
|
169 | const keys = Object.keys(source); | |
|
|
170 | for (const p of keys) { | |
|
|
178 | const _keys = Object.keys(source); | |
|
|
179 | for (const p of _keys) { | |
|
|
171 | 180 | if (p in template) |
|
|
172 | 181 | _res[template[p]] = source[p]; |
|
|
173 | 182 | } |
| @@ -184,7 +193,7 export function mixin<T extends object, | |||
|
|
184 | 193 | * @param{Object} thisArg [Optional] Object which will be passed as 'this' to the function. |
|
|
185 | 194 | * @param{Function|String} fn [Required] Function wich will be wrapped. |
|
|
186 | 195 | */ |
|
|
187 | export function async(_fn: (...args: any[]) => any, thisArg): (...args: any[]) => PromiseLike<any> { | |
|
|
196 | export function async(_fn: (...args: any[]) => any, thisArg: any): (...args: any[]) => PromiseLike<any> { | |
|
|
188 | 197 | let fn = _fn; |
|
|
189 | 198 | |
|
|
190 | 199 | if (arguments.length === 2 && !(fn instanceof Function)) |
| @@ -284,7 +293,7 export function pmap<T, T2>( | |||
|
|
284 | 293 | let i = 0; |
|
|
285 | 294 | const result = new Array<T2>(); |
|
|
286 | 295 | |
|
|
287 | const next = () => { | |
|
|
296 | const next = (): any => { | |
|
|
288 | 297 | while (i < items.length) { |
|
|
289 | 298 | const r = cb(items[i], i); |
|
|
290 | 299 | const ri = i; |
| @@ -319,7 +328,7 export function pfor<T>( | |||
|
|
319 | 328 | |
|
|
320 | 329 | let i = 0; |
|
|
321 | 330 | |
|
|
322 | const next = () => { | |
|
|
331 | const next = (): any => { | |
|
|
323 | 332 | while (i < items.length) { |
|
|
324 | 333 | const r = cb(items[i], i); |
|
|
325 | 334 | i++; |
| @@ -1,6 +1,13 | |||
|
|
1 |
import { |
|
|
|
1 | import { services } from "../di/Annotations"; | |
|
|
2 | import { Bar } from "./Bar"; | |
|
|
2 | 3 | |
|
|
3 | const service = config.build("barBox"); | |
|
|
4 | // declare required dependencies | |
|
|
5 | const config = services<{ | |
|
|
6 | bar: Bar; | |
|
|
7 | }>(); | |
|
|
8 | ||
|
|
9 | // export service descriptor | |
|
|
10 | export const service = config.build<Box<Bar>>(); | |
|
|
4 | 11 | |
|
|
5 | 12 | @service.consume(config.dependency("bar")) |
|
|
6 | 13 | export class Box<T> { |
| @@ -25,4 +32,4 export class Box<T> { | |||
|
|
25 | 32 | |
|
|
26 | 33 | return this._value; |
|
|
27 | 34 | } |
|
|
28 | } No newline at end of file | |
|
|
35 | } | |
General Comments 0
You need to be logged in to leave comments.
Login now
