Blame view

src/layouts/page/useCache.ts 1.37 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
18
19
20
21
22
23
    if (routeName && ![ParentLayoutName].includes(routeName)) {
      name.value = routeName;
    } else {
      const matched = currentRoute.value.matched;
      const len = matched.length;
      if (len < 2) return;
      name.value = matched[len - 2].name as string;
    }
  });
vben authored
24
vben authored
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
  const { getOpenKeepAlive } = useRootSetting();

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

    if (isPage) {
      //  page Layout
      // not parent layout
      return cached.get(PAGE_LAYOUT_KEY) || [];
    }

    const cacheSet = new Set<string>();
    cacheSet.add(unref(name));

    const list = cached.get(unref(name));
    if (!list) {
      return Array.from(cacheSet);
    }
    list.forEach((item) => {
      cacheSet.add(item);
    });
    return Array.from(cacheSet);
  });
  return { getCaches };
}