Skip to content

Commit

Permalink
Auto updater
Browse files Browse the repository at this point in the history
  • Loading branch information
jameskerr committed Oct 31, 2023
1 parent 965c311 commit f832383
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 34 deletions.
102 changes: 82 additions & 20 deletions apps/zui/src/domain/updates/app-updater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,105 @@ import {autoUpdater} from "electron-updater"
import Updates from "src/js/state/Updates"
import {Store} from "src/js/state/types"

autoUpdater.autoDownload = false

class AppUpdater {
initialize(store: Store) {
const dispatch = store.dispatch
static interval = 1000 * 60 * 60 * 24 // 1 day
private unlisten = () => {}

initialize(
store: Store,
mode: "disabled" | "manual" | "startup" | "default"
) {
this.cleanup()
this.unlisten = this.listen(store.dispatch)

autoUpdater.on("update-available", (response) => {
switch (mode) {
case "disabled":
autoUpdater.autoDownload = false
autoUpdater.autoInstallOnAppQuit = false
break
case "manual":
autoUpdater.autoDownload = false
autoUpdater.autoInstallOnAppQuit = false
break
case "startup":
autoUpdater.autoDownload = true
autoUpdater.autoInstallOnAppQuit = true
this.check()
break
default:
autoUpdater.autoDownload = true
autoUpdater.autoInstallOnAppQuit = true
this.check()
this.scheduleCheck()
}
}

listen(dispatch) {
const onAvailable = (response) => {
dispatch(Updates.setIsChecking(false))
dispatch(Updates.setNextVersion(response.version))
})
autoUpdater.on("update-not-available", () => {
}
const onNotAvailable = () => {
dispatch(Updates.setIsChecking(false))
dispatch(Updates.setNextVersion(null))
})
autoUpdater.on("checking-for-update", (response) => {
console.log("checking", response)
}
const onChecking = () => {
dispatch(Updates.setIsChecking(true))
})
autoUpdater.on("download-progress", (response) => {
}
const onDownloadProgress = (response) => {
console.log("progress", response)
dispatch(Updates.setDownloadProgress(0.5))
})
autoUpdater.on("error", (error) => {
console.log("error", error)
}
const onError = (error) => {
dispatch(Updates.setError(error))
})
autoUpdater.on("update-downloaded", (response) => {
console.log("downloaded", response)
})
}

autoUpdater
.on("update-available", onAvailable)
.on("update-not-available", onNotAvailable)
.on("checking-for-update", onChecking)
.on("download-progress", onDownloadProgress)
.on("error", onError)

return () => {
autoUpdater
.off("update-available", onAvailable)
.off("update-not-available", onNotAvailable)
.off("checking-for-update", onChecking)
.off("download-progress", onDownloadProgress)
.off("error", onError)
}
}

check() {
autoUpdater.checkForUpdates()
}

download() {
autoUpdater.downloadUpdate()
return new Promise((resolve, reject) => {
autoUpdater.on("update-downloaded", resolve).on("error", reject)
autoUpdater.downloadUpdate()
})
}

install() {
autoUpdater.quitAndInstall()
}

private cleanup() {
this.unlisten()
this.cancelSchedule()
}

private scheduleId: any
private scheduleCheck() {
this.scheduleId = setTimeout(() => {
this.check()
this.scheduleCheck()
}, AppUpdater.interval)
}
private cancelSchedule() {
clearTimeout(this.scheduleId)
}
}

Expand Down
2 changes: 1 addition & 1 deletion apps/zui/src/domain/updates/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ import * as operations from "./operations"
export type UpdatesOperations = {
"updates.open": typeof operations.open
"updates.check": typeof operations.check
"updates.install": typeof operations.install
"updates.downloadAndInstall": typeof operations.downloadAndInstall
}
14 changes: 11 additions & 3 deletions apps/zui/src/domain/updates/operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,17 @@ export const check = createOperation("updates.check", async () => {
appUpdater.check()
})

export const install = createOperation("updates.install", async () => {
appUpdater.download()
})
export const downloadAndInstall = createOperation(
"updates.downloadAndInstall",
async () => {
try {
await appUpdater.download()
appUpdater.install()
} catch (e) {
console.log("Error", e)
}
}
)

// MANUAL FLOW
// 1. user click check for updates
Expand Down
25 changes: 16 additions & 9 deletions apps/zui/src/initializers/auto-update.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
import log from "electron-log"
import {setupAutoUpdater} from "../electron/autoUpdater"
// import log from "electron-log"
// import {setupAutoUpdater} from "../electron/autoUpdater"
import {MainObject} from "../core/main/main-object"
import isDev from "../electron/isDev"
// import isDev from "../electron/isDev"
import {appUpdater} from "src/domain/updates/app-updater"
import {configurations} from "src/zui"

export function initialize(main: MainObject) {
// autoUpdater should not run in dev, and will fail if the code has not been signed
if (!isDev && main.args.autoUpdater) {
setupAutoUpdater(main).catch((err) => {
log.error("Failed to initiate autoUpdater: " + err)
})
}
// if (!isDev && main.args.autoUpdater) {
// setupAutoUpdater(main).catch((err) => {
// log.error("Failed to initiate autoUpdater: " + err)
// })
// }

appUpdater.initialize(main.store)
let prev = null
main.store.subscribe(() => {
const mode = configurations.get("application", "updateMode")
if (mode !== prev) {
appUpdater.initialize(main.store, mode)
}
})
}
2 changes: 1 addition & 1 deletion apps/zui/src/views/update-window/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function useTemplate() {
const downloadProgress = useSelector(Updates.getDownloadProgress)
const error = useSelector(Updates.getError)
const closeWindow = () => invoke("window.close", globalThis.windowId)
const install = () => invoke("updates.install")
const install = () => invoke("updates.downloadAndInstall")
const check = () => invoke("updates.check")

switch (status) {
Expand Down

0 comments on commit f832383

Please sign in to comment.