##// END OF EJS Templates
added provided and configure methods to the fluent container configuration, added applyConfig method to the container
added provided and configure methods to the fluent container configuration, added applyConfig method to the container

File last commit:

r135:03e32ec7c20b ioc ts support
r142:be7edf08a115 v1.4.0-rc3 default
Show More
ActivationContext.ts
170 lines | 5.1 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
Completely removed IoC annotations...
r135 /** This class is created once per `Container.resolve` method call and used to
* cache dependencies and to track created instances. The activation context
* tracks services with `context` activation type.
*/
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
Completely removed IoC annotations...
r135 /** Creates a new activation context with the specified parameters.
* @param container the container which starts the activation process
* @param services the initial service registrations
* @param name the name of the service being activated, this parameter is
* used for the debug purpose.
* @param service the service to activate, this parameter is used for the
* debug purpose.
*/
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 }
cin
Completely removed IoC annotations...
r135 /** the name of the current resolving dependency */
cin
changed the project structure
r49 getName() {
return this._name;
}
cin
Completely removed IoC annotations...
r135 /** Returns the container for which 'resolve' method was called */
cin
Refactoring, working on services lifetime
r131 getContainer() {
return this._container;
}
cin
Completely removed IoC annotations...
r135 /** Resolves the specified dependency in the current context
* @param name The name of the dependency being resolved
*/
cin
working on fluent configuration
r133 resolve<K extends ContainerKeys<S>>(name: K): TypeOfService<S, K>;
cin
Completely removed IoC annotations...
r135 /** Resolves the specified dependency with the specified default value if
* the dependency is missing.
*
* @param name The name of the dependency being resolved
* @param def A default value to return in case of the specified dependency
* is missing.
*/
cin
working on fluent configuration
r133 resolve<K extends ContainerKeys<S>, T>(name: K, def: T): TypeOfService<S, K> | T;
cin
Completely removed IoC annotations...
r135 /** Resolves the specified dependency and returns undefined in case if the
* dependency is missing.
*
* @param name The name of the dependency being resolved
*/
cin
working on fluent configuration
r133 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
Completely removed IoC annotations...
r135
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 }