@@ -35,16 +35,21 @@ import { Element } from "./type/discovery/element/Element";
3535import { DiscoveredObjectType } from "./type/discovery/object/DiscoveredType" ;
3636import { Relation } from "./type/discovery/relation/Relation" ;
3737import { TypePool } from "./type/resolving/TypePool" ;
38+ import TypedEmitter from "typed-emitter" ;
39+ import { Events } from "./Events" ;
3840
3941export class RootContext extends CoreRootContext < t . Node > {
4042 protected _exportFactory : ExportFactory ;
4143 protected _typeExtractor : TypeExtractor ;
4244 protected _typeResolver : TypeModelFactory ;
4345
4446 protected _files : string [ ] ;
45- protected _elementMap : Map < string , Element > ;
46- protected _relationMap : Map < string , Relation > ;
47- protected _objectMap : Map < string , DiscoveredObjectType > ;
47+ // filepath -> id -> element
48+ protected _elementMap : Map < string , Map < string , Element > > ;
49+ // filepath -> id -> relation
50+ protected _relationMap : Map < string , Map < string , Relation > > ;
51+ // filepath -> id -> object
52+ protected _objectMap : Map < string , Map < string , DiscoveredObjectType > > ;
4853
4954 protected _typeModel : TypeModel ;
5055 protected _typePool : TypePool ;
@@ -126,13 +131,26 @@ export class RootContext extends CoreRootContext<t.Node> {
126131 return this . _sources . get ( absoluteTargetPath ) ;
127132 }
128133
129- private getExports ( filePath : string ) : Export [ ] {
134+ getExports ( filePath : string ) : Export [ ] {
130135 const absolutePath = this . resolvePath ( filePath ) ;
131136
132137 if ( ! this . _exportMap . has ( absolutePath ) ) {
133- return this . _exportFactory . extract (
138+ ( < TypedEmitter < Events > > process ) . emit (
139+ "exportExtractionStart" ,
140+ this ,
141+ absolutePath
142+ ) ;
143+ this . _exportMap . set (
134144 absolutePath ,
135- this . getAbstractSyntaxTree ( absolutePath )
145+ this . _exportFactory . extract (
146+ absolutePath ,
147+ this . getAbstractSyntaxTree ( absolutePath )
148+ )
149+ ) ;
150+ ( < TypedEmitter < Events > > process ) . emit (
151+ "exportExtractionComplete" ,
152+ this ,
153+ absolutePath
136154 ) ;
137155 }
138156
@@ -150,26 +168,137 @@ export class RootContext extends CoreRootContext<t.Node> {
150168 return this . _exportMap ;
151169 }
152170
153- extractTypes ( ) : void {
154- if ( ! this . _elementMap || ! this . _relationMap || ! this . _objectMap ) {
155- this . _typeExtractor . extractAll ( this ) ;
156- this . _elementMap = this . _typeExtractor . elementMap ;
157- this . _relationMap = this . _typeExtractor . relationMap ;
158- this . _objectMap = this . _typeExtractor . objectMap ;
171+ getElements ( filepath : string ) {
172+ const absolutePath = this . resolvePath ( filepath ) ;
173+
174+ if ( ! this . _elementMap . has ( absolutePath ) ) {
175+ ( < TypedEmitter < Events > > process ) . emit (
176+ "elementExtractionStart" ,
177+ this ,
178+ absolutePath
179+ ) ;
180+ const elementMap = this . _typeExtractor . extractElements (
181+ absolutePath ,
182+ this . getAbstractSyntaxTree ( absolutePath )
183+ ) ;
184+
185+ this . _elementMap . set ( absolutePath , elementMap ) ;
186+ ( < TypedEmitter < Events > > process ) . emit (
187+ "elementExtractionComplete" ,
188+ this ,
189+ absolutePath
190+ ) ;
191+ }
192+
193+ return this . _elementMap . get ( absolutePath ) ;
194+ }
195+
196+ getAllElements ( ) {
197+ if ( ! this . _elementMap ) {
198+ this . _elementMap = new Map ( ) ;
199+
200+ for ( const filepath of this . getFiles ( ) ) {
201+ this . _elementMap . set ( filepath , this . getElements ( filepath ) ) ;
202+ }
203+ }
204+ return this . _elementMap ;
205+ }
206+
207+ getRelations ( filepath : string ) {
208+ const absolutePath = this . resolvePath ( filepath ) ;
209+
210+ if ( ! this . _relationMap . has ( absolutePath ) ) {
211+ ( < TypedEmitter < Events > > process ) . emit (
212+ "relationExtractionStart" ,
213+ this ,
214+ absolutePath
215+ ) ;
216+ const relationsMap = this . _typeExtractor . extractRelations (
217+ absolutePath ,
218+ this . getAbstractSyntaxTree ( absolutePath )
219+ ) ;
220+
221+ this . _relationMap . set ( absolutePath , relationsMap ) ;
222+ ( < TypedEmitter < Events > > process ) . emit (
223+ "relationExtractionComplete" ,
224+ this ,
225+ absolutePath
226+ ) ;
227+ }
228+
229+ return this . _relationMap . get ( absolutePath ) ;
230+ }
231+
232+ getAllRelations ( ) {
233+ if ( ! this . _relationMap ) {
234+ this . _relationMap = new Map ( ) ;
235+
236+ for ( const filepath of this . getFiles ( ) ) {
237+ this . _relationMap . set ( filepath , this . getRelations ( filepath ) ) ;
238+ }
239+ }
240+ return this . _relationMap ;
241+ }
242+
243+ getObjectTypes ( filepath : string ) {
244+ const absolutePath = this . resolvePath ( filepath ) ;
245+
246+ if ( ! this . _objectMap . has ( absolutePath ) ) {
247+ ( < TypedEmitter < Events > > process ) . emit (
248+ "objectTypeExtractionStart" ,
249+ this ,
250+ absolutePath
251+ ) ;
252+ const objectsMap = this . _typeExtractor . extractObjectTypes (
253+ absolutePath ,
254+ this . getAbstractSyntaxTree ( absolutePath )
255+ ) ;
256+
257+ this . _objectMap . set ( absolutePath , objectsMap ) ;
258+ ( < TypedEmitter < Events > > process ) . emit (
259+ "objectTypeExtractionComplete" ,
260+ this ,
261+ absolutePath
262+ ) ;
263+ }
264+
265+ return this . _objectMap . get ( absolutePath ) ;
266+ }
267+
268+ getAllObjectTypes ( ) {
269+ if ( ! this . _objectMap ) {
270+ this . _objectMap = new Map ( ) ;
271+
272+ for ( const filepath of this . getFiles ( ) ) {
273+ this . _objectMap . set ( filepath , this . getObjectTypes ( filepath ) ) ;
274+ }
159275 }
276+ return this . _objectMap ;
160277 }
161278
162279 resolveTypes ( ) : void {
163- if ( ! this . _elementMap || ! this . _relationMap || ! this . _objectMap ) {
164- this . extractTypes ( ) ;
280+ // TODO allow sub selections of files (do not consider entire context)
281+ if ( ! this . _elementMap ) {
282+ this . getAllElements ( ) ;
283+ }
284+ if ( ! this . _relationMap ) {
285+ this . getAllRelations ( ) ;
286+ }
287+ if ( ! this . _objectMap ) {
288+ this . getAllObjectTypes ( ) ;
289+ }
290+ if ( ! this . _exportMap ) {
291+ this . getAllExports ( ) ;
165292 }
166293
167294 if ( ! this . _typeModel ) {
295+ ( < TypedEmitter < Events > > process ) . emit ( "typeResolvingStart" , this ) ;
168296 this . _typeModel = this . _typeResolver . resolveTypes (
169297 this . _elementMap ,
170298 this . _relationMap
171299 ) ;
172- this . _typePool = new TypePool ( this . _objectMap , this . getAllExports ( ) ) ;
300+ this . _typePool = new TypePool ( this . _objectMap , this . _exportMap ) ;
301+ ( < TypedEmitter < Events > > process ) . emit ( "typeResolvingComplete" , this ) ;
173302 }
174303 }
175304
@@ -188,25 +317,4 @@ export class RootContext extends CoreRootContext<t.Node> {
188317
189318 return this . _typePool ;
190319 }
191-
192- getElement ( id : string ) : Element {
193- if ( ! this . _elementMap || ! this . _elementMap . has ( id ) ) {
194- this . extractTypes ( ) ;
195- }
196- return this . _elementMap . get ( id ) ;
197- }
198-
199- getRelation ( id : string ) : Relation {
200- if ( ! this . _relationMap || ! this . _relationMap . has ( id ) ) {
201- this . extractTypes ( ) ;
202- }
203- return this . _relationMap . get ( id ) ;
204- }
205-
206- getObject ( id : string ) : DiscoveredObjectType {
207- if ( ! this . _objectMap || ! this . _objectMap . has ( id ) ) {
208- this . extractTypes ( ) ;
209- }
210- return this . _objectMap . get ( id ) ;
211- }
212320}
0 commit comments