@@ -8,12 +8,11 @@ import {
8
8
PropertyMetadata ,
9
9
ResolverClassMetadata ,
10
10
} from '../metadata' ;
11
- import { InterfaceMetadata } from '../metadata/interface.metadata' ;
12
11
import { ObjectTypeMetadata } from '../metadata/object-type.metadata' ;
13
12
14
13
export class MapByName < T > {
15
14
protected map = new Map < string , T > ( ) ;
16
- protected all : T [ ] = [ ] ;
15
+ protected all : ( T extends any [ ] ? T [ number ] : T ) [ ] = [ ] ;
17
16
18
17
getAll ( ) {
19
18
return this . all ;
@@ -39,19 +38,36 @@ export class MapByName<T> {
39
38
}
40
39
41
40
export class MapArrayByName < T > extends MapByName < T [ ] > {
41
+ constructor ( protected globalArray : Array < T > = null ) {
42
+ super ( ) ;
43
+ }
44
+
42
45
getByName ( name : string ) : T [ ] {
43
46
return super . getByName ( name ) || [ ] ;
44
47
}
45
48
46
- add ( value : T [ ] extends any [ ] ? T [ ] [ number ] : T [ ] , name : string ) {
47
- let arrayResult = this . map . get ( name ) ;
49
+ add ( value : T , name : string ) {
50
+ let arrayResult = super . getByName ( name ) ;
48
51
if ( ! arrayResult ) {
49
52
arrayResult = [ ] ;
50
53
this . map . set ( name , arrayResult ) ;
51
54
}
52
55
53
56
arrayResult . push ( value ) ;
54
57
this . all . push ( value ) ;
58
+ this . globalArray && this . globalArray . push ( value ) ;
59
+ }
60
+
61
+ unshift ( value : T , name : string ) {
62
+ let arrayResult = super . getByName ( name ) ;
63
+ if ( ! arrayResult ) {
64
+ arrayResult = [ ] ;
65
+ this . map . set ( name , arrayResult ) ;
66
+ }
67
+
68
+ arrayResult . unshift ( value ) ;
69
+ this . all . push ( value ) ;
70
+ this . globalArray && this . globalArray . unshift ( value ) ;
55
71
}
56
72
}
57
73
@@ -67,63 +83,133 @@ export class FieldDirectiveMap extends MapArrayByName<PropertyDirectiveMetadata>
67
83
68
84
this . sdls . add ( value . sdl ) ;
69
85
this . fieldNames . add ( value . fieldName ) ;
86
+ this . globalArray && this . globalArray . push ( value ) ;
87
+ }
88
+ }
89
+
90
+ class ArrayWithGlobalValues < T > extends Array < T > {
91
+ constructor ( private globalArray : Array < T > ) {
92
+ super ( ) ;
93
+ }
94
+
95
+ push ( ...items ) : number {
96
+ this . globalArray . push ( ...items ) ;
97
+ return super . push ( ...items ) ;
98
+ }
99
+
100
+ unshift ( ...items ) : number {
101
+ this . globalArray . unshift ( ...items ) ;
102
+ return super . unshift ( ...items ) ;
70
103
}
71
104
}
72
105
73
106
export class TypeMetadataStorageModel {
74
- fieldDirectives = new FieldDirectiveMap ( ) ;
75
- fieldExtensions = new MapArrayByName < PropertyExtensionsMetadata > ( ) ;
107
+ constructor ( private all : AllMetadata ) { }
108
+
76
109
fields = new MapByName < PropertyMetadata > ( ) ;
77
110
params = new MapArrayByName < MethodArgsMetadata > ( ) ;
78
- classDirectives = new Array < ClassDirectiveMetadata > ( ) ;
79
- classExtensions = new Array < ClassExtensionsMetadata > ( ) ;
80
- argumentType : ClassMetadata ;
81
- interface : InterfaceMetadata ;
82
- inputType : ClassMetadata ;
83
- objectType : ObjectTypeMetadata ;
84
- resolver : ResolverClassMetadata ;
111
+ fieldDirectives = new FieldDirectiveMap ( this . all . fieldDirectives ) ;
112
+ fieldExtensions = new MapArrayByName < PropertyExtensionsMetadata > (
113
+ this . all . fieldExtensions ,
114
+ ) ;
115
+ classDirectives = new ArrayWithGlobalValues < ClassDirectiveMetadata > (
116
+ this . all . classDirectives ,
117
+ ) ;
118
+ classExtensions = new ArrayWithGlobalValues < ClassExtensionsMetadata > (
119
+ this . all . classExtensions ,
120
+ ) ;
121
+
122
+ set argumentType ( val : ClassMetadata ) {
123
+ this . _argumentType = val ;
124
+ this . all . argumentType . push ( val ) ;
125
+ }
126
+ get argumentType ( ) {
127
+ return this . _argumentType ;
128
+ }
129
+
130
+ set interface ( val : ClassMetadata ) {
131
+ this . _interface = val ;
132
+ this . all . interface . push ( val ) ;
133
+ }
134
+ get interface ( ) {
135
+ return this . _interface ;
136
+ }
137
+
138
+ set inputType ( val : ClassMetadata ) {
139
+ this . _inputType = val ;
140
+ this . all . inputType . push ( val ) ;
141
+ }
142
+ get inputType ( ) {
143
+ return this . _inputType ;
144
+ }
145
+
146
+ set objectType ( val : ObjectTypeMetadata ) {
147
+ this . _objectType = val ;
148
+ this . all . objectType . push ( val ) ;
149
+ }
150
+ get objectType ( ) {
151
+ return this . _objectType ;
152
+ }
153
+
154
+ set resolver ( val : ResolverClassMetadata ) {
155
+ this . _resolver = val ;
156
+ this . all . resolver . push ( val ) ;
157
+ }
158
+ get resolver ( ) {
159
+ return this . _resolver ;
160
+ }
161
+
162
+ _argumentType : ClassMetadata ;
163
+ _interface : ClassMetadata ;
164
+ _inputType : ClassMetadata ;
165
+ _objectType : ObjectTypeMetadata ;
166
+ _resolver : ResolverClassMetadata ;
167
+ }
168
+
169
+ interface AllMetadata {
170
+ argumentType : ClassMetadata [ ] ;
171
+ interface : ClassMetadata [ ] ;
172
+ inputType : ClassMetadata [ ] ;
173
+ objectType : ObjectTypeMetadata [ ] ;
174
+ resolver : ResolverClassMetadata [ ] ;
175
+ classDirectives : [ ] ;
176
+ classExtensions : [ ] ;
177
+ fieldDirectives : [ ] ;
178
+ fieldExtensions : [ ] ;
85
179
}
86
180
87
181
export class TypeMetadataStorageModelList {
88
182
private map = new Map < Function , TypeMetadataStorageModel > ( ) ;
89
183
private array = new Array < TypeMetadataStorageModel > ( ) ;
90
184
185
+ public all : AllMetadata = {
186
+ argumentType : [ ] ,
187
+ interface : [ ] ,
188
+ inputType : [ ] ,
189
+ objectType : [ ] ,
190
+ resolver : [ ] ,
191
+ classDirectives : [ ] ,
192
+ classExtensions : [ ] ,
193
+ fieldDirectives : [ ] ,
194
+ fieldExtensions : [ ] ,
195
+ } ;
196
+
91
197
get ( target : Function ) {
92
198
let metadata = this . map . get ( target ) ;
93
199
94
200
if ( ! metadata ) {
95
- metadata = new TypeMetadataStorageModel ( ) ;
201
+ metadata = new TypeMetadataStorageModel ( this . all ) ;
96
202
this . map . set ( target , metadata ) ;
97
203
this . array . push ( metadata ) ;
98
204
}
99
205
100
206
return metadata ;
101
207
}
102
208
103
- private getAllWithoutNullsByPredicate < V > (
104
- predicate : ( t : TypeMetadataStorageModel ) => V ,
105
- ) {
106
- return this . array . reduce ( ( prev , curr ) => {
107
- const val = predicate ( curr ) ;
108
- if ( val ) prev . push ( val ) ;
109
- return prev ;
110
- } , new Array < V > ( ) ) ;
111
- }
112
-
113
- getAll ( ) {
114
- return {
115
- argumentType : ( ) =>
116
- this . getAllWithoutNullsByPredicate ( ( t ) => t . argumentType ) ,
117
- interface : ( ) => this . getAllWithoutNullsByPredicate ( ( t ) => t . interface ) ,
118
- inputType : ( ) => this . getAllWithoutNullsByPredicate ( ( t ) => t . inputType ) ,
119
- objectType : ( ) => this . getAllWithoutNullsByPredicate ( ( t ) => t . objectType ) ,
120
- resolver : ( ) => this . getAllWithoutNullsByPredicate ( ( t ) => t . resolver ) ,
121
- classDirectives : ( ) => this . array . flatMap ( ( t ) => t . classDirectives ) ,
122
- classExtensions : ( ) => this . array . flatMap ( ( t ) => t . classExtensions ) ,
123
- fieldDirectives : ( ) =>
124
- this . array . flatMap ( ( t ) => t . fieldDirectives . getAll ( ) ) ,
125
- fieldExtensions : ( ) =>
126
- this . array . flatMap ( ( t ) => t . fieldExtensions . getAll ( ) ) ,
127
- } ;
209
+ compile ( ) {
210
+ this . all . classDirectives . reverse ( ) ;
211
+ this . all . classExtensions . reverse ( ) ;
212
+ this . all . fieldDirectives . reverse ( ) ;
213
+ this . all . fieldExtensions . reverse ( ) ;
128
214
}
129
215
}
0 commit comments