ActivationContext.ts
133 lines
| 3.5 KiB
| video/mp2t
|
TypeScriptLexer
|
|
r49 | import { TraceSource } from "../log/TraceSource"; | ||
|
|
r133 | import { argumentNotEmptyString } from "../safe"; | ||
| import { Descriptor, ContainerServiceMap, ContainerKeys, TypeOfService } from "./interfaces"; | ||||
|
|
r49 | import { Container } from "./Container"; | ||
|
|
r114 | import { MapOf } from "../interfaces"; | ||
|
|
r49 | |||
| const trace = TraceSource.get("@implab/core/di/ActivationContext"); | ||||
|
|
r131 | export interface ActivationContextInfo { | ||
|
|
r49 | name: string; | ||
| service: string; | ||||
| } | ||||
|
|
r120 | export class ActivationContext<S extends object> { | ||
|
|
r114 | _cache: MapOf<any>; | ||
|
|
r49 | |||
|
|
r120 | _services: ContainerServiceMap<S>; | ||
|
|
r49 | |||
|
|
r114 | _visited: MapOf<any>; | ||
|
|
r49 | |||
| _name: string; | ||||
|
|
r131 | _service: Descriptor<S, any>; | ||
|
|
r49 | |||
|
|
r131 | _container: Container<S>; | ||
| _parent: ActivationContext<S> | undefined; | ||||
|
|
r49 | |||
|
|
r131 | constructor(container: Container<S>, services: ContainerServiceMap<S>, name: string, service: Descriptor<S, any>) { | ||
| this._name = name; | ||||
| this._service = service; | ||||
| this._visited = {}; | ||||
| this._cache = {}; | ||||
|
|
r49 | this._services = services; | ||
|
|
r131 | this._container = container; | ||
|
|
r49 | } | ||
| getName() { | ||||
| return this._name; | ||||
| } | ||||
|
|
r131 | getContainer() { | ||
| return this._container; | ||||
| } | ||||
|
|
r133 | resolve<K extends ContainerKeys<S>>(name: K): TypeOfService<S, K>; | ||
| resolve<K extends ContainerKeys<S>, T>(name: K, def: T): TypeOfService<S, K> | T; | ||||
| resolve<K extends ContainerKeys<S>>(name: K, def: undefined): TypeOfService<S, K> | undefined; | ||||
| resolve<K extends ContainerKeys<S>, T>(name: K, def?: T): TypeOfService<S, K> | T | undefined { | ||||
|
|
r49 | const d = this._services[name]; | ||
|
|
r114 | if (d !== undefined) { | ||
|
|
r120 | return this.activate(d, name.toString()); | ||
|
|
r114 | } else { | ||
|
|
r133 | if (arguments.length > 1) | ||
|
|
r49 | return def; | ||
| else | ||||
| throw new Error(`Service ${name} not found`); | ||||
|
|
r114 | } | ||
|
|
r49 | } | ||
| /** | ||||
| * registers services local to the the activation context | ||||
| * | ||||
| * @name{string} the name of the service | ||||
| * @service{string} the service descriptor to register | ||||
| */ | ||||
|
|
r115 | register<K extends keyof S>(name: K, service: Descriptor<S, S[K]>) { | ||
|
|
r49 | argumentNotEmptyString(name, "name"); | ||
|
|
r120 | this._services[name] = service as any; | ||
|
|
r49 | } | ||
| has(id: string) { | ||||
| return id in this._cache; | ||||
| } | ||||
|
|
r114 | get<T>(id: string) { | ||
|
|
r49 | return this._cache[id]; | ||
| } | ||||
|
|
r114 | store(id: string, value: any) { | ||
|
|
r49 | return (this._cache[id] = value); | ||
| } | ||||
|
|
r115 | activate<T>(d: Descriptor<S, T>, name: string) { | ||
|
|
r49 | if (trace.isLogEnabled()) | ||
| trace.log(`enter ${name} ${d}`); | ||||
|
|
r131 | const ctx = this.enter(d, name); | ||
| const v = d.activate(ctx); | ||||
|
|
r49 | |||
| if (trace.isLogEnabled()) | ||||
| trace.log(`leave ${name}`); | ||||
| return v; | ||||
| } | ||||
| visit(id: string) { | ||||
| const count = this._visited[id] || 0; | ||||
| this._visited[id] = count + 1; | ||||
| return count; | ||||
| } | ||||
|
|
r131 | getStack(): ActivationContextInfo[] { | ||
| const stack = [{ | ||||
| name: this._name, | ||||
| service: this._service.toString() | ||||
| }]; | ||||
| return this._parent ? | ||||
| stack.concat(this._parent.getStack()) : | ||||
| stack; | ||||
|
|
r49 | } | ||
|
|
r131 | private enter(service: Descriptor<S, any>, name: string): this { | ||
| const clone = Object.create(this); | ||||
| clone._name = name; | ||||
| clone._services = Object.create(this._services); | ||||
| clone._parent = this; | ||||
| clone._service = service; | ||||
| return clone; | ||||
|
|
r49 | } | ||
|
|
r132 | |||
| /** Creates a clone for the current context, used to protect it from modifications */ | ||||
| clone(): this { | ||||
| const clone = Object.create(this); | ||||
| clone._services = Object.create(this._services); | ||||
| return clone; | ||||
| } | ||||
|
|
r49 | } | ||
