import { ComponentType } from '@angular/cdk/portal' ; import { Compiler , ComponentFactory , Injectable , Injector , Type , } from '@angular/core' ; import { Subject } from 'rxjs' ; import { StackableModalEvent , StackableModalService , StackableModalState , } from '../../services/ux/stackable-modal.service' ; export interface LazyModule { resolveComponent ( ) : ComponentFactory < unknown > ; } export interface LazyLoadingService { open ( ) : Promise < any > ; } export interface LazyComponent { opts : Object ; onSaveIntent ( ) : void ; onDismissIntent ( ) : void ; } @ Injectable ( { providedIn : 'root' } ) export class ModalLazyLoadService { module : Type < unknown > ; component : ComponentType < unknown > ; opts : Object ; constructor ( private compiler , private injector , private stackableModal ) { } param { Type < unknown > } mod returns { ModalLazyLoadService } setLazyModule ( mod ) { this . module = mod ; return this ; } param { ComponentType < unknown > } returns { ModalLazyLoadService } setComponent ( component ) { this . component = component ; return this ; } param { Object } opts returns { ModalLazyLoadService } setOpts ( opts ) { this . opts = opts ; return this ; } returns { Promise < any > } public async open ( ) { const moduleFactory = await this . compiler . compileModuleAsync ( this . module ) ; const moduleRef = moduleFactory . create ( this . injector ) ; if ( typeof ( moduleRef . instance as any ) . resolveComponent != 'function' ) { console . error ( 'Your lazy loaded module MUST have a resolveComponent function' ) ; return ; } const componentFactory = ( moduleRef as any ) . instance . resolveComponent ( ) ; const onSuccess$ = new Subject ( ) ; const evt = await this . stackableModal . present ( this . component , null , this . opts ) . toPromise ( ) ; if ( evt . state === StackableModalState . Dismissed && ! onSuccess$ . isStopped ) { throw 'DismissedModalException' ; } return onSuccess$ . toPromise ( ) ; } returns { void } public dismiss ( ) { try { this . stackableModal . dismiss ( ) ; } catch ( e ) { } } }