moved the event bus to typescript
This commit is contained in:
parent
6d2dc79889
commit
26edfd46fe
|
|
@ -17,9 +17,7 @@ const logout = () => {
|
|||
const isOnline = ref(navigator.onLine);
|
||||
|
||||
onMounted(() => {
|
||||
EventBus.on("logout", () => {
|
||||
logout();
|
||||
});
|
||||
EventBus.on("logout", logout);
|
||||
|
||||
window.addEventListener("online", () => isOnline.value = true);
|
||||
window.addEventListener("offline", () => isOnline.value = false);
|
||||
|
|
@ -32,7 +30,7 @@ onMounted(() => {
|
|||
});
|
||||
|
||||
onBeforeUnmount(() => {
|
||||
EventBus.remove("logout", null);
|
||||
EventBus.remove("logout", logout);
|
||||
})
|
||||
</script>
|
||||
|
||||
|
|
|
|||
|
|
@ -1,13 +1,27 @@
|
|||
const eventBus = {
|
||||
on(event: string, callback: EventListenerOrEventListenerObject) {
|
||||
document.addEventListener(event, (e) => callback(e.detail));
|
||||
},
|
||||
dispatch(event: string, data: any) {
|
||||
document.dispatchEvent(new CustomEvent(event, { detail: data }));
|
||||
},
|
||||
remove(event: string, callback: EventListenerOrEventListenerObject) {
|
||||
document.removeEventListener(event, callback);
|
||||
},
|
||||
};
|
||||
// const eventBus = {
|
||||
// on(event: string, callback: EventListenerOrEventListenerObject) {
|
||||
// document.addEventListener(event, (e: CustomEvent) => callback(e.detail));
|
||||
// },
|
||||
// dispatch(event: string, data: any) {
|
||||
// document.dispatchEvent(new CustomEvent(event, { detail: data }));
|
||||
// },
|
||||
// remove(event: string, callback: EventListenerOrEventListenerObject) {
|
||||
// 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();
|
||||
|
|
|
|||
Loading…
Reference in New Issue