| @@ -1,11 +1,37 | |||||
| 1 | import { Constructor } from "../interfaces"; |
|
1 | import { Constructor } from "../interfaces"; | |
| 2 |
|
2 | |||
| 3 | export interface InjectOptions { |
|
3 | export interface InjectOptions { | |
| 4 | lazy?: boolean; |
|
4 | lazy?: boolean; | |
| 5 | } |
|
5 | } | |
| 6 |
|
6 | |||
| 7 | export function inject<I extends { [name in keyof I]: (v: any) => void } >(dependency: string) { |
|
7 | type Setter<T = any> = (v: T) => void; | |
|
|
8 | ||||
|
|
9 | type Injector<T> = { | |||
|
|
10 | [k in keyof T]: Setter; | |||
|
|
11 | }; | |||
|
|
12 | ||||
|
|
13 | export class Builder<T, S> { | |||
|
|
14 | provides(name: string) { | |||
|
|
15 | return <C extends Constructor<T>>(constructor: C) => { | |||
|
|
16 | return constructor; | |||
|
|
17 | }; | |||
|
|
18 | } | |||
|
|
19 | ||||
|
|
20 | inject<K extends keyof S>(dependency: K) { | |||
|
|
21 | return <M extends keyof T>(target: any, memberName: M, descriptor: TypedPropertyDescriptor<Setter<S[K]>> ) => { | |||
|
|
22 | ||||
|
|
23 | }; | |||
|
|
24 | } | |||
|
|
25 | ||||
|
|
26 | prop<K extends keyof S>(dependency: K) { | |||
|
|
27 | return <M extends keyof T>(target: any, memberName: M, descriptor: TypedPropertyDescriptor<S[K]> ) => { | |||
|
|
28 | ||||
|
|
29 | }; | |||
|
|
30 | } | |||
|
|
31 | } | |||
|
|
32 | ||||
|
|
33 | export function inject<I extends Injector<I> >(dependency: string) { | |||
| 8 | return <M extends keyof I>(target: any, memberName: M, descriptor: TypedPropertyDescriptor<I[M]> ) => { |
|
34 | return <M extends keyof I>(target: any, memberName: M, descriptor: TypedPropertyDescriptor<I[M]> ) => { | |
| 9 |
|
35 | |||
| 10 | }; |
|
36 | }; | |
| 11 | } |
|
37 | } | |
| @@ -1,18 +1,23 | |||||
| 1 |
import { |
|
1 | import { Builder } from "../di/Annotations"; | |
|
|
2 | import { Bar } from "./Bar"; | |||
|
|
3 | import { Foo } from "./Foo"; | |||
| 2 |
|
4 | |||
| 3 | export interface Injector<T> { |
|
5 | export interface Injector<T> { | |
| 4 | setValue(value: T); |
|
6 | setValue(value: T); | |
| 5 | } |
|
7 | } | |
| 6 |
|
8 | |||
|
|
9 | const builder = new Builder<Box<any>, { bar: Bar; foo: Foo}>(); | |||
|
|
10 | ||||
|
|
11 | @builder.provides("barBox") | |||
| 7 | export class Box<T> implements Injector<T> { |
|
12 | export class Box<T> implements Injector<T> { | |
| 8 | private _value: T; |
|
13 | private _value: T; | |
| 9 |
|
14 | |||
| 10 |
@ |
|
15 | @builder.inject("bar") | |
| 11 | setValue(value: T) { |
|
16 | setValue(value: T) { | |
| 12 | this._value = value; |
|
17 | this._value = value; | |
| 13 | } |
|
18 | } | |
| 14 |
|
19 | |||
| 15 | getValue() { |
|
20 | getValue() { | |
| 16 | return this._value; |
|
21 | return this._value; | |
| 17 | } |
|
22 | } | |
| 18 | } |
|
23 | } | |
General Comments 0
You need to be logged in to leave comments.
Login now
