##// END OF EJS Templates
sync
sync

File last commit:

r15:3985e8405319 tip default
r15:3985e8405319 tip default
Show More
ContainerBuilder.ts
56 lines | 1.7 KiB | video/mp2t | TypeScriptLexer
/ src / main / ts / ContainerBuilder.ts
cin
Working on container builder
r5 import { Container } from "./Container";
import { DescriptorBuilder } from "./DescriptorBuilder";
cin
sync
r15 import { Descriptor, IContainerBuilder, IDescriptorBuilder, DescriptorMap, ServiceLocator, ILifetimeManager } from "../typings/interfaces";
cin
Working on container builder
r5
cin
almost woking typings
r9 /**
* Container builder used to prepare service descriptors and create a IoC container
*/
cin
WIP lifetime, service descriptors
r14 export class ContainerBuilder<S, U extends keyof S> implements
cin
almost woking typings
r9 IContainerBuilder<S, U> {
cin
Working on container builder
r5
private _pending = 1;
cin
sync
r15 private readonly _services: DescriptorMap<S>;
cin
Working on container builder
r5
cin
sync
r15 private readonly _scope: ILifetimeManager[];
cin
Working on container builder
r5
cin
sync
r15 private readonly _level: number;
cin
almost woking typings
r9
cin
sync
r15 constructor(parentServices: DescriptorMap<S> | null, scope: ILifetimeManager[] = []) {
cin
WIP lifetime, service descriptors
r14 this._services = { ...parentServices }; // create a copy
cin
sync
r15 this._level = scope.length;
this._scope = scope;
cin
Working on container builder
r5 }
cin
sync
r15 createServiceBuilder<K extends U>(name: K): IDescriptorBuilder<S, S[K], Record<never, never>, U> {
cin
Working on container builder
r5
cin
sync
r15 return new DescriptorBuilder(this._level, String(name), 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
cin
sync
r15 return new Container(this._services, this._scope);
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);
}
}