mirror of
https://github.com/monkeytypegame/monkeytype.git
synced 2025-02-25 15:24:03 +08:00
added notification event to avoid circular dep
This commit is contained in:
parent
2081f29f32
commit
fcebba7adc
3 changed files with 35 additions and 8 deletions
|
@ -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);
|
||||
});
|
||||
|
|
|
@ -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);
|
||||
|
|
26
frontend/src/ts/observables/notification-event.ts
Normal file
26
frontend/src/ts/observables/notification-event.ts
Normal file
|
@ -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);
|
||||
}
|
||||
});
|
||||
}
|
Loading…
Reference in a new issue