From 24f58bec2d2cf8adbb41258fbbd8d1e8f9047655 Mon Sep 17 00:00:00 2001 From: LynithDev <61880709+LynithDev@users.noreply.github.com> Date: Sun, 27 Oct 2024 13:00:37 +0100 Subject: [PATCH] fix: Unlisten notifications event listener on unmount --- apps/frontend/src/ui/hooks/useNotifications.ts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/apps/frontend/src/ui/hooks/useNotifications.ts b/apps/frontend/src/ui/hooks/useNotifications.ts index 97c3bc62..0846143a 100644 --- a/apps/frontend/src/ui/hooks/useNotifications.ts +++ b/apps/frontend/src/ui/hooks/useNotifications.ts @@ -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; type OnUpdateFn = () => unknown; function useNotifications(onUpdate?: OnUpdateFn): Signal { const [notifications, setNotifications] = createSignal({}); + let unlisten: UnlistenFn | undefined; async function update(id: string, data: NotificationData) { setNotifications((notifications) => { @@ -25,7 +27,11 @@ function useNotifications(onUpdate?: OnUpdateFn): Signal { title: e.payload.event.type.replaceAll('_', ' '), message: e.payload.message, fraction: e.payload.fraction ?? undefined, - })); + })).then(u => unlisten = u); + }); + + onCleanup(() => { + unlisten?.(); }); return [notifications, setNotifications];