##// END OF EJS Templates
WIP service descriptors
WIP service descriptors

File last commit:

r13:dc3d64c43573 default
r13:dc3d64c43573 default
Show More
Container.ts
64 lines | 2.2 KiB | video/mp2t | TypeScriptLexer
cin
initial commit
r0 import { ActivationContext } from "./ActivationContext";
import { ActivationError } from "./ActivationError";
cin
Working on container builder
r5 import { ContainerBuilder } from "./ContainerBuilder";
cin
WIP service descriptors
r13 import { LifetimeManager } from "./LifetimeManager";
cin
almost woking typings
r9 import { DescriptorMap, IContainerBuilder, IDestroyable, ILifetimeManager, ServiceLocator } from "./interfaces";
cin
initial commit
r0
cin
almost woking typings
r9 export class Container<S> implements ServiceLocator<S>, IDestroyable {
private readonly _services: DescriptorMap<S>;
cin
initial commit
r0
cin
WIP service descriptors
r13 private readonly _lifetimeManagers: ILifetimeManager[];
cin
initial commit
r0
cin
working on fluent configuration
r1 private _disposed: boolean;
cin
initial commit
r0
cin
almost woking typings
r9 private readonly _onDestroyed: () => void;
cin
WIP service descriptors
r13 constructor(services: DescriptorMap<S>, lifetimeManagers: ILifetimeManager[], destroyed: () => void) {
this._services = services;
cin
initial commit
r0 this._disposed = false;
cin
WIP service descriptors
r13 this._lifetimeManagers = lifetimeManagers.concat(new LifetimeManager());
cin
almost woking typings
r9 this._onDestroyed = destroyed;
cin
initial commit
r0 }
cin
almost woking typings
r9 private _assertNotDestroyed() {
if (this._disposed)
throw new Error("The container is destroyed");
cin
initial commit
r0 }
cin
almost woking typings
r9 createChildBuilder(): IContainerBuilder<S, keyof S> {
this._assertNotDestroyed();
const lifetime = this._lifetimeManager.create<IDestroyable>();
return new ContainerBuilder(this._services, lifetime);
}
resolve<K extends keyof S>(name: K): NonNullable<S[K]>;
resolve<K extends keyof S, T>(name: K, def: T): NonNullable<S[K]> | T;
resolve<K extends keyof S, T>(name: K, def?: T) {
this._assertNotDestroyed();
cin
initial commit
r0 const d = this._services[name];
if (d === undefined) {
if (arguments.length > 1)
return def;
else
cin
working on fluent configuration
r1 throw new Error(`Service '${String(name)}' isn't found`);
cin
initial commit
r0 } else {
cin
WIP service descriptors
r13 const context = new ActivationContext(this._lifetimeManagers, this._services, String(name), d);
cin
initial commit
r0 try {
return d.activate(context);
} catch (error) {
throw new ActivationError(name.toString(), context.getStack(), error);
}
}
}
destroy() {
if (this._disposed)
return;
this._disposed = true;
this._lifetimeManager.destroy();
cin
almost woking typings
r9 (0,this._onDestroyed)();
cin
initial commit
r0 }
}