Skip to content

Commit 83035b0

Browse files
committed
Code review
1 parent 18825b6 commit 83035b0

File tree

3 files changed

+47
-60
lines changed

3 files changed

+47
-60
lines changed

desktop/src/app.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -315,23 +315,17 @@ impl App {
315315
}
316316
}
317317
DesktopFrontendMessage::PersistenceLoadDocuments => {
318-
let current_id = self.persistent_data.current_document_id();
319-
let mut seen_current = false;
320-
318+
// Open all documents in persisted tab order, then select the current one
321319
for (id, document) in self.persistent_data.all_documents() {
322-
let is_current = current_id == Some(id);
323-
let to_front = !seen_current && !is_current;
324-
seen_current |= is_current;
325-
326320
responses.push(DesktopWrapperMessage::LoadDocument {
327321
id,
328322
document,
329-
to_front,
323+
to_front: false,
330324
select_after_open: false,
331325
});
332326
}
333327

334-
if let Some(id) = current_id {
328+
if let Some(id) = self.persistent_data.current_document_id() {
335329
responses.push(DesktopWrapperMessage::SelectDocument { id });
336330
}
337331
}

desktop/src/persist.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ impl PersistentData {
3030
}
3131

3232
if let Some(order) = &self.document_order {
33-
self.force_order(order.clone());
33+
self.force_order(&order.clone());
3434
}
3535
self.flush();
3636
}
@@ -65,7 +65,7 @@ impl PersistentData {
6565
}
6666

6767
pub(crate) fn force_document_order(&mut self, order: Vec<DocumentId>) {
68-
self.force_order(order.clone());
68+
self.force_order(&order);
6969
self.document_order = Some(order);
7070
self.flush();
7171
}
@@ -83,9 +83,9 @@ impl PersistentData {
8383
}
8484

