38 lines
1,021 B
TypeScript
38 lines
1,021 B
TypeScript
|
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<Notification> = new Subject();
|
||
|
private readonly subscribers: Map<string, Subscription> = new Map<string, Subscription>();
|
||
|
|
||
|
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();
|
||
|
}
|
||
|
}
|