File

angular/projects/researchdatabox/portal-ng-common/src/lib/base.component.ts

Description

Base component class for ReDBox Portal.

Encapsulates boilerplate NG-specific tasks, so extensions can focus on specific features.

See https://angular.io/guide/lifecycle-hooks

Author: Shilo Banihit

Implements

Initable

Metadata

Index

Properties
Methods

Constructor

constructor()

Methods

getInitSubject
getInitSubject()

For those interested in the init from RXJS-land.

Note that it returns a BehaviorSubject instance, and will have an initial value of false, so process the return value as needed.

Returns : Subject<any>
Protected Abstract initComponent
initComponent()

Called when component specific initialisation can happen, extensions need to override.

Returns : Promise<void>
isInitializing
isInitializing()

Main flag to indicate the init status

Returns : boolean
ngOnInit
ngOnInit()
Returns : void
Async waitForDeps
waitForDeps()

Wait for dependencies to initialise

Returns : Promise<any>
Async waitForInit
waitForInit()

For those interested in the init from the Promise-land

Returns : Promise<any>

Properties

Protected brandingAndPortalUrl
Type : string
Default value : ''
Private filterFn
Default value : function(initStat: boolean) { return initStat; }
Protected initDependencies
Type : Initable[]
Default value : []
Protected initSubject
Type : BehaviorSubject<any>
Default value : new BehaviorSubject(false)
Protected isReady
Type : boolean
Default value : false
import { Initable } from './initable.interface';
import { Component } from '@angular/core';
import { BehaviorSubject, Subject, firstValueFrom, filter } from 'rxjs';
/**
 * Base component class for ReDBox Portal.
 * 
 * Encapsulates boilerplate NG-specific tasks, so extensions can focus on specific features.
 *  
 * See https://angular.io/guide/lifecycle-hooks
 * 
 * Author: <a href='https://github.com/shilob' target='_blank'>Shilo Banihit</a>
 */
@Component({
  template: '<p>Base Component</p>'
})
export abstract class BaseComponent implements Initable {

  protected isReady: boolean = false;
  protected initSubject: BehaviorSubject<any> = new BehaviorSubject(false);
  protected initDependencies: Initable[] = [];
  private filterFn = function(initStat: boolean) { return initStat; };
  // convenience properties
  protected brandingAndPortalUrl: string = '';

  constructor() {
  }
  
  /**
   * See https://angular.io/api/core/OnInit
   */
  ngOnInit() {
    (async () => {
      // really it is just for dependencies
      await this.waitForDeps();
      // call this to inform child class to begin own init
      await this.initComponent();
      this.isReady = true;
      // inform interested parties in RXJS-land
      this.initSubject.next(this.isReady);
    })();
  }
  /** 
   * Called when component specific initialisation can happen, extensions need to override. 
   */
  protected abstract initComponent():Promise<void>;

  /**
   * For those interested in the init from RXJS-land. 
   * 
   * Note that it returns a BehaviorSubject instance, and will have an initial value of false, so process the return value as needed.
   */
  getInitSubject(): Subject<any> {
    return this.initSubject;
  }

  /**
   * Wait for dependencies to initialise
   */
  async waitForDeps(): Promise<any> {
    if (!this.isReady) {
      for (let dep of this.initDependencies) {
        await dep.waitForInit();
      }
    }
  }
  /**
   * 
   * For those interested in the init from the Promise-land
   */
  async waitForInit(): Promise<any> {
    return await firstValueFrom(this.getInitSubject().pipe(filter(this.filterFn)));
  }
  /**
   * Main flag to indicate the init status
   */
  isInitializing(): boolean {
    return !this.isReady;
  }
}
Legend
Html element
Component
Html element with directive

results matching ""

    No results matching ""