##// END OF EJS Templates
sync
sync

File last commit:

r120:1b124b65514a ioc ts support
r121:876264d00a17 ioc ts support
Show More
ReferenceDescriptor.ts
95 lines | 2.9 KiB | video/mp2t | TypeScriptLexer
/ src / main / ts / di / ReferenceDescriptor.ts
cin
improved interfaces and more tight type checking
r120 import { argumentNotEmptyString, each } from "../safe";
cin
changed the project structure
r49 import { ActivationContext } from "./ActivationContext";
cin
improved interfaces and more tight type checking
r120 import { Descriptor, PartialServiceMap, ContainerResolve, ContainerKeys } from "./interfaces";
cin
changed the project structure
r49 import { ActivationError } from "./ActivationError";
cin
improved interfaces and more tight type checking
r120 export interface ReferenceDescriptorParams<S extends object, K extends ContainerKeys<S>> {
cin
working on IoC configuration
r113 name: K;
cin
changed the project structure
r49 lazy?: boolean;
optional?: boolean;
cin
improved interfaces and more tight type checking
r120 default?: ContainerResolve<S, K>;
cin
corrected code to support ts strict mode...
r115 services?: PartialServiceMap<S>;
cin
changed the project structure
r49 }
cin
improved interfaces and more tight type checking
r120 export class ReferenceDescriptor<S extends object = any, K extends ContainerKeys<S> = ContainerKeys<S>>
implements Descriptor<S, ContainerResolve<S, K> | ((args?: PartialServiceMap<S>) => ContainerResolve<S, K>)> {
cin
working on IoC configuration
r114
cin
working on IoC configuration
r113 _name: K;
cin
changed the project structure
r49
_lazy = false;
_optional = false;
cin
improved interfaces and more tight type checking
r120 _default: ContainerResolve<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
improved interfaces and more tight type checking
r120 this._services = (opts.services || {}) as PartialServiceMap<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("");
}
}