##// END OF EJS Templates
tests, refactoring, fixes
tests, refactoring, fixes

File last commit:

r40:6559c5b81a19 di-typescript
r40:6559c5b81a19 di-typescript
Show More
ActivationContext.ts
134 lines | 3.3 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
ts code cleanup, linting
r39 service: Descriptor;
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
ts code cleanup, linting
r39 container: Container;
cin
minor fixes, code cleanup...
r33
constructor(container: Container, services: ServiceMap, cache?: object, visited?) {
argumentNotNull(container, "container");
argumentNotNull(services, "services");
this._visited = visited || {};
this._stack = [];
this._cache = cache || {};
this._services = services;
this.container = container;
}
getService(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, refactoring, fixes
r40 return isDescriptor(d) ? d.activate(this, name) : d;
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,
Object.create(this._services),
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, refactoring, fixes
r40 parse(data, name: string) {
cin
minor fixes, code cleanup...
r33 if (isPrimitive(data))
return data;
if (isDescriptor(data)) {
return data.activate(this, name);
} else if (data instanceof Array) {
cin
ts code cleanup, linting
r39 this.enter(name);
const v = data.map( (x, i) => this.parse(x, `[${i}]`));
this.leave();
cin
minor fixes, code cleanup...
r33 return v;
} else {
cin
ts code cleanup, linting
r39 this.enter(name);
const result = {};
for (const p in data)
result[p] = this.parse(data[p], "." + p);
this.leave();
cin
minor fixes, code cleanup...
r33 return result;
}
}
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
ts code cleanup, linting
r39 enter(name: string, d?: Descriptor, localize?: boolean) {
cin
minor fixes, code cleanup...
r33 if (trace.isLogEnabled())
trace.log("enter " + name + " " + (d || "") +
(localize ? " localize" : ""));
this._stack.push({
cin
ts code cleanup, linting
r39 name,
cin
minor fixes, code cleanup...
r33 service: d,
scope: this._services
});
if (localize)
this._services = Object.create(this._services);
}
leave() {
cin
ts code cleanup, linting
r39 const ctx = this._stack.pop();
cin
minor fixes, code cleanup...
r33 this._services = ctx.scope;
if (trace.isLogEnabled())
trace.log("leave " + ctx.name + " " + (ctx.service || ""));
}
cin
ts code cleanup, linting
r39 }