功能:更新消息中心和 YumiNail 模块
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
import { ElLoading, type LoadingInstance } from 'element-plus'
|
||||
|
||||
export interface GlobalLoadingOptions {
|
||||
text?: string
|
||||
lock?: boolean
|
||||
}
|
||||
|
||||
let activeLoadingCount = 0
|
||||
let loadingInstance: LoadingInstance | null = null
|
||||
|
||||
const openGlobalLoading = (options?: GlobalLoadingOptions) => {
|
||||
activeLoadingCount += 1
|
||||
|
||||
if (typeof window === 'undefined' || loadingInstance) {
|
||||
return
|
||||
}
|
||||
|
||||
loadingInstance = ElLoading.service({
|
||||
lock: options?.lock ?? true,
|
||||
text: options?.text ?? '\u52a0\u8f7d\u4e2d',
|
||||
background: 'rgba(255, 255, 255, 0.72)'
|
||||
})
|
||||
}
|
||||
|
||||
const closeGlobalLoading = () => {
|
||||
activeLoadingCount = Math.max(0, activeLoadingCount - 1)
|
||||
|
||||
if (activeLoadingCount > 0) {
|
||||
return
|
||||
}
|
||||
|
||||
loadingInstance?.close()
|
||||
loadingInstance = null
|
||||
}
|
||||
|
||||
export const runWithGlobalLoading = async <T>(
|
||||
task: (() => Promise<T>) | Promise<T>,
|
||||
options?: GlobalLoadingOptions
|
||||
) => {
|
||||
openGlobalLoading(options)
|
||||
|
||||
try {
|
||||
if (typeof task === 'function') {
|
||||
return await task()
|
||||
}
|
||||
|
||||
return await task
|
||||
} finally {
|
||||
closeGlobalLoading()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user