58 lines
1.4 KiB
TypeScript
58 lines
1.4 KiB
TypeScript
import { computed, ref } from 'vue';
|
|
|
|
/**
|
|
* Глобальный индикатор загрузки. Баннер показывается только если операция длится
|
|
* дольше SHOW_DELAY_MS — чтобы не мигать на быстрых переходах.
|
|
*
|
|
* Перенесено из проекта onboard (Composables/useGlobalLoading.js).
|
|
*/
|
|
const pending = ref(0);
|
|
const visible = ref(false);
|
|
let showTimer: ReturnType<typeof setTimeout> | null = null;
|
|
|
|
const SHOW_DELAY_MS = 3000;
|
|
|
|
const scheduleShow = (): void => {
|
|
if (visible.value || showTimer) {
|
|
return;
|
|
}
|
|
|
|
showTimer = setTimeout(() => {
|
|
showTimer = null;
|
|
|
|
if (pending.value > 0) {
|
|
visible.value = true;
|
|
}
|
|
}, SHOW_DELAY_MS);
|
|
};
|
|
|
|
const cancelScheduledShow = (): void => {
|
|
if (!showTimer) {
|
|
return;
|
|
}
|
|
|
|
clearTimeout(showTimer);
|
|
showTimer = null;
|
|
};
|
|
|
|
export const startGlobalLoading = (): void => {
|
|
pending.value += 1;
|
|
scheduleShow();
|
|
};
|
|
|
|
export const stopGlobalLoading = (): void => {
|
|
pending.value = Math.max(0, pending.value - 1);
|
|
|
|
if (pending.value === 0) {
|
|
cancelScheduledShow();
|
|
visible.value = false;
|
|
}
|
|
};
|
|
|
|
export const useGlobalLoading = () => {
|
|
return {
|
|
isGlobalLoading: computed(() => visible.value),
|
|
pendingCount: computed(() => pending.value),
|
|
};
|
|
};
|