import { Subject } from "rxjs"; import { filter } from "rxjs/operators"; export const notificationService = { onAlert, success, error, info, warn, alert, clear, }; export const NotificationType = { Success: "Success", Error: "Error", Info: "Info", Warning: "Warning", }; const alertSubject = new Subject(); const defaultId = "default-notif"; // enable subscribing to alerts observable function onAlert(id = defaultId) { return alertSubject.asObservable().pipe(filter((x) => x && x.id === id)); } // convenience methods function success(title,message, options) { alert({ ...options, type: NotificationType.Success, message, title }); } function error(title, message, options) { alert({ ...options, type: NotificationType.Error, message, title }); } function info(title,message, options) { alert({ ...options, type: NotificationType.Info, message, title }); } function warn(title,message, options) { alert({ ...options, type: NotificationType.Warning, message, title }); } // core alert method function alert(alert) { alert.id = alert.id || defaultId; alert.autoClose = alert.autoClose === undefined ? true : alert.autoClose; alertSubject.next(alert); } // clear alerts function clear(id = defaultId) { alertSubject.next({ id }); }