UNPKG

@ducor/react

Version:

admin template ui interface

35 lines (34 loc) 1.04 kB
import { useCallback, useMemo, useRef } from "react"; import useBoolean from "./use-boolean"; import useUnmountEffect from "./use-unmount-effect"; /** * `useProcessing` is a custom hook for handling processing states. * * @see Docs https://ui.ducor.net/hooks/use-processing */ export var useProcessing = function (init) { var _a = useBoolean(init), loading = _a[0], _b = _a[1], off = _b.off, on = _b.on; var countRef = useRef(0); var start = useCallback(function () { countRef.current += 1; on(); }, [on]); var finish = useCallback(function () { countRef.current -= 1; if (countRef.current <= 0) off(); }, [off]); useUnmountEffect(function () { countRef.current = 0; }); var controls = useMemo(function () { return ({ finish: finish, /** * @deprecated Use `loading` instead. */ isLoading: loading, loading: loading, start: start, }); }, [finish, loading, start]); return controls; };