##// END OF EJS Templates
configuration draft-1
configuration draft-1

File last commit:

r118:6738ac4c3072 ioc ts support
r119:86e3aa3c3eea ioc ts support
Show More
ReferenceDescriptor.ts
99 lines | 2.8 KiB | video/mp2t | TypeScriptLexer
/ src / main / ts / di / ReferenceDescriptor.ts
cin
working on IoC configuration
r114 import { isNull, argumentNotEmptyString, each, keys } from "../safe";
cin
changed the project structure
r49 import { ActivationContext } from "./ActivationContext";
cin
corrected code to support ts strict mode...
r115 import { ServiceMap, Descriptor, PartialServiceMap } from "./interfaces";
cin
changed the project structure
r49 import { ActivationError } from "./ActivationError";
cin
working on IoC configuration
r113 export interface ReferenceDescriptorParams<S, K extends keyof S> {
name: K;
cin
changed the project structure
r49 lazy?: boolean;
optional?: boolean;
cin
working on IoC configuration
r113 default?: S[K];
cin
corrected code to support ts strict mode...
r115 services?: PartialServiceMap<S>;
cin
changed the project structure
r49 }
cin
corrected code to support ts strict mode...
r115 function defined<T>(v: T | undefined) {
cin
working on IoC configuration
r114 if (v === undefined)
throw Error();
return v;
}
cin
corrected code to support ts strict mode...
r115 export class ReferenceDescriptor<S = any, K extends keyof S = keyof S> implements Descriptor<S, S[K] | ((args?: PartialServiceMap<S> ) => S[K])> {
cin
working on IoC configuration
r113 _name: K;
cin
changed the project structure
r49
_lazy = false;
_optional = false;
cin
corrected code to support ts strict mode...
r115 _default: S[K] | undefined;
cin
changed the project structure
r49
cin
configuration interfaces moved to di/Configuration module...
r118 _services: PartialServiceMap<S>;
cin
changed the project structure
r49
cin
working on IoC configuration
r113 constructor(opts: ReferenceDescriptorParams<S, K>) {
cin
changed the project structure
r49 argumentNotEmptyString(opts && opts.name, "opts.name");
this._name = opts.name;
this._lazy = !!opts.lazy;
this._optional = !!opts.optional;
this._default = opts.default;
cin
working on IoC configuration
r113
cin
corrected code to support ts strict mode...
r115 this._services = (opts.services || {}) as ServiceMap<S>;
cin
changed the project structure
r49 }
cin
working on IoC configuration
r114 activate(context: ActivationContext<S>) {
cin
changed the project structure
r49 // добавляем сервисы
if (this._services) {
cin
corrected code to support ts strict mode...
r115 each(this._services, (v, k) => context.register(k, v));
cin
changed the project structure
r49 }
if (this._lazy) {
const saved = context.clone();
cin
corrected code to support ts strict mode...
r115 return (cfg?: PartialServiceMap<S>) => {
cin
changed the project structure
r49 // защищаем контекст на случай исключения в процессе
// активации
const ct = saved.clone();
try {
if (cfg) {
cin
configuration interfaces moved to di/Configuration module...
r118 each(cfg, (v, k) => ct.register(k, v));
cin
changed the project structure
r49 }
return this._optional ? ct.resolve(this._name, this._default) : ct
.resolve(this._name);
} catch (error) {
cin
working on IoC configuration
r113 throw new ActivationError(this._name.toString(), ct.getStack(), error);
cin
changed the project structure
r49 }
};
} else {
const v = this._optional ?
context.resolve(this._name, this._default) :
context.resolve(this._name);
return v;
}
}
toString() {
const opts = [];
if (this._optional)
opts.push("optional");
if (this._lazy)
opts.push("lazy");
const parts = [
"@ref "
];
if (opts.length) {
parts.push("{");
parts.push(opts.join());
parts.push("} ");
}
cin
working on IoC configuration
r113 parts.push(this._name.toString());
cin
changed the project structure
r49
cin
corrected code to support ts strict mode...
r115 if (this._default !== undefined && this._default !== null) {
cin
changed the project structure
r49 parts.push(" = ");
cin
corrected code to support ts strict mode...
r115 parts.push(String(this._default));
cin
changed the project structure
r49 }
return parts.join("");
}
}