-
Notifications
You must be signed in to change notification settings - Fork 27
/
store.ts
258 lines (215 loc) · 5.63 KB
/
store.ts
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
import Gio from "gi://Gio";
import GLib from "gi://GLib";
import { StickyError, StickyErrorType } from "./errors.js";
import { INote, Note } from "./util.js";
Gio._promisify(
Gio.File.prototype,
"load_contents_async",
"load_contents_finish",
);
Gio._promisify(
Gio.File.prototype,
"replace_contents_async",
"replace_contents_finish",
);
const decoder = new TextDecoder();
const encoder = new TextEncoder();
interface SavedNoteData {
v: 1;
notes: INote[];
state: State;
}
interface LoadNotesReturn {
notes: Note[];
state: State;
}
interface State {
all_notes: boolean;
}
export const NotesDir = Gio.file_new_for_path(
GLib.build_filenamev([GLib.get_user_data_dir(), pkg.name]),
);
export const NewNotesDir = Gio.file_new_for_path(
GLib.build_filenamev([GLib.get_user_data_dir(), pkg.name, "notes"]),
);
export const Notes = Gio.file_new_for_path(
GLib.build_filenamev([NotesDir.get_path()!, "notes.json"]),
);
export const load_file = (file: Gio.File) => {
try {
Notes.get_parent()!.make_directory_with_parents(null);
} catch (e: unknown) {
if (e instanceof GLib.Error) {
if (!e.matches(Gio.IOErrorEnum, Gio.IOErrorEnum.EXISTS)) {
throw new StickyError(
StickyErrorType.NO_PERMISSION,
"Failed to create notes directory",
);
}
}
}
try {
const [_, contents] = file.load_contents(null);
try {
return JSON.parse(decoder.decode(contents));
} catch (e: unknown) {
throw new StickyError(
StickyErrorType.FILE_CORRUPTED,
"Failed to parse notes",
);
}
} catch (e) {
if (e instanceof GLib.Error) {
if (e.matches(Gio.IOErrorEnum, Gio.IOErrorEnum.NOT_FOUND)) {
throw new StickyError(
StickyErrorType.FILE_NOT_FOUND,
"Failed to load notes",
);
} else {
throw new StickyError(
StickyErrorType.UNKNOWN,
"Failed to load notes",
);
}
}
}
};
export const save_file = (file: Gio.File, data: any) => {
const string = JSON.stringify(data, null, 2);
try {
return file.replace_contents(
encoder.encode(string),
null,
false,
Gio.FileCreateFlags.NONE,
null,
);
} catch (e) {
if (e instanceof GLib.Error) {
if (e.matches(Gio.IOErrorEnum, Gio.IOErrorEnum.PERMISSION_DENIED)) {
throw new StickyError(
StickyErrorType.NO_PERMISSION,
"Failed to save notes",
);
} else {
throw new StickyError(
StickyErrorType.UNKNOWN,
"Failed to save notes",
);
}
}
}
};
export const ensure_dir = (dir: Gio.File) => {
try {
dir.make_directory_with_parents(null);
} catch (e: unknown) {
if (e instanceof GLib.Error) {
if (!e.matches(Gio.IOErrorEnum, Gio.IOErrorEnum.EXISTS)) {
throw new StickyError(
StickyErrorType.NO_PERMISSION,
"Failed to create notes directory",
);
}
}
}
};
const bogus_data: LoadNotesReturn = { state: { all_notes: true }, notes: [] };
const load_notes_v1 = (notes: any) => {
if (notes.v !== 1) {
console.error(`Invalid notes version ${notes.v}`);
return bogus_data;
}
return {
notes: notes.notes.map((note: INote) => new Note(note)),
state: notes.state,
} as LoadNotesReturn;
};
export function load_notes() {
let index = null;
try {
index = load_file(Notes) ?? null;
} catch (e) {
if (e instanceof StickyError) {
if (
e.type == StickyErrorType.FILE_NOT_FOUND ||
e.type == StickyErrorType.FILE_CORRUPTED
) {
// ignore
} else {
throw e;
}
}
}
if (index !== null) {
// if the index exists, load the notes from the index
const notes = load_notes_v1(index).notes;
Notes.delete(null);
save_notes(notes);
return notes;
}
ensure_dir(NewNotesDir);
const files = list_files(NewNotesDir);
return files
.filter((file) => file.get_basename()!.endsWith(".json"))
.map((file) => load_file(file))
.map((note) => new Note(note));
}
export const list_files = (dir: Gio.File) => {
const enumerator = dir.enumerate_children(
"standard::name,standard::type",
Gio.FileQueryInfoFlags.NONE,
null,
);
const files: Gio.File[] = [];
let info: Gio.FileInfo | null;
while ((info = enumerator.next_file(null)) !== null) {
const file = enumerator.get_child(info);
files.push(file);
}
return files;
};
export function save_notes(notes: Note[]) {
ensure_dir(NewNotesDir);
const files = list_files(NewNotesDir);
let writes: [Note, Gio.File][] = [];
const deletes: Gio.File[] = [];
for (const file of files) {
const basename = file.get_basename()!;
if (basename.endsWith(".json")) {
const uuid = basename.slice(0, -5);
if (uuid.length !== 36) {
deletes.push(file);
continue;
}
const note = notes.find((note) => note.uuid === uuid);
if (note) {
writes.push([note, file]);
} else {
deletes.push(file);
}
} else {
// deletes.push(file);
}
}
for (const note of notes) {
if (!writes.some(([n]) => n.uuid === note.uuid)) {
const file = NewNotesDir.get_child(`${note.uuid}.json`);
writes.push([note, file]);
}
}
for (const file of deletes) {
file.delete(null);
}
return writes.map(([note, file]) => save_file(file, note));
}
export function save_note(note: Note) {
const file = NewNotesDir.get_child(`${note.uuid}.json`);
return save_file(file, note);
}
export function delete_note(uuid: string) {
const file = NewNotesDir.get_child(`${uuid}.json`);
try {
return file.delete(null);
} catch {}
}