@yamada-ui/react
Version:
React UI components of the Yamada, by the Yamada, for the Yamada built with React and Emotion
46 lines (44 loc) • 1.21 kB
JavaScript
import { useLoading } from "../../components/loading/loading-provider.js";
import { useProcessing } from "../use-processing/index.js";
import { useCallback, useMemo } from "react";
//#region src/hooks/use-async-callback/index.ts
/**
* `useAsyncCallback` is a custom hook used to manage async callbacks.
*
* @see https://yamada-ui.com/docs/hooks/use-async-callback
*/
const useAsyncCallback = (callback, deps, { loading: method = false, loadingOptions, processing: shouldProcessing = true } = {}) => {
const context = useLoading();
const { finish, loading, start } = useProcessing();
const shouldLoading = !!method;
return [
loading,
useCallback(async (...args) => {
try {
if (shouldProcessing) start();
if (shouldLoading) context[method].start(loadingOptions);
return await callback(...args);
} finally {
if (shouldProcessing) finish();
if (shouldLoading) context[method].finish();
}
}, [
...deps,
shouldProcessing,
shouldLoading,
context,
method,
loadingOptions,
callback,
start,
finish
]),
useMemo(() => ({
finish,
start
}), [finish, start])
];
};
//#endregion
export { useAsyncCallback };
//# sourceMappingURL=index.js.map