import { Injectable , Type } from '@angular/core' ; import { Observable , Subscription } from 'rxjs' ; export interface TaskBarOptions { showCloseButton ? : boolean ; taskBarWidth ? : any ; title ? : string ; } export interface ITaskBar { opened : boolean ; open < T > ( component , data , taskBarOptions ) : T ; close ( ) : Promise < any > ; } export interface TaskBarRef < T > { instance : T ; closeWhen ( close ? ) : void ; } export class TaskBarReference < T > implements TaskBarRef < T > { private subscription : Subscription ; constructor ( private taskBar , public instance ) { } public closeWhen ( observable ? ) { if ( observable ) { this . subscription = observable . subscribe ( response => { if ( response === true ) { this . taskBar . close ( ) ; this . subscription . unsubscribe ( ) ; } } ) ; } } public dispose ( ) { if ( this . subscription && ! this . subscription . closed ) { this . subscription . unsubscribe ( ) ; } } } @ Injectable ( { providedIn : 'root' } ) export class TaskBarService { private taskBar : ITaskBar ; private taskBarOpen = false ; private reference : TaskBarReference < any > = null ; public get isOpen ( ) : boolean { return this . taskBarOpen ; } public open < T > ( component , data ? , taskBarOptions ? ) { return new Promise ( ( ( resolve , reject ) => { if ( this . taskBarOpen ) { if ( ! ( this . reference . instance instanceof component ) ) { this . close ( ) . then ( ( ) => { resolve ( this . openInternal ( component , data , taskBarOptions ) ) ; } ) ; return ; } reject ( new Error ( 'Task bar is already open' ) ) ; return ; } resolve ( this . openInternal ( component , data , taskBarOptions ) ) ; return ; } ) ) ; } private openInternal < T > ( component , data ? , taskBarOptions ? ) { if ( ! this . taskBarOpen ) { const instance = this . taskBar . open ( component , data , taskBarOptions ) ; this . reference = new TaskBarReference ( this . taskBar , instance ) ; this . taskBarOpen = true ; return this . reference ; } } public registerTaskBar ( taskBar ) { this . taskBar = taskBar ; } public markAsClosed ( ) { this . taskBarOpen = false ; this . clearReference ( ) ; } public close ( ) { this . markAsClosed ( ) ; return this . taskBar . close ( ) ; } public clearReference ( ) { if ( this . reference ) { this . reference . dispose ( ) ; this . reference = null ; } } }