feat(updater): download and prepare desktop updates in background
This commit is contained in:
77 files changed
+10536
-814
No files matched your search
@@ -1,3 +1,4 @@
|
||||
import { assertUpdateAllowsInteraction, beginUpdateSensitiveOperation } from "@/lib/app/updatePreparation";
|
||||
import { detachedWindowLabel, detachedWindowUrl } from "@/lib/app/windowContext";
|
||||
import { isTauriRuntime } from "@/lib/backend/tauriRuntime";
|
||||
|
||||
@@ -19,7 +20,9 @@ function errorMessage(error: unknown): string {
|
||||
|
||||
export async function openDetachedTabWindow(tabId: string, title: string, position?: DetachedWindowOpenPosition): Promise<DetachedWindowOpenResult> {
|
||||
if (!isTauriRuntime()) return { opened: false, error: "Detached windows are only available in the desktop app." };
|
||||
let finishOperation: (() => void) | undefined;
|
||||
try {
|
||||
finishOperation = beginUpdateSensitiveOperation();
|
||||
const { WebviewWindow } = await import("@tauri-apps/api/webviewWindow");
|
||||
const label = detachedWindowLabel(tabId);
|
||||
const existing = await WebviewWindow.getByLabel(label);
|
||||
@@ -29,6 +32,7 @@ export async function openDetachedTabWindow(tabId: string, title: string, positi
|
||||
return { opened: true };
|
||||
}
|
||||
|
||||
assertUpdateAllowsInteraction();
|
||||
const child = new WebviewWindow(label, {
|
||||
url: detachedWindowUrl(tabId),
|
||||
title,
|
||||
@@ -42,14 +46,32 @@ export async function openDetachedTabWindow(tabId: string, title: string, positi
|
||||
center: false,
|
||||
});
|
||||
|
||||
// A caller timeout does not prove native window creation has stopped.
|
||||
// Keep this second reservation until a native terminal event arrives.
|
||||
const finishNativeCreation = beginUpdateSensitiveOperation();
|
||||
return await new Promise((resolve) => {
|
||||
let settled = false;
|
||||
let timedOut = false;
|
||||
let timeout: ReturnType<typeof setTimeout> | undefined;
|
||||
const finish = (result: DetachedWindowOpenResult) => {
|
||||
if (settled) return;
|
||||
settled = true;
|
||||
clearTimeout(timeout);
|
||||
resolve(result);
|
||||
};
|
||||
void child.once("tauri://created", async () => {
|
||||
if (timedOut) {
|
||||
try {
|
||||
// The caller already restored its tab; never surface a duplicate.
|
||||
await child.destroy();
|
||||
finishNativeCreation();
|
||||
} catch (error) {
|
||||
// Keep the reservation if removal is uncertain, rather than
|
||||
// allowing restart to race a still-initializing webview.
|
||||
console.error("[DBX][detached-tab:late-create:close-error]", error);
|
||||
}
|
||||
return;
|
||||
}
|
||||
try {
|
||||
if (position) {
|
||||
const { PhysicalPosition } = await import("@tauri-apps/api/dpi");
|
||||
@@ -60,18 +82,26 @@ export async function openDetachedTabWindow(tabId: string, title: string, positi
|
||||
finish({ opened: true });
|
||||
} catch (error) {
|
||||
finish({ opened: false, error: errorMessage(error) });
|
||||
} finally {
|
||||
finishNativeCreation();
|
||||
}
|
||||
});
|
||||
void child.once("tauri://error", (event) => {
|
||||
const error = errorMessage(event?.payload);
|
||||
console.error("[DBX][detached-tab:create:error]", error);
|
||||
finish({ opened: false, error });
|
||||
finishNativeCreation();
|
||||
});
|
||||
setTimeout(() => finish({ opened: false, error: "Timed out while creating the detached window." }), 10_000);
|
||||
timeout = setTimeout(() => {
|
||||
timedOut = true;
|
||||
finish({ opened: false, error: "Timed out while creating the detached window." });
|
||||
}, 10_000);
|
||||
});
|
||||
} catch (error) {
|
||||
const message = errorMessage(error);
|
||||
console.error("[DBX][detached-tab:create:error]", error);
|
||||
return { opened: false, error: message };
|
||||
} finally {
|
||||
finishOperation?.();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user