import { Injectable } from '@angular/core'; import { Subject, Subscription } from 'rxjs'; export interface Notification { msg: string; type: NotificationType; } export enum NotificationType { Information = 'info', Error = 'error', } export type NotificationCallback = (notification: Notification) => void; @Injectable({ providedIn: 'root' }) export class NotificationService { private readonly bus: Subject = new Subject(); private readonly subscribers: Map = new Map(); subscribe(subscriberId: string, callback: NotificationCallback): void { this.subscribers.set( subscriberId, this.bus.subscribe({ next: callback }) ); } publish(msg: string, type: NotificationType = NotificationType.Information) { this.bus.next({ msg, type }); } unsubscribe(subscriberId: string) { this.subscribers.get(subscriberId)?.unsubscribe(); } }