import { Injectable } from '@angular/core' ; import { BehaviorSubject , Observable } from 'rxjs' ; import { ProgressSpinner } from './progress-spinner/progress-spinner.types' ; @ Injectable ( { providedIn : 'root' } ) export class ProgressSpinnerService { public static readonly DEFAULT_TEXT : string = 'Loading' ; private private spinner : { [ id ] : BehaviorSubject < ProgressSpinner > ; } = { } ; param id private getSpinnerInstance ( id = 'app' ) { if ( ! this . spinner [ id ] ) { this . spinner [ id ] = new BehaviorSubject < ProgressSpinner > ( { show : false , text : ProgressSpinnerService . DEFAULT_TEXT } ) ; } return this . spinner [ id ] ; } public getSpinner ( id = 'app' ) { return this . getSpinnerInstance ( id ) . asObservable ( ) ; } public show ( id = 'app' , text ? ) { this . getSpinnerInstance ( id ) . next ( { show : true , text } ) ; } public hide ( id = 'app' ) { this . getSpinnerInstance ( id ) . next ( { show : false } ) ; } }