##// END OF EJS Templates
sync
sync

File last commit:

r120:1b124b65514a ioc ts support
r123:264497557e79 ioc ts support
Show More
ActivationContext.ts
138 lines | 3.4 KiB | video/mp2t | TypeScriptLexer
/ src / main / ts / di / ActivationContext.ts
cin
changed the project structure
r49 import { TraceSource } from "../log/TraceSource";
cin
improved interfaces and more tight type checking
r120 import { argumentNotNull, argumentNotEmptyString } from "../safe";
import { Descriptor, ContainerProvided, ContainerServiceMap, ContainerKeys, ContainerResolve } from "./interfaces";
cin
changed the project structure
r49 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
improved interfaces and more tight type checking
r120 export interface ActivationContextInfo<S extends object> {
cin
changed the project structure
r49 name: string;
service: string;
cin
improved interfaces and more tight type checking
r120 scope: ContainerServiceMap<S>;
cin
changed the project structure
r49 }
cin
improved interfaces and more tight type checking
r120 export class ActivationContext<S extends object> {
cin
working on IoC configuration
r114 _cache: MapOf<any>;
cin
changed the project structure
r49
cin
improved interfaces and more tight type checking
r120 _services: ContainerServiceMap<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
improved interfaces and more tight type checking
r120 constructor(container: Container<S>, services: ContainerServiceMap<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
improved interfaces and more tight type checking
r120 resolve<K extends ContainerKeys<S>>(name: K, def?: ContainerResolve<S, K>) {
cin
changed the project structure
r49 const d = this._services[name];
cin
working on IoC configuration
r114 if (d !== undefined) {
cin
improved interfaces and more tight type checking
r120 return this.activate(d, name.toString());
cin
working on IoC configuration
r114 } 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
corrected code to support ts strict mode...
r115 register<K extends keyof S>(name: K, service: Descriptor<S, S[K]>) {
cin
changed the project structure
r49 argumentNotEmptyString(name, "name");
cin
improved interfaces and more tight type checking
r120 this._services[name] = service as any;
cin
changed the project structure
r49 }
clone() {
cin
corrected code to support ts strict mode...
r115 return new ActivationContext<S>(
cin
changed the project structure
r49 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
corrected code to support ts strict mode...
r115 activate<T>(d: Descriptor<S, T>, name: string) {
cin
changed the project structure
r49 if (trace.isLogEnabled())
trace.log(`enter ${name} ${d}`);
this.enter(name, d.toString());
cin
corrected code to support ts strict mode...
r115 const v = d.activate(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 }
}