import { argumentNotEmptyString, each } from "../safe"; import { ActivationContext } from "./ActivationContext"; import { Descriptor, PartialServiceMap, TypeOfService, ContainerKeys } from "./interfaces"; export interface ReferenceDescriptorParams> { /** * The name of the descriptor */ name: K; /** * 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. */ optional?: boolean; /** * a default value for the reference when the referenced service doesn't exist. */ default?: TypeOfService; /** * The service overrides */ services?: PartialServiceMap; } export class ReferenceDescriptor = ContainerKeys> implements Descriptor> { _name: K; _optional = false; _default: TypeOfService | undefined; _services: PartialServiceMap; constructor(opts: ReferenceDescriptorParams) { argumentNotEmptyString(opts && opts.name, "opts.name"); this._name = opts.name; this._optional = !!opts.optional; this._default = opts.default; this._services = (opts.services || {}) as PartialServiceMap; } /** This method activates the referenced service if one exists * @param context activation context which is used during current activation */ activate(context: ActivationContext): any { // добавляем сервисы if (this._services) { each(this._services, (v, k) => context.register(k, v)); } const res = this._optional ? context.resolve(this._name, this._default) : context.resolve(this._name); return res; } toString() { const opts = []; if (this._optional) opts.push("optional"); 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(""); } }