import { useEffect, useState } from "react";
export const useAsyncEffect = (fn, deps, updateInterval) => {
    useEffect(() => {
        let unsub;
        const timeout = setTimeout(async () => {
            unsub = await fn();
        }, updateInterval ?? 20);
        return () => {
            if (unsub) {
                unsub();
            }
            clearTimeout(timeout);
        };
    }, deps);
};
export const useAsyncMemo = (fn, deps) => {
    const [data, setData] = useState();
    const [error, setError] = useState();
    const [pending, setPending] = useState(false);
    useEffect(() => {
        let canceled = false;
        setError(undefined);
        setPending(true);
        fn()
            .then((data) => {
            if (!canceled) {
                setData(data);
            }
        })
            .catch((e) => {
            if (!canceled) {
                setError(e);
            }
        })
            .finally(() => {
            setPending(false);
        });
        return () => {
            canceled = true;
        };
    }, deps);
    return { data, pending, error };
};
//# sourceMappingURL=async.js.map