diff --git a/src/main/events/autoupdater/check-for-updates.ts b/src/main/events/autoupdater/check-for-updates.ts index fc4f7d20..22437eec 100644 --- a/src/main/events/autoupdater/check-for-updates.ts +++ b/src/main/events/autoupdater/check-for-updates.ts @@ -6,73 +6,76 @@ import { app } from "electron"; const { autoUpdater } = updater; -const checkForUpdates = async (_event: Electron.IpcMainInvokeEvent) => { - const sendEvent = (event: AppUpdaterEvents) => { - WindowManager.splashWindow?.webContents.send("autoUpdaterEvent", event); - }; +const sendEvent = (event: AppUpdaterEvents) => { + WindowManager.splashWindow?.webContents.send("autoUpdaterEvent", event); +}; +const mockValuesForDebug = async () => { + const sleep = (ms: number) => + new Promise((resolve) => setTimeout(resolve, ms)); + + sendEvent({ type: "checking-for-updates" }); + + await sleep(1500); + sendEvent({ + type: "update-available", + info: { + version: "1.2.3", + files: [], + releaseDate: "19/05/2024", + path: "", + sha512: "", + }, + }); + + await sleep(1000); + + const total = 123456; + for (let i = 0; i <= 5; i++) { + sendEvent({ + type: "download-progress", + info: { + total: total, + delta: 123, + transferred: (total * i) / 5, + percent: ((total * i) / 5 / total) * 100, + bytesPerSecond: 4568, + }, + }); + await sleep(1000); + } + + sendEvent({ type: "update-downloaded" }); +}; + +const checkForUpdates = async (_event: Electron.IpcMainInvokeEvent) => { autoUpdater - .once("error", () => { + .addListener("error", () => { sendEvent({ type: "error" }); }) - .once("checking-for-update", () => { + .addListener("checking-for-update", () => { sendEvent({ type: "checking-for-updates" }); }) - .once("update-not-available", (info: UpdateInfo) => { + .addListener("update-not-available", (info: UpdateInfo) => { sendEvent({ type: "update-not-available", info }); }) - .once("update-available", (info: UpdateInfo) => { + .addListener("update-available", (info: UpdateInfo) => { sendEvent({ type: "update-available", info }); }) - .once("update-downloaded", () => { + .addListener("update-downloaded", () => { sendEvent({ type: "update-downloaded" }); }) .addListener("download-progress", (info: ProgressInfo) => { sendEvent({ type: "download-progress", info }); }) - .once("update-cancelled", (info: UpdateInfo) => { + .addListener("update-cancelled", (info: UpdateInfo) => { sendEvent({ type: "update-cancelled", info }); }); if (app.isPackaged) { autoUpdater.checkForUpdates(); } else { - // electron updater does not check for updates in dev build, so mocking here to test the ui - const sleep = (ms: number) => - new Promise((resolve) => setTimeout(resolve, ms)); - - sendEvent({ type: "checking-for-updates" }); - - await sleep(1500); - sendEvent({ - type: "update-available", - info: { - version: "1.2.2", - files: [], - releaseDate: "19/05/2024", - path: "", - sha512: "", - }, - }); - - await sleep(500); - - const total = 123456; - for (let i = 0; i <= 5; i++) { - sendEvent({ - type: "download-progress", - info: { - total: total, - delta: 123, - transferred: (total * i) / 5, - percent: ((total * i) / 5 / total) * 100, - bytesPerSecond: 4568, - }, - }); - await sleep(1000); - } - - sendEvent({ type: "update-downloaded" }); + await mockValuesForDebug(); } }; diff --git a/src/main/events/autoupdater/continue-to-main-window.ts b/src/main/events/autoupdater/continue-to-main-window.ts index f8b2bb2c..2bbbe5cc 100644 --- a/src/main/events/autoupdater/continue-to-main-window.ts +++ b/src/main/events/autoupdater/continue-to-main-window.ts @@ -1,7 +1,9 @@ import { WindowManager } from "@main/services"; import { registerEvent } from "../register-event"; +import { autoUpdater } from "electron-updater"; const continueToMainWindow = async (_event: Electron.IpcMainInvokeEvent) => { + autoUpdater.removeAllListeners(); WindowManager.splashWindow?.close(); WindowManager.createMainWindow(); }; diff --git a/src/main/events/autoupdater/restart-and-install-update.ts b/src/main/events/autoupdater/restart-and-install-update.ts index e0e9495c..b99a1a36 100644 --- a/src/main/events/autoupdater/restart-and-install-update.ts +++ b/src/main/events/autoupdater/restart-and-install-update.ts @@ -9,6 +9,7 @@ const restartAndInstallUpdate = async (_event: Electron.IpcMainInvokeEvent) => { if (app.isPackaged) { autoUpdater.quitAndInstall(true, true); } else { + autoUpdater.removeAllListeners(); WindowManager.splashWindow?.close(); WindowManager.createMainWindow(); }