| @@ -1,35 +1,35 | |||||
| 1 |
|
|
1 | import { services } from "../di/Annotations"; | |
| 2 |
|
|
2 | import { Bar } from "./Bar"; | |
| 3 |
|
|
3 | ||
| 4 | // declare required dependencies |
|
4 | // declare required dependencies | |
| 5 |
|
|
5 | const config = services<{ | |
| 6 |
|
|
6 | bar: Bar; | |
| 7 |
|
|
7 | }>(); | |
| 8 |
|
|
8 | ||
| 9 | // export service descriptor |
|
9 | // export service descriptor | |
| 10 |
|
|
10 | export const service = config.build<Box<Bar>>(); | |
| 11 |
|
|
11 | ||
| 12 |
|
|
12 | @service.consume(config.dependency("bar")) | |
| 13 | export class Box<T> { |
|
13 | export class Box<T> { | |
| 14 | private _value: T | undefined; |
|
14 | private _value: T | undefined; | |
| 15 |
|
15 | |||
| 16 | constructor(value: T) { |
|
16 | constructor(value: T) { | |
| 17 | this._value = value; |
|
17 | this._value = value; | |
| 18 | } |
|
18 | } | |
| 19 |
|
19 | |||
| 20 | // @service.inject("bar") |
|
20 | // @service.inject("bar") | |
| 21 | setValue(value: T) { |
|
21 | setValue(value: T) { | |
| 22 | this._value = value; |
|
22 | this._value = value; | |
| 23 | } |
|
23 | } | |
| 24 |
|
24 | |||
| 25 | setObj(value: object) { |
|
25 | setObj(value: object) { | |
| 26 |
|
26 | |||
| 27 | } |
|
27 | } | |
| 28 |
|
28 | |||
| 29 | getValue() { |
|
29 | getValue() { | |
| 30 | if (this._value === undefined) |
|
30 | if (this._value === undefined) | |
| 31 | throw new Error("Trying to get a value from the empty box"); |
|
31 | throw new Error("Trying to get a value from the empty box"); | |
| 32 |
|
32 | |||
| 33 | return this._value; |
|
33 | return this._value; | |
| 34 | } |
|
34 | } | |
| 35 | } |
|
35 | } | |
| @@ -1,36 +1,38 | |||||
| 1 | import { Foo } from "./Foo"; |
|
1 | import { Foo } from "./Foo"; | |
| 2 | import { Bar } from "./Bar"; |
|
2 | import { Bar } from "./Bar"; | |
| 3 | import { Box } from "./Box"; |
|
3 | import { Box } from "./Box"; | |
| 4 | import { primitive } from "../safe"; |
|
4 | import { primitive } from "../safe"; | |
| 5 | import { Constructor } from "../interfaces"; |
|
5 | import { Constructor } from "../interfaces"; | |
| 6 |
|
6 | |||
| 7 | interface Services { |
|
7 | interface Services { | |
| 8 | foo: Foo; |
|
8 | foo: Foo; | |
| 9 |
|
9 | |||
| 10 | bar: Bar; |
|
10 | bar: Bar; | |
| 11 |
|
11 | |||
| 12 | box: Box<Foo>; |
|
12 | box: Box<Foo>; | |
| 13 |
|
13 | |||
| 14 | host: string; |
|
14 | host: string; | |
| 15 |
|
15 | |||
| 16 | } |
|
16 | } | |
| 17 |
|
17 | |||
| 18 | interface TypeDescriptor<T, C extends Constructor<T>> { |
|
18 | interface TypeDescriptor<T, C extends Constructor<T>> { | |
| 19 | $type: C; |
|
19 | $type: C; | |
| 20 |
|
20 | |||
| 21 | params: Wrap<ConstructorParameters<C>>; |
|
21 | params: Wrap<ConstructorParameters<C>>; | |
| 22 | } |
|
22 | } | |
| 23 |
|
23 | |||
| 24 | function typeRegistration<T, C extends Constructor<T>>(target: C, params: Wrap<ConstructorParameters<C>>): TypeDescriptor<T, C> { |
|
24 | function typeRegistration<T, C extends Constructor<T>>(target: C, params: Wrap<ConstructorParameters<C>>): TypeDescriptor<T, C> { | |
| 25 | throw new Error(); |
|
25 | throw new Error(); | |
| 26 | } |
|
26 | } | |
| 27 |
|
27 | |||
|
|
28 | declare function register<T>(): { type<C extends Constructor<T>>(target: C, params: Wrap<ConstructorParameters<C>>): TypeDescriptor<T, C>}; | |||
|
|
29 | ||||
| 28 | type Wrap<T> = T extends primitive ? T : |
|
30 | type Wrap<T> = T extends primitive ? T : | |
| 29 | { [k in keyof T]: Wrap<T[k]> } | TypeDescriptor<T, Constructor<T>>; |
|
31 | { [k in keyof T]: Wrap<T[k]> } | TypeDescriptor<T, Constructor<T>>; | |
| 30 |
|
32 | |||
| 31 | const config: Wrap<Services> = { |
|
33 | const config: Wrap<Services> = { | |
| 32 | foo: typeRegistration(Foo, []), |
|
34 | foo: typeRegistration(Foo, []), | |
| 33 | bar: typeRegistration(Bar, [{ foo: null as any, nested: null as any }]), |
|
35 | bar: typeRegistration(Bar, [{ foo: null as any, nested: null as any }]), | |
| 34 |
box: |
|
36 | box: register<Box<Foo>>().type(Box, [{ $type: Bar, params: [] }]), | |
| 35 | host: "" |
|
37 | host: "" | |
| 36 | }; |
|
38 | }; | |
General Comments 0
You need to be logged in to leave comments.
Login now
