##// END OF EJS Templates
working on fluent configuration
working on fluent configuration

File last commit:

r133:09ea4b9e3735 ioc ts support
r133:09ea4b9e3735 ioc ts support
Show More
ReferenceDescriptor.ts
68 lines | 1.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
working on fluent configuration
r133 import { Descriptor, PartialServiceMap, TypeOfService, ContainerKeys } from "./interfaces";
cin
changed the project structure
r49
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 optional?: boolean;
cin
working on fluent configuration
r133 default?: TypeOfService<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>>
cin
working on fluent configuration
r133 implements Descriptor<S, TypeOfService<S, K>> {
cin
working on IoC configuration
r114
cin
working on IoC configuration
r113 _name: K;
cin
changed the project structure
r49
_optional = false;
cin
working on fluent configuration
r133 _default: TypeOfService<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._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 }
cin
Added LazyReferenceDescriptor, removed lazy behaviour from ReferenceDescriptor.
r122 const res = this._optional ?
context.resolve(this._name, this._default) :
context.resolve(this._name);
cin
changed the project structure
r49
cin
Added LazyReferenceDescriptor, removed lazy behaviour from ReferenceDescriptor.
r122 return res;
cin
changed the project structure
r49 }
toString() {
const opts = [];
if (this._optional)
opts.push("optional");
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("");
}
}