##// END OF EJS Templates
sync
sync

File last commit:

r15:3985e8405319 tip default
r15:3985e8405319 tip default
Show More
ActivationContext.ts
111 lines | 4.1 KiB | video/mp2t | TypeScriptLexer
/ src / main / ts / ActivationContext.ts
cin
sync
r15 import { ActivationError, ActivationItem } from "./ActivationError";
import { IActivationContext, DescriptorMap, ILifetimeManager, ILifetimeSlot, ServiceLocator, IContainerBuilder } from "../typings/interfaces";
import { argumentNotNull, each, prototype } from "./traits";
import { LifetimeManager } from "./LifetimeManager";
import { ContainerBuilder } from "./ContainerBuilder";
cin
initial commit
r0
cin
almost woking typings
r9 /** This object is created once per `Container.resolve` method call and used to
cin
initial commit
r0 * cache dependencies and to track created instances. The activation context
* tracks services with `context` activation type.
cin
almost woking typings
r9 *
* @template S The service map used in the activation context, services from
* this map are available to resolution.
* @template U A set of keys from the service map which can be overridden in
* this activation context.
cin
initial commit
r0 */
cin
almost woking typings
r9 export class ActivationContext<S> implements IActivationContext<S> {
cin
initial commit
r0
cin
sync
r15 private readonly _container: ServiceLocator<S>;
cin
initial commit
r0
cin
sync
r15 private readonly _contextScope: ILifetimeManager;
cin
initial commit
r0
cin
sync
r15 private _services: DescriptorMap<S>;
cin
initial commit
r0
cin
sync
r15 private readonly _scope: ILifetimeManager[];
cin
initial commit
r0
/** Creates a new activation context with the specified parameters.
cin
working on fluent configuration
r1 * @param containerLifetimeManager the container which starts the activation process
cin
initial commit
r0 * @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
sync
r15 constructor(container: ServiceLocator<S>, scope: ILifetimeManager[], services: DescriptorMap<S>, contextScope: ILifetimeManager = new LifetimeManager()) {
this._container = container;
this._contextScope = contextScope;
cin
initial commit
r0 this._services = services;
cin
sync
r15 this._scope = scope;
cin
initial commit
r0 }
cin
sync
r15 resolve<K extends keyof S>(name: K, stack: ActivationItem[]): NonNullable<S[K]>;
resolve<K extends keyof S, T>(name: K, stack: ActivationItem[], def: T): NonNullable<S[K]> | T;
resolve<K extends keyof S, T>(name: K, stack: ActivationItem[], def?: T): NonNullable<S[K]> | T | undefined {
const service = this._services[name];
cin
initial commit
r0
cin
sync
r15 if (service !== undefined) {
return service.activate(
this,
stack.concat({
name: name.toString(),
descriptor: service.toString()
})
);
cin
initial commit
r0 } else {
cin
sync
r15 if (arguments.length > 2)
cin
initial commit
r0 return def;
else
cin
sync
r15 throw new Error("Service not found");
cin
initial commit
r0 }
}
/**
* registers services local to the the activation context
*
* @name{string} the name of the service
* @service{string} the service descriptor to register
*/
cin
sync
r15 private _register<K extends keyof S>(name: K, service: DescriptorMap<S>[K]) {
cin
working on fluent configuration
r1 argumentNotNull(name, "name");
cin
initial commit
r0
cin
almost woking typings
r9 const d = this._services[name];
if (d !== undefined && !d.configurable)
throw new Error(`Service ${String(name)} can't be overridden`);
cin
working on fluent configuration
r1 this._services[name] = service;
cin
initial commit
r0 }
cin
sync
r15 scopeSlot<T>(level: number, slotId: string | number): ILifetimeSlot<T> {
if (level < 0 || level >= this._scope.length)
throw new Error("The scope level is out of range");
return this._scope[level].slot(slotId);
cin
WIP lifetime, service descriptors
r14 }
cin
sync
r15 hierarchySlot<T>(slotId: string | number): ILifetimeSlot<T> {
return this._scope[this._scope.length - 1].slot(slotId);
}
selfContainer(): ServiceLocator<S> {
return this._container;
}
createChildContainer(): IContainerBuilder<S, keyof S> {
return new ContainerBuilder(this._services, this._scope);
cin
WIP lifetime, service descriptors
r14 }
contextSlot<T>(slotId: string | number): ILifetimeSlot<T> {
cin
sync
r15 return this._contextScope.slot(slotId);
cin
initial commit
r0 }
cin
sync
r15 withOverrides<X>(overrides: DescriptorMap<S>, action: () => X) {
const services = this._services;
this._services = prototype(this._services);
try {
each(overrides, (v, k) => this._register(k, v));
return action();
} finally {
this._services = services;
}
cin
initial commit
r0 }
cin
WIP lifetime services, change target to ES2018
r11
cin
initial commit
r0 }