You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
21 lines
589 B
TypeScript
21 lines
589 B
TypeScript
import {useEffect, useRef, useCallback} from 'react'
|
|
import { EventCallback, listen, UnlistenFn } from "@tauri-apps/api/event";
|
|
|
|
export default function useTauriEvent<T>(eventName : string, callback : EventCallback<T>, depArray : any[]) {
|
|
const unlistenPromiseRef = useRef<Promise<UnlistenFn>>()
|
|
|
|
const stableCallback = useCallback(callback, depArray)
|
|
|
|
useEffect(() => {
|
|
unlistenPromiseRef.current = listen(eventName, stableCallback)
|
|
|
|
return () => {
|
|
unlistenPromiseRef.current?.then(unlisten => {
|
|
unlisten()
|
|
})
|
|
}
|
|
}, [eventName, stableCallback])
|
|
}
|
|
|
|
|