moved the event bus to typescript

This commit is contained in:
Sascha Kühl 2025-04-04 07:32:55 +02:00
parent 6d2dc79889
commit 26edfd46fe
2 changed files with 28 additions and 16 deletions

View File

@ -17,9 +17,7 @@ const logout = () => {
const isOnline = ref(navigator.onLine); const isOnline = ref(navigator.onLine);
onMounted(() => { onMounted(() => {
EventBus.on("logout", () => { EventBus.on("logout", logout);
logout();
});
window.addEventListener("online", () => isOnline.value = true); window.addEventListener("online", () => isOnline.value = true);
window.addEventListener("offline", () => isOnline.value = false); window.addEventListener("offline", () => isOnline.value = false);
@ -32,7 +30,7 @@ onMounted(() => {
}); });
onBeforeUnmount(() => { onBeforeUnmount(() => {
EventBus.remove("logout", null); EventBus.remove("logout", logout);
}) })
</script> </script>

View File

@ -1,13 +1,27 @@
const eventBus = { // const eventBus = {
on(event: string, callback: EventListenerOrEventListenerObject) { // on(event: string, callback: EventListenerOrEventListenerObject) {
document.addEventListener(event, (e) => callback(e.detail)); // document.addEventListener(event, (e: CustomEvent) => callback(e.detail));
}, // },
dispatch(event: string, data: any) { // dispatch(event: string, data: any) {
document.dispatchEvent(new CustomEvent(event, { detail: data })); // document.dispatchEvent(new CustomEvent(event, { detail: data }));
}, // },
remove(event: string, callback: EventListenerOrEventListenerObject) { // remove(event: string, callback: EventListenerOrEventListenerObject) {
document.removeEventListener(event, callback); // document.removeEventListener(event, callback);
}, // },
}; // };
export default eventBus; class EventBus {
on<T extends Event>(type: string, listener: (event: T) => void, options?: boolean | AddEventListenerOptions): void {
document.addEventListener(type, listener as EventListener, options);
}
dispatch<T extends Event>(event: T): void {
document.dispatchEvent(event);
}
remove<T extends Event>(type: string, listener: (event: T) => void, options?: boolean | EventListenerOptions): void {
document.removeEventListener(type, listener as EventListener, options);
}
}
export default new EventBus();