autoupdater is using a destroyed window (#539)

This commit is contained in:
ABevier 2023-05-01 19:10:13 -04:00 committed by GitHub
parent a7e9e86da4
commit 9f2c698cf1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 6 deletions

View file

@ -127,7 +127,7 @@ function createMainWindow() {
mainWindow = createWindow(); mainWindow = createWindow();
} }
checkUpdater(mainWindow); checkUpdater(notifyMainWindow);
mainWindow.once("close", () => { mainWindow.once("close", () => {
mainWindow = null; mainWindow = null;
@ -197,7 +197,7 @@ app.on("open-url", (event, url) => {
}); });
function notifyMainWindow(channel: string, data: unknown) { function notifyMainWindow(channel: string, data: unknown) {
if (mainWindow) { if (mainWindow && !mainWindow.isDestroyed()) {
mainWindow.webContents.send(channel, data); mainWindow.webContents.send(channel, data);
} }
} }

View file

@ -1,6 +1,7 @@
import { type AppUpdater, autoUpdater } from "electron-updater"; import { type AppUpdater, autoUpdater } from "electron-updater";
import log from "./logger"; import log from "./logger";
import { BrowserWindow } from "electron"; import { BrowserWindow } from "electron";
import { MainWindowNotifier } from "./types";
type AutoUpdateStatus = { type AutoUpdateStatus = {
status: "up-to-date" | "available" | "ready"; status: "up-to-date" | "available" | "ready";
@ -9,7 +10,7 @@ type AutoUpdateStatus = {
autoUpdater.logger = log; autoUpdater.logger = log;
let window: BrowserWindow; let mainWindowNotifier: MainWindowNotifier | null = null;
let initalized = false; let initalized = false;
// keep the last status to resend to the window when it's opened becuase the store is destroyed when the window is closed // keep the last status to resend to the window when it's opened becuase the store is destroyed when the window is closed
@ -17,9 +18,9 @@ let lastStatus: AutoUpdateStatus = { status: "up-to-date" };
export const getUpdater = () => autoUpdater; export const getUpdater = () => autoUpdater;
export function checkUpdater(mainWindow: BrowserWindow): AppUpdater { export function checkUpdater(notifier: MainWindowNotifier): AppUpdater {
try { try {
window = mainWindow; mainWindowNotifier = notifier;
autoUpdater.checkForUpdatesAndNotify(); autoUpdater.checkForUpdatesAndNotify();
if (!initalized) { if (!initalized) {
@ -44,7 +45,9 @@ export function getAutoUpdateStatus() {
function sendStatusToWindow(status: AutoUpdateStatus) { function sendStatusToWindow(status: AutoUpdateStatus) {
lastStatus = status; lastStatus = status;
window?.webContents.send("app-update-status", status); if (mainWindowNotifier) {
mainWindowNotifier("app-update-status", status);
}
} }
autoUpdater.on("checking-for-update", () => { autoUpdater.on("checking-for-update", () => {
@ -52,6 +55,7 @@ autoUpdater.on("checking-for-update", () => {
}); });
autoUpdater.on("update-available", (info) => { autoUpdater.on("update-available", (info) => {
log.info("update-available", info);
sendStatusToWindow({ status: "available" }); sendStatusToWindow({ status: "available" });
}); });
@ -72,5 +76,6 @@ autoUpdater.on("download-progress", (progressObj) => {
}); });
autoUpdater.on("update-downloaded", (info) => { autoUpdater.on("update-downloaded", (info) => {
log.info("update-downloaded");
sendStatusToWindow({ status: "ready", version: info.version }); sendStatusToWindow({ status: "ready", version: info.version });
}); });