useCache.ts 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import type { FunctionalComponent } from 'vue';
  2. import type { RouteLocation } from 'vue-router';
  3. import { computed, ref, unref } from 'vue';
  4. import { useRootSetting } from '/@/hooks/setting/useRootSetting';
  5. import { tryTsxEmit } from '/@/utils/helper/vueHelper';
  6. import { tabStore, PAGE_LAYOUT_KEY } from '/@/store/modules/tab';
  7. import { useRouter } from 'vue-router';
  8. const ParentLayoutName = 'ParentLayout';
  9. export function getKey(component: FunctionalComponent & { type: Indexable }, route: RouteLocation) {
  10. return !!component?.type.parentView ? {} : { key: route.fullPath };
  11. }
  12. export function useCache(isPage: boolean) {
  13. const name = ref('');
  14. const { currentRoute } = useRouter();
  15. tryTsxEmit((instance) => {
  16. const routeName = instance.type.name;
  17. if (routeName && ![ParentLayoutName].includes(routeName)) {
  18. name.value = routeName;
  19. } else {
  20. const matched = currentRoute.value?.matched;
  21. if (!matched) {
  22. return;
  23. }
  24. const len = matched.length;
  25. if (len < 2) return;
  26. name.value = matched[len - 2].name as string;
  27. }
  28. });
  29. const { getOpenKeepAlive } = useRootSetting();
  30. const getCaches = computed((): string[] => {
  31. if (!unref(getOpenKeepAlive)) {
  32. return [];
  33. }
  34. const cached = tabStore.getCachedMapState;
  35. if (isPage) {
  36. // page Layout
  37. return cached.get(PAGE_LAYOUT_KEY) || [];
  38. }
  39. const cacheSet = new Set<string>();
  40. cacheSet.add(unref(name));
  41. const list = cached.get(unref(name));
  42. if (!list) {
  43. return Array.from(cacheSet);
  44. }
  45. list.forEach((item) => {
  46. cacheSet.add(item);
  47. });
  48. return Array.from(cacheSet);
  49. });
  50. return { getCaches };
  51. }