import { onMounted, onUnmounted } from "vue"; import type { Ref } from "vue"; export function useAutoSave( dirty: Ref, saving: Ref, saveFn: () => Promise, intervalMs = 5 * 60 * 1000, ): void { let timer: ReturnType | null = null; onMounted(() => { timer = setInterval(async () => { if (!dirty.value || saving.value) return; await saveFn(); }, intervalMs); }); onUnmounted(() => { if (timer !== null) clearInterval(timer); }); }