fix: Unlisten notifications event listener on unmount

This commit is contained in:
LynithDev 2024-10-27 13:00:37 +01:00
parent 1539062d36
commit 24f58bec2d

View file

@ -1,12 +1,14 @@
import type { UnlistenFn } from '@tauri-apps/api/event';
import type { NotificationData } from '~ui/components/overlay/notifications/NotificationComponent';
import { events } from '@onelauncher/client/bindings';
import { createSignal, onMount, type Signal } from 'solid-js';
import { createSignal, onCleanup, onMount, type Signal } from 'solid-js';
type Notifications = Record<string, NotificationData>;
type OnUpdateFn = () => unknown;
function useNotifications(onUpdate?: OnUpdateFn): Signal<Notifications> {
const [notifications, setNotifications] = createSignal<Notifications>({});
let unlisten: UnlistenFn | undefined;
async function update(id: string, data: NotificationData) {
setNotifications((notifications) => {
@ -25,7 +27,11 @@ function useNotifications(onUpdate?: OnUpdateFn): Signal<Notifications> {
title: e.payload.event.type.replaceAll('_', ' '),
message: e.payload.message,
fraction: e.payload.fraction ?? undefined,
}));
})).then(u => unlisten = u);
});
onCleanup(() => {
unlisten?.();
});
return [notifications, setNotifications];