cache.ts 515 B

12345678910111213141516171819202122232425262728293031323334353637
  1. import { reactive, watch } from "vue";
  2. import { isDark } from "../theme";
  3. import { router } from "../router";
  4. type CacheData = {
  5. key: number;
  6. };
  7. type UseCache = {
  8. cache: CacheData;
  9. };
  10. export const useCache = (source: () => any[]): UseCache => {
  11. const id = router.path();
  12. const cache = reactive<CacheData>({
  13. key: 0
  14. });
  15. const update = () => {
  16. if (id == router.path()) {
  17. cache.key++;
  18. }
  19. };
  20. watch(source, () => {
  21. update();
  22. });
  23. watch(isDark, () => {
  24. update();
  25. });
  26. return {
  27. cache
  28. };
  29. };