Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/bridges/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ const utilsBridge = {
)) as string;
},

autosaveBackupData: (writeContent: string): Promise<void> => {
return ipcRenderer.invoke("write-autosave", writeContent);
},

getLastAutosaveTimestamp: (): Promise<number | undefined> => {
return ipcRenderer.invoke("get-last-autosave-timestamp");
},

getCacheSize: async (): Promise<number> => {
return await ipcRenderer.invoke("get-cache");
},
Expand Down
3 changes: 3 additions & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { initApp, openTextMenu } from "./scripts/models/app";
import { startPool } from "./scripts/fetch-pool";
import { rootStore } from "./scripts/reducer";
import { initThemeUpdateListener } from "./scripts/settings";
import { renewAutosave } from "./scripts/autosave";

startPool(); // Start global fetch pool.
initThemeUpdateListener();
Expand All @@ -22,6 +23,8 @@ initializeIcons("icons/");

rootStore.dispatch(initApp());

renewAutosave();

window.utils.addMainContextListener((pos, text) => {
rootStore.dispatch(openTextMenu(pos, text));
});
Expand Down
42 changes: 40 additions & 2 deletions src/main/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,20 @@ import {
clipboard,
systemPreferences,
} from "electron";
import path from "path";
import { getFonts } from "font-list";
import { promises, writeFile } from "fs";

import pkg from "../../package.json";
import { WindowManager } from "./window";
import { trimContent, trimString } from "./pure-utils";
import { promises, writeFile } from "fs";
import { ImageCallbackTypes, TouchBarTexts } from "../schema-types";
import { initMainTouchBar } from "./touchbar";
import { getFonts } from "font-list";

function getAutosavePath() {
const userDataPath = app.getPath("userData");
return `${userDataPath}/autosaves/ffr_autosave.frdata`;
}

export function setUtilsListeners(manager: WindowManager) {
async function openExternal(url: string, background = false) {
Expand Down Expand Up @@ -159,6 +166,37 @@ export function setUtilsListeners(manager: WindowManager) {
},
);

ipcMain.handle("write-autosave", async (_, writeContent: string) => {
const autosavePath = getAutosavePath();
try {
const dirStat = await promises.stat(path.dirname(autosavePath), {
throwIfNoEntry: false,
});
if (dirStat == undefined) {
await promises.mkdir(path.dirname(autosavePath));
}
await promises.writeFile(getAutosavePath(), writeContent);
} catch (err: any) {
console.error("Failed to write autosave", err);
}
});

ipcMain.handle(
"get-last-autosave-timestamp",
async (): Promise<number | undefined> => {
const autosavePath = getAutosavePath();
try {
const result = await promises.stat(autosavePath, {
throwIfNoEntry: false,
});
return result?.mtimeMs;
} catch (err: any) {
console.error(`Failed to stat ${autosavePath}`, err);
return undefined;
}
},
);

ipcMain.handle("get-cache", async () => {
return await session.defaultSession.getCacheSize();
});
Expand Down
37 changes: 37 additions & 0 deletions src/scripts/autosave.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import * as db from "./db";

/**
* Renew the autosave file in userData if it's been long enough.
*/
export async function renewAutosave() {
const oneDay = 24 * 60 * 60 * 1000;
const autosaveMTimeMs = await window.utils.getLastAutosaveTimestamp();
const autosaveStartTime = Date.now();
if (autosaveStartTime < autosaveMTimeMs + oneDay) {
console.debug("Autosave still current, not rewriting.");
return;
}
let sources: db.SourceEntry[];
let items: db.ItemEntry[];
const transaction = db.fluentDB.transaction(
"r",
db.fluentDB.sources,
db.fluentDB.items,
async () => {
sources = await db.fluentDB.sources.toArray();
items = await db.fluentDB.items.toArray();
},
);
const output = window.settings.getAll();
await transaction;
output["database"] = {
sources: sources,
items: items,
};
await window.utils.autosaveBackupData(JSON.stringify(output));
const autosaveEndTime = Date.now();
console.debug(
"Completed autosave in " +
`${Math.round(autosaveEndTime - autosaveStartTime)}ms.`,
);
}
6 changes: 5 additions & 1 deletion src/scripts/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,11 @@ function byteLength(str: string) {
}

export async function init() {
await migrateLovefieldSourcesDB("sourcesDB", 3);
try {
await migrateLovefieldSourcesDB("sourcesDB", 3);
} catch (e: any) {
console.error("Error migrating sources DB", e);
}
try {
await migrateLovefieldItemsDB("itemsDB", 1);
} catch (e) {
Expand Down
Loading