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

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