useViewStack.ts 530 B

123456789101112131415161718192021222324252627282930
  1. import {
  2. nextTick,
  3. onActivated,
  4. onDeactivated,
  5. onMounted,
  6. onUnmounted
  7. } from 'vue'
  8. type HookArgs = (...args: any) => () => any
  9. export const useViewStack = (hook: HookArgs) => {
  10. let destroy: ReturnType<HookArgs>
  11. const deHandler = () => {
  12. if (destroy) {
  13. destroy()
  14. destroy = null
  15. }
  16. }
  17. const handler = () => {
  18. nextTick(() => {
  19. if (!destroy) {
  20. destroy = hook()
  21. }
  22. })
  23. }
  24. onActivated(handler)
  25. onMounted(handler)
  26. onDeactivated(deHandler)
  27. onUnmounted(deHandler)
  28. }