mirror of
https://github.com/Myxelium/Bridge-Multi.git
synced 2026-04-11 22:29:38 +00:00
Restructure; use DaisyUI
This commit is contained in:
@@ -1,135 +1,71 @@
|
||||
import { autoUpdater, UpdateInfo } from 'electron-updater'
|
||||
import { inspect } from 'util'
|
||||
|
||||
import { emitIPCEvent } from '../main'
|
||||
import { IPCEmitHandler, IPCInvokeHandler } from '../shared/IPCHandler'
|
||||
import { UpdateProgress } from '../../src-shared/interfaces/update.interface'
|
||||
import { emitIpcEvent } from '../main'
|
||||
|
||||
export interface UpdateProgress {
|
||||
bytesPerSecond: number
|
||||
percent: number
|
||||
transferred: number
|
||||
total: number
|
||||
let updateAvailable: boolean | null = false
|
||||
|
||||
autoUpdater.autoDownload = false
|
||||
autoUpdater.logger = null
|
||||
|
||||
autoUpdater.on('error', (err: Error) => {
|
||||
updateAvailable = null
|
||||
emitIpcEvent('updateError', inspect(err))
|
||||
})
|
||||
|
||||
autoUpdater.on('update-available', (info: UpdateInfo) => {
|
||||
updateAvailable = true
|
||||
emitIpcEvent('updateAvailable', info)
|
||||
})
|
||||
|
||||
autoUpdater.on('update-not-available', () => {
|
||||
updateAvailable = false
|
||||
emitIpcEvent('updateAvailable', null)
|
||||
})
|
||||
|
||||
|
||||
export async function retryUpdate() {
|
||||
try {
|
||||
await autoUpdater.checkForUpdates()
|
||||
} catch (err) {
|
||||
updateAvailable = null
|
||||
emitIpcEvent('updateError', inspect(err))
|
||||
}
|
||||
}
|
||||
|
||||
let updateAvailable = false
|
||||
export async function getUpdateAvailable() {
|
||||
return updateAvailable
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks for updates when the program is launched.
|
||||
* @returns the current version of Bridge.
|
||||
*/
|
||||
class UpdateChecker implements IPCEmitHandler<'retry-update'> {
|
||||
event = 'retry-update' as const
|
||||
|
||||
constructor() {
|
||||
autoUpdater.autoDownload = false
|
||||
autoUpdater.logger = null
|
||||
this.registerUpdaterListeners()
|
||||
}
|
||||
|
||||
/**
|
||||
* Check for an update.
|
||||
*/
|
||||
handler() {
|
||||
this.checkForUpdates()
|
||||
}
|
||||
|
||||
checkForUpdates() {
|
||||
autoUpdater.checkForUpdates().catch(reason => {
|
||||
updateAvailable = null
|
||||
emitIPCEvent('update-error', reason)
|
||||
})
|
||||
}
|
||||
|
||||
private registerUpdaterListeners() {
|
||||
autoUpdater.on('error', (err: Error) => {
|
||||
updateAvailable = null
|
||||
emitIPCEvent('update-error', err)
|
||||
})
|
||||
|
||||
autoUpdater.on('update-available', (info: UpdateInfo) => {
|
||||
updateAvailable = true
|
||||
emitIPCEvent('update-available', info)
|
||||
})
|
||||
|
||||
autoUpdater.on('update-not-available', (info: UpdateInfo) => {
|
||||
updateAvailable = false
|
||||
emitIPCEvent('update-available', null)
|
||||
})
|
||||
}
|
||||
export async function getCurrentVersion() {
|
||||
return autoUpdater.currentVersion.raw
|
||||
}
|
||||
|
||||
export const updateChecker = new UpdateChecker()
|
||||
|
||||
/**
|
||||
* Handles the 'get-update-available' event.
|
||||
* Begins the process of downloading the latest update.
|
||||
*/
|
||||
class GetUpdateAvailableHandler implements IPCInvokeHandler<'get-update-available'> {
|
||||
event = 'get-update-available' as const
|
||||
export function downloadUpdate() {
|
||||
if (this.downloading) { return }
|
||||
this.downloading = true
|
||||
|
||||
/**
|
||||
* @returns `true` if an update is available.
|
||||
*/
|
||||
handler() {
|
||||
return updateAvailable
|
||||
}
|
||||
autoUpdater.on('download-progress', (updateProgress: UpdateProgress) => {
|
||||
emitIpcEvent('updateProgress', updateProgress)
|
||||
})
|
||||
|
||||
autoUpdater.on('update-downloaded', () => {
|
||||
emitIpcEvent('updateDownloaded', undefined)
|
||||
})
|
||||
|
||||
autoUpdater.downloadUpdate()
|
||||
}
|
||||
|
||||
export const getUpdateAvailableHandler = new GetUpdateAvailableHandler()
|
||||
|
||||
/**
|
||||
* Handles the 'get-current-version' event.
|
||||
* Immediately closes the application and installs the update.
|
||||
*/
|
||||
class GetCurrentVersionHandler implements IPCInvokeHandler<'get-current-version'> {
|
||||
event = 'get-current-version' as const
|
||||
|
||||
/**
|
||||
* @returns the current version of Bridge.
|
||||
*/
|
||||
handler() {
|
||||
return autoUpdater.currentVersion.raw
|
||||
}
|
||||
export function quitAndInstall() {
|
||||
autoUpdater.quitAndInstall() // autoUpdater installs a downloaded update on the next program restart by default
|
||||
}
|
||||
|
||||
export const getCurrentVersionHandler = new GetCurrentVersionHandler()
|
||||
|
||||
/**
|
||||
* Handles the 'download-update' event.
|
||||
*/
|
||||
class DownloadUpdateHandler implements IPCEmitHandler<'download-update'> {
|
||||
event = 'download-update' as const
|
||||
downloading = false
|
||||
|
||||
/**
|
||||
* Begins the process of downloading the latest update.
|
||||
*/
|
||||
handler() {
|
||||
if (this.downloading) { return }
|
||||
this.downloading = true
|
||||
|
||||
autoUpdater.on('download-progress', (updateProgress: UpdateProgress) => {
|
||||
emitIPCEvent('update-progress', updateProgress)
|
||||
})
|
||||
|
||||
autoUpdater.on('update-downloaded', () => {
|
||||
emitIPCEvent('update-downloaded', undefined)
|
||||
})
|
||||
|
||||
autoUpdater.downloadUpdate()
|
||||
}
|
||||
}
|
||||
|
||||
export const downloadUpdateHandler = new DownloadUpdateHandler()
|
||||
|
||||
/**
|
||||
* Handles the 'quit-and-install' event.
|
||||
*/
|
||||
class QuitAndInstallHandler implements IPCEmitHandler<'quit-and-install'> {
|
||||
event = 'quit-and-install' as const
|
||||
|
||||
/**
|
||||
* Immediately closes the application and installs the update.
|
||||
*/
|
||||
handler() {
|
||||
autoUpdater.quitAndInstall() // autoUpdater installs a downloaded update on the next program restart by default
|
||||
}
|
||||
}
|
||||
|
||||
export const quitAndInstallHandler = new QuitAndInstallHandler()
|
||||
|
||||
Reference in New Issue
Block a user