refactor events

This commit is contained in:
Zamitto 2024-05-20 14:57:23 -03:00
parent ae6edaa058
commit 6fdb591784
3 changed files with 52 additions and 46 deletions

View file

@ -6,38 +6,11 @@ import { app } from "electron";
const { autoUpdater } = updater;
const checkForUpdates = async (_event: Electron.IpcMainInvokeEvent) => {
const sendEvent = (event: AppUpdaterEvents) => {
WindowManager.splashWindow?.webContents.send("autoUpdaterEvent", event);
};
autoUpdater
.once("error", () => {
sendEvent({ type: "error" });
})
.once("checking-for-update", () => {
sendEvent({ type: "checking-for-updates" });
})
.once("update-not-available", (info: UpdateInfo) => {
sendEvent({ type: "update-not-available", info });
})
.once("update-available", (info: UpdateInfo) => {
sendEvent({ type: "update-available", info });
})
.once("update-downloaded", () => {
sendEvent({ type: "update-downloaded" });
})
.addListener("download-progress", (info: ProgressInfo) => {
sendEvent({ type: "download-progress", info });
})
.once("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 mockValuesForDebug = async () => {
const sleep = (ms: number) =>
new Promise((resolve) => setTimeout(resolve, ms));
@ -47,7 +20,7 @@ const checkForUpdates = async (_event: Electron.IpcMainInvokeEvent) => {
sendEvent({
type: "update-available",
info: {
version: "1.2.2",
version: "1.2.3",
files: [],
releaseDate: "19/05/2024",
path: "",
@ -55,7 +28,7 @@ const checkForUpdates = async (_event: Electron.IpcMainInvokeEvent) => {
},
});
await sleep(500);
await sleep(1000);
const total = 123456;
for (let i = 0; i <= 5; i++) {
@ -73,6 +46,36 @@ const checkForUpdates = async (_event: Electron.IpcMainInvokeEvent) => {
}
sendEvent({ type: "update-downloaded" });
};
const checkForUpdates = async (_event: Electron.IpcMainInvokeEvent) => {
autoUpdater
.addListener("error", () => {
sendEvent({ type: "error" });
})
.addListener("checking-for-update", () => {
sendEvent({ type: "checking-for-updates" });
})
.addListener("update-not-available", (info: UpdateInfo) => {
sendEvent({ type: "update-not-available", info });
})
.addListener("update-available", (info: UpdateInfo) => {
sendEvent({ type: "update-available", info });
})
.addListener("update-downloaded", () => {
sendEvent({ type: "update-downloaded" });
})
.addListener("download-progress", (info: ProgressInfo) => {
sendEvent({ type: "download-progress", info });
})
.addListener("update-cancelled", (info: UpdateInfo) => {
sendEvent({ type: "update-cancelled", info });
});
if (app.isPackaged) {
autoUpdater.checkForUpdates();
} else {
await mockValuesForDebug();
}
};

View file

@ -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();
};

View file

@ -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();
}