##// END OF EJS Templates
working on IoC configuration
working on IoC configuration

File last commit:

r114:475b8ce3e850 ioc ts support
r114:475b8ce3e850 ioc ts support
Show More
ActivationContext.ts
138 lines | 3.3 KiB | video/mp2t | TypeScriptLexer
/ src / main / ts / di / ActivationContext.ts
cin
changed the project structure
r49 import { TraceSource } from "../log/TraceSource";
import { argumentNotNull, argumentNotEmptyString, isPrimitive, each, isNull } from "../safe";
import { Descriptor, ServiceMap } from "./interfaces";
import { Container } from "./Container";
cin
working on IoC configuration
r114 import { MapOf } from "../interfaces";
cin
changed the project structure
r49
const trace = TraceSource.get("@implab/core/di/ActivationContext");
cin
working on IoC configuration
r114 export interface ActivationContextInfo<S> {
cin
changed the project structure
r49 name: string;
service: string;
cin
working on IoC configuration
r114 scope: ServiceMap<S>;
cin
changed the project structure
r49 }
cin
working on IoC configuration
r114 export class ActivationContext<S> {
_cache: MapOf<any>;
cin
changed the project structure
r49
cin
working on IoC configuration
r114 _services: ServiceMap<S>;
cin
changed the project structure
r49
cin
working on IoC configuration
r114 _stack: ActivationContextInfo<S>[];
cin
changed the project structure
r49
cin
working on IoC configuration
r114 _visited: MapOf<any>;
cin
changed the project structure
r49
_name: string;
cin
working on IoC configuration
r114 _localized: boolean = false;
cin
changed the project structure
r49
cin
working on IoC configuration
r114 container: Container<S>;
cin
changed the project structure
r49
cin
working on IoC configuration
r114 constructor(container: Container<S>, services: ServiceMap<S>, name?: string, cache?: object, visited?: MapOf<any>) {
cin
changed the project structure
r49 argumentNotNull(container, "container");
argumentNotNull(services, "services");
cin
working on IoC configuration
r114 this._name = name || "<unnamed>";
cin
changed the project structure
r49 this._visited = visited || {};
this._stack = [];
this._cache = cache || {};
this._services = services;
this.container = container;
}
getName() {
return this._name;
}
cin
working on IoC configuration
r114 resolve<K extends keyof S, T extends S[K]>(name: K, def?: T) {
cin
changed the project structure
r49 const d = this._services[name];
cin
working on IoC configuration
r114 if (d !== undefined) {
return this.activate(d as Descriptor<T>, name.toString());
} else {
if (def !== undefined && def !== null)
cin
changed the project structure
r49 return def;
else
throw new Error(`Service ${name} not found`);
cin
working on IoC configuration
r114 }
cin
changed the project structure
r49 }
/**
* registers services local to the the activation context
*
* @name{string} the name of the service
* @service{string} the service descriptor to register
*/
cin
working on IoC configuration
r114 register<K extends keyof S>(name: K, service: Descriptor<S[K]>) {
cin
changed the project structure
r49 argumentNotEmptyString(name, "name");
this._services[name] = service;
}
clone() {
return new ActivationContext(
this.container,
this._services,
this._name,
this._cache,
this._visited
);
}
has(id: string) {
return id in this._cache;
}
cin
working on IoC configuration
r114 get<T>(id: string) {
cin
changed the project structure
r49 return this._cache[id];
}
cin
working on IoC configuration
r114 store(id: string, value: any) {
cin
changed the project structure
r49 return (this._cache[id] = value);
}
cin
working on IoC configuration
r114 activate<T>(d: Descriptor<T>, name: string) {
cin
changed the project structure
r49 if (trace.isLogEnabled())
trace.log(`enter ${name} ${d}`);
this.enter(name, d.toString());
cin
working on IoC configuration
r114 const v = d.activate<S>(this);
cin
changed the project structure
r49 this.leave();
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;
}
getStack() {
return this._stack.slice().reverse();
}
private enter(name: string, service: string) {
this._stack.push({
name,
service,
scope: this._services
});
this._name = name;
this._services = Object.create(this._services);
}
private leave() {
const ctx = this._stack.pop();
cin
working on IoC configuration
r114 if (ctx) {
this._services = ctx.scope;
this._name = ctx.name;
} else {
trace.error("Trying to leave the last activation scope");
}
cin
changed the project structure
r49 }
}