##// END OF EJS Templates
Added CancelledError, fixed lint warnings
Added CancelledError, fixed lint warnings

File last commit:

r134:511bcc634d65 ioc ts support
r172:3969a8fb8049 release v1.4.6 default
Show More
ReferenceDescriptor.ts
88 lines | 2.4 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 fluent configuration, di annotations removed
r134 /**
* The name of the descriptor
*/
cin
working on IoC configuration
r113 name: K;
cin
working on fluent configuration, di annotations removed
r134
/**
* The flag that indicates that the referenced service isn't required to exist.
* If the reference is optional and the referenced service doesn't exist,
* the undefined or a default value will be returned.
*/
cin
changed the project structure
r49 optional?: boolean;
cin
working on fluent configuration, di annotations removed
r134
/**
* a default value for the reference when the referenced service doesn't exist.
*/
cin
working on fluent configuration
r133 default?: TypeOfService<S, K>;
cin
working on fluent configuration, di annotations removed
r134
/**
* The service overrides
*/
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 fluent configuration, di annotations removed
r134 /** This method activates the referenced service if one exists
* @param context activation context which is used during current activation
*/
activate(context: ActivationContext<S>): any {
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("");
}
}