From fcebba7adcdd33cb5f2cc74579d8df0752751539 Mon Sep 17 00:00:00 2001 From: Miodec Date: Sat, 3 Sep 2022 14:45:23 +0200 Subject: [PATCH] added notification event to avoid circular dep --- frontend/src/ts/elements/alerts.ts | 7 ++++- frontend/src/ts/elements/notifications.ts | 10 +++---- .../src/ts/observables/notification-event.ts | 26 +++++++++++++++++++ 3 files changed, 35 insertions(+), 8 deletions(-) create mode 100644 frontend/src/ts/observables/notification-event.ts diff --git a/frontend/src/ts/elements/alerts.ts b/frontend/src/ts/elements/alerts.ts index aad4ae2d4..ca833d4fd 100644 --- a/frontend/src/ts/elements/alerts.ts +++ b/frontend/src/ts/elements/alerts.ts @@ -3,6 +3,7 @@ import Ape from "../ape"; import { Auth } from "../firebase"; import * as AccountButton from "../elements/account-button"; import * as DB from "../db"; +import * as NotificationEvent from "../observables/notification-event"; let accountAlerts: MonkeyTypes.MonkeyMail[] = []; @@ -208,7 +209,7 @@ export function addPSA(message: string, level: number): void { `); } -export function addNotification( +function addNotification( message: string, level: number, customTitle?: string @@ -305,3 +306,7 @@ $(document).on("keydown", (e) => { hide(); } }); + +NotificationEvent.subscribe((message, level, customTitle) => { + addNotification(message, level, customTitle); +}); diff --git a/frontend/src/ts/elements/notifications.ts b/frontend/src/ts/elements/notifications.ts index 830981bd3..5ab64790b 100644 --- a/frontend/src/ts/elements/notifications.ts +++ b/frontend/src/ts/elements/notifications.ts @@ -1,7 +1,8 @@ import { debounce } from "throttle-debounce"; import * as Misc from "../utils/misc"; import * as BannerEvent from "../observables/banner-event"; -import * as Alerts from "./alerts"; +// import * as Alerts from "./alerts"; +import * as NotificationEvent from "../observables/notification-event"; function updateMargin(): void { console.log("updating margin"); @@ -232,9 +233,7 @@ export function add( closeCallback?: () => void, allowHTML?: boolean ): void { - // notificationHistory.push( - - Alerts.addNotification(message, level, customTitle); + NotificationEvent.dispatch(message, level, customTitle); new Notification( "notification", @@ -246,7 +245,6 @@ export function add( closeCallback, allowHTML ).show(); - // ); } export function addBanner( @@ -257,7 +255,6 @@ export function addBanner( closeCallback?: () => void, allowHTML?: boolean ): void { - // notificationHistory.push( new Notification( "banner", message, @@ -268,7 +265,6 @@ export function addBanner( closeCallback, allowHTML ).show(); - // ); } const debouncedMarginUpdate = debounce(100, updateMargin); diff --git a/frontend/src/ts/observables/notification-event.ts b/frontend/src/ts/observables/notification-event.ts new file mode 100644 index 000000000..ea4fdf9cd --- /dev/null +++ b/frontend/src/ts/observables/notification-event.ts @@ -0,0 +1,26 @@ +type SubscribeFunction = ( + message: string, + level: number, + customTitle?: string +) => void; + +const subscribers: SubscribeFunction[] = []; + +export function subscribe(fn: SubscribeFunction): void { + subscribers.push(fn); +} + +export function dispatch( + message: string, + level: number, + customTitle?: string +): void { + subscribers.forEach((fn) => { + try { + fn(message, level, customTitle); + } catch (e) { + console.error("Notification event subscriber threw an error"); + console.error(e); + } + }); +}