import { INvModule } from '../types' ; import { NvInstanceFactory , rootInjector , Injector , TInjectTokenProvider } from '@indiv/di' ; export param { Function } FindNvModule param { boolean } isRoot returns { INvModule } export function NvModuleFactory ( FindNvModule , isRoot ? ) { let moduleFound ; if ( ! rootInjector . getInstance ( FindNvModule ) ) { const injector = isRoot ? rootInjector : rootInjector . fork ( ) ; moduleFound = ModuleWithInjectorFactory ( FindNvModule , injector ) ; moduleFound . $privateInjector = injector ; rootInjector . setInstance ( FindNvModule , moduleFound ) ; } else moduleFound = rootInjector . getInstance ( FindNvModule ) ; return moduleFound ; } param { INvModule } moduleInstance param { Injector } injector returns { void } function buildProviderList ( moduleInstance , injector ? ) { if ( ! moduleInstance . $providers ) return ; const length = moduleInstance . $providers . length ; for ( let i = 0 ; i < length ; i ++ ) { const service = moduleInstance . $providers [ i ] ; injector . setProvider ( ( service as TInjectTokenProvider ) . provide , service ) ; } } param { INvModule } moduleInstance returns { void } function buildImports ( moduleInstance ) { if ( ! moduleInstance . $imports ) return ; const length = moduleInstance . $imports . length ; for ( let i = 0 ; i < length ; i ++ ) { const ModuleImport = moduleInstance . $imports [ i ] ; const moduleImport = NvModuleFactory ( ModuleImport , true ) ; if ( moduleImport . $exportsList ) { const exportsLength = moduleImport . $exportsList . length ; for ( let i = 0 ; i < exportsLength ; i ++ ) { const exportFromModule = moduleImport . $exportsList [ i ] ; if ( moduleInstance . $declarations && ! moduleInstance . $declarations . find ( ( declaration ) => declaration . selector === ( exportFromModule as any ) . selector ) ) { moduleInstance . $declarations . push ( exportFromModule ) ; } } } } } param { INvModule } moduleInstance returns { void } function buildDeclarations4Declarations ( moduleInstance ) { if ( ! moduleInstance . $declarations ) return ; const length = moduleInstance . $declarations . length ; for ( let i = 0 ; i < length ; i ++ ) { const FindDeclaration = moduleInstance . $declarations [ i ] ; moduleInstance . $declarations . forEach ( ( needInjectDeclaration ) => { if ( ! FindDeclaration . prototype . $declarationMap . has ( needInjectDeclaration . selector ) ) FindDeclaration . prototype . $declarationMap . set ( needInjectDeclaration . selector , needInjectDeclaration ) ; } ) ; } } param { INvModule } moduleInstance returns { void } function buildExports ( moduleInstance ) { if ( ! moduleInstance . $exports ) return ; const length = moduleInstance . $exports . length ; for ( let i = 0 ; i < length ; i ++ ) { const ModuleExport = moduleInstance . $exports [ i ] ; if ( ( ModuleExport as any ) . nvType === 'nvModule' ) { const moduleInstanceOfExport = NvModuleFactory ( ModuleExport ) ; const moduleInstanceOfExportLength = moduleInstanceOfExport . $exportsList . length ; for ( let j = 0 ; j < moduleInstanceOfExportLength ; j ++ ) { const moduleExportFromModuleOfExport = moduleInstanceOfExport . $exportsList [ j ] ; if ( ! moduleInstance . $exportsList . find ( ( declaration ) => declaration . selector === ( moduleExportFromModuleOfExport as any ) . selector ) ) moduleInstance . $exportsList . push ( moduleExportFromModuleOfExport ) ; } } if ( ( ModuleExport as any ) . nvType !== 'nvModule' ) { if ( ! moduleInstance . $exportsList . find ( ( declaration ) => declaration . selector === ( ModuleExport as any ) . selector ) ) moduleInstance . $exportsList . push ( ModuleExport ) ; } } } export param { Function } NM param { Injector } injector returns { INvModule } export function ModuleWithInjectorFactory ( NM , injector ) { buildProviderList ( NM . prototype , injector ) ; buildImports ( NM . prototype ) ; buildDeclarations4Declarations ( NM . prototype ) ; buildExports ( NM . prototype ) ; return NvInstanceFactory ( NM , null , injector ) ; }