mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-01-16 14:06:49 +01:00
23 lines
677 B
TypeScript
23 lines
677 B
TypeScript
import type { EventCallback } from '@tauri-apps/api/event';
|
|
import { listen as tauriListen } from '@tauri-apps/api/event';
|
|
import type { DependencyList } from 'react';
|
|
import { useEffect } from 'react';
|
|
|
|
export function useTauriEvent<T>(event: string, fn: EventCallback<T>, deps: DependencyList = []) {
|
|
useEffect(() => {
|
|
let unMounted = false;
|
|
let unsubFn: (() => void) | undefined = undefined;
|
|
|
|
tauriListen(event, fn).then((unsub) => {
|
|
if (unMounted) unsub();
|
|
else unsubFn = unsub;
|
|
});
|
|
|
|
return () => {
|
|
unMounted = true;
|
|
unsubFn?.();
|
|
};
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
}, [event, fn, ...deps]);
|
|
}
|