mirror of
https://github.com/hydralauncher/hydra.git
synced 2025-03-09 15:40:26 +00:00
feat: events working
This commit is contained in:
parent
3b17953a82
commit
811878e364
12 changed files with 182 additions and 82 deletions
|
|
@ -1,42 +1,79 @@
|
|||
import { AppUpdaterEvents } from "@types";
|
||||
import { registerEvent } from "../register-event";
|
||||
import updater, {
|
||||
ProgressInfo,
|
||||
UpdateDownloadedEvent,
|
||||
UpdateInfo,
|
||||
} from "electron-updater";
|
||||
import updater, { ProgressInfo, UpdateInfo } from "electron-updater";
|
||||
import { WindowManager } from "@main/services";
|
||||
import { app } from "electron";
|
||||
|
||||
const { autoUpdater } = updater;
|
||||
|
||||
const checkForUpdates = async (
|
||||
_event: Electron.IpcMainInvokeEvent,
|
||||
cb: (value: AppUpdaterEvents) => void
|
||||
) => {
|
||||
console.log("check for updates event");
|
||||
const checkForUpdates = async (_event: Electron.IpcMainInvokeEvent) => {
|
||||
const sendEvent = (event: AppUpdaterEvents) => {
|
||||
WindowManager.splashWindow?.webContents.send("autoUpdaterEvent", event);
|
||||
};
|
||||
|
||||
autoUpdater
|
||||
.addListener("error", (error: Error, message?: string) => {
|
||||
cb({ error, message });
|
||||
.once("error", () => {
|
||||
sendEvent({ type: "error" });
|
||||
})
|
||||
.addListener("checking-for-update", () => {
|
||||
cb("checking-for-updates");
|
||||
.once("checking-for-update", () => {
|
||||
sendEvent({ type: "checking-for-updates" });
|
||||
})
|
||||
.addListener("update-not-available", (info: UpdateInfo) => {
|
||||
cb(info);
|
||||
.once("update-not-available", (info: UpdateInfo) => {
|
||||
sendEvent({ type: "update-not-available", info });
|
||||
})
|
||||
.addListener("update-available", (info: UpdateInfo) => {
|
||||
cb(info);
|
||||
.once("update-available", (info: UpdateInfo) => {
|
||||
sendEvent({ type: "update-available", info });
|
||||
})
|
||||
.addListener("update-downloaded", (event: UpdateDownloadedEvent) => {
|
||||
cb(event);
|
||||
.once("update-downloaded", () => {
|
||||
sendEvent({ type: "update-downloaded" });
|
||||
})
|
||||
.addListener("download-progress", (info: ProgressInfo) => {
|
||||
cb(info);
|
||||
sendEvent({ type: "download-progress", info });
|
||||
})
|
||||
.addListener("update-cancelled", (info: UpdateInfo) => {
|
||||
cb(info);
|
||||
.once("update-cancelled", (info: UpdateInfo) => {
|
||||
sendEvent({ type: "update-cancelled", info });
|
||||
});
|
||||
|
||||
autoUpdater.checkForUpdates();
|
||||
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,
|
||||
bytesPerSecond: 4568,
|
||||
},
|
||||
});
|
||||
await sleep(500);
|
||||
}
|
||||
|
||||
sendEvent({ type: "update-downloaded" });
|
||||
}
|
||||
};
|
||||
|
||||
registerEvent("checkForUpdates", checkForUpdates);
|
||||
|
|
|
|||
9
src/main/events/autoupdater/continue-to-main-window.ts
Normal file
9
src/main/events/autoupdater/continue-to-main-window.ts
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
import { WindowManager } from "@main/services";
|
||||
import { registerEvent } from "../register-event";
|
||||
|
||||
const continueToMainWindow = async (_event: Electron.IpcMainInvokeEvent) => {
|
||||
WindowManager.splashWindow?.close();
|
||||
WindowManager.createMainWindow();
|
||||
};
|
||||
|
||||
registerEvent("continueToMainWindow", continueToMainWindow);
|
||||
17
src/main/events/autoupdater/restart-and-install-update.ts
Normal file
17
src/main/events/autoupdater/restart-and-install-update.ts
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
import { app } from "electron";
|
||||
import { registerEvent } from "../register-event";
|
||||
import updater from "electron-updater";
|
||||
import { WindowManager } from "@main/services";
|
||||
|
||||
const { autoUpdater } = updater;
|
||||
|
||||
const restartAndInstallUpdate = async (_event: Electron.IpcMainInvokeEvent) => {
|
||||
if (app.isPackaged) {
|
||||
autoUpdater.quitAndInstall();
|
||||
} else {
|
||||
WindowManager.splashWindow?.close();
|
||||
WindowManager.createMainWindow();
|
||||
}
|
||||
};
|
||||
|
||||
registerEvent("restartAndInstallUpdate", restartAndInstallUpdate);
|
||||
|
|
@ -28,6 +28,8 @@ import "./user-preferences/get-user-preferences";
|
|||
import "./user-preferences/update-user-preferences";
|
||||
import "./user-preferences/auto-launch";
|
||||
import "./autoupdater/check-for-updates";
|
||||
import "./autoupdater/restart-and-install-update";
|
||||
import "./autoupdater/continue-to-main-window";
|
||||
|
||||
ipcMain.handle("ping", () => "pong");
|
||||
ipcMain.handle("getVersion", () => app.getVersion());
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ import { resolveDatabaseUpdates, WindowManager } from "@main/services";
|
|||
import { dataSource } from "@main/data-source";
|
||||
import * as resources from "@locales";
|
||||
import { userPreferencesRepository } from "@main/repository";
|
||||
import electronLog from "electron-log";
|
||||
import electronLog, { MainLogger } from "electron-log";
|
||||
const { autoUpdater } = updater;
|
||||
|
||||
autoUpdater.setFeedURL({
|
||||
|
|
@ -17,7 +17,7 @@ autoUpdater.setFeedURL({
|
|||
});
|
||||
|
||||
autoUpdater.logger = electronLog;
|
||||
autoUpdater.logger.transports.file.level = "info";
|
||||
(autoUpdater.logger as MainLogger).transports.file.level = "info";
|
||||
|
||||
const gotTheLock = app.requestSingleInstanceLock();
|
||||
if (!gotTheLock) app.quit();
|
||||
|
|
@ -67,18 +67,7 @@ app.whenReady().then(() => {
|
|||
});
|
||||
|
||||
WindowManager.createSplashScreen();
|
||||
|
||||
WindowManager.createSystemTray(userPreferences?.language || "en");
|
||||
|
||||
WindowManager.splashWindow?.on("ready-to-show", () => {
|
||||
console.log("ready to show");
|
||||
autoUpdater.checkForUpdates().then((r) => {
|
||||
console.log(r);
|
||||
|
||||
//WindowManager.splashWindow?.close();
|
||||
//WindowManager.createMainWindow();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -57,7 +57,7 @@ export class WindowManager {
|
|||
this.splashWindow = new BrowserWindow({
|
||||
width: 400,
|
||||
height: 400,
|
||||
frame: true,
|
||||
frame: false,
|
||||
alwaysOnTop: false,
|
||||
webPreferences: {
|
||||
preload: path.join(__dirname, "../preload/index.mjs"),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue