Blame view

src/layouts/page/useCache.ts 1.66 KB
1
2
import type { FunctionalComponent } from 'vue';
import type { RouteLocation } from 'vue-router';
vben authored
3
4
5
6
7
8
9
10
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';
11
12
13
14
15

export function getKey(component: FunctionalComponent & { type: Indexable }, route: RouteLocation) {
  return !!component?.type.parentView ? {} : { key: route.fullPath };
}
vben authored
16
17
18
19
export function useCache(isPage: boolean) {
  const name = ref('');
  const { currentRoute } = useRouter();
vben authored
20
21
  tryTsxEmit((instance) => {
    const routeName = instance.type.name;
vben authored
22
23
24
    if (routeName && ![ParentLayoutName].includes(routeName)) {
      name.value = routeName;
    } else {
25
26
27
28
      const matched = currentRoute.value?.matched;
      if (!matched) {
        return;
      }
vben authored
29
30
31
32
33
      const len = matched.length;
      if (len < 2) return;
      name.value = matched[len - 2].name as string;
    }
  });
vben authored
34
vben authored
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
  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
51
vben authored
52
53
54
55
56
57
    if (!list) {
      return Array.from(cacheSet);
    }
    list.forEach((item) => {
      cacheSet.add(item);
    });
vben authored
58
vben authored
59
60
61
62
    return Array.from(cacheSet);
  });
  return { getCaches };
}