import { nextTick, onActivated, onDeactivated, onMounted, onUnmounted } from 'vue' type HookArgs = (...args: any) => () => any export const useViewStack = (hook: HookArgs) => { let destroy: ReturnType const deHandler = () => { if (destroy) { destroy() destroy = null } } const handler = () => { nextTick(() => { if (!destroy) { destroy = hook() } }) } onActivated(handler) onMounted(handler) onDeactivated(deHandler) onUnmounted(deHandler) }