##// END OF EJS Templates
tests, refactoring, fixes
tests, refactoring, fixes

File last commit:

r40:6559c5b81a19 di-typescript
r40:6559c5b81a19 di-typescript
Show More
ReferenceDescriptor.ts
114 lines | 3.1 KiB | video/mp2t | TypeScriptLexer
/ src / ts / di / ReferenceDescriptor.ts
cin
minor fixes, code cleanup...
r33 import { isNull, argumentNotEmptyString, each } from "../safe";
cin
ported IoC container to typescript...
r34 import { ActivationContext } from "./ActivationContext";
import { ServiceMap, Descriptor } from "./interfaces";
import { ActivationError } from "./ActivationError";
cin
minor fixes, code cleanup...
r33
cin
tests, refactoring, fixes
r40 export interface ReferenceDescriptorParams {
name: string;
lazy?: boolean;
optional?: boolean;
default?;
services?: ServiceMap;
}
cin
ported IoC container to typescript...
r34 export class ReferenceDescriptor implements Descriptor {
cin
ts code cleanup, linting
r39 _name: string;
cin
minor fixes, code cleanup...
r33
cin
ts code cleanup, linting
r39 _lazy = false;
cin
minor fixes, code cleanup...
r33
cin
ts code cleanup, linting
r39 _optional = false;
cin
minor fixes, code cleanup...
r33
cin
ts code cleanup, linting
r39 _default: any;
cin
minor fixes, code cleanup...
r33
cin
ts code cleanup, linting
r39 _services: ServiceMap;
cin
minor fixes, code cleanup...
r33
cin
tests, refactoring, fixes
r40 constructor(opts: ReferenceDescriptorParams) {
argumentNotEmptyString(opts && opts.name, "opts.name");
this._name = opts.name;
this._lazy = !!opts.lazy;
this._optional = !!opts.optional;
this._default = !!opts.default;
this._services = opts.services;
cin
minor fixes, code cleanup...
r33 }
activate(context: ActivationContext, name: string) {
cin
ts code cleanup, linting
r39 if (this._lazy) {
cin
minor fixes, code cleanup...
r33 // сохраняем контекст активации
context = context.clone();
cin
ts code cleanup, linting
r39
// добавляем сервисы
if (this._services) {
for (const p of Object.keys(this._services))
context.register(p, this._services[p]);
}
return (cfg: ServiceMap) => {
cin
minor fixes, code cleanup...
r33 // защищаем контекст на случай исключения в процессе
// активации
cin
ts code cleanup, linting
r39 const ct = context.clone();
cin
minor fixes, code cleanup...
r33 try {
cin
ts code cleanup, linting
r39 if (cfg) {
for (const k in cfg)
ct.register(k, cfg[k]);
}
cin
ported IoC container to typescript...
r34
cin
ts code cleanup, linting
r39 return this._optional ? ct.getService(this._name, this._default) : ct
.getService(this._name);
cin
minor fixes, code cleanup...
r33 } catch (error) {
cin
ts code cleanup, linting
r39 throw new ActivationError(this._name, ct.getStack(), error);
cin
minor fixes, code cleanup...
r33 }
};
cin
ts code cleanup, linting
r39 } else {
context.enter(name, this, !!this._services);
// добавляем сервисы
if (this._services) {
for (const p of Object.keys(this._services))
context.register(p, this._services[p]);
}
cin
minor fixes, code cleanup...
r33
cin
ts code cleanup, linting
r39 const v = this._optional ?
context.getService(this._name, this._default) :
context.getService(this._name);
cin
minor fixes, code cleanup...
r33
cin
ts code cleanup, linting
r39 context.leave();
return v;
}
cin
minor fixes, code cleanup...
r33 }
isInstanceCreated() {
return false;
}
getInstance() {
throw new Error("The reference descriptor doesn't allowed to hold an instance");
}
toString() {
cin
ts code cleanup, linting
r39 const opts = [];
cin
minor fixes, code cleanup...
r33 if (this._optional)
opts.push("optional");
if (this._lazy)
opts.push("lazy");
cin
ts code cleanup, linting
r39 const parts = [
cin
minor fixes, code cleanup...
r33 "@ref "
];
if (opts.length) {
parts.push("{");
parts.push(opts.join());
parts.push("} ");
}
parts.push(this._name);
if (!isNull(this._default)) {
parts.push(" = ");
parts.push(this._default);
}
return parts.join("");
}
cin
ts code cleanup, linting
r39 }