From ac540b09beadca07fd0c3c7d58b26c7616c27241 Mon Sep 17 00:00:00 2001 From: sw3205933776 <3205933776@qq.com> Date: Mon, 20 Oct 2025 17:59:46 +0800 Subject: [PATCH] fix: implement protocol URL queue to handle requests before window ready --- electron/main/index.ts | 42 ++++++++++++++++++++++++++++++++++++++++++ src/api/http.ts | 2 +- 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/electron/main/index.ts b/electron/main/index.ts index 682f3d8ca..548468bfc 100644 --- a/electron/main/index.ts +++ b/electron/main/index.ts @@ -40,6 +40,10 @@ let python_process: ChildProcessWithoutNullStreams | null = null; let backendPort: number = 5001; let browser_port = 9222; +// Protocol URL queue for handling URLs before window is ready +let protocolUrlQueue: string[] = []; +let isWindowReady = false; + // ==================== path config ==================== const preload = path.join(__dirname, '../preload/index.mjs'); const indexHtml = path.join(RENDERER_DIST, 'index.html'); @@ -97,6 +101,19 @@ const setupProtocolHandlers = () => { // ==================== protocol url handle ==================== function handleProtocolUrl(url: string) { log.info('enter handleProtocolUrl', url); + + // If window is not ready, queue the URL + if (!isWindowReady || !win || win.isDestroyed()) { + log.info('Window not ready, queuing protocol URL:', url); + protocolUrlQueue.push(url); + return; + } + + processProtocolUrl(url); +} + +// Process a single protocol URL +function processProtocolUrl(url: string) { const urlObj = new URL(url); const code = urlObj.searchParams.get('code'); const share_token = urlObj.searchParams.get('share_token'); @@ -130,6 +147,19 @@ function handleProtocolUrl(url: string) { } } +// Process all queued protocol URLs +function processQueuedProtocolUrls() { + if (protocolUrlQueue.length > 0) { + log.info('Processing queued protocol URLs:', protocolUrlQueue.length); + const urls = [...protocolUrlQueue]; + protocolUrlQueue = []; + + urls.forEach(url => { + processProtocolUrl(url); + }); + } +} + // ==================== single instance lock ==================== const setupSingleInstanceLock = () => { const gotLock = app.requestSingleInstanceLock(); @@ -1114,6 +1144,11 @@ async function createWindow() { }); }); + // Mark window as ready and process any queued protocol URLs + isWindowReady = true; + log.info('Window is ready, processing queued protocol URLs...'); + processQueuedProtocolUrls(); + // Now check and install dependencies let res:PromiseReturnType = await checkAndInstallDepsOnUpdate({ win }); if (!res.success) { @@ -1346,7 +1381,10 @@ app.on('window-all-closed', () => { webViewManager = null; } + // Reset window state win = null; + isWindowReady = false; + protocolUrlQueue = []; if (process.platform !== 'darwin') { app.quit(); @@ -1399,6 +1437,10 @@ app.on('before-quit', async (event) => { global.gc(); } + // Reset protocol handling state + isWindowReady = false; + protocolUrlQueue = []; + log.info('All cleanup completed, exiting...'); } catch (error) { log.error('Error during cleanup:', error); diff --git a/src/api/http.ts b/src/api/http.ts index 39960be94..ae1870527 100644 --- a/src/api/http.ts +++ b/src/api/http.ts @@ -170,7 +170,7 @@ async function proxyFetchRequest( ...customHeaders, } - console.debug('url', url, token) + // console.debug('url', url, token) if (!url.includes('http://') && !url.includes('https://') && token) { headers['Authorization'] = `Bearer ${token}` }