Confirm page unload when save is 5 Min+ old

This commit is contained in:
Tobias Berger 2021-10-05 13:23:32 +02:00
parent 0e5e19b150
commit a9ce798490
2 changed files with 28 additions and 1 deletions

View file

@ -31,9 +31,25 @@
closeAllModals(); 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.");
}
}
</script> </script>
<svelte:body on:keyup={handleKeyUp} /> <svelte:body on:keyup={handleKeyUp} />
<svelte:window on:beforeunload={handleBeforeUnload} />
<Modals> <Modals>
<div slot="backdrop" id="modal-backdrop" on:click={closeAllModals} /> <div slot="backdrop" id="modal-backdrop" on:click={closeAllModals} />

View file

@ -22,6 +22,13 @@ type Save = Version0Save | Version1Save;
export class SaveHandler extends StaticClass { export class SaveHandler extends StaticClass {
static readonly saveName = "sharg-save"; 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<typeof setInterval> | undefined = undefined; static #saveInterval: ReturnType<typeof setInterval> | undefined = undefined;
@ -31,7 +38,10 @@ export class SaveHandler extends StaticClass {
clearInterval(SaveHandler.#saveInterval); clearInterval(SaveHandler.#saveInterval);
} }
if (settings.behavior.autoSave.current !== "Off") { 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); localStorage.setItem(SaveHandler.saveName, encodedSave);
SaveHandler.#lastSaved = Date.now();
console.timeEnd("Done saving"); console.timeEnd("Done saving");
} }