| 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; |
|
|
| |
| |
| |
| |
| |
| export async function deleteAutoSave(): Promise<void> { |
| await localStorageRemoveItem("autoSave"); |
| } |
|
|
| |
| |
| |
| export function stopAutoSaveTimer() { |
| if (timerId !== undefined) { |
| clearTimeout(timerId); |
| } |
| } |
|
|
| |
| |
| |
| export function restartAutoSaveTimer() { |
| stopAutoSaveTimer(); |
|
|
| const timerTick = async () => { |
| const tickInterval = await getSetting("autoSaveFrequencyMinutes"); |
| if (tickInterval <= 0) { |
| |
| return; |
| } |
| const tickIntervalMS = tickInterval * 60 * 1000; |
| |
| timerId = setTimeout(async () => { |
| |
| if (!(await localStorageGetItem("allowCookies", false))) { |
| timerTick(); |
| return; |
| } |
|
|
| if (store.state.molecules.length === 0) { |
| |
| await deleteAutoSave(); |
| timerTick(); |
| return; |
| } |
|
|
| const dataToSave = stateToJsonStr(store.state); |
| localStorageSetItem("autoSave", dataToSave); |
| console.log("Auto saved..."); |
| timerTick(); |
| }, tickIntervalMS); |
| } |
|
|
| timerTick(); |
| } |
|
|
| |
| |
| |
| async function loadSessionFromLocalStorage() { |
| |
| 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); |
| } |
| } |
|
|
| |
| |
| |
| export async function setupAutoSave(): Promise<void> { |
| |
| if (await localStorageGetItem("allowCookies", false)) { |
| await loadSessionFromLocalStorage(); |
| } |
|
|
| restartAutoSaveTimer(); |
| } |
|
|