From c5732666705bb6f626dd3d63ec33f74ce6aa4a3c Mon Sep 17 00:00:00 2001 From: thzxx Date: Mon, 18 May 2026 10:51:38 +0800 Subject: [PATCH] fix: resolve did-finish-load race condition, remove unused import - Move did-finish-load listener before loadFile() to prevent missing the event - Remove unused nativeImage import --- main.js | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/main.js b/main.js index 7db877e..2f73d12 100644 --- a/main.js +++ b/main.js @@ -1,4 +1,4 @@ -const { app, BrowserWindow, dialog, ipcMain, Menu, nativeImage } = require('electron'); +const { app, BrowserWindow, dialog, ipcMain, Menu } = require('electron'); const path = require('path'); const fs = require('fs'); @@ -23,6 +23,14 @@ function createWindow() { show: false }); + // Register did-finish-load BEFORE loadFile to avoid race condition + mainWindow.webContents.on('did-finish-load', () => { + if (pendingFilePath) { + openFile(pendingFilePath); + pendingFilePath = null; + } + }); + // Load the index.html mainWindow.loadFile(path.join(__dirname, 'renderer', 'index.html')); @@ -261,16 +269,7 @@ app.whenReady().then(() => { if (fileToOpen) { pendingFilePath = fileToOpen; } - - // Open pending file once window is fully ready - if (pendingFilePath) { - mainWindow.webContents.on('did-finish-load', () => { - if (pendingFilePath) { - openFile(pendingFilePath); - pendingFilePath = null; - } - }); - } + // pendingFilePath will be opened by the did-finish-load handler in createWindow() }); app.on('window-all-closed', () => {