From f1a590f51b5cd96a0a4077faa39ac38d9f90e49e Mon Sep 17 00:00:00 2001
From: Mike Sawka <mike@commandline.dev>
Date: Tue, 10 Dec 2024 10:58:50 -0800
Subject: [PATCH] minor v10 cleanup (waveReadyPromise, focusedWaveWindow)
 (#1456)

---
 emain/emain-window.ts | 19 ++++++++-----------
 emain/emain.ts        |  4 +++-
 emain/updater.ts      |  4 +++-
 3 files changed, 14 insertions(+), 13 deletions(-)

diff --git a/emain/emain-window.ts b/emain/emain-window.ts
index bb7598c79..8fb1f1d8c 100644
--- a/emain/emain-window.ts
+++ b/emain/emain-window.ts
@@ -23,7 +23,10 @@ export type WindowOpts = {
 };
 
 export const waveWindowMap = new Map<string, WaveBrowserWindow>(); // waveWindowId -> WaveBrowserWindow
-export let focusedWaveWindow = null; // on blur we do not set this to null (but on destroy we do)
+
+// on blur we do not set this to null (but on destroy we do), so this tracks the *last* focused window
+// e.g. it persists when the app itself is not focused
+export let focusedWaveWindow: WaveBrowserWindow = null;
 
 let cachedClientId: string = null;
 
@@ -62,7 +65,6 @@ function showCloseConfirmDialog(workspace: Workspace): boolean {
 export class WaveBrowserWindow extends BaseWindow {
     waveWindowId: string;
     workspaceId: string;
-    waveReadyPromise: Promise<void>;
     allLoadedTabViews: Map<string, WaveTabView>;
     activeTabView: WaveTabView;
     private canClose: boolean;
@@ -211,12 +213,7 @@ export class WaveBrowserWindow extends BaseWindow {
             setWasActive(true);
         });
         this.on("blur", () => {
-            if (this.isDestroyed()) {
-                return;
-            }
-            if (focusedWaveWindow == this) {
-                focusedWaveWindow = null;
-            }
+            // nothing for now
         });
         this.on("close", (e) => {
             if (this.canClose) {
@@ -470,6 +467,9 @@ export class WaveBrowserWindow extends BaseWindow {
     private async processActionQueue() {
         while (this.actionQueue.length > 0) {
             try {
+                if (this.isDestroyed()) {
+                    break;
+                }
                 const entry = this.actionQueue[0];
                 let tabId: string = null;
                 // have to use "===" here to get the typechecker to work :/
@@ -725,7 +725,6 @@ export async function createNewWaveWindow() {
         const existingWindowData = (await ObjectService.GetObject("window:" + existingWindowId)) as WaveWindow;
         if (existingWindowData != null) {
             const win = await createBrowserWindow(existingWindowData, fullConfig, { unamePlatform });
-            await win.waveReadyPromise;
             win.show();
             recreatedWindow = true;
         }
@@ -736,7 +735,6 @@ export async function createNewWaveWindow() {
     }
     console.log("creating new window");
     const newBrowserWindow = await createBrowserWindow(null, fullConfig, { unamePlatform });
-    await newBrowserWindow.waveReadyPromise;
     newBrowserWindow.show();
 }
 
@@ -768,7 +766,6 @@ export async function relaunchBrowserWindows() {
         wins.push(win);
     }
     for (const win of wins) {
-        await win.waveReadyPromise;
         console.log("show window", win.waveWindowId);
         win.show();
     }
diff --git a/emain/emain.ts b/emain/emain.ts
index b16d13f55..487f17c22 100644
--- a/emain/emain.ts
+++ b/emain/emain.ts
@@ -94,7 +94,6 @@ function handleWSEvent(evtMsg: WSEventType) {
             }
             const fullConfig = await services.FileService.GetFullConfig();
             const newWin = await createBrowserWindow(windowData, fullConfig, { unamePlatform });
-            await newWin.waveReadyPromise;
             newWin.show();
         } else if (evtMsg.eventtype == "electron:closewindow") {
             console.log("electron:closewindow", evtMsg.data);
@@ -378,6 +377,9 @@ function saveImageFileWithNativeDialog(defaultFileName: string, mimeType: string
         defaultFileName = "image";
     }
     const ww = focusedWaveWindow;
+    if (ww == null) {
+        return;
+    }
     const mimeToExtension: { [key: string]: string } = {
         "image/png": "png",
         "image/jpeg": "jpg",
diff --git a/emain/updater.ts b/emain/updater.ts
index 03a526e27..2c84feb8a 100644
--- a/emain/updater.ts
+++ b/emain/updater.ts
@@ -164,7 +164,9 @@ export class Updater {
                     type: "info",
                     message: "There are currently no updates available.",
                 };
-                dialog.showMessageBox(focusedWaveWindow, dialogOpts);
+                if (focusedWaveWindow) {
+                    dialog.showMessageBox(focusedWaveWindow, dialogOpts);
+                }
             }
 
             // Only update the last check time if this is an automatic check. This ensures the interval remains consistent.