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

File last commit:

r9:988f0f6aab67 default
r9:988f0f6aab67 default
Show More
ContainerBuilder.ts
66 lines | 2.1 KiB | video/mp2t | TypeScriptLexer
/ src / main / ts / ContainerBuilder.ts
cin
Working on container builder
r5 import { Container } from "./Container";
import { DescriptorBuilder } from "./DescriptorBuilder";
cin
almost woking typings
r9 import { Descriptor, IContainerBuilder, IDescriptorBuilder, DescriptorMap, ServiceLocator, ILifetime, IDestroyable } from "./interfaces";
import { emptyLifetime, LifetimeManager } from "./LifetimeManager";
import { isDestroyable } from "./traits";
cin
Working on container builder
r5
cin
almost woking typings
r9 /**
* Container builder used to prepare service descriptors and create a IoC container
*/
export class ContainerBuilder<S, U extends keyof S> implements
IContainerBuilder<S, U> {
cin
Working on container builder
r5
private _pending = 1;
cin
almost woking typings
r9 private readonly _services: DescriptorMap<S>;
cin
Working on container builder
r5
private readonly _lifetimeManager = new LifetimeManager();
cin
almost woking typings
r9 private readonly _lifetime: ILifetime<IDestroyable>;
constructor(parentServices?: DescriptorMap<S>, lifetime?: ILifetime<IDestroyable>) {
this._services = Object.create(parentServices ? parentServices : null) as DescriptorMap<S>;
this._lifetimeManager = new LifetimeManager();
this._lifetime = lifetime ?? emptyLifetime();
cin
Working on container builder
r5 }
cin
almost woking typings
r9 createServiceBuilder<K extends U>(name: K):
IDescriptorBuilder<S, S[K], Record<never, never>, U> {
cin
Working on container builder
r5
return new DescriptorBuilder(this._lifetimeManager, this._register(name), this._fail);
cin
almost woking typings
r9
cin
Working on container builder
r5 }
cin
almost woking typings
r9 build(): ServiceLocator<S> {
cin
Working on container builder
r5 this._assertBuilding();
cin
almost woking typings
r9 if (!this._complete())
cin
Working on container builder
r5 throw new Error("The configuration didn't complete.");
cin
almost woking typings
r9
const lifetime = this._lifetime;
const detach = isDestroyable(lifetime) ? () => lifetime.destroy() : () => void (0);
const container = new Container(this._services, this._lifetimeManager, detach);
lifetime.store(container);
return container;
cin
Working on container builder
r5 }
cin
almost woking typings
r9 private readonly _register = <K extends U>(name: K) =>
(descriptor: Descriptor<S, S[K]>) => {
this._complete();
this._services[name] = descriptor;
};
cin
Working on container builder
r5
private readonly _fail = (ex: unknown) => {
cin
almost woking typings
r9 throw ex;
cin
Working on container builder
r5 };
private _assertBuilding() {
cin
almost woking typings
r9 if (!this._pending)
throw new Error("The descriptor builder is finalized");
cin
Working on container builder
r5 }
private _complete() {
return !(--this._pending);
}
}