viewStack.ts 829 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import {
  2. nextTick,
  3. onActivated,
  4. onDeactivated,
  5. onMounted,
  6. onUnmounted,
  7. ref,
  8. } from "vue";
  9. export type ViewStackProps = (...args: any) => (() => any) | void;
  10. export const useViewStack = (hook: ViewStackProps) => {
  11. let destroy: ReturnType<ViewStackProps>;
  12. let isRun = false
  13. const deHandler = () => {
  14. if (isRun) {
  15. destroy && destroy();
  16. destroy = undefined;
  17. isRun = false
  18. }
  19. };
  20. const handler = () => {
  21. nextTick(() => {
  22. if (!isRun) {
  23. isRun = true
  24. destroy = hook();
  25. }
  26. });
  27. };
  28. onActivated(handler);
  29. onMounted(handler);
  30. onDeactivated(deHandler);
  31. onUnmounted(deHandler);
  32. };
  33. export const useActive = () => {
  34. const active = ref(true)
  35. useViewStack(() => {
  36. active.value = true
  37. return () => active.value = false
  38. })
  39. return active
  40. }