##// END OF EJS Templates
tests
tests

File last commit:

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