fix: Unlisten notifications event listener on unmount
This commit is contained in:
parent
1539062d36
commit
24f58bec2d
1 changed files with 8 additions and 2 deletions
|
|
@ -1,12 +1,14 @@
|
||||||
|
import type { UnlistenFn } from '@tauri-apps/api/event';
|
||||||
import type { NotificationData } from '~ui/components/overlay/notifications/NotificationComponent';
|
import type { NotificationData } from '~ui/components/overlay/notifications/NotificationComponent';
|
||||||
import { events } from '@onelauncher/client/bindings';
|
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 Notifications = Record<string, NotificationData>;
|
||||||
type OnUpdateFn = () => unknown;
|
type OnUpdateFn = () => unknown;
|
||||||
|
|
||||||
function useNotifications(onUpdate?: OnUpdateFn): Signal<Notifications> {
|
function useNotifications(onUpdate?: OnUpdateFn): Signal<Notifications> {
|
||||||
const [notifications, setNotifications] = createSignal<Notifications>({});
|
const [notifications, setNotifications] = createSignal<Notifications>({});
|
||||||
|
let unlisten: UnlistenFn | undefined;
|
||||||
|
|
||||||
async function update(id: string, data: NotificationData) {
|
async function update(id: string, data: NotificationData) {
|
||||||
setNotifications((notifications) => {
|
setNotifications((notifications) => {
|
||||||
|
|
@ -25,7 +27,11 @@ function useNotifications(onUpdate?: OnUpdateFn): Signal<Notifications> {
|
||||||
title: e.payload.event.type.replaceAll('_', ' '),
|
title: e.payload.event.type.replaceAll('_', ' '),
|
||||||
message: e.payload.message,
|
message: e.payload.message,
|
||||||
fraction: e.payload.fraction ?? undefined,
|
fraction: e.payload.fraction ?? undefined,
|
||||||
}));
|
})).then(u => unlisten = u);
|
||||||
|
});
|
||||||
|
|
||||||
|
onCleanup(() => {
|
||||||
|
unlisten?.();
|
||||||
});
|
});
|
||||||
|
|
||||||
return [notifications, setNotifications];
|
return [notifications, setNotifications];
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue