mirror of
https://github.com/Zagrios/bs-manager.git
synced 2026-07-03 14:08:25 +02:00
81 lines
2.4 KiB
TypeScript
81 lines
2.4 KiB
TypeScript
import { BehaviorSubject } from "rxjs";
|
|
import { v4 as uuidv4 } from 'uuid';
|
|
|
|
export class NotificationService{
|
|
|
|
private static instance: NotificationService;
|
|
|
|
public readonly notifications$: BehaviorSubject<ResolvableNotification[]>;
|
|
|
|
public static getInstance(): NotificationService{
|
|
if(!NotificationService.instance){ NotificationService.instance = new NotificationService(); }
|
|
return NotificationService.instance;
|
|
}
|
|
|
|
private constructor(){
|
|
this.notifications$ = new BehaviorSubject<ResolvableNotification[]>([]);
|
|
}
|
|
|
|
public notify(notification: Notification): Promise<NotificationResult|string>{
|
|
const resovableNotification: ResolvableNotification = {id: uuidv4(), notification, resolver: null };
|
|
const promise = new Promise<NotificationResult|string>(resolve => {
|
|
resovableNotification.resolver = resolve;
|
|
setTimeout(() => resolve(NotificationResult.NO_CHOICE), notification.duration || 7000);
|
|
});
|
|
|
|
this.notifications$.next([...this.notifications$.value, resovableNotification]);
|
|
|
|
promise.then(() => {
|
|
this.notifications$.next(this.notifications$.value.filter(n => n.id !== resovableNotification.id));
|
|
})
|
|
|
|
return promise;
|
|
}
|
|
|
|
public notifyError(notification: Notification): Promise<NotificationResult|string>{
|
|
notification.type = NotificationType.ERROR;
|
|
return this.notify(notification);
|
|
}
|
|
|
|
public notifyWarning(notification: Notification): Promise<NotificationResult|string>{
|
|
notification.type = NotificationType.WARNING;
|
|
return this.notify(notification);
|
|
}
|
|
|
|
public notifySuccess(notification: Notification): Promise<NotificationResult|string>{
|
|
notification.type = NotificationType.SUCCESS;
|
|
return this.notify(notification);
|
|
}
|
|
|
|
}
|
|
|
|
export interface Notification {
|
|
title: string,
|
|
type?: NotificationType
|
|
desc?: string,
|
|
actions?: NotificationAction[],
|
|
duration?: number,
|
|
}
|
|
|
|
export enum NotificationType {
|
|
SUCCESS = 0,
|
|
WARNING = 1,
|
|
ERROR = 2,
|
|
}
|
|
|
|
export interface NotificationAction {
|
|
id: string,
|
|
title: string,
|
|
cancel?: boolean
|
|
}
|
|
|
|
export enum NotificationResult {
|
|
NO_CHOICE = "no_choice",
|
|
CLOSE = "close",
|
|
}
|
|
|
|
interface ResolvableNotification{
|
|
id: string,
|
|
resolver: (value: NotificationResult|string) => void,
|
|
notification: Notification
|
|
} |