diff --git a/src/App.svelte b/src/App.svelte
index e313ddf..39f1a87 100644
--- a/src/App.svelte
+++ b/src/App.svelte
@@ -31,9 +31,25 @@
closeAllModals();
}
}
+
+ function handleBeforeUnload(event: BeforeUnloadEvent) {
+ console.debug(
+ "beforeUnload",
+ Date.now() - SharkGame.SaveHandler.lastSaved >= 60 * 1000,
+ Date.now() - SharkGame.SaveHandler.lastSaved,
+ 60 * 1000
+ );
+ // If last save is over a minute old
+ if (Date.now() - SharkGame.SaveHandler.lastSaved >= 60 * 1000) {
+ // Annotyingly, the standardized way isn't supported, so both outdated ones will have to suffice
+ event.preventDefault();
+ return (event.returnValue = "Last save is over a minute old.");
+ }
+ }
+
diff --git a/src/shark/SaveHandler.ts b/src/shark/SaveHandler.ts
index 115ce94..78267f1 100644
--- a/src/shark/SaveHandler.ts
+++ b/src/shark/SaveHandler.ts
@@ -22,6 +22,13 @@ type Save = Version0Save | Version1Save;
export class SaveHandler extends StaticClass {
static readonly saveName = "sharg-save";
+ static #lastSaved = Date.now();
+ static get lastSaved(): number {
+ return SaveHandler.#lastSaved;
+ }
+ private static set lastSaved(val) {
+ SaveHandler.#lastSaved = val;
+ }
static #saveInterval: ReturnType | undefined = undefined;
@@ -31,7 +38,10 @@ export class SaveHandler extends StaticClass {
clearInterval(SaveHandler.#saveInterval);
}
if (settings.behavior.autoSave.current !== "Off") {
- SaveHandler.#saveInterval = setInterval(() => game.save(), settings.behavior.autoSave.current * 1000);
+ SaveHandler.#saveInterval = setInterval(
+ () => SaveHandler.save(game),
+ settings.behavior.autoSave.current * 1000
+ );
}
});
}
@@ -86,6 +96,7 @@ export class SaveHandler extends StaticClass {
);
localStorage.setItem(SaveHandler.saveName, encodedSave);
+ SaveHandler.#lastSaved = Date.now();
console.timeEnd("Done saving");
}