Skip to content

Commit

Permalink
fix: When cancel download should notify last update info.
Browse files Browse the repository at this point in the history
  • Loading branch information
yanguoyu committed Jul 31, 2023
1 parent e9d2c31 commit c90b01d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 30 deletions.
61 changes: 33 additions & 28 deletions packages/neuron-wallet/src/controllers/update.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { autoUpdater, UpdateInfo, CancellationToken } from 'electron-updater'
import AppUpdaterSubject from '../models/subjects/app-updater'
import AppUpdaterSubject, { AppUpdater } from '../models/subjects/app-updater'

export default class UpdateController {
static isChecking = false // One instance is already running and checking

static downCancellationToken = new CancellationToken()

static lastNotifyInfo: AppUpdater

constructor(check: boolean = true) {
autoUpdater.autoDownload = false

Expand Down Expand Up @@ -34,14 +36,14 @@ export default class UpdateController {
}

public downloadUpdate() {
this.notify(0)
this.notify({ downloadProgress: 0 })
UpdateController.downCancellationToken = new CancellationToken()
autoUpdater.downloadUpdate(UpdateController.downCancellationToken)
}

public cancelDownloadUpdate() {
UpdateController.downCancellationToken.cancel()
this.notify()
this.notify(UpdateController.lastNotifyInfo)
autoUpdater.removeAllListeners()
}

Expand All @@ -50,54 +52,57 @@ export default class UpdateController {

autoUpdater.on('error', error => {
UpdateController.isChecking = false
this.notify(-1, null, false, '', '', '', error == null ? 'unknown' : (error.stack || error).toString())
this.notify({
version: '',
releaseDate: '',
releaseNotes: '',
errorMsg: error == null ? 'unknown' : (error.stack || error).toString(),
})
})

autoUpdater.on('update-available', (info: UpdateInfo) => {
if (UpdateController.isChecking) {
UpdateController.isChecking = false
this.notify(-1, null, false, info.version, info.releaseDate, info.releaseNotes as string)
this.notify({
version: info.version,
releaseDate: info.releaseDate,
releaseNotes: info.releaseNotes as string,
})
}
})

autoUpdater.on('update-not-available', () => {
if (UpdateController.isChecking) {
UpdateController.isChecking = false
this.notify(-1, null, true)
this.notify({ isUpdated: true })
}
})

autoUpdater.on('download-progress', progress => {
const progressPercent = progress.percent / 100
autoUpdater.on('download-progress', progressInfo => {
const progressPercent = progressInfo.percent / 100
if (progressPercent !== 1) {
this.notify(progressPercent, progress)
this.notify({ downloadProgress: progressPercent, progressInfo })
}
})

autoUpdater.on('update-downloaded', () => {
UpdateController.isChecking = false
this.notify(1)
this.notify({ downloadProgress: 1 })
})
}

private notify(
downloadProgress: number = -1,
progressInfo = null,
isUpdated = false,
version = '',
releaseDate = '',
releaseNotes = '',
errorMsg = ''
) {
AppUpdaterSubject.next({
downloadProgress,
progressInfo,
isUpdated,
version,
releaseDate,
releaseNotes,
errorMsg,
private notify(appUpdater?: Partial<Omit<AppUpdater, 'checking'>>) {
UpdateController.lastNotifyInfo = {
downloadProgress: -1,
progressInfo: null,
isUpdated: false,
version: '',
releaseDate: '',
releaseNotes: '',
errorMsg: '',
...appUpdater,
checking: UpdateController.isChecking,
})
}
AppUpdaterSubject.next(UpdateController.lastNotifyInfo)
}
}
6 changes: 4 additions & 2 deletions packages/neuron-wallet/src/models/subjects/app-updater.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Subject } from 'rxjs'

const AppUpdaterSubject = new Subject<{
export interface AppUpdater {
checking: boolean
isUpdated: boolean
downloadProgress: number // -1: not started, 1: finished, 0~1: downloading
Expand All @@ -13,6 +13,8 @@ const AppUpdaterSubject = new Subject<{
releaseDate: string
releaseNotes: string
errorMsg: string
}>()
}

const AppUpdaterSubject = new Subject<AppUpdater>()

export default AppUpdaterSubject

0 comments on commit c90b01d

Please sign in to comment.