mirror of
https://github.com/Myxelium/Bridge-Multi.git
synced 2026-04-11 22:29:38 +00:00
Added update ipc handler
This commit is contained in:
99
src/electron/ipc/UpdateHandler.ipc.ts
Normal file
99
src/electron/ipc/UpdateHandler.ipc.ts
Normal file
@@ -0,0 +1,99 @@
|
||||
import { IPCEmitHandler, IPCInvokeHandler } from '../shared/IPCHandler'
|
||||
import { autoUpdater, UpdateInfo } from 'electron-updater'
|
||||
import { emitIPCEvent } from '../main'
|
||||
|
||||
export interface UpdateProgress {
|
||||
bytesPerSecond: number
|
||||
percent: number
|
||||
transferred: number
|
||||
total: number
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks for updates when the program is launched.
|
||||
*/
|
||||
class UpdateChecker {
|
||||
|
||||
constructor() {
|
||||
autoUpdater.autoDownload = false
|
||||
autoUpdater.logger = null
|
||||
this.registerUpdaterListeners()
|
||||
autoUpdater.checkForUpdates()
|
||||
}
|
||||
|
||||
private registerUpdaterListeners() {
|
||||
autoUpdater.on('error', (err: Error) => {
|
||||
console.log('token:', process.env.GH_TOKEN)
|
||||
console.log('error callback', err)
|
||||
emitIPCEvent('update-error', err)
|
||||
})
|
||||
|
||||
autoUpdater.on('update-available', (info: UpdateInfo) => {
|
||||
console.log('update available callback', info)
|
||||
emitIPCEvent('update-available', info)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
new UpdateChecker()
|
||||
|
||||
/**
|
||||
* Handles the 'get-current-version' event.
|
||||
*/
|
||||
class GetCurrentVersionHandler implements IPCInvokeHandler<'get-current-version'> {
|
||||
event: 'get-current-version' = 'get-current-version'
|
||||
|
||||
/**
|
||||
* @returns the current version of Bridge.
|
||||
*/
|
||||
handler() {
|
||||
console.log('Printing version:', autoUpdater.currentVersion.raw)
|
||||
return autoUpdater.currentVersion.raw // TODO: display this on the about page
|
||||
}
|
||||
}
|
||||
|
||||
export const getCurrentVersionHandler = new GetCurrentVersionHandler()
|
||||
|
||||
/**
|
||||
* Handles the 'download-update' event.
|
||||
*/
|
||||
class DownloadUpdateHandler implements IPCEmitHandler<'download-update'> {
|
||||
event: 'download-update' = 'download-update'
|
||||
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' = 'quit-and-install'
|
||||
|
||||
/**
|
||||
* 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()
|
||||
@@ -1,13 +1,15 @@
|
||||
import { SongSearch, SongResult } from './interfaces/search.interface'
|
||||
import { VersionResult, AlbumArtResult } from './interfaces/songDetails.interface'
|
||||
import { searchHandler } from '../ipc/SearchHandler.ipc'
|
||||
import { songDetailsHandler } from '../ipc/SongDetailsHandler.ipc'
|
||||
import { albumArtHandler } from '../ipc/AlbumArtHandler.ipc'
|
||||
import { searchHandler } from '../ipc/browse/SearchHandler.ipc'
|
||||
import { songDetailsHandler } from '../ipc/browse/SongDetailsHandler.ipc'
|
||||
import { albumArtHandler } from '../ipc/browse/AlbumArtHandler.ipc'
|
||||
import { Download, DownloadProgress } from './interfaces/download.interface'
|
||||
import { downloadHandler } from '../ipc/download/DownloadHandler'
|
||||
import { Settings } from './Settings'
|
||||
import { batchSongDetailsHandler } from '../ipc/BatchSongDetailsHandler.ipc'
|
||||
import { batchSongDetailsHandler } from '../ipc/browse/BatchSongDetailsHandler.ipc'
|
||||
import { getSettingsHandler, setSettingsHandler } from '../ipc/SettingsHandler.ipc'
|
||||
import { UpdateProgress, getCurrentVersionHandler, downloadUpdateHandler, quitAndInstallHandler } from '../ipc/UpdateHandler.ipc'
|
||||
import { UpdateInfo } from 'electron-updater'
|
||||
|
||||
/**
|
||||
* To add a new IPC listener:
|
||||
@@ -23,7 +25,8 @@ export function getIPCInvokeHandlers(): IPCInvokeHandler<keyof IPCInvokeEvents>[
|
||||
searchHandler,
|
||||
songDetailsHandler,
|
||||
batchSongDetailsHandler,
|
||||
albumArtHandler
|
||||
albumArtHandler,
|
||||
getCurrentVersionHandler
|
||||
]
|
||||
}
|
||||
|
||||
@@ -51,6 +54,10 @@ export type IPCInvokeEvents = {
|
||||
input: number[]
|
||||
output: VersionResult[]
|
||||
}
|
||||
'get-current-version': {
|
||||
input: undefined
|
||||
output: string
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -65,7 +72,9 @@ export interface IPCInvokeHandler<E extends keyof IPCInvokeEvents> {
|
||||
export function getIPCEmitHandlers(): IPCEmitHandler<keyof IPCEmitEvents>[] {
|
||||
return [
|
||||
downloadHandler,
|
||||
setSettingsHandler
|
||||
setSettingsHandler,
|
||||
downloadUpdateHandler,
|
||||
quitAndInstallHandler
|
||||
]
|
||||
}
|
||||
|
||||
@@ -77,6 +86,13 @@ export type IPCEmitEvents = {
|
||||
'download-updated': DownloadProgress
|
||||
'set-settings': Settings
|
||||
'queue-updated': number[]
|
||||
|
||||
'update-error': Error
|
||||
'update-available': UpdateInfo
|
||||
'update-progress': UpdateProgress
|
||||
'update-downloaded': undefined
|
||||
'download-update': undefined
|
||||
'quit-and-install': undefined
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user