Blame view

src/layouts/page/useCache.ts 1.39 KB
vben authored
1
2
3
4
5
6
7
8
9
10
11
12
import { computed, ref, unref } from 'vue';
import { useRootSetting } from '/@/hooks/setting/useRootSetting';
import { tryTsxEmit } from '/@/utils/helper/vueHelper';
import { tabStore, PAGE_LAYOUT_KEY } from '/@/store/modules/tab';

import { useRouter } from 'vue-router';

const ParentLayoutName = 'ParentLayout';
export function useCache(isPage: boolean) {
  const name = ref('');
  const { currentRoute } = useRouter();
vben authored
13
14
  tryTsxEmit((instance) => {
    const routeName = instance.type.name;
vben authored
15
16
17
    if (routeName && ![ParentLayoutName].includes(routeName)) {
      name.value = routeName;
    } else {
18
19
20
21
      const matched = currentRoute.value?.matched;
      if (!matched) {
        return;
      }
vben authored
22
23
24
25
26
      const len = matched.length;
      if (len < 2) return;
      name.value = matched[len - 2].name as string;
    }
  });
vben authored
27
vben authored
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
  const { getOpenKeepAlive } = useRootSetting();

  const getCaches = computed((): string[] => {
    if (!unref(getOpenKeepAlive)) {
      return [];
    }
    const cached = tabStore.getCachedMapState;

    if (isPage) {
      //  page Layout
      return cached.get(PAGE_LAYOUT_KEY) || [];
    }
    const cacheSet = new Set<string>();
    cacheSet.add(unref(name));

    const list = cached.get(unref(name));
vben authored
44
vben authored
45
46
47
48
49
50
    if (!list) {
      return Array.from(cacheSet);
    }
    list.forEach((item) => {
      cacheSet.add(item);
    });
vben authored
51
vben authored
52
53
54
55
    return Array.from(cacheSet);
  });
  return { getCaches };
}