refactor: 重构 useFullscreenLoading 代码细节

This commit is contained in:
pany 2024-11-25 10:40:24 +08:00
parent f121c01227
commit 592fa82650

View File

@ -1,15 +1,6 @@
import type { LoadingOptions } from "element-plus" import type { LoadingOptions } from "element-plus"
import { ElLoading } from "element-plus" import { ElLoading } from "element-plus"
const defaultOptions = {
lock: true,
text: "加载中..."
}
interface LoadingInstance {
close: () => void
}
interface UseFullscreenLoading { interface UseFullscreenLoading {
<T extends (...args: Parameters<T>) => ReturnType<T>>( <T extends (...args: Parameters<T>) => ReturnType<T>>(
fn: T, fn: T,
@ -17,8 +8,17 @@ interface UseFullscreenLoading {
): (...args: Parameters<T>) => Promise<ReturnType<T>> ): (...args: Parameters<T>) => Promise<ReturnType<T>>
} }
interface LoadingInstance {
close: () => void
}
const DEFAULT_OPTIONS = {
lock: true,
text: "加载中..."
}
/** /**
* fnloading * fnLoading
* @param fn * @param fn
* @param options LoadingOptions * @param options LoadingOptions
* @returns Promise * @returns Promise
@ -27,10 +27,10 @@ export const useFullscreenLoading: UseFullscreenLoading = (fn, options = {}) =>
let loadingInstance: LoadingInstance let loadingInstance: LoadingInstance
return async (...args) => { return async (...args) => {
try { try {
loadingInstance = ElLoading.service({ ...defaultOptions, ...options }) loadingInstance = ElLoading.service({ ...DEFAULT_OPTIONS, ...options })
return await fn(...args) return await fn(...args)
} finally { } finally {
loadingInstance?.close() loadingInstance.close()
} }
} }
} }