##// END OF EJS Templates
Corrected method signatures
Corrected method signatures

File last commit:

r6:5683fe88e2a5 default
r6:5683fe88e2a5 default
Show More
DescriptorBuilder.ts
189 lines | 6.1 KiB | video/mp2t | TypeScriptLexer
/ src / main / ts / DescriptorBuilder.ts
cin
Removed ContextResolver, added DescriptoBuilder.wants(...), dependencies are declared statically
r4 import { RegistrationBuilder, LifetimeContainer, ConfigurableKeys, IDescriptorBuilder, Ref, Resolved, DepsMap } from "./interfaces";
cin
working on fluent configuration
r1 import { Descriptor, ILifetime, ActivationType } from "./interfaces";
import { DescriptorImpl, RegistrationOverridesMap } from "./DescriptorImpl";
cin
initial commit
r0 import { LifetimeManager } from "./LifetimeManager";
cin
Removed ContextResolver, added DescriptoBuilder.wants(...), dependencies are declared statically
r4 import { each, isKey, isPromise, isString, key, oid } from "./traits";
cin
initial commit
r0
cin
working on fluent configuration
r1 /**
* @template {S} Карта доступных зависимостей, как правило `ContainerServices`
* @template {T} Тип сервиса
*/
cin
Corrected method signatures
r6 export class DescriptorBuilder<S extends object, T, R extends object, O extends keyof S> implements IDescriptorBuilder<S, T, R, O> {
cin
Working on container builder
r5 private readonly _lifetimeManager: LifetimeManager;
cin
initial commit
r0 private readonly _cb: (d: Descriptor<S, T>) => void;
cin
working on fluent configuration
r1 private readonly _eb: (err: unknown) => void;
cin
initial commit
r0
cin
Removed ContextResolver, added DescriptoBuilder.wants(...), dependencies are declared statically
r4 private readonly _refs: DepsMap<key, keyof S>;
cin
working on fluent configuration
r1 private _lifetime = LifetimeManager.empty<T>();
cin
initial commit
r0
cin
working on fluent configuration
r1 private _overrides: RegistrationOverridesMap<S>;
cin
initial commit
r0
private _cleanup?: (item: T) => void;
cin
Removed ContextResolver, added DescriptoBuilder.wants(...), dependencies are declared statically
r4 private _factory?: (refs: R) => T;
cin
initial commit
r0
private _pending = 1;
private _failed = false;
cin
working on fluent configuration
r1 private _finalized = false;
/**
* Creates new DescriptorBuilder. Accepts a lifetime container for resolving "container"
* lifetime.
*
cin
Working on container builder
r5 * @param lifetimeManager The lifetime container is the container where the service is to be registered.
cin
working on fluent configuration
r1 * @param cb The callback to receive the built service descriptor
* @param eb The callback to receive the error due
*/
cin
Working on container builder
r5 constructor(lifetimeManager: LifetimeManager, cb: (d: Descriptor<S, T>) => void, eb: (err: unknown) => void) {
this._lifetimeManager = lifetimeManager;
cin
initial commit
r0 this._cb = cb;
this._eb = eb;
cin
working on fluent configuration
r1 this._overrides = {};
cin
Removed ContextResolver, added DescriptoBuilder.wants(...), dependencies are declared statically
r4 this._refs = {};
cin
initial commit
r0 }
cin
Removed ContextResolver, added DescriptoBuilder.wants(...), dependencies are declared statically
r4 wants<X extends { [k in Exclude<key, keyof R>]: keyof S | Ref<keyof S, boolean, unknown>; }>(refs: X):
IDescriptorBuilder<S, T, R & {
[k in keyof X]:
X[k] extends keyof S ? NonNullable<S[X[k]]> :
X[k] extends Ref<infer K, infer L, infer D> ? Resolved<S, K & keyof S, L, D> :
never;
cin
Corrected method signatures
r6 }, O> {
cin
Removed ContextResolver, added DescriptoBuilder.wants(...), dependencies are declared statically
r4
each(refs, (v, k) => this._refs[k] = v);
return this as IDescriptorBuilder<S, T, R & {
[k in keyof X]:
X[k] extends keyof S ? NonNullable<S[X[k]]> :
X[k] extends Ref<infer K, infer L, infer D> ? Resolved<S, K & keyof S, L, D> :
never;
cin
Corrected method signatures
r6 }, O>;
cin
Removed ContextResolver, added DescriptoBuilder.wants(...), dependencies are declared statically
r4 }
factory(f: (refs: R) => T): void {
this._assertBuilding();
this._factory = f;
this._finalize();
this._complete();
}
cin
initial commit
r0
cin
working on fluent configuration
r1 private _assertBuilding() {
if (this._finalized)
throw new Error("The descriptor builder is finalized");
cin
initial commit
r0 }
cin
working on fluent configuration
r1 private _finalize() {
this._finalized = true;
}
cin
initial commit
r0
cin
Corrected method signatures
r6 override<K extends O>(name: K, builder: RegistrationBuilder<S, NonNullable<S[K]>>): this;
override<K extends O>(services: { [k in K]: RegistrationBuilder<S, NonNullable<S[k]>> }): this;
override<K extends O>(nameOrServices: K | { [name in K]: RegistrationBuilder<S, NonNullable<S[K]>> }, builder?: RegistrationBuilder<S, NonNullable<S[K]>>): this {
cin
working on fluent configuration
r1 this._assertBuilding();
cin
initial commit
r0 const guard = (v: void | Promise<void>) => {
if (isPromise(v))
v.catch(err => this._fail(err));
};
cin
working on fluent configuration
r1 if (isKey(nameOrServices)) {
cin
initial commit
r0 if (builder) {
this._defer();
cin
Corrected method signatures
r6 const d = new DescriptorBuilder<S, NonNullable<S[K]>, object, O>(
cin
Working on container builder
r5 this._lifetimeManager,
cin
initial commit
r0 result => {
cin
working on fluent configuration
r1 this._overrides[nameOrServices] = result;
cin
initial commit
r0 this._complete();
},
err => this._fail(err)
);
try {
guard(builder(d));
} catch (err) {
this._fail(err);
}
}
} else {
each(nameOrServices, (v, k) => this.override(k, v));
}
return this;
}
lifetime(lifetime: "singleton", typeId: string): this;
cin
working on fluent configuration
r1 lifetime(lifetime: ILifetime<T> | Exclude<ActivationType, "singleton">): this;
lifetime(lifetime: ILifetime<T> | ActivationType, typeId?: string): this {
this._assertBuilding();
cin
initial commit
r0 if (isString(lifetime)) {
this._lifetime = this._resolveLifetime(lifetime, typeId);
} else {
this._lifetime = lifetime;
}
return this;
}
cleanup(cb: (item: T) => void): this {
cin
working on fluent configuration
r1 this._assertBuilding();
cin
initial commit
r0 this._cleanup = cb;
return this;
}
value(v: T): void {
cin
working on fluent configuration
r1 this._assertBuilding();
cin
initial commit
r0 this._cb({
activate() {
return v;
}
});
cin
working on fluent configuration
r1 this._finalize();
cin
initial commit
r0 }
cin
working on fluent configuration
r1 _resolveLifetime<T>(activation: ActivationType, typeId?: string | object): ILifetime<T> {
cin
initial commit
r0 switch (activation) {
case "container":
cin
Working on container builder
r5 return this._lifetimeManager.create();
cin
initial commit
r0 case "hierarchy":
return LifetimeManager.hierarchyLifetime();
case "context":
return LifetimeManager.contextLifetime();
cin
working on fluent configuration
r1 case "singleton": {
cin
initial commit
r0 if (!typeId)
throw Error("The singleton activation requires a typeId");
const _oid = isString(typeId) ? typeId : oid(typeId);
return LifetimeManager.singletonLifetime(_oid);
cin
working on fluent configuration
r1 }
cin
initial commit
r0 default:
return LifetimeManager.empty();
}
}
_defer() {
this._pending++;
}
_complete() {
if (--this._pending === 0) {
if (!this._factory)
throw new Error("The factory must be specified");
this._cb(new DescriptorImpl<S, T>({
lifetime: this._lifetime,
factory: this._factory,
overrides: this._overrides,
cleanup: this._cleanup
}));
}
}
cin
working on fluent configuration
r1 _fail(err: unknown) {
cin
initial commit
r0 if (!this._failed) {
this._failed = true;
this._eb.call(undefined, err);
}
}
}