import Bus from '@/utils/eventBus/index';
import { onActivated, onUnmounted } from 'vue';

let isLoadComplete = false;

// 未缓存页初始化数据
export const initData = (callback: Function): void => {
    // 刷新整个页面时候，需要等数据加载数据
    const cb = () => {
        if (isLoadComplete) {
            callback && callback();
        }
    };
    Bus.$on('loadComplete', cb);
    if (isLoadComplete) {
        callback && callback();
    }
    onUnmounted(() => {
        Bus.$off('loadComplete', cb);
    });
};

// 缓存页初始化数据
export const cacheInitData = (callback: Function): void => {
    // 刷新整个页面时候，需要等数据加载数据
    const cb = () => {
        if (isLoadComplete) {
            callback && callback();
        }
    };
    Bus.$on('loadComplete', cb);
    onActivated(() => {
        if (isLoadComplete) {
            callback && callback();
        }
    });
    onUnmounted(() => {
        Bus.$off('loadComplete', cb);
    });
};

// 存储加载状态数据
export const setLoadComplete = (status: boolean) => {
    isLoadComplete = status;
};

export const getLoadIsComplete = (): boolean => {
    return isLoadComplete
}