1- import { Scope } from '@nestjs/common' ;
21import {
32 OPTIONAL_DEPS_METADATA ,
43 OPTIONAL_PROPERTY_DEPS_METADATA ,
@@ -18,15 +17,13 @@ import {
1817import { RuntimeException } from '../errors/exceptions/runtime.exception' ;
1918import { UndefinedDependencyException } from '../errors/exceptions/undefined-dependency.exception' ;
2019import { UnknownDependenciesException } from '../errors/exceptions/unknown-dependencies.exception' ;
21- import { AsyncContext } from './../hooks' ;
2220import { STATIC_CONTEXT } from './constants' ;
2321import {
2422 ContextId ,
2523 InstancePerContext ,
2624 InstanceWrapper ,
2725} from './instance-wrapper' ;
2826import { Module } from './module' ;
29- import { ModulesContainer } from './modules-container' ;
3027
3128/**
3229 * The type of an injectable dependency
@@ -75,17 +72,14 @@ export class Injector {
7572 contextId = STATIC_CONTEXT ,
7673 ) {
7774 const { metatype } = wrapper ;
78- const targetMetatype = collection . get ( metatype . name ) ;
79- if ( targetMetatype . instance !== null ) {
75+ const targetWrapper = collection . get ( metatype . name ) ;
76+ if ( targetWrapper . instance !== null ) {
8077 return ;
8178 }
8279 const loadInstance = ( instances : any [ ] ) => {
83- const instanceWrapper = new InstanceWrapper ( {
84- instance : new metatype ( ...instances ) ,
85- metatype,
86- host : module ,
87- } ) ;
88- collection . set ( metatype . name , instanceWrapper ) ;
80+ targetWrapper . instance = targetWrapper . isDependencyTreeStatic ( )
81+ ? new metatype ( ...instances )
82+ : Object . create ( metatype ) ;
8983 } ;
9084 await this . resolveConstructorParams (
9185 wrapper ,
@@ -199,7 +193,6 @@ export class Injector {
199193 instances ,
200194 wrapper ,
201195 targetWrapper ,
202- module . id ,
203196 contextId ,
204197 ) ;
205198 this . applyProperties ( instance , properties ) ;
@@ -526,89 +519,55 @@ export class Injector {
526519 instances : any [ ] ,
527520 wrapper : InstanceWrapper ,
528521 targetMetatype : InstanceWrapper ,
529- moduleId : string ,
530522 contextId = STATIC_CONTEXT ,
531523 ) : Promise < T > {
532- const { metatype, inject, name , scope } = wrapper ;
524+ const { metatype, inject } = wrapper ;
533525 const instanceHost = targetMetatype . getInstanceByContextId ( contextId ) ;
534- const instanceKey = moduleId + name ;
526+ const isDependencyTreeStatic = wrapper . isDependencyTreeStatic ( ) ;
527+ const isInContext =
528+ ( isDependencyTreeStatic && contextId === STATIC_CONTEXT ) ||
529+ ( ! isDependencyTreeStatic && contextId !== STATIC_CONTEXT ) ;
535530
536- if ( isNil ( inject ) ) {
531+ if ( isNil ( inject ) && isInContext ) {
537532 const targetInstance = wrapper . getInstanceByContextId ( contextId ) ;
533+
538534 targetInstance . instance = wrapper . forwardRef
539535 ? Object . assign ( targetInstance . instance , new metatype ( ...instances ) )
540536 : new metatype ( ...instances ) ;
541-
542- if ( scope === Scope . LAZY_ASYNC ) {
543- this . mergeAsyncProxy (
544- instances ,
545- targetInstance ,
546- instanceKey ,
547- metatype ,
548- ( ...args : any [ ] ) => new metatype ( ...args ) ,
549- ) ;
550- }
551- } else {
537+ } else if ( isInContext ) {
552538 const factoryReturnValue = ( ( targetMetatype . metatype as any ) as Function ) (
553539 ...instances ,
554540 ) ;
555541 instanceHost . instance = await factoryReturnValue ;
556- if ( scope === Scope . LAZY_ASYNC ) {
557- this . mergeAsyncProxy (
558- instances ,
559- instanceHost ,
560- instanceKey ,
561- metatype ,
562- ( ...args : any [ ] ) =>
563- ( ( targetMetatype . metatype as any ) as Function ) ( ...args ) ,
564- ) ;
565- }
566542 }
567543 instanceHost . isResolved = true ;
568544 return instanceHost . instance ;
569545 }
570546
571- mergeAsyncProxy (
572- instances : any [ ] ,
573- targetInstance : InstancePerContext < any > ,
574- instanceKey : string ,
575- metatype : Type < any > ,
576- factory : ( ...intances : any [ ] ) => any ,
577- ) {
578- const asyncContext = AsyncContext . getInstance ( ) ;
579- asyncContext . set ( instanceKey , targetInstance . instance ) ;
580-
581- const proxy = ( target : unknown , property : string | number | symbol ) => {
582- if ( ! ( property in ( target as object ) ) ) {
583- return ;
584- }
585- const cachedInstance = asyncContext . get ( instanceKey ) ;
586- if ( cachedInstance ) {
587- return cachedInstance [ property ] ;
588- }
589- const value = factory ( ...instances ) ;
590- asyncContext . set ( instanceKey , value ) ;
591- return value [ property ] ;
592- } ;
593- targetInstance . instance = new Proxy ( targetInstance . instance , {
594- get : proxy ,
595- set : proxy ,
596- } ) ;
597- }
598-
599- async loadControllerPerContext < T > (
600- instance : Controller ,
601- modulesContainer : ModulesContainer ,
602- moduleKey : string ,
547+ async loadPerContext < T = any > (
548+ instance : T ,
549+ module : Module ,
550+ collection : Map < string , InstanceWrapper > ,
603551 ctx : ContextId ,
604552 ) : Promise < T > {
605- const module = modulesContainer . get ( moduleKey ) ;
553+ const wrapper = collection . get (
554+ instance . constructor && instance . constructor . name ,
555+ ) ;
556+ await this . loadInstance ( wrapper , collection , module , ctx ) ;
557+ await this . loadEnhancersPerContext ( wrapper , module , ctx ) ;
606558
607- const { controllers } = module ;
608- const controller = controllers . get ( instance . constructor . name ) ;
609- await this . loadInstance ( controller , controllers , module , ctx ) ;
559+ const host = wrapper . getInstanceByContextId ( ctx ) ;
560+ return host && ( host . instance as T ) ;
561+ }
610562
611- const wrapper = controller . getInstanceByContextId ( ctx ) ;
612- return wrapper && ( wrapper . instance as T ) ;
563+ async loadEnhancersPerContext (
564+ wrapper : InstanceWrapper ,
565+ module : Module ,
566+ ctx : ContextId ,
567+ ) {
568+ const enhancers = wrapper . getEnhancersMetadata ( ) ;
569+ const loadEnhancer = ( item : InstanceWrapper ) =>
570+ this . loadInstance ( item , module . injectables , module , ctx ) ;
571+ await Promise . all ( enhancers . map ( loadEnhancer ) ) ;
613572 }
614573}
0 commit comments