-
Notifications
You must be signed in to change notification settings - Fork 565
Expand file tree
/
Copy pathpersister.ts
More file actions
109 lines (95 loc) · 2.9 KB
/
persister.ts
File metadata and controls
109 lines (95 loc) · 2.9 KB
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
import { createCustomPersister } from "tinybase/persisters/with-schemas";
import type { MergeableStore, OptionalSchemas } from "tinybase/with-schemas";
import { commands as fsSyncCommands } from "@hypr/plugin-fs-sync";
import {
commands as notifyCommands,
events as notifyEvents,
} from "@hypr/plugin-notify";
import { DEFAULT_USER_ID } from "../../../../utils";
import { StoreOrMergeableStore } from "../../store/shared";
import { asTableChanges } from "../utils";
export function createFolderPersister<Schemas extends OptionalSchemas>(
store: MergeableStore<Schemas>,
) {
const loadFn = async () => {
try {
const result = await fsSyncCommands.listFolders();
if (result.status === "error") {
console.error("[FolderPersister] list error:", result.error);
return undefined;
}
const { folders, session_folder_map } = result.data;
const now = new Date().toISOString();
const foldersData: Record<
string,
{
user_id: string;
created_at: string;
name: string;
parent_folder_id: string;
}
> = {};
for (const [folderId, folder] of Object.entries(folders)) {
if (!folder) continue;
foldersData[folderId] = {
user_id: DEFAULT_USER_ID,
created_at: now,
name: folder.name,
parent_folder_id: folder.parent_folder_id ?? "",
};
}
// @ts-ignore - update session folder_id only (do not delete sessions here)
store.transaction(() => {
for (const [sessionId, folderPath] of Object.entries(
session_folder_map,
)) {
// @ts-ignore
if (store.hasRow("sessions", sessionId)) {
// @ts-ignore
store.setCell("sessions", sessionId, "folder_id", folderPath);
}
}
});
return asTableChanges("folders", foldersData) as any;
} catch (error) {
console.error("[FolderPersister] load error:", error);
return undefined;
}
};
const saveFn = async () => {};
return createCustomPersister(
store,
loadFn,
saveFn,
() => null,
() => {},
(error) => console.error("[FolderPersister]:", error),
StoreOrMergeableStore,
);
}
interface Loadable {
load(): Promise<unknown>;
}
export async function startFolderWatcher(
persister: Loadable,
): Promise<() => void> {
const result = await notifyCommands.start();
if (result.status === "error") {
console.error("[FolderWatcher] Failed to start:", result.error);
return () => {};
}
const unlisten = await notifyEvents.fileChanged.listen(async (event) => {
const path = event.payload.path;
if (path.startsWith("sessions/")) {
try {
await persister.load();
} catch (error) {
console.error("[FolderWatcher] Failed to reload:", error);
}
}
});
return () => {
unlisten();
void notifyCommands.stop();
};
}