Files
bio_frontend/composables/useLoading.ts

66 lines
1.9 KiB
TypeScript
Raw Permalink Normal View History

/**
*
* .
*/
export const useLoading = () => {
const loadingStore = useLoadingStore();
/**
* .
* , .
*
* @param asyncFn
* @param message ()
* @returns Promise<T>
*/
const withLoading = async <T>(
asyncFn: () => Promise<T>,
message?: string
): Promise<T> => {
loadingStore.startLoading(message);
try {
return await asyncFn();
} finally {
loadingStore.stopLoading(message);
}
};
/**
* .
*
* @param message ()
* @returns
*/
const startLoading = (message?: string) => {
loadingStore.startLoading(message);
return () => loadingStore.stopLoading(message);
};
/**
* , .
*
* @param message
* @returns
*/
const withMessage = (message: string) => {
loadingStore.startLoading(message);
return () => loadingStore.stopLoading(message);
};
return {
// 상태
isLoading: computed(() => loadingStore.isLoading),
loadingCount: computed(() => loadingStore.loadingCount),
currentMessage: computed(() => loadingStore.currentMessage),
loadingMessages: computed(() => loadingStore.loadingMessages),
// 액션
startLoading,
stopLoading: loadingStore.stopLoading,
withLoading,
withMessage,
clearAll: loadingStore.clearAllLoading,
reset: loadingStore.reset,
};
};