{"version":3,"file":"gernsdorfer-async-component-loader.mjs","sources":["../../../../libs/async-component-loader/src/services/lazy-component-creator.service.ts","../../../../libs/async-component-loader/src/components/async-component-loader.component.ts","../../../../libs/async-component-loader/src/gernsdorfer-async-component-loader.ts"],"sourcesContent":["import {EventEmitter, Type} from \"@angular/core\";\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport type IComponent = { [index: string]: EventEmitter<unknown> | any };\ntype KeysMatching<T extends { [index: string]: EventEmitter<unknown> | unknown }, V> = {\n  [K in keyof T]-?: T[K] extends V ? K : never;\n}[keyof T];\n\nexport type OutputKeys<LAZY_COMPONENT extends IComponent> = KeysMatching<LAZY_COMPONENT, EventEmitter<unknown>>;\n\nexport class LazyComponentCreator<LAZY_COMPONENT extends IComponent> {\n  constructor(\n    private lazyComponent: {\n      component: () => Promise<Type<LAZY_COMPONENT>>;\n      outputs?: {\n        [index in OutputKeys<LAZY_COMPONENT>]?: LAZY_COMPONENT[index]['emit'];\n      };\n    },\n  ) {\n  }\n\n  outputs = this.lazyComponent.outputs;\n\n  async component() {\n    return await this.lazyComponent.component();\n  }\n}\n","import {\n  Component,\n  ComponentRef,\n  EventEmitter,\n  Input,\n  OnDestroy,\n  OnInit,\n  Type,\n  ViewChild,\n  ViewContainerRef\n} from '@angular/core';\nimport {CommonModule} from '@angular/common';\nimport {IComponent, LazyComponentCreator, OutputKeys} from \"../services/lazy-component-creator.service\";\nimport {Subject, takeUntil} from \"rxjs\";\n\n\n@Component({\n  selector: 'async-component-loader',\n  template: `\n    <ng-template #component></ng-template>\n    <ng-container *ngIf=\"isLoading\">\n      <ng-content select=\"[isLoading]\"></ng-content>\n    </ng-container>\n  `,\n  standalone: true,\n  imports: [CommonModule],\n})\nexport class AsyncComponentLoaderComponent<LAZY_COMPONENT extends IComponent> implements OnInit, OnDestroy {\n  @Input() lazyComponentCreator!: LazyComponentCreator<LAZY_COMPONENT>;\n\n  @Input() set inputs(props: Partial<LAZY_COMPONENT>) {\n    this._inputs = props;\n    this.setInputs();\n  }\n\n  isLoading = false;\n  component!: Type<LAZY_COMPONENT>;\n\n  @ViewChild('component', {read: ViewContainerRef})\n  private viewContainerRef!: ViewContainerRef;\n  private componentRef?: ComponentRef<LAZY_COMPONENT>;\n  private _inputs!: Partial<LAZY_COMPONENT>;\n  notifier = new Subject<void>()\n\n\n  ngOnInit() {\n    if (!this.lazyComponentCreator) throw 'Input lazyComponentCreator is not defined';\n    this.isLoading = true\n    this.createComponent().then(() => {\n      this.isLoading = false;\n    });\n  }\n\n  ngOnDestroy() {\n    this.notifier.next()\n    this.notifier.complete()\n  }\n\n  private async createComponent() {\n    const componentRef = await this.loadComponent();\n    this.getAllOutputs(componentRef).forEach((output) => this.listenOnOutput(output));\n    this.setInputs(componentRef);\n    this.componentRef = componentRef;\n  }\n\n  private async loadComponent() {\n    const component = await this.lazyComponentCreator.component();\n    this.viewContainerRef.clear();\n    return this.viewContainerRef.createComponent(component);\n  }\n\n  private getAllOutputs(componentRef: ComponentRef<LAZY_COMPONENT>) {\n    return Object.keys(componentRef.instance)\n      .filter((property) => componentRef.instance[property] instanceof EventEmitter)\n      .map((property) => ({\n        componentRef: componentRef,\n        property: property as OutputKeys<LAZY_COMPONENT>,\n        output: this.lazyComponentCreator.outputs?.[property as OutputKeys<LAZY_COMPONENT>]\n      }))\n      .filter(({output}) => output);\n  }\n\n  private listenOnOutput({\n                           output,\n                           property,\n                           componentRef\n                         }: { componentRef: ComponentRef<LAZY_COMPONENT>, property: OutputKeys<LAZY_COMPONENT>, output: LAZY_COMPONENT[typeof property]['emit'] }): void {\n    componentRef.instance[property].pipe(\n      takeUntil(this.notifier)\n    ).subscribe(\n      (value: ThisParameterType<LAZY_COMPONENT[typeof property]['emit']>) => output(value));\n  }\n\n  private setInputs(componentRef: ComponentRef<LAZY_COMPONENT> | undefined = this.componentRef) {\n    if(!this._inputs) return;\n    Object.keys(this._inputs).forEach((key) => {\n      if (!componentRef?.instance) return;\n      (componentRef.instance[key] as LAZY_COMPONENT[typeof key]) = this._inputs[key] as LAZY_COMPONENT[typeof key];\n    });\n  }\n}\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './index';\n"],"names":[],"mappings":";;;;;;MAUa,oBAAoB,CAAA;AAC/B,IAAA,WAAA,CACU,aAKP,EAAA;QALO,IAAa,CAAA,aAAA,GAAb,aAAa,CAKpB;AAIH,QAAA,IAAA,CAAA,OAAO,GAAG,IAAI,CAAC,aAAa,CAAC,OAAO,CAAC;KAFpC;AAID,IAAA,MAAM,SAAS,GAAA;AACb,QAAA,OAAO,MAAM,IAAI,CAAC,aAAa,CAAC,SAAS,EAAE,CAAC;KAC7C;AACF;;MCCY,6BAA6B,CAAA;AAX1C,IAAA,WAAA,GAAA;QAmBE,IAAS,CAAA,SAAA,GAAG,KAAK,CAAC;AAOlB,QAAA,IAAA,CAAA,QAAQ,GAAG,IAAI,OAAO,EAAQ,CAAA;AA0D/B,KAAA;IAtEC,IAAa,MAAM,CAAC,KAA8B,EAAA;AAChD,QAAA,IAAI,CAAC,OAAO,GAAG,KAAK,CAAC;QACrB,IAAI,CAAC,SAAS,EAAE,CAAC;KAClB;IAYD,QAAQ,GAAA;QACN,IAAI,CAAC,IAAI,CAAC,oBAAoB;AAAE,YAAA,MAAM,2CAA2C,CAAC;AAClF,QAAA,IAAI,CAAC,SAAS,GAAG,IAAI,CAAA;AACrB,QAAA,IAAI,CAAC,eAAe,EAAE,CAAC,IAAI,CAAC,MAAK;AAC/B,YAAA,IAAI,CAAC,SAAS,GAAG,KAAK,CAAC;AACzB,SAAC,CAAC,CAAC;KACJ;IAED,WAAW,GAAA;AACT,QAAA,IAAI,CAAC,QAAQ,CAAC,IAAI,EAAE,CAAA;AACpB,QAAA,IAAI,CAAC,QAAQ,CAAC,QAAQ,EAAE,CAAA;KACzB;AAEO,IAAA,MAAM,eAAe,GAAA;AAC3B,QAAA,MAAM,YAAY,GAAG,MAAM,IAAI,CAAC,aAAa,EAAE,CAAC;QAChD,IAAI,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