Fixed 2 Electron Crashes (#288)

Fixes #277

* fixed crash on attempting to open new instance, required refactoring BrowserWindow to global

Uncaught Exception:
ReferenceError: win is not defined
    at click (C:\Users\jacks\AppData\Local\Programs\better-chatgpt\resources\app.asar\electron\index.cjs:53:9)
    at MenuItem.click (node:electron/js2c/browser_init:2:30166)
    at a._executeCommand (node:electron/js2c/browser_init:2:35562)

* fixed crash on attempting to open new instance, required refactoring BrowserWindow to global

Uncaught Exception:
ReferenceError: win is not defined
    at click (C:\Users\jacks\AppData\Local\Programs\better-chatgpt\resources\app.asar\electron\index.cjs:53:9)
    at MenuItem.click (node:electron/js2c/browser_init:2:30166)
    at a._executeCommand (node:electron/js2c/browser_init:2:35562)

* fixed Electron process not closing on crash

previously left the process still existing on crash (only viewable in task manager). error behavior is the same as it was before, just now must be explicitly defined.
This commit is contained in:
Jack Schedel 2023-05-10 11:54:22 -04:00 committed by GitHub
parent 58cafd20a5
commit 82f8761397
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,8 +1,10 @@
const path = require('path'); const path = require('path');
const { app, BrowserWindow, Tray, Menu } = require('electron'); const {dialog, app, BrowserWindow, Tray, Menu } = require('electron');
const isDev = require('electron-is-dev'); const isDev = require('electron-is-dev');
const { autoUpdater } = require('electron-updater'); const { autoUpdater } = require('electron-updater');
let win = null;
const instanceLock = app.requestSingleInstanceLock();
if (require('electron-squirrel-startup')) app.quit(); if (require('electron-squirrel-startup')) app.quit();
@ -17,7 +19,7 @@ function createWindow() {
} }
autoUpdater.checkForUpdatesAndNotify(); autoUpdater.checkForUpdatesAndNotify();
const win = new BrowserWindow({ win = new BrowserWindow({
autoHideMenuBar: true, autoHideMenuBar: true,
show: false, show: false,
icon: iconPath, icon: iconPath,
@ -73,20 +75,35 @@ const createTray = (window) => {
return tray; return tray;
}; };
app.whenReady().then(createWindow);
app.on('window-all-closed', () => { app.on('window-all-closed', () => {
if (process.platform !== 'darwin') { if (process.platform !== 'darwin') {
app.quit(); app.quit();
} }
}); });
app.on('activate', () => { process.on('uncaughtException', (error) => {
if (BrowserWindow.getAllWindows().length === 0) { // Perform any necessary cleanup tasks here
createWindow(); dialog.showErrorBox('An error occurred', error.stack);
}
// Exit the app
process.exit(1);
}); });
if (!instanceLock) {
app.quit()
} else {
app.on('second-instance', (event, commandLine, workingDirectory) => {
if (win) {
if (win.isMinimized()) win.restore()
win.focus()
}
})
app.whenReady().then(() => {
win = createWindow()
})
}
const createServer = () => { const createServer = () => {
// Dependencies // Dependencies
const http = require('http'); const http = require('http');