2026-05-23 18:44:57 +08:00
|
|
|
/**
|
|
|
|
|
* Purpose: LoadingInstance supports action-loading.ts by holding or executing the loading instance page or API role.
|
|
|
|
|
* Usage: import, call, read, or update this type from the owning page/module while preserving the surrounding loading and permission flow.
|
|
|
|
|
*/
|
2026-05-13 12:43:01 +08:00
|
|
|
import { ElLoading, type LoadingInstance } from 'element-plus'
|
2026-05-15 21:10:41 +08:00
|
|
|
import 'element-plus/es/components/loading/style/css'
|
2026-05-13 12:43:01 +08:00
|
|
|
|
2026-05-23 18:44:57 +08:00
|
|
|
/**
|
|
|
|
|
* Purpose: GlobalLoadingOptions supports action-loading.ts by holding or executing the global loading options page or API role.
|
|
|
|
|
* Usage: import, call, read, or update this interface from the owning page/module while preserving the surrounding loading and permission flow.
|
|
|
|
|
*/
|
2026-05-13 12:43:01 +08:00
|
|
|
export interface GlobalLoadingOptions {
|
|
|
|
|
text?: string
|
|
|
|
|
lock?: boolean
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-23 18:44:57 +08:00
|
|
|
/**
|
|
|
|
|
* Purpose: activeLoadingCount supports action-loading.ts by holding or executing the active loading count page or API role.
|
|
|
|
|
* Usage: import, call, read, or update this let from the owning page/module while preserving the surrounding loading and permission flow.
|
|
|
|
|
*/
|
2026-05-13 12:43:01 +08:00
|
|
|
let activeLoadingCount = 0
|
|
|
|
|
let loadingInstance: LoadingInstance | null = null
|
|
|
|
|
|
2026-05-23 18:44:57 +08:00
|
|
|
/**
|
|
|
|
|
* Purpose: openGlobalLoading supports action-loading.ts by holding or executing the open global loading page or API role.
|
|
|
|
|
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
|
|
|
|
|
*/
|
2026-05-13 12:43:01 +08:00
|
|
|
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)'
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-23 18:44:57 +08:00
|
|
|
/**
|
|
|
|
|
* Purpose: closeGlobalLoading supports action-loading.ts by holding or executing the close global loading page or API role.
|
|
|
|
|
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
|
|
|
|
|
*/
|
2026-05-13 12:43:01 +08:00
|
|
|
const closeGlobalLoading = () => {
|
|
|
|
|
activeLoadingCount = Math.max(0, activeLoadingCount - 1)
|
|
|
|
|
|
|
|
|
|
if (activeLoadingCount > 0) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
loadingInstance?.close()
|
|
|
|
|
loadingInstance = null
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-23 18:44:57 +08:00
|
|
|
/**
|
|
|
|
|
* Purpose: runWithGlobalLoading supports action-loading.ts by holding or executing the run with global loading page or API role.
|
|
|
|
|
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
|
|
|
|
|
*/
|
2026-05-13 12:43:01 +08:00
|
|
|
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()
|
|
|
|
|
}
|
|
|
|
|
}
|