import { Injectable } from '@angular/core' ; import { Observable , ReplaySubject } from 'rxjs' ; import { BlockUIActions } from '../constants/block-ui-actions.constant' ; import { BlockUIDefaultName } from '../constants/block-ui-default-name.constant' ; import { NgBlockUI } from '../models/block-ui.model' ; import { BlockUISettings } from '../models/block-ui-settings.model' ; import { BlockUIEvent } from '../models/block-ui-event.model' ; @ Injectable ( ) export class BlockUIInstanceService { blockUISettings : BlockUISettings | any = { } ; blockUIInstances : any = { } ; private blockUISubject : ReplaySubject < any > = new ReplaySubject ( 1 ) ; private blockUIObservable : Observable < any > = this . blockUISubject . asObservable ( ) ; constructor ( ) { this . blockUIObservable . subscribe ( this . blockUIMiddleware . bind ( this ) ) ; } getSettings ( ) { return this . blockUISettings ; } updateSettings ( settings = { } ) { this . blockUISettings = { ... this . blockUISettings , ... settings } ; } decorate ( name = BlockUIDefaultName ) { const blockUI = { name , isActive : false , blockCount : 0 , start : this . dispatch ( this . blockUISubject , BlockUIActions . START , name ) , update : this . dispatch ( this . blockUISubject , BlockUIActions . UPDATE , name ) , stop : this . dispatch ( this . blockUISubject , BlockUIActions . STOP , name ) , reset : this . dispatch ( this . blockUISubject , BlockUIActions . RESET , name ) , resetGlobal : this . dispatch ( this . blockUISubject , BlockUIActions . RESET_GLOBAL , name ) , unsubscribe : this . dispatch ( this . blockUISubject , BlockUIActions . UNSUBSCRIBE , name ) } as NgBlockUI ; this . blockUIInstances [ name ] = this . blockUIInstances [ name ] || blockUI ; return blockUI ; } observe ( ) { return this . blockUIObservable ; } clearInstance ( instanceName ) { this . dispatch ( this . blockUISubject , BlockUIActions . RESET , instanceName ) ; } private blockUIMiddleware ( { action , name } ) { let isActive = null ; switch ( action ) { case ( BlockUIActions . START ) : isActive = true ; break ; case ( BlockUIActions . STOP ) : case ( BlockUIActions . RESET ) : isActive = false ; break ; } if ( isActive !== null ) { this . blockUIInstances [ name ] . isActive = isActive ; } } private dispatch ( subject , action , name = BlockUIDefaultName ) { return ( message ? ) : void => { subject . next ( { name , action , message } ) ; } ; } }