ReferenceDescriptor.ts
100 lines
| 2.8 KiB
| video/mp2t
|
TypeScriptLexer
|
|
r33 | import { isNull, argumentNotEmptyString, each } from "../safe"; | ||
|
|
r34 | import { ActivationContext } from "./ActivationContext"; | ||
| import { ServiceMap, Descriptor } from "./interfaces"; | ||||
| import { ActivationError } from "./ActivationError"; | ||||
|
|
r33 | |||
|
|
r40 | export interface ReferenceDescriptorParams { | ||
| name: string; | ||||
| lazy?: boolean; | ||||
| optional?: boolean; | ||||
| default?; | ||||
| services?: ServiceMap; | ||||
| } | ||||
|
|
r34 | export class ReferenceDescriptor implements Descriptor { | ||
|
|
r39 | _name: string; | ||
|
|
r33 | |||
|
|
r39 | _lazy = false; | ||
|
|
r33 | |||
|
|
r39 | _optional = false; | ||
|
|
r33 | |||
|
|
r39 | _default: any; | ||
|
|
r33 | |||
|
|
r39 | _services: ServiceMap; | ||
|
|
r33 | |||
|
|
r40 | constructor(opts: ReferenceDescriptorParams) { | ||
| argumentNotEmptyString(opts && opts.name, "opts.name"); | ||||
| this._name = opts.name; | ||||
| this._lazy = !!opts.lazy; | ||||
| this._optional = !!opts.optional; | ||||
|
|
r41 | this._default = opts.default; | ||
|
|
r40 | this._services = opts.services; | ||
|
|
r33 | } | ||
| activate(context: ActivationContext, name: string) { | ||||
|
|
r41 | // добавляем сервисы | ||
| if (this._services) { | ||||
| for (const p of Object.keys(this._services)) | ||||
| context.register(p, this._services[p]); | ||||
| } | ||||
|
|
r33 | |||
|
|
r39 | if (this._lazy) { | ||
|
|
r41 | const saved = context.clone(); | ||
|
|
r39 | |||
| return (cfg: ServiceMap) => { | ||||
|
|
r33 | // защищаем контекст на случай исключения в процессе | ||
| // активации | ||||
|
|
r41 | const ct = saved.clone(); | ||
|
|
r33 | try { | ||
|
|
r39 | if (cfg) { | ||
| for (const k in cfg) | ||||
| ct.register(k, cfg[k]); | ||||
| } | ||||
|
|
r34 | |||
|
|
r41 | return this._optional ? ct.resolve(this._name, this._default) : ct | ||
| .resolve(this._name); | ||||
|
|
r33 | } catch (error) { | ||
|
|
r39 | throw new ActivationError(this._name, ct.getStack(), error); | ||
|
|
r33 | } | ||
| }; | ||||
|
|
r39 | } else { | ||
| // добавляем сервисы | ||||
| if (this._services) { | ||||
| for (const p of Object.keys(this._services)) | ||||
| context.register(p, this._services[p]); | ||||
| } | ||||
|
|
r33 | |||
|
|
r39 | const v = this._optional ? | ||
|
|
r41 | context.resolve(this._name, this._default) : | ||
| context.resolve(this._name); | ||||
|
|
r39 | |||
| return v; | ||||
| } | ||||
|
|
r33 | } | ||
| toString() { | ||||
|
|
r39 | const opts = []; | ||
|
|
r33 | if (this._optional) | ||
| opts.push("optional"); | ||||
| if (this._lazy) | ||||
| opts.push("lazy"); | ||||
|
|
r39 | const parts = [ | ||
|
|
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(""); | ||||
| } | ||||
|
|
r39 | } | ||
