diff --git a/src/BeatStore.ts b/src/BeatStore.ts index bf81f14..77a27d9 100644 --- a/src/BeatStore.ts +++ b/src/BeatStore.ts @@ -1,5 +1,5 @@ import { type Beat, createBeat, deserialise as deserialiseBeat, type BeatManager } from "@/Beat"; -import { inject, computed, type InjectionKey, ref, shallowRef, triggerRef, watch, onScopeDispose } from "vue"; +import { inject, computed, type InjectionKey, ref, shallowRef, triggerRef, watch, onScopeDispose, nextTick } from "vue"; function defaultMainBeatGroup(manager: BeatManager): Beat { const defaultSettings = { @@ -28,7 +28,6 @@ export function createBeatStore() { const beats = shallowRef([ defaultMainBeatGroup(manager) ]); const activeBeatIndex = ref(0); const activeBeat = computed(() => beats.value[activeBeatIndex.value] ?? null); - const autoSave = ref(true); const orientation = ref<"horizontal" | "vertical">("horizontal"); function resetActiveBeat(): void { @@ -48,9 +47,6 @@ export function createBeatStore() { function addNewBeat(): void { const newBeat = defaultMainBeatGroup(manager); beats.value.push(newBeat); - if (autoSave.value) { - save("localStorage"); - } triggerRef(beats); } @@ -83,7 +79,7 @@ export function createBeatStore() { } else { resetActiveBeat(); } - saveDirty.value = false; + nextTick(() => saveDirty.value = false); } function bakeAll(): void { @@ -91,7 +87,18 @@ export function createBeatStore() { } watch([activeBeatIndex, orientation, beats], () => { - save("localStorage"); + saveDirty.value = true; + }); + + const saveInterval = setInterval(() => saveDirty && save("localStorage"), 5 * 60 * 1000); + + onScopeDispose(() => clearInterval(saveInterval)); + + window.addEventListener("beforeunload", (e) => { + if (saveDirty.value) { + e.preventDefault(); + e.returnValue = true; + } }); const savedItem = localStorage.getItem("drum-slayer-save"); @@ -101,24 +108,10 @@ export function createBeatStore() { loadFromSave(serial); } - const saveInterval = setInterval(() => saveDirty && save("localStorage"), 5 * 60 * 1000); - - watch(saveDirty, () => console.log(saveDirty.value)); - - onScopeDispose(() => clearInterval(saveInterval)); - - window.addEventListener("beforeunload", (e) => { - if (saveDirty) { - e.preventDefault(); - e.returnValue = true; - } - }); - return { beats, activeBeatIndex, activeBeat, - autoSave, saveDirty, orientation,