Skip to content

Commit

Permalink
fixed auto updates
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom4nt committed Sep 24, 2023
1 parent 867fb54 commit bdec3a2
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 19 deletions.
2 changes: 1 addition & 1 deletion dev-app-update.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
owner: Tom4nt
repo: Mega-Soundboard
provider: github
updaterCacheDirName: mega-soundboard-updater
updaterCacheDirName: mega-soundboard-updater
22 changes: 14 additions & 8 deletions src/main/updater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,17 @@ export default class Updater {

autoUpdater.on("update-downloaded", () => {
this._state = "downloaded";
EventSender.send("onUpdateReady");
EventSender.send("onUpdateStateChanged", "downloaded");
});

autoUpdater.on("update-available", () => {
this._state = "downloading";
EventSender.send("onUpdateStateChanged", "downloading");
});

autoUpdater.on("update-not-available", () => {
this._state = "upToDate";
EventSender.send("onUpdateStateChanged", "upToDate");
});

const hour = 60 * 60 * 1000;
Expand All @@ -29,12 +39,8 @@ export default class Updater {
if (this._state === "downloading" || this._state === "downloaded") {
return this.state;
}
const result = await autoUpdater.checkForUpdates();
if (result) {
this._state = "downloading";
} else {
this._state = "upToDate";
}
return this.state;
await autoUpdater.checkForUpdates();
// FWIK, there is no way to get if the update is available or not from the return value.
return this.state; // The state is updated on the event handlers instead.
}
}
9 changes: 6 additions & 3 deletions src/renderer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import AudioManager from "./audioManager";
import GlobalEvents from "./util/globalEvents";
import * as MessageQueue from "./messageQueue";
import Utils from "./util/utils";
import { UpdaterState } from "../shared/interfaces";

//#region Elements

Expand Down Expand Up @@ -366,9 +367,11 @@ function showSoundDragTutorial(): void {

//#region Main Events

GlobalEvents.addHandler("onUpdateReady", () => {
updateButton.style.display = "inherit";
console.log("READY TO UPDATE");
GlobalEvents.addHandler("onUpdateStateChanged", (state: UpdaterState) => {
if (state == "downloaded") {
updateButton.style.display = "inherit";
console.log("READY TO UPDATE");
}
});

GlobalEvents.addHandler("onWindowFocusChanged", s => {
Expand Down
16 changes: 10 additions & 6 deletions src/renderer/modals/msModal.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { UpdaterState } from "../../shared/interfaces";
import { Modal, NewsModal } from "../modals";
import GlobalEvents from "../util/globalEvents";

Expand All @@ -22,7 +23,7 @@ export default class MSModal extends Modal {
protected override disconnectedCallback(): void {
super.disconnectedCallback();
if (this.isListeningToUpdateReady)
GlobalEvents.removeHandler("onUpdateReady", this.handleUpdateReady);
GlobalEvents.removeHandler("onUpdateStateChanged", this.handleUpdateStateChanged);
}

getContent(): HTMLElement[] {
Expand Down Expand Up @@ -90,13 +91,14 @@ export default class MSModal extends Modal {
}

private async checkUpdate(): Promise<void> {
const state = await window.actions.checkUpdate();
this.updateInfoElement.innerText = "Checking...";
this.updateInfoElement.style.display = "";
const state = await window.actions.checkUpdate();
switch (state) {
case "downloading":
this.updateInfoElement.innerText = "The update is being downloaded...";
if (!this.isListeningToUpdateReady)
GlobalEvents.addHandler("onUpdateReady", this.handleUpdateReady);
GlobalEvents.addHandler("onUpdateStateChanged", this.handleUpdateStateChanged);
this.isListeningToUpdateReady = true;
break;
case "downloaded":
Expand All @@ -118,8 +120,10 @@ export default class MSModal extends Modal {
this.updateButton.innerHTML = "<span>Restart</span>";
}

private handleUpdateReady = (): void => {
this.isUpdateReady = true;
this.showUpdateReady();
private handleUpdateStateChanged = (state: UpdaterState): void => {
if (state == "downloaded") {
this.isUpdateReady = true;
this.showUpdateReady();
}
};
}
3 changes: 2 additions & 1 deletion src/shared/ipcEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
SoundAddedArgs,
SoundboardAddedArgs,
SoundChangedArgs,
UpdaterState,
WindowState
} from "./interfaces";
import { Settings, Sound, Soundboard } from "./models";
Expand All @@ -27,7 +28,7 @@ class ConcreteEvents {
onKeybindPressed: ExposedEvent<number[]> | null = null;
onCurrentSoundboardChanged: ExposedEvent<Soundboard> | null = null;
onMinToTrayChanged: ExposedEvent<boolean> | null = null;
onUpdateReady: ExposedEvent<void> | null = null;
onUpdateStateChanged: ExposedEvent<UpdaterState> | null = null;
onSoundPlayRequested: ExposedEvent<Sound> | null = null;
onStopAllSounds: ExposedEvent<void> | null = null;
onZoomFactorChanged: ExposedEvent<number> | null = null;
Expand Down

0 comments on commit bdec3a2

Please sign in to comment.