File size: 3,136 Bytes
71174bc | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 | import {
localStorageGetItem,
localStorageRemoveItem,
localStorageSetItem,
} from "@/Core/LocalStorage";
import { store } from ".";
import { stateToJsonStr } from "@/FileSystem/LoadSaveMolModels/SaveMolModels/SaveMolModa";
import { parseUsingMolModa } from "@/FileSystem/LoadSaveMolModels/ParseMolModels/_ParseUsingMolModa";
import { FileInfo } from "@/FileSystem/FileInfo";
import { messagesApi } from "@/Api/Messages";
import { YesNo } from "@/UI/MessageAlerts/Popups/InterfacesAndEnums";
import { getSetting } from "@/Plugins/Core/Settings/LoadSaveSettings";
let timerId: any = undefined;
/**
* Deletes the autosave.
*
* @returns {Promise<void>} A promise that resolves when the autosave is deleted.
*/
export async function deleteAutoSave(): Promise<void> {
await localStorageRemoveItem("autoSave");
}
/**
* Stops the autosave timer.
*/
export function stopAutoSaveTimer() {
if (timerId !== undefined) {
clearTimeout(timerId);
}
}
/**
* Restarts the autosave timer.
*/
export function restartAutoSaveTimer() {
stopAutoSaveTimer();
const timerTick = async () => {
const tickInterval = await getSetting("autoSaveFrequencyMinutes");
if (tickInterval <= 0) {
// disabled
return;
}
const tickIntervalMS = tickInterval * 60 * 1000;
timerId = setTimeout(async () => {
// If cookies not allowed, features in unacailable.
if (!(await localStorageGetItem("allowCookies", false))) {
timerTick();
return;
}
if (store.state.molecules.length === 0) {
// Empty. So nothing to autosave.
await deleteAutoSave();
timerTick();
return;
}
const dataToSave = stateToJsonStr(store.state);
localStorageSetItem("autoSave", dataToSave);
console.log("Auto saved...");
timerTick();
}, tickIntervalMS);
}
timerTick();
}
/**
* Loads the session from local storage.
*/
async function loadSessionFromLocalStorage() {
// Cookies are allowed. Check to see if existing autosave.
const existingAutoSave = await localStorageGetItem("autoSave");
if (existingAutoSave !== null) {
const resp = await messagesApi.popupYesNo(
"You have unsaved changes from your last session. Would you like to restore them?",
"Restore Unsaved Changes?",
"Restore Session",
"Discard Session"
)
if (resp === YesNo.No) {
await deleteAutoSave();
return;
}
const fileInfo = new FileInfo({
name: "tmp.molmoda",
contents: existingAutoSave,
});
await parseUsingMolModa(fileInfo);
}
}
/**
* Sets up the autosave.
*/
export async function setupAutoSave(): Promise<void> {
// If cookies not allowed, features in unacailable.
if (await localStorageGetItem("allowCookies", false)) {
await loadSessionFromLocalStorage();
}
restartAutoSaveTimer();
}
|