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