##// END OF EJS Templates
working on fluent configuration, di annotations removed
working on fluent configuration, di annotations removed

File last commit:

r134:511bcc634d65 ioc ts support
r134:511bcc634d65 ioc ts support
Show More
ActivationContext.ts
140 lines | 3.7 KiB | video/mp2t | TypeScriptLexer
/ src / main / ts / di / ActivationContext.ts
cin
changed the project structure
r49 import { TraceSource } from "../log/TraceSource";
cin
working on fluent configuration
r133 import { argumentNotEmptyString } from "../safe";
cin
working on fluent configuration, di annotations removed
r134 import { Descriptor, ContainerServiceMap, ContainerKeys, TypeOfService, ILifetime } 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
Refactoring, working on services lifetime
r131 export interface ActivationContextInfo {
cin
changed the project structure
r49 name: string;
service: string;
}
cin
working on fluent configuration, di annotations removed
r134 let nextId = 1;
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 _visited: MapOf<any>;
cin
changed the project structure
r49
_name: string;
cin
Refactoring, working on services lifetime
r131 _service: Descriptor<S, any>;
cin
changed the project structure
r49
cin
Refactoring, working on services lifetime
r131 _container: Container<S>;
_parent: ActivationContext<S> | undefined;
cin
changed the project structure
r49
cin
Refactoring, working on services lifetime
r131 constructor(container: Container<S>, services: ContainerServiceMap<S>, name: string, service: Descriptor<S, any>) {
this._name = name;
this._service = service;
this._visited = {};
this._cache = {};
cin
changed the project structure
r49 this._services = services;
cin
Refactoring, working on services lifetime
r131 this._container = container;
cin
changed the project structure
r49 }
getName() {
return this._name;
}
cin
Refactoring, working on services lifetime
r131 getContainer() {
return this._container;
}
cin
working on fluent configuration
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 {
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 {
cin
working on fluent configuration
r133 if (arguments.length > 1)
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 }
cin
working on fluent configuration, di annotations removed
r134 createLifetime(): ILifetime {
const id = nextId++;
const me = this;
return {
initialize() {
},
has() {
return id in me._cache;
},
get() {
return me._cache[id];
},
store(item: any) {
me._cache[id] = item;
}
};
cin
changed the project structure
r49 }
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}`);
cin
Refactoring, working on services lifetime
r131 const ctx = this.enter(d, name);
const v = d.activate(ctx);
cin
changed the project structure
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;
}
cin
Refactoring, working on services lifetime
r131 getStack(): ActivationContextInfo[] {
const stack = [{
name: this._name,
service: this._service.toString()
}];
return this._parent ?
stack.concat(this._parent.getStack()) :
stack;
cin
changed the project structure
r49 }
cin
Refactoring, working on services lifetime
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;
cin
changed the project structure
r49 }
cin
working on fluent configuration
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;
}
cin
changed the project structure
r49 }