8585
// Reorders the documents array to match a desired ordering, keeping unmentioned documents at the end
86-
fn force_order(&mut self, desired_order: Vec<DocumentId>) {
86+
fn force_order(&mut self, desired_order: &[DocumentId]) {
8787
let mut ordered_prefix_length = 0;
88-
for id in &desired_order {
88+
for id in desired_order {
8989
if let Some(offset) = self.documents[ordered_prefix_length..].iter().position(|doc| doc.id == *id) {
9090
let found_index = ordered_prefix_length + offset;
9191
if found_index != ordered_prefix_length {
@@ -132,6 +132,7 @@ impl PersistentData {
132132
*self = loaded;
133133

134134
self.garbage_collect_document_files();
135+
Self::delete_old_cef_browser_directory();
135136
}
136137

137138
// Remove orphaned document content files that have no corresponding entry in the persisted state
@@ -158,6 +159,14 @@ impl PersistentData {
158159
}
159160
}
160161

162+
// TODO: Eventually remove this cleanup code for the old "browser" CEF directory (renamed to "cef")
163+
fn delete_old_cef_browser_directory() {
164+
let old_browser_dir = crate::dirs::app_data_dir().join("browser");
165+
if old_browser_dir.is_dir() {
166+
let _ = std::fs::remove_dir_all(&old_browser_dir);
167+
}
168+
}
169+
161170
fn state_file_path() -> std::path::PathBuf {
162171
let mut path = crate::dirs::app_data_dir();
163172
path.push(crate::consts::APP_STATE_FILE_NAME);

frontend/src/utility-functions/persistence.ts

Lines changed: 31 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,25 @@ function createDocumentInfo(id: bigint, name: string, isSaved: boolean): Persist
1616
return { id, name, is_saved: isSaved };
1717
}
1818

19+
// Reorder document entries to match the given ID ordering, appending any unmentioned entries at the end
20+
function reorderDocuments(documents: PersistedDocumentInfo[], orderedIds: bigint[]): PersistedDocumentInfo[] {
21+
const byId = new Map(documents.map((entry) => [entry.id, entry]));
22+
const reordered: PersistedDocumentInfo[] = [];
23+
24+
orderedIds.forEach((id) => {
25+
const existing = byId.get(id);
26+
if (existing) {
27+
reordered.push(existing);
28+
byId.delete(id);
29+
}
30+
});
31+
32+
// Append any entries not yet present in the portfolio (e.g. documents still loading at startup)
33+
byId.forEach((entry) => reordered.push(entry));
34+
35+
return reordered;
36+
}
37+
1938
// ====================================
2039
// State-based persistence (new format)
2140
// ====================================
@@ -26,22 +45,7 @@ export async function storeDocumentTabOrder(portfolio: PortfolioStore) {
2645

2746
await databaseUpdate<PersistedState>("state", (old) => {
2847
const state = old || emptyPersistedState();
29-
30-
// Reorder existing document entries to match the portfolio's tab order, preserving metadata
31-
const byId = new Map(state.documents.map((entry) => [entry.id, entry]));
32-
const reordered: PersistedDocumentInfo[] = [];
33-
orderedIds.forEach((id) => {
34-
const existing = byId.get(id);
35-
if (existing) {
36-
reordered.push(existing);
37-
byId.delete(id);
38-
}
39-
});
40-
41-
// Append any entries not yet present in the portfolio (e.g. documents still loading at startup)
42-
byId.forEach((entry) => reordered.push(entry));
43-
44-
return { ...state, documents: reordered };
48+
return { ...state, documents: reorderDocuments(state.documents, orderedIds) };
4549
});
4650
}
4751

@@ -71,23 +75,9 @@ export async function storeDocument(autoSaveDocument: MessageBody<"TriggerPersis
7175
state.documents.push(entry);
7276
}
7377

74-
// Reorder to match the portfolio's tab order
75-
const byId = new Map(state.documents.map((doc) => [doc.id, doc]));
76-
const reordered: PersistedDocumentInfo[] = [];
77-
orderedIds.forEach((id) => {
78-
const existing = byId.get(id);
79-
if (existing) {
80-
reordered.push(existing);
81-
byId.delete(id);
82-
}
83-
});
84-
85-
// Append any entries not yet present in the portfolio (e.g. documents still loading at startup)
86-
byId.forEach((entry) => reordered.push(entry));
87-
8878
// eslint-disable-next-line camelcase
8979
state.current_document = documentId;
90-
state.documents = reordered;
80+
state.documents = reorderDocuments(state.documents, orderedIds);
9181
return state;
9282
});
9383
}
@@ -131,15 +121,13 @@ export async function loadDocuments(editor: EditorWrapper) {
131121
const currentId = state.current_document;
132122
const currentEntry = currentId !== undefined ? state.documents.find((doc) => doc.id === currentId) : undefined;
133123
const current = currentEntry || state.documents[state.documents.length - 1];
134-
const currentIndex = state.documents.indexOf(current);
135124

136-
// Open documents in order around the current document, placing earlier ones before it and later ones after
137-
state.documents.forEach((entry, index) => {
125+
// Open all documents in persisted tab order, then select the current one
126+
state.documents.forEach((entry) => {
138127
const content = documentContents[String(entry.id)];
139128
if (content === undefined) return;
140129

141-
const toFront = index < currentIndex;
142-
editor.openAutoSavedDocument(entry.id, entry.name, entry.is_saved, content, toFront);
130+
editor.openAutoSavedDocument(entry.id, entry.name, entry.is_saved, content, false);
143131
});
144132

145133
editor.selectDocument(current.id);
@@ -252,9 +240,9 @@ async function migrateToNewFormat() {
252240
let name = "";
253241
if ("name" in details && typeof details.name === "string") name = details.name;
254242

255-
const status = savedStatusFromUnknown(details);
243+
const isSaved = extractIsSavedFromUnknown(details);
256244

257-
newDocumentInfos.push(createDocumentInfo(id, name, status.isSaved));
245+
newDocumentInfos.push(createDocumentInfo(id, name, isSaved));
258246
});
259247
}
260248

@@ -271,20 +259,16 @@ async function migrateToNewFormat() {
271259
}
272260

273261
// TODO: Eventually remove this document upgrade code
274-
function savedStatusFromUnknown(details: unknown): { isSaved: boolean; isAutoSaved: boolean } {
275-
if (typeof details !== "object" || details === null) return { isSaved: false, isAutoSaved: false };
262+
function extractIsSavedFromUnknown(details: unknown): boolean {
263+
if (typeof details !== "object" || details === null) return false;
276264

277265
// Old camelCase format
278-
if ("isSaved" in details && "isAutoSaved" in details) {
279-
return { isSaved: Boolean(details.isSaved), isAutoSaved: Boolean(details.isAutoSaved) };
280-
}
266+
if ("isSaved" in details) return Boolean(details.isSaved);
281267

282268
// New snake_case format
283-
if ("is_saved" in details && "is_auto_saved" in details) {
284-
return { isSaved: Boolean(details.is_saved), isAutoSaved: Boolean(details.is_auto_saved) };
285-
}
269+
if ("is_saved" in details) return Boolean(details.is_saved);
286270

287-
return { isSaved: false, isAutoSaved: false };
271+
return false;
288272
}
289273

290274
// =================

0 commit comments

Comments
 (0)