1
- import { deepClone , Enumerable , getClass , getKeys , isClass , nameOf , NotEnumerable , Store , Type } from "@tsed/core" ;
1
+ import { classOf , Enumerable , getKeys , isClass , nameOf , NotEnumerable , Store , Type } from "@tsed/core" ;
2
2
import { IDIConfigurationOptions , ProviderScope } from "../interfaces" ;
3
3
import { IProvider } from "../interfaces/IProvider" ;
4
4
import { ProviderType } from "../interfaces/ProviderType" ;
5
5
import { TokenProvider } from "../interfaces/TokenProvider" ;
6
6
7
7
export class Provider < T > implements IProvider < T > {
8
- /**
9
- *
10
- */
8
+ @ Enumerable ( )
9
+ public root : boolean = false ;
10
+
11
11
@Enumerable ( )
12
12
public type : ProviderType | any = ProviderType . PROVIDER ;
13
- /**
14
- *
15
- */
13
+
16
14
@Enumerable ( )
17
15
public injectable : boolean = true ;
18
- /**
19
- *
20
- */
16
+
21
17
@Enumerable ( )
22
18
public instance : T ;
23
- /**
24
- *
25
- */
19
+
26
20
@Enumerable ( )
27
21
public deps : any [ ] ;
28
- /**
29
- *
30
- */
22
+
23
+ @Enumerable ( )
24
+ public imports : any [ ] ;
25
+
31
26
@Enumerable ( )
32
27
public useFactory : Function ;
33
28
34
29
@Enumerable ( )
35
30
public useAsyncFactory : Function ;
36
- /**
37
- *
38
- */
31
+
39
32
@Enumerable ( )
40
33
public useValue : any ;
41
- /**
42
- *
43
- */
34
+
44
35
@NotEnumerable ( )
45
36
protected _provide : TokenProvider ;
46
- /**
47
- *
48
- */
37
+
49
38
@NotEnumerable ( )
50
39
protected _useClass : Type < T > ;
51
- /**
52
- *
53
- */
40
+
54
41
@NotEnumerable ( )
55
42
protected _instance : T ;
56
- /**
57
- *
58
- */
59
- @NotEnumerable ( )
60
- protected _scope : ProviderScope ;
61
- /**
62
- *
63
- */
43
+
64
44
@NotEnumerable ( )
65
45
private _store : Store ;
66
46
67
- private _configuration : Partial < IDIConfigurationOptions > ;
68
-
69
47
[ key : string ] : any ;
70
48
71
49
constructor ( token : TokenProvider ) {
@@ -77,26 +55,17 @@ export class Provider<T> implements IProvider<T> {
77
55
return this . _provide ;
78
56
}
79
57
80
- /**
81
- *
82
- * @returns {any }
83
- */
84
58
get provide ( ) : TokenProvider {
85
59
return this . _provide ;
86
60
}
87
61
88
- /**
89
- *
90
- * @param value
91
- */
92
62
set provide ( value : TokenProvider ) {
93
- this . _provide = isClass ( value ) ? getClass ( value ) : value ;
63
+ if ( value ) {
64
+ this . _provide = isClass ( value ) ? classOf ( value ) : value ;
65
+ this . _store = Store . from ( value ) ;
66
+ }
94
67
}
95
68
96
- /**
97
- *
98
- * @returns {Type<T> }
99
- */
100
69
get useClass ( ) : Type < T > {
101
70
return this . _useClass ;
102
71
}
@@ -108,30 +77,19 @@ export class Provider<T> implements IProvider<T> {
108
77
@Enumerable ( )
109
78
set useClass ( value : Type < T > ) {
110
79
if ( isClass ( value ) ) {
111
- this . _useClass = getClass ( value ) ;
80
+ this . _useClass = classOf ( value ) ;
112
81
this . _store = Store . from ( value ) ;
113
82
}
114
83
}
115
84
116
- /**
117
- *
118
- * @returns {string }
119
- */
120
85
get className ( ) {
121
86
return this . name ;
122
87
}
123
88
124
- /**
125
- *
126
- */
127
89
get name ( ) {
128
90
return nameOf ( this . provide ) ;
129
91
}
130
92
131
- /**
132
- *
133
- * @returns {Store }
134
- */
135
93
public get store ( ) : Store {
136
94
return this . _store ;
137
95
}
@@ -150,7 +108,7 @@ export class Provider<T> implements IProvider<T> {
150
108
return ProviderScope . SINGLETON ;
151
109
}
152
110
153
- return this . _store ? this . store . get ( "scope" ) : this . _scope ;
111
+ return this . store . get ( "scope" ) ;
154
112
}
155
113
156
114
/**
@@ -159,29 +117,29 @@ export class Provider<T> implements IProvider<T> {
159
117
*/
160
118
@Enumerable ( )
161
119
set scope ( scope : ProviderScope ) {
162
- this . _store ? this . store . set ( "scope" , scope ) : this . _scope ;
120
+ this . store . set ( "scope" , scope ) ;
163
121
}
164
122
165
123
get configuration ( ) : Partial < IDIConfigurationOptions > {
166
- return this . _configuration && deepClone ( this . _configuration ) ;
124
+ return this . store . get ( "configuration" ) ;
167
125
}
168
126
127
+ @Enumerable ( )
169
128
set configuration ( configuration : Partial < IDIConfigurationOptions > ) {
170
- this . _configuration = configuration ;
129
+ this . store . set ( "configuration" , configuration ) ;
171
130
}
172
131
173
132
isAsync ( ) : boolean {
174
133
return ! ! this . useAsyncFactory ;
175
134
}
176
135
177
- /**
178
- *
179
- */
180
136
clone ( ) : Provider < any > {
181
- const provider = new ( getClass ( this ) ) ( this . token ) ;
137
+ const provider = new ( classOf ( this ) ) ( this . token ) ;
182
138
183
139
getKeys ( this ) . forEach ( key => {
184
- provider [ key ] = this [ key ] ;
140
+ if ( this [ key ] !== undefined ) {
141
+ provider [ key ] = this [ key ] ;
142
+ }
185
143
} ) ;
186
144
187
145
return provider ;
0 commit comments