import { Injectable } from '@angular/core' ; import { Observable , Subject } from 'rxjs' ; export interface FormToast { type ? : 'success' | 'error' | 'warning' | 'info' | null ; message ? : string ; dismissed ? : boolean ; } @ Injectable ( ) export class FormToastService { toasts : FormToast [ ] = [ ] ; timeoutIds : number [ ] = [ ] ; private subject = new Subject < FormToast > ( ) ; constructor ( ) { } onToast ( ) { return this . subject . asObservable ( ) ; } success ( message ) { const toast = { message : message , type : 'success' , } ; this . trigger ( toast ) ; } error ( message ) { const toast = { message : message , type : 'error' , } ; this . trigger ( toast ) ; } warn ( message ) { const toast = { message : message , type : 'warning' , } ; this . trigger ( toast ) ; } inform ( message ) { const toast = { message : message , type : 'info' , } ; this . trigger ( toast ) ; } trigger ( toast ) { if ( ! toast . type ) { toast . type = 'info' ; } this . subject . next ( toast ) ; } isToastActive ( message ) { return ( this . toasts . findIndex ( value => value . message === message && ! value . dismissed ) !== - 1 ) ; } }