ActivationContext.ts
132 lines
| 3.0 KiB
| video/mp2t
|
TypeScriptLexer
|
|
r33 | import { TraceSource } from "../log/TraceSource"; | ||
| import { argumentNotNull, argumentNotEmptyString, isPrimitive, each, isNull } from "../safe"; | ||||
|
|
r34 | import { Descriptor, ServiceMap, isDescriptor } from "./interfaces"; | ||
| import { Container } from "./Container"; | ||||
|
|
r33 | |||
|
|
r39 | const trace = TraceSource.get("@implab/core/di/ActivationContext"); | ||
|
|
r33 | |||
|
|
r39 | export interface ActivationContextInfo { | ||
| name: string; | ||||
|
|
r34 | |||
|
|
r41 | service: string; | ||
|
|
r34 | |||
|
|
r39 | scope: ServiceMap; | ||
|
|
r34 | } | ||
|
|
r39 | export class ActivationContext { | ||
| _cache: object; | ||||
|
|
r33 | |||
|
|
r39 | _services: ServiceMap; | ||
|
|
r33 | |||
|
|
r39 | _stack: ActivationContextInfo[]; | ||
|
|
r33 | |||
|
|
r39 | _visited: object; | ||
|
|
r33 | |||
|
|
r41 | _name: string; | ||
| _localized: boolean; | ||||
|
|
r39 | container: Container; | ||
|
|
r33 | |||
|
|
r41 | constructor(container: Container, services: ServiceMap, name?: string, cache?: object, visited?) { | ||
|
|
r33 | argumentNotNull(container, "container"); | ||
| argumentNotNull(services, "services"); | ||||
|
|
r41 | this._name = name; | ||
|
|
r33 | this._visited = visited || {}; | ||
| this._stack = []; | ||||
| this._cache = cache || {}; | ||||
| this._services = services; | ||||
| this.container = container; | ||||
| } | ||||
|
|
r41 | getName() { | ||
| return this._name; | ||||
| } | ||||
| resolve(name, def?): any { | ||||
|
|
r39 | const d = this._services[name]; | ||
|
|
r33 | |||
| if (!d) | ||||
| if (arguments.length > 1) | ||||
| return def; | ||||
| else | ||||
|
|
r39 | throw new Error(`Service ${name} not found`); | ||
|
|
r33 | |||
|
|
r41 | return this.activate(d, name); | ||
|
|
r33 | } | ||
| /** | ||||
| * registers services local to the the activation context | ||||
|
|
r39 | * | ||
|
|
r33 | * @name{string} the name of the service | ||
| * @service{string} the service descriptor to register | ||||
| */ | ||||
| register(name: string, service: Descriptor) { | ||||
| argumentNotEmptyString(name, "name"); | ||||
| this._services[name] = service; | ||||
| } | ||||
| clone() { | ||||
| return new ActivationContext( | ||||
| this.container, | ||||
|
|
r41 | this._services, | ||
| this._name, | ||||
|
|
r33 | this._cache, | ||
| this._visited | ||||
| ); | ||||
| } | ||||
|
|
r39 | has(id: string) { | ||
|
|
r33 | return id in this._cache; | ||
| } | ||||
|
|
r39 | get(id: string) { | ||
|
|
r33 | return this._cache[id]; | ||
| } | ||||
|
|
r39 | store(id: string, value) { | ||
|
|
r33 | return (this._cache[id] = value); | ||
| } | ||||
|
|
r41 | activate(d: Descriptor, name: string) { | ||
| if (trace.isLogEnabled()) | ||||
| trace.log(`enter ${name} ${d}`); | ||||
|
|
r33 | |||
|
|
r41 | this.enter(name, d.toString()); | ||
| const v = d.activate(this); | ||||
| this.leave(); | ||||
| if (trace.isLogEnabled()) | ||||
| trace.log(`leave ${name}`); | ||||
| return v; | ||||
|
|
r33 | } | ||
|
|
r39 | visit(id: string) { | ||
| const count = this._visited[id] || 0; | ||||
|
|
r33 | this._visited[id] = count + 1; | ||
| return count; | ||||
| } | ||||
| getStack() { | ||||
| return this._stack.slice().reverse(); | ||||
| } | ||||
|
|
r41 | private enter(name: string, service: string) { | ||
|
|
r33 | this._stack.push({ | ||
|
|
r39 | name, | ||
|
|
r41 | service, | ||
|
|
r33 | scope: this._services | ||
| }); | ||||
|
|
r41 | this._name = name; | ||
| this._services = Object.create(this._services); | ||||
|
|
r33 | } | ||
|
|
r41 | private leave() { | ||
|
|
r39 | const ctx = this._stack.pop(); | ||
|
|
r33 | this._services = ctx.scope; | ||
|
|
r41 | this._name = ctx.name; | ||
|
|
r33 | } | ||
|
|
r39 | } | ||
