Skip to content

Commit 1b86118

Browse files
committed
feat: Improve handling of intents before files are restored on startup
1 parent 14c445b commit 1b86118

File tree

2 files changed

+47
-5
lines changed

2 files changed

+47
-5
lines changed

src/handlers/intent.js

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ import openFile from "lib/openFile";
33
import helpers from "utils/helpers";
44

55
const handlers = [];
6+
/**
7+
* Queue to store intents that arrive before files are restored
8+
* @type {Array<{url: string, options: object}>}
9+
*/
10+
const pendingIntents = [];
611

712
/**
813
*
@@ -39,10 +44,21 @@ export default async function HandleIntent(intent = {}) {
3944
return;
4045
}
4146

42-
await openFile(url, {
43-
mode: "single",
44-
render: true,
45-
});
47+
if (sessionStorage.getItem("isfilesRestored") === "true") {
48+
await openFile(url, {
49+
mode: "single",
50+
render: true,
51+
});
52+
} else {
53+
// Store the intent for later processing when files are restored
54+
pendingIntents.push({
55+
url,
56+
options: {
57+
mode: "single",
58+
render: true,
59+
},
60+
});
61+
}
4662
}
4763
}
4864

@@ -59,6 +75,25 @@ export function removeIntentHandler(handler) {
5975
if (index > -1) handlers.splice(index, 1);
6076
}
6177

78+
/**
79+
* Process all pending intents that were queued before files were restored.
80+
* This function is called after isfilesRestored is set to true in main.js.
81+
* @returns {Promise<void>}
82+
*/
83+
export async function processPendingIntents() {
84+
if (sessionStorage.getItem("isfilesRestored") !== "true") return;
85+
86+
// Process all pending intents
87+
while (pendingIntents.length > 0) {
88+
const pendingIntent = pendingIntents.shift();
89+
try {
90+
await openFile(pendingIntent.url, pendingIntent.options);
91+
} catch (error) {
92+
helpers.error(error);
93+
}
94+
}
95+
}
96+
6297
class IntentEvent {
6398
module;
6499
action;

src/lib/main.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import tile from "components/tile";
1717
import toast from "components/toast";
1818
import tutorial from "components/tutorial";
1919
import fsOperation from "fileSystem";
20-
import intentHandler from "handlers/intent";
20+
import intentHandler, { processPendingIntents } from "handlers/intent";
2121
import keyboardHandler from "handlers/keyboard";
2222
import quickToolsInit from "handlers/quickToolsInit";
2323
import windowResize from "handlers/windowResize";
@@ -481,12 +481,19 @@ async function loadApp() {
481481
if (Array.isArray(files) && files.length) {
482482
try {
483483
await restoreFiles(files);
484+
// save state to handle file loading gracefully
485+
sessionStorage.setItem("isfilesRestored", true);
486+
// Process any pending intents that were queued before files were restored
487+
await processPendingIntents();
484488
} catch (error) {
485489
window.log("error", "File loading failed!");
486490
window.log("error", error);
487491
toast("File loading failed!");
488492
}
489493
} else {
494+
// Even when no files need to be restored, mark as restored and process pending intents
495+
sessionStorage.setItem("isfilesRestored", true);
496+
await processPendingIntents();
490497
onEditorUpdate(undefined, false);
491498
}
492499

0 commit comments

Comments
 (0)