Added update ipc handler

This commit is contained in:
Geomitron
2020-05-21 14:17:51 -04:00
parent 64701e4909
commit 9e0d332a1a
7 changed files with 128 additions and 27 deletions

View File

@@ -8,7 +8,6 @@
], ],
"publish": { "publish": {
"provider": "github", "provider": "github",
"private": true,
"releaseType": "release" "releaseType": "release"
}, },
"nsis": { "nsis": {

View File

@@ -10,7 +10,7 @@
}, },
"repository": { "repository": {
"type": "git", "type": "git",
"url": "https://github.com/Geomitron/Bridge" "url": "https://github.com/Geomitron/Bridge.git"
}, },
"main": "dist/electron/main.js", "main": "dist/electron/main.js",
"private": true, "private": true,

View File

@@ -2,7 +2,7 @@
<a class="item" routerLinkActive="active" routerLink="/browse">Browse</a> <a class="item" routerLinkActive="active" routerLink="/browse">Browse</a>
<a class="item" routerLinkActive="active" routerLink="/library">Library</a> <a class="item" routerLinkActive="active" routerLink="/library">Library</a>
<a class="item" routerLinkActive="active" routerLink="/settings">Settings</a> <a class="item" routerLinkActive="active" routerLink="/settings">Settings</a>
<a class="item" routerLinkActive="active" routerLink="/about">About</a> <a class="item" routerLinkActive="active" routerLink="/about"><i class="teal small circle icon"></i>About</a>
<div class="right menu"> <div class="right menu">
<a class="item traffic-light" (click)="minimize()"><i class="minus icon"></i></a> <a class="item traffic-light" (click)="minimize()"><i class="minus icon"></i></a>

View File

@@ -1,22 +1,5 @@
import { Component, OnInit, ChangeDetectorRef } from '@angular/core' import { Component, OnInit, ChangeDetectorRef } from '@angular/core'
import { ElectronService } from '../../core/services/electron.service' import { ElectronService } from '../../core/services/electron.service'
// import { autoUpdater, UpdateInfo } from 'electron-updater'
// autoUpdater.autoDownload = false
// autoUpdater.on('error', (err) => {})
// autoUpdater.on('checking-for-update', () => {})
// autoUpdater.on('update-available', (info: UpdateInfo) => {})
// autoUpdater.on('update-not-available', () => {})
// autoUpdater.on('download-progress', (progress) => {
// console.log(progress.bytesPerSecond, progress.percent, progress.transferred, progress.total)
// })
// autoUpdater.on('update-downloaded', (info: UpdateInfo) => {})
// autoUpdater.currentVersion // TODO: display this in the corner or on the about page?
// autoUpdater.logger = null
// autoUpdater.checkForUpdates()
// autoUpdater.downloadUpdate()
// autoUpdater.quitAndInstall(false) // By default; autoUpdater installs a downloaded update on the next program restart
// TODO: check for updates on initialization; show a button indicating a new version can be downloaded
@Component({ @Component({
selector: 'app-toolbar', selector: 'app-toolbar',
@@ -26,6 +9,7 @@ import { ElectronService } from '../../core/services/electron.service'
export class ToolbarComponent implements OnInit { export class ToolbarComponent implements OnInit {
isMaximized: boolean isMaximized: boolean
updateAvailable = false
constructor(private electronService: ElectronService, private ref: ChangeDetectorRef) { } constructor(private electronService: ElectronService, private ref: ChangeDetectorRef) { }
@@ -39,6 +23,10 @@ export class ToolbarComponent implements OnInit {
this.isMaximized = true this.isMaximized = true
this.ref.detectChanges() this.ref.detectChanges()
}) })
this.electronService.receiveIPC('update-available', () => {
this.updateAvailable = true
})
} }
minimize() { minimize() {

View File

@@ -33,7 +33,6 @@ export class DownloadService {
const thisDownloadIndex = this.downloads.findIndex(download => download.versionID == result.versionID) const thisDownloadIndex = this.downloads.findIndex(download => download.versionID == result.versionID)
if (thisDownloadIndex == -1) { if (thisDownloadIndex == -1) {
this.downloads.push(result) this.downloads.push(result)
// TODO: this.downloads.sort(downloadSorter)
} else { } else {
this.downloads[thisDownloadIndex] = result this.downloads[thisDownloadIndex] = result
} }

View 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()

View File

@@ -1,13 +1,15 @@
import { SongSearch, SongResult } from './interfaces/search.interface' import { SongSearch, SongResult } from './interfaces/search.interface'
import { VersionResult, AlbumArtResult } from './interfaces/songDetails.interface' import { VersionResult, AlbumArtResult } from './interfaces/songDetails.interface'
import { searchHandler } from '../ipc/SearchHandler.ipc' import { searchHandler } from '../ipc/browse/SearchHandler.ipc'
import { songDetailsHandler } from '../ipc/SongDetailsHandler.ipc' import { songDetailsHandler } from '../ipc/browse/SongDetailsHandler.ipc'
import { albumArtHandler } from '../ipc/AlbumArtHandler.ipc' import { albumArtHandler } from '../ipc/browse/AlbumArtHandler.ipc'
import { Download, DownloadProgress } from './interfaces/download.interface' import { Download, DownloadProgress } from './interfaces/download.interface'
import { downloadHandler } from '../ipc/download/DownloadHandler' import { downloadHandler } from '../ipc/download/DownloadHandler'
import { Settings } from './Settings' 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 { 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: * To add a new IPC listener:
@@ -23,7 +25,8 @@ export function getIPCInvokeHandlers(): IPCInvokeHandler<keyof IPCInvokeEvents>[
searchHandler, searchHandler,
songDetailsHandler, songDetailsHandler,
batchSongDetailsHandler, batchSongDetailsHandler,
albumArtHandler albumArtHandler,
getCurrentVersionHandler
] ]
} }
@@ -51,6 +54,10 @@ export type IPCInvokeEvents = {
input: number[] input: number[]
output: VersionResult[] 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>[] { export function getIPCEmitHandlers(): IPCEmitHandler<keyof IPCEmitEvents>[] {
return [ return [
downloadHandler, downloadHandler,
setSettingsHandler setSettingsHandler,
downloadUpdateHandler,
quitAndInstallHandler
] ]
} }
@@ -77,6 +86,13 @@ export type IPCEmitEvents = {
'download-updated': DownloadProgress 'download-updated': DownloadProgress
'set-settings': Settings 'set-settings': Settings
'queue-updated': number[] 'queue-updated': number[]
'update-error': Error
'update-available': UpdateInfo
'update-progress': UpdateProgress
'update-downloaded': undefined
'download-update': undefined
'quit-and-install': undefined
} }
/** /**