Live location tracking and playback for the game "manhunt"
1import { useEffect } from "react";
2import { events } from "@/bindings";
3import { SWRConfiguration } from "swr";
4
5type ExtractCallback<E extends keyof typeof events> = (
6 payload: Parameters<Parameters<(typeof events)[E]["listen"]>[0]>[0]["payload"]
7) => void;
8
9type SWRConfigTyp = { suspense: true } & SWRConfiguration;
10
11export const sharedSwrConfig: SWRConfigTyp = { suspense: true, dedupingInterval: 100 };
12
13/**
14 * Convenience hook that does useEffect for a Tauri event and handles unsubscribing on unmount
15 */
16export function useTauriEvent<E extends keyof typeof events>(
17 tauriEvent: E,
18 cb: ExtractCallback<E>
19) {
20 useEffect(() => {
21 const unlisten = events[tauriEvent].listen((e) => {
22 cb(e.payload);
23 });
24
25 return () => {
26 unlisten.then((f) => f());
27 };
28 }, [tauriEvent, cb]);
29}