import { Injectable } from '@angular/core'; import { BehaviorSubject, Observable } from 'rxjs'; export interface Toast { message: string; type: 'success' | 'error'; } @Injectable({ providedIn: 'root' }) export class ToastService { private readonly subject = new BehaviorSubject(null); readonly current$: Observable = this.subject.asObservable(); private timer: ReturnType | null = null; show(message: string, type: 'success' | 'error' = 'success', duration = 3000): void { if (this.timer !== null) { clearTimeout(this.timer); } this.subject.next({ message, type }); this.timer = setTimeout(() => { this.subject.next(null); this.timer = null; }, duration); } }