ReferenceDescriptor.ts
114 lines
| 3.1 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; | ||||
| this._default = !!opts.default; | ||||
| this._services = opts.services; | ||||
|
|
r33 | } | ||
| activate(context: ActivationContext, name: string) { | ||||
|
|
r39 | if (this._lazy) { | ||
|
|
r33 | // сохраняем контекст активации | ||
| context = context.clone(); | ||||
|
|
r39 | |||
| // добавляем сервисы | ||||
| if (this._services) { | ||||
| for (const p of Object.keys(this._services)) | ||||
| context.register(p, this._services[p]); | ||||
| } | ||||
| return (cfg: ServiceMap) => { | ||||
|
|
r33 | // защищаем контекст на случай исключения в процессе | ||
| // активации | ||||
|
|
r39 | const ct = context.clone(); | ||
|
|
r33 | try { | ||
|
|
r39 | if (cfg) { | ||
| for (const k in cfg) | ||||
| ct.register(k, cfg[k]); | ||||
| } | ||||
|
|
r34 | |||
|
|
r39 | return this._optional ? ct.getService(this._name, this._default) : ct | ||
| .getService(this._name); | ||||
|
|
r33 | } catch (error) { | ||
|
|
r39 | throw new ActivationError(this._name, ct.getStack(), error); | ||
|
|
r33 | } | ||
| }; | ||||
|
|
r39 | } else { | ||
| context.enter(name, this, !!this._services); | ||||
| // добавляем сервисы | ||||
| if (this._services) { | ||||
| for (const p of Object.keys(this._services)) | ||||
| context.register(p, this._services[p]); | ||||
| } | ||||
|
|
r33 | |||
|
|
r39 | const v = this._optional ? | ||
| context.getService(this._name, this._default) : | ||||
| context.getService(this._name); | ||||
|
|
r33 | |||
|
|
r39 | context.leave(); | ||
| return v; | ||||
| } | ||||
|
|
r33 | } | ||
| isInstanceCreated() { | ||||
| return false; | ||||
| } | ||||
| getInstance() { | ||||
| throw new Error("The reference descriptor doesn't allowed to hold an instance"); | ||||
| } | ||||
| 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 | } | ||
