@@ -52,8 +52,9 @@ export class Injector {
5252 ) {
5353 const { metatype } = wrapper ;
5454 const currentMetatype = collection . get ( metatype . name ) ;
55- if ( currentMetatype . instance !== null ) return ;
56-
55+ if ( currentMetatype . instance !== null ) {
56+ return ;
57+ }
5758 await this . resolveConstructorParams (
5859 wrapper as any ,
5960 module ,
@@ -87,12 +88,13 @@ export class Injector {
8788 { metatype, name } : InstanceWrapper < T > ,
8889 collection : Map < string , InstanceWrapper < T > > ,
8990 ) {
90- if ( ! collection ) return null ;
91-
91+ if ( ! collection ) {
92+ return null ;
93+ }
9294 const target = collection . get ( name ) ;
93- if ( target . isResolved || ! isNil ( target . inject ) || ! metatype . prototype )
95+ if ( target . isResolved || ! isNil ( target . inject ) || ! metatype . prototype ) {
9496 return null ;
95-
97+ }
9698 collection . set ( name , {
9799 ...collection . get ( name ) ,
98100 instance : Object . create ( metatype . prototype ) ,
@@ -125,32 +127,21 @@ export class Injector {
125127 return wrapper . done$ ;
126128 }
127129 const done = this . applyDoneHook ( wrapper ) ;
128- const { metatype, name, inject } = wrapper ;
129- const currentMetatype = collection . get ( name ) ;
130- if ( isUndefined ( currentMetatype ) ) {
130+ const { name, inject } = wrapper ;
131+
132+ const targetMetatype = collection . get ( name ) ;
133+ if ( isUndefined ( targetMetatype ) ) {
131134 throw new RuntimeException ( ) ;
132135 }
133- if ( currentMetatype . isResolved ) {
136+ if ( targetMetatype . isResolved ) {
134137 return void 0 ;
135138 }
136-
137139 await this . resolveConstructorParams < T > (
138140 wrapper ,
139141 module ,
140142 inject ,
141- async instances => {
142- if ( isNil ( inject ) ) {
143- currentMetatype . instance = Object . assign (
144- currentMetatype . instance ,
145- new metatype ( ...instances ) ,
146- ) ;
147- } else {
148- const factoryResult = currentMetatype . metatype ( ...instances ) ;
149- currentMetatype . instance = await factoryResult ;
150- }
151- currentMetatype . isResolved = true ;
152- done ( ) ;
153- } ,
143+ async instances =>
144+ this . instantiateClass ( instances , wrapper , targetMetatype , done ) ,
154145 ) ;
155146 }
156147
@@ -269,17 +260,11 @@ export class Injector {
269260 ) {
270261 const { name } = dependencyContext ;
271262 const scanInExports = ( ) =>
272- this . lookupComponentInExports (
273- components ,
274- dependencyContext ,
275- module ,
276- wrapper ,
277- ) ;
263+ this . lookupComponentInExports ( dependencyContext , module , wrapper ) ;
278264 return components . has ( name ) ? components . get ( name ) : scanInExports ( ) ;
279265 }
280266
281267 public async lookupComponentInExports < T = any > (
282- components : Map < string , any > ,
283268 dependencyContext : InjectorDependencyContext ,
284269 module : Module ,
285270 wrapper : InstanceWrapper < T > ,
@@ -328,4 +313,26 @@ export class Injector {
328313 }
329314 return componentRef ;
330315 }
316+
317+ public async instantiateClass (
318+ instances : any [ ] ,
319+ wrapper : InstanceWrapper < any > ,
320+ targetMetatype : InstanceWrapper < any > ,
321+ done : Function ,
322+ ) {
323+ const { metatype, inject } = wrapper ;
324+ if ( isNil ( inject ) ) {
325+ targetMetatype . instance = Object . assign (
326+ targetMetatype . instance ,
327+ new metatype ( ...instances ) ,
328+ ) ;
329+ } else {
330+ const factoryResult = ( ( targetMetatype . metatype as any ) as Function ) (
331+ ...instances ,
332+ ) ;
333+ targetMetatype . instance = await factoryResult ;
334+ }
335+ targetMetatype . isResolved = true ;
336+ done ( ) ;
337+ }
331338}
0 commit comments