##// END OF EJS Templates
code comments
cin -
r2:154d88dba49c default
parent child
Show More
@@ -1,21 +1,41
1 import { DescriptorBuilder } from "./DescriptorBuilder";
1 import { DescriptorBuilder } from "./DescriptorBuilder";
2 import { ConfigurableKeys, ContainerServices, ConfigurableServices, RegistrationBuildersMap, RequiredKeys } from "./interfaces";
2 import { ConfigurableKeys, ContainerServices, ConfigurableServices, RegistrationBuildersMap, ExtractRequired } from "./interfaces";
3 import { ServiceContainer } from "./interfaces";
3 import { ServiceContainer } from "./interfaces";
4 import { argumentNotNull, each, isKey } from "./traits";
4 import { argumentNotNull, each, isKey } from "./traits";
5
5
6 export class FluentConfiguration<S extends object, Y extends ConfigurableKeys<S> = ConfigurableKeys<S>> {
6 export class FluentConfiguration<S extends object, Y extends ConfigurableKeys<S> = ConfigurableKeys<S>> {
7
7
8 _builders: Partial<RegistrationBuildersMap<S>> = {};
8 private _builders: Partial<RegistrationBuildersMap<S>> = {};
9
9
10 /** Adds a declaration of the services to the current config.
11 *
12 * @template D The map of the services
13 * @returns self
14 */
10 declare<D extends Partial<Pick<S, keyof D & keyof S>>>(): FluentConfiguration<S & D, Y | ConfigurableKeys<D>> {
15 declare<D extends Partial<Pick<S, keyof D & keyof S>>>(): FluentConfiguration<S & D, Y | ConfigurableKeys<D>> {
11 return this as FluentConfiguration<S & D, Y | ConfigurableKeys<D>>;
16 return this as FluentConfiguration<S & D, Y | ConfigurableKeys<D>>;
12 }
17 }
13
18
19 /** Adds compile-time information about the already provided services
20 *
21 * @template P The map of the provided services
22 * @returns self
23 */
14 provided<P extends Pick<S, keyof P & keyof S>>(): FluentConfiguration<S & P, Exclude<Y, keyof P>> {
24 provided<P extends Pick<S, keyof P & keyof S>>(): FluentConfiguration<S & P, Exclude<Y, keyof P>> {
15 return this as FluentConfiguration<S & P, Exclude<Y, keyof P>>;
25 return this as FluentConfiguration<S & P, Exclude<Y, keyof P>>;
16 }
26 }
17
27
28 /** Register the service.
29 *
30 * @param name The name of the service
31 * @param builder The service builder
32 * @returns self
33 */
18 register<K extends Y>(name: K, builder: RegistrationBuildersMap<S>[K]): FluentConfiguration<S, Exclude<Y, K>>;
34 register<K extends Y>(name: K, builder: RegistrationBuildersMap<S>[K]): FluentConfiguration<S, Exclude<Y, K>>;
35 /** Registers the collection of services
36 * @param config The collection of services to register.
37 * @returns self
38 */
19 register<K extends Y>(config: RegistrationBuildersMap<S, K>): FluentConfiguration<S, Exclude<Y, K>>;
39 register<K extends Y>(config: RegistrationBuildersMap<S, K>): FluentConfiguration<S, Exclude<Y, K>>;
20 register<K extends Y>(nameOrConfig: K | RegistrationBuildersMap<S, K>, builder?: RegistrationBuildersMap<S>[K]) {
40 register<K extends Y>(nameOrConfig: K | RegistrationBuildersMap<S, K>, builder?: RegistrationBuildersMap<S>[K]) {
21 if (isKey(nameOrConfig)) {
41 if (isKey(nameOrConfig)) {
@@ -28,9 +48,16 export class FluentConfiguration<S exten
28 return this as FluentConfiguration<S, Exclude<Y, K>>;
48 return this as FluentConfiguration<S, Exclude<Y, K>>;
29 }
49 }
30
50
31 done(missing: RequiredKeys<S, Y> extends never ? void : RequiredKeys<S, Y>) {
51 /**
32 if (missing !== undefined)
52 * This method is used to enable a compile time check of the configuration.
33 throw new Error("The configuration isn't complete");
53 * If there are not configured services in the configuration the compiler
54 * will trigger the error.
55 *
56 * @param missing Empty object literal should always be specified.
57 * @returns self
58 */
59 // eslint-disable-next-line @typescript-eslint/no-unused-vars
60 done<M extends ExtractRequired<S,Y>>(missing: M) {
34 return this;
61 return this;
35 }
62 }
36
63
@@ -117,5 +117,5 export interface ILifetime<T> {
117 store(item: T, cleanup?: (item: T) => void): void;
117 store(item: T, cleanup?: (item: T) => void): void;
118 }
118 }
119
119
120 export type RequiredKeys<T, K extends keyof T = keyof T> = keyof { [p in K as (T[p] extends undefined ? never : p)]-?: T[p] };
120 export type ExtractRequired<T, K extends keyof T = keyof T> = { [p in K as (undefined extends T[p] ? never : p)]-?: T[p] };
121
121
@@ -49,7 +49,7 const config = fluent()
49 foo: it => it.factory($ => new Foo()),
49 foo: it => it.factory($ => new Foo()),
50 baz: it => it.value(new Foo())
50 baz: it => it.value(new Foo())
51 })
51 })
52 .done();
52 .done({});
53
53
54 declare const container: Container<SharedServices>;
54 declare const container: Container<SharedServices>;
55
55
General Comments 0
You need to be logged in to leave comments. Login now