Files
web-temp/layers/stores/useLoadingStore.ts

87 lines
2.0 KiB
TypeScript

import { defineStore } from 'pinia'
export const useLoadingStore = defineStore('loadingStore', () => {
// 글로벌 로딩 표기
const fullLoading = ref(true)
const hasApiCallStarted = ref(false)
const isPAssApiLoading = ref(false)
// 컴포넌트별 로딩 표기 - Map 대신 일반 객체 사용
const localLoadings = ref<Record<string, { active: boolean }>>({})
const apiLoadingTimeoutId = ref<ReturnType<typeof setTimeout> | null>(null)
/**
* 로딩 상태 초기화
*/
const initializeStore = () => {
localLoadings.value = {}
hasApiCallStarted.value = false
isPAssApiLoading.value = false
}
/**
* Full 로딩
*/
const startFullLoading = () => {
initializeStore()
fullLoading.value = true
}
const startApiLoading = () => {
hasApiCallStarted.value = true
isPAssApiLoading.value = false
}
const stopFullLoading = () => {
if (!hasApiCallStarted.value || isPAssApiLoading.value) {
fullLoading.value = false
}
}
const finishApiLoading = () => {
// 이전 타이머가 있으면 정리
if (apiLoadingTimeoutId.value) {
clearTimeout(apiLoadingTimeoutId.value)
apiLoadingTimeoutId.value = null
}
apiLoadingTimeoutId.value = setTimeout(() => {
hasApiCallStarted.value = false
isPAssApiLoading.value = true
apiLoadingTimeoutId.value = null
}, 300)
}
/**
* Local 로딩
*/
const startLocalLoading = (localId: string) => {
localLoadings.value[localId] = { active: true }
}
const stopLocalLoading = (localId: string) => {
if (localLoadings.value[localId]) {
localLoadings.value[localId].active = false
}
}
const isLocalLoading = (localId: string) => {
return !!localLoadings.value[localId]?.active
}
return {
fullLoading,
localLoadings,
hasApiCallStarted,
isPAssApiLoading,
startApiLoading,
finishApiLoading,
initializeStore,
startFullLoading,
stopFullLoading,
startLocalLoading,
stopLocalLoading,
isLocalLoading,
}
})