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,73 +6,76 @@ import { app } from "electron";
const { autoUpdater } = updater; const { autoUpdater } = updater;
const checkForUpdates = async (_event: Electron.IpcMainInvokeEvent) => { const sendEvent = (event: AppUpdaterEvents) => {
const sendEvent = (event: AppUpdaterEvents) => { WindowManager.splashWindow?.webContents.send("autoUpdaterEvent", event);
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 autoUpdater
.once("error", () => { .addListener("error", () => {
sendEvent({ type: "error" }); sendEvent({ type: "error" });
}) })
.once("checking-for-update", () => { .addListener("checking-for-update", () => {
sendEvent({ type: "checking-for-updates" }); sendEvent({ type: "checking-for-updates" });
}) })
.once("update-not-available", (info: UpdateInfo) => { .addListener("update-not-available", (info: UpdateInfo) => {
sendEvent({ type: "update-not-available", info }); sendEvent({ type: "update-not-available", info });
}) })
.once("update-available", (info: UpdateInfo) => { .addListener("update-available", (info: UpdateInfo) => {
sendEvent({ type: "update-available", info }); sendEvent({ type: "update-available", info });
}) })
.once("update-downloaded", () => { .addListener("update-downloaded", () => {
sendEvent({ type: "update-downloaded" }); sendEvent({ type: "update-downloaded" });
}) })
.addListener("download-progress", (info: ProgressInfo) => { .addListener("download-progress", (info: ProgressInfo) => {
sendEvent({ type: "download-progress", info }); sendEvent({ type: "download-progress", info });
}) })
.once("update-cancelled", (info: UpdateInfo) => { .addListener("update-cancelled", (info: UpdateInfo) => {
sendEvent({ type: "update-cancelled", info }); sendEvent({ type: "update-cancelled", info });
}); });
if (app.isPackaged) { if (app.isPackaged) {
autoUpdater.checkForUpdates(); autoUpdater.checkForUpdates();
} else { } else {
// electron updater does not check for updates in dev build, so mocking here to test the ui await mockValuesForDebug();
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" });
} }
}; };

View file

@ -1,7 +1,9 @@
import { WindowManager } from "@main/services"; import { WindowManager } from "@main/services";
import { registerEvent } from "../register-event"; import { registerEvent } from "../register-event";
import { autoUpdater } from "electron-updater";
const continueToMainWindow = async (_event: Electron.IpcMainInvokeEvent) => { const continueToMainWindow = async (_event: Electron.IpcMainInvokeEvent) => {
autoUpdater.removeAllListeners();
WindowManager.splashWindow?.close(); WindowManager.splashWindow?.close();
WindowManager.createMainWindow(); WindowManager.createMainWindow();
}; };

View file

@ -9,6 +9,7 @@ const restartAndInstallUpdate = async (_event: Electron.IpcMainInvokeEvent) => {
if (app.isPackaged) { if (app.isPackaged) {
autoUpdater.quitAndInstall(true, true); autoUpdater.quitAndInstall(true, true);
} else { } else {
autoUpdater.removeAllListeners();
WindowManager.splashWindow?.close(); WindowManager.splashWindow?.close();
WindowManager.createMainWindow(); WindowManager.createMainWindow();
} }