##// END OF EJS Templates
Added childContainer service to container services, ServiceContaier is destroyable, fixed browser Uuid version
Added childContainer service to container services, ServiceContaier is destroyable, fixed browser Uuid version

File last commit:

r134:511bcc634d65 ioc ts support
r146:f3f5c56d3b3e v1.4.0-rc5 default
Show More
LazyReferenceDescriptor.ts
83 lines | 2.5 KiB | video/mp2t | TypeScriptLexer
/ src / main / ts / di / LazyReferenceDescriptor.ts
cin
Added LazyReferenceDescriptor, removed lazy behaviour from ReferenceDescriptor.
r122 import { argumentNotEmptyString, each } from "../safe";
import { ActivationContext } from "./ActivationContext";
cin
working on fluent configuration
r133 import { Descriptor, PartialServiceMap, TypeOfService, ContainerKeys } from "./interfaces";
cin
Added LazyReferenceDescriptor, removed lazy behaviour from ReferenceDescriptor.
r122 import { ActivationError } from "./ActivationError";
export interface ReferenceDescriptorParams<S extends object, K extends ContainerKeys<S>> {
name: K;
optional?: boolean;
cin
working on fluent configuration
r133 default?: TypeOfService<S, K>;
cin
Added LazyReferenceDescriptor, removed lazy behaviour from ReferenceDescriptor.
r122 services?: PartialServiceMap<S>;
}
export class LazyReferenceDescriptor<S extends object = any, K extends ContainerKeys<S> = ContainerKeys<S>>
cin
working on fluent configuration
r133 implements Descriptor<S, ((args?: PartialServiceMap<S>) => TypeOfService<S, K>)> {
cin
Added LazyReferenceDescriptor, removed lazy behaviour from ReferenceDescriptor.
r122
_name: K;
_optional = false;
cin
working on fluent configuration
r133 _default: TypeOfService<S, K> | undefined;
cin
Added LazyReferenceDescriptor, removed lazy behaviour from ReferenceDescriptor.
r122
_services: PartialServiceMap<S>;
constructor(opts: ReferenceDescriptorParams<S, K>) {
argumentNotEmptyString(opts && opts.name, "opts.name");
this._name = opts.name;
this._optional = !!opts.optional;
this._default = opts.default;
this._services = (opts.services || {}) as PartialServiceMap<S>;
}
activate(context: ActivationContext<S>) {
// добавляем сервисы
if (this._services) {
each(this._services, (v, k) => context.register(k, v));
}
const saved = context.clone();
cin
working on fluent configuration, di annotations removed
r134 return (cfg?: PartialServiceMap<S>): any => {
cin
Added LazyReferenceDescriptor, removed lazy behaviour from ReferenceDescriptor.
r122 // защищаем контекст на случай исключения в процессе
// активации
cin
working on fluent configuration
r132 const ct = cfg ? saved.clone() : saved;
cin
Added LazyReferenceDescriptor, removed lazy behaviour from ReferenceDescriptor.
r122 try {
if (cfg) {
each(cfg, (v, k) => ct.register(k, v));
}
return this._optional ? ct.resolve(this._name, this._default) : ct
.resolve(this._name);
} catch (error) {
throw new ActivationError(this._name.toString(), ct.getStack(), error);
}
};
}
toString() {
const opts = [];
if (this._optional)
opts.push("optional");
opts.push("lazy");
const parts = [
"@ref "
];
if (opts.length) {
parts.push("{");
parts.push(opts.join());
parts.push("} ");
}
parts.push(this._name.toString());
if (this._default !== undefined && this._default !== null) {
parts.push(" = ");
parts.push(String(this._default));
}
return parts.join("");
}
}