Skip to content

Commit 807e111

Browse files
committed
Backup and Data Retention for PWV
1 parent ad4c7eb commit 807e111

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

src/defs/PyWebview.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export interface PyWebview {
1010
load_file: () => Promise<string>;
1111
load_project_id: () => Promise<string | null>;
1212
backup_project: (content: string) => Promise<boolean>;
13-
restore_project: () => Promise<void>;
13+
restore_project: () => Promise<string>;
1414
}
1515
}
1616

src/utils/appData.tsx

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ import { usePromptContext } from "@utils/prompt";
1616
import { usePalleteContext } from "@utils/pallete";
1717
import { useVariablesContext } from "@utils/variables";
1818
import { loadProject, saveProject } from "@utils/engineTools";
19+
import { packProject } from "@utils/packerTools";
20+
import { backupProject, getRestoredProject } from "@utils/desktopTools";
21+
import { saveData } from "@utils/persistentTools";
1922
import {
2023
getBreadcrumb,
2124
getWindowTools,
@@ -155,6 +158,8 @@ const createAppDataContext = () => {
155158
useEffect(() => {
156159
(async () => {
157160
const existingId = await loadExistingId();
161+
const restoredProject = await getRestoredProject();
162+
saveData(`p:${existingId}`, restoredProject);
158163
setProjectId(existingId);
159164

160165
loadProject(
@@ -209,6 +214,32 @@ const createAppDataContext = () => {
209214
statesList,
210215
customComponents,
211216
]);
217+
218+
useEffect(() => {
219+
const backupInterval = setInterval(() => {
220+
const project = packProject(
221+
projectId!,
222+
nodeSystem,
223+
entries,
224+
{
225+
states: statesList,
226+
customComponents,
227+
}
228+
);
229+
230+
backupProject(JSON.stringify(project, null, 2));
231+
}, 5000);
232+
233+
return () => {
234+
clearInterval(backupInterval);
235+
}
236+
}, [
237+
customComponents,
238+
entries,
239+
nodeSystem,
240+
projectId,
241+
statesList,
242+
]);
212243

213244
const exposed = {
214245
projectId,

src/utils/desktopTools.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,27 @@ const setupDesktopTools = () => {
9999
return (await getApi()!.load_project_id()) || null;
100100
}
101101

102+
const backupProject = async (content: string): Promise<boolean> => {
103+
if (!isDesktop()) return false;
104+
await waitForPWV();
105+
return getApi()!.backup_project(content);
106+
}
107+
108+
const getRestoredProject = async (): Promise<string> => {
109+
if (!isDesktop()) return "";
110+
await waitForPWV();
111+
return getApi()!.restore_project();
112+
}
113+
102114
return {
103115
isDesktop,
104116
closeWindow,
105117
toggleWindowFullscreen,
106118
saveFile,
107119
loadFile,
108120
loadProjectId,
121+
backupProject,
122+
getRestoredProject
109123
}
110124
};
111125

@@ -116,5 +130,7 @@ export const {
116130
saveFile,
117131
loadFile,
118132
loadProjectId,
133+
backupProject,
134+
getRestoredProject,
119135
} = setupDesktopTools();
120136

0 commit comments

Comments
 (0)