##// END OF EJS Templates
almost woking typings
almost woking typings

File last commit:

r9:988f0f6aab67 default
r9:988f0f6aab67 default
Show More
Container.ts
74 lines | 2.4 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
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
almost woking typings
r9 private readonly _lifetimeManager: 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;
constructor(services: DescriptorMap<S>, lifetimeManager: ILifetimeManager, destroyed: () => void) {
this._services = {
...services,
container: {
configurable: false,
activate: () => this
},
childContainer: {
configurable: false,
activate: () => this.createChildBuilder(),
}
};
cin
initial commit
r0 this._disposed = false;
cin
Working on container builder
r5 this._lifetimeManager = 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
Working on container builder
r5 const context = new ActivationContext(this._lifetimeManager, 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 }
}