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

File last commit:

r14:3f8a82c8ce73 default
r14:3f8a82c8ce73 default
Show More
ContainerBuilder.ts
65 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
WIP service descriptors
r13 import { containerSelfDescriptor } from "./DescriptorImpl";
import { Descriptor, IContainerBuilder, IDescriptorBuilder, DescriptorMap, ServiceLocator, ILifetime, IDestroyable, ContainerServices, ContainerServicesConstraint } from "./interfaces";
cin
almost woking typings
r9 import { emptyLifetime, LifetimeManager } from "./LifetimeManager";
cin
WIP lifetime services, change target to ES2018
r11 import { isDestroyable, prototype } 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
*/
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
WIP service descriptors
r13 private readonly _services: DescriptorMap<ContainerServices<S>>;
cin
Working on container builder
r5
private readonly _lifetimeManager = new LifetimeManager();
cin
almost woking typings
r9 private readonly _lifetime: ILifetime<IDestroyable>;
cin
WIP lifetime services, change target to ES2018
r11 constructor(parentServices: DescriptorMap<S> | null = null, lifetime?: ILifetime<IDestroyable>) {
cin
WIP lifetime, service descriptors
r14 this._services = { ...parentServices }; // create a copy
cin
almost woking typings
r9 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
cin
WIP lifetime services
r12 const {remove, store} = this._lifetime(null);
cin
almost woking typings
r9
cin
WIP lifetime services
r12 const container = new Container(this._services, this._lifetimeManager, remove);
cin
almost woking typings
r9
cin
WIP lifetime services
r12 store(container);
cin
almost woking typings
r9
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);
}
}