| @@ -1,27 +1,17 | |||||
| 1 | import { primitive } from "../safe"; |
|
1 | import { primitive } from "../safe"; | |
| 2 | import { TypeRegistration } from "./Configuration"; |
|
2 | import { TypeRegistration, DependencyRegistration, LazyDependencyRegistration, Registration, StrictTypeRegistration } from "./Configuration"; | |
| 3 |
|
3 | |||
| 4 | export interface InjectOptions { |
|
4 | export interface InjectOptions { | |
| 5 | lazy?: boolean; |
|
5 | lazy?: boolean; | |
| 6 | } |
|
6 | } | |
| 7 |
|
7 | |||
| 8 | export interface Dependency<K extends keyof any> { |
|
|||
| 9 | $dependency: K; |
|
|||
| 10 |
|
||||
| 11 | lazy?: boolean; |
|
|||
| 12 |
|
||||
| 13 | } |
|
|||
| 14 |
|
||||
| 15 | export interface Lazy<K extends keyof any> extends Dependency<K> { |
|
|||
| 16 | lazy: true; |
|
|||
| 17 | } |
|
|||
| 18 |
|
||||
| 19 | type Compatible<T1, T2> = T2 extends T1 ? any : never; |
|
8 | type Compatible<T1, T2> = T2 extends T1 ? any : never; | |
| 20 |
|
9 | |||
| 21 | type ExtractService<K, S> = K extends keyof S ? S[K] : K; |
|
10 | type ExtractService<K, S> = K extends keyof S ? S[K] : K; | |
| 22 |
|
11 | |||
| 23 | type ExtractDependency<D, S> = D extends { $dependency: infer K } ? |
|
12 | type ExtractDependency<D, S> = D extends { $dependency: infer K } ? | |
| 24 | D extends { lazy: true } ? () => ExtractService<K, S> : ExtractService<K, S> : |
|
13 | D extends { lazy: true } ? () => ExtractService<K, S> : ExtractService<K, S> : | |
|
|
14 | D extends { $type: new (...args: any[]) => infer I } ? I : | |||
| 25 | WalkDependencies<D, S>; |
|
15 | WalkDependencies<D, S>; | |
| 26 |
|
16 | |||
| 27 | type WalkDependencies<D, S> = D extends primitive ? D : |
|
17 | type WalkDependencies<D, S> = D extends primitive ? D : | |
| @@ -62,8 +52,10 export interface LazyDependencyOptions<T | |||||
| 62 | interface Declaration<S extends object> { |
|
52 | interface Declaration<S extends object> { | |
| 63 | define<T>(): Builder<T, S>; |
|
53 | define<T>(): Builder<T, S>; | |
| 64 |
|
54 | |||
| 65 | dependency<K extends keyof S>(name: K, opts: LazyDependencyOptions<S[K]>): Lazy<K>; |
|
55 | dependency<K extends keyof S>(name: K, opts: LazyDependencyOptions<S[K]>): LazyDependencyRegistration<S, K>; | |
| 66 | dependency<K extends keyof S>(name: K, opts?: DependencyOptions<S[K]>): Dependency<K>; |
|
56 | dependency<K extends keyof S>(name: K, opts?: DependencyOptions<S[K]>): DependencyRegistration<S, K>; | |
|
|
57 | ||||
|
|
58 | $type<P extends any[], C extends new (...args: ExtractDependency<P, S>) => any>(target: C, ...params: P): StrictTypeRegistration<C, S>; | |||
| 67 |
|
59 | |||
| 68 | configure(): Config<S>; |
|
60 | configure(): Config<S>; | |
| 69 | } |
|
61 | } | |
| @@ -76,9 +68,12 type PromiseOrValue<T> = PromiseLike<T> | |||||
| 76 |
|
68 | |||
| 77 |
|
69 | |||
| 78 | export interface Config<S extends object, Y extends keyof S = keyof S> { |
|
70 | export interface Config<S extends object, Y extends keyof S = keyof S> { | |
| 79 |
register<K extends Y>(name: K, m: { $from: Promise<ServiceModule<S[K], S>> } |
|
71 | register<K extends Y>(name: K, m: { $from: Promise<ServiceModule<S[K], S>> }): Config<S, Exclude<Y, K>>; | |
| 80 | register<K extends Y, M extends string>(name: K, m: { $from: Promise<ServiceModule<S[K], S, M>>, service: M }): Config<S, Exclude<Y, K>>; |
|
72 | register<K extends Y, M extends string>(name: K, m: { $from: Promise<ServiceModule<S[K], S, M>>, service: M }): Config<S, Exclude<Y, K>>; | |
| 81 | register<K extends Y, C extends new (...args: any) => S[K]>(name: K, d: TypeRegistration<C, S>): Config<S, Exclude<Y, K>>; |
|
73 | ||
|
|
74 | register<K extends Y>(name: K, m: Registration<S[K], S>): Config<S, Exclude<Y, K>>; | |||
|
|
75 | registerType<K extends Y, P extends any[]>( | |||
|
|
76 | name: K, $type: new (...args: ExtractDependency<P, S>) => S[K], ...params: P): Config<S, Exclude<Y, K>>; | |||
| 82 | } |
|
77 | } | |
| 83 |
|
78 | |||
| 84 | export declare function declare<S extends object>(): Declaration<S>; |
|
79 | export declare function declare<S extends object>(): Declaration<S>; | |
| @@ -44,12 +44,17 export interface ServiceRegistration<T, | |||||
| 44 | cleanup?: ((instance: T) => void) | string; |
|
44 | cleanup?: ((instance: T) => void) | string; | |
| 45 | } |
|
45 | } | |
| 46 |
|
46 | |||
| 47 | export interface TypeRegistration<C extends new () => any, S extends object> extends ServiceRegistration<InstanceType<C>, S> { |
|
47 | export interface TypeRegistration<C extends new (...args: any[]) => any, S extends object> extends ServiceRegistration<InstanceType<C>, S> { | |
| 48 | $type: string | C; |
|
48 | $type: string | C; | |
| 49 | params?: ConstructorParameters<C>; |
|
49 | params?: Registration<ConstructorParameters<C>, S>; | |
| 50 | } |
|
50 | } | |
| 51 |
|
51 | |||
| 52 |
export interface |
|
52 | export interface StrictTypeRegistration<C extends new (...args: any[]) => any, S extends object> extends ServiceRegistration<InstanceType<C>, S> { | |
|
|
53 | $type: C; | |||
|
|
54 | params?: Registration<ConstructorParameters<C>, S>; | |||
|
|
55 | } | |||
|
|
56 | ||||
|
|
57 | export interface FactoryRegistration<F extends (...args: any[]) => any, S extends object> extends ServiceRegistration<ReturnType<F>, S> { | |||
| 53 | $factory: string | F; |
|
58 | $factory: string | F; | |
| 54 | } |
|
59 | } | |
| 55 |
|
60 | |||
| @@ -69,12 +74,14 export interface LazyDependencyRegistrat | |||||
| 69 | lazy: true; |
|
74 | lazy: true; | |
| 70 | } |
|
75 | } | |
| 71 |
|
76 | |||
|
|
77 | type OfType<K extends keyof S, S, T> = Extract<{ [k in K]: T}, S>; | |||
|
|
78 | ||||
| 72 | export type Registration<T, S extends object> = T extends primitive ? T : |
|
79 | export type Registration<T, S extends object> = T extends primitive ? T : | |
| 73 | ( |
|
80 | ( | |
| 74 | T | |
|
81 | T | | |
| 75 | { [k in keyof T]: Registration<T[k], S> } | |
|
82 | { [k in keyof T]: Registration<T[k], S> } | | |
| 76 | TypeRegistration<new () => T, S> | |
|
83 | TypeRegistration<new (...args: any[]) => T, S> | | |
| 77 | FactoryRegistration<() => T, S> | |
|
84 | FactoryRegistration<(...args: any[]) => T, S> | | |
| 78 | ValueRegistration<any> | |
|
85 | ValueRegistration<any> | | |
| 79 | DependencyRegistration<S, keyof S> |
|
86 | DependencyRegistration<S, keyof S> | |
| 80 | ); |
|
87 | ); | |
| @@ -8,20 +8,20 export const service = define<Bar>(); | |||||
| 8 | nested: { |
|
8 | nested: { | |
| 9 | lazy: dependency("foo", { lazy: true }) |
|
9 | lazy: dependency("foo", { lazy: true }) | |
| 10 | }, |
|
10 | }, | |
| 11 | host: "value" |
|
11 | host: dependency("host") | |
| 12 | }) |
|
12 | }, "") | |
| 13 | export class Bar { |
|
13 | export class Bar { | |
| 14 | barName = "bar"; |
|
14 | barName = "bar"; | |
| 15 |
|
15 | |||
| 16 | _v: Foo | undefined; |
|
16 | _v: Foo | undefined; | |
| 17 |
|
17 | |||
| 18 |
constructor(_opts |
|
18 | constructor(_opts: { | |
| 19 | foo?: Foo; |
|
19 | foo?: Foo; | |
| 20 | nested?: { |
|
20 | nested?: { | |
| 21 | lazy: () => Foo |
|
21 | lazy: () => Foo | |
| 22 | }, |
|
22 | }, | |
| 23 | host: string |
|
23 | host: string | |
| 24 | }) { |
|
24 | }, s: string) { | |
| 25 |
|
25 | |||
| 26 | if (_opts && _opts.foo) |
|
26 | if (_opts && _opts.foo) | |
| 27 | this._v = _opts.foo; |
|
27 | this._v = _opts.foo; | |
| @@ -1,15 +1,23 | |||||
| 1 | import { configure } from "./services"; |
|
1 | import { configure, dependency, Services, $type } from "./services"; | |
| 2 | import { Foo } from "./Foo"; |
|
2 | import { Foo } from "./Foo"; | |
| 3 | import { Bar } from "./Bar"; |
|
3 | import { Bar } from "./Bar"; | |
|
|
4 | import { Box } from "./Box"; | |||
|
|
5 | ||||
| 4 |
|
6 | |||
| 5 | export const config = configure() |
|
7 | export const config = configure() | |
| 6 | .register("bar", { $from: import("./Bar"), service: "service" }) |
|
8 | .register("bar", { $from: import("./Bar"), service: "service" }) | |
| 7 | .register("box", { $from: import("./Box") }) |
|
9 | // .register("box", { $from: import("./Box") }) | |
| 8 | .register("host", "example.com") |
|
10 | .register("host", "example.com") | |
| 9 | .register("foo", { |
|
11 | // .registerType("bar2", Bar, [{ foo: dependency("foo"), host: "" }]); | |
| 10 | $type: Foo |
|
12 | .register("bar2", $type(Bar, | |
| 11 | }) |
|
13 | { | |
| 12 | .register("bar2", { |
|
14 | foo: $type(Foo) | |
| 13 | $type: Bar, |
|
15 | .override("host", "foo.example.com") | |
| 14 | params: [] |
|
16 | .inject("setName", dependency("host")) | |
| 15 | }); |
|
17 | .activate("context"), | |
|
|
18 | host: "" | |||
|
|
19 | }, | |||
|
|
20 | "") | |||
|
|
21 | ) | |||
|
|
22 | .registerType("box", Box, dependency("bar")); | |||
|
|
23 | ||||
| @@ -22,4 +22,4 export interface Services { | |||||
| 22 | /** |
|
22 | /** | |
| 23 | * Экспортируем вспомогательные функции для описания сервисов и кинфогурации |
|
23 | * Экспортируем вспомогательные функции для описания сервисов и кинфогурации | |
| 24 | */ |
|
24 | */ | |
| 25 | export const { define, dependency, configure } = declare<Services>(); |
|
25 | export const { define, dependency, configure, $type } = declare<Services>(); | |
General Comments 0
You need to be logged in to leave comments.
Login now
