useCache.ts 1.3 KB

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