Skip to content

Commit

Permalink
Support Headless Auto-Update #8
Browse files Browse the repository at this point in the history
  • Loading branch information
Soltares committed Oct 17, 2024
1 parent d80e4cf commit f9e0153
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 96 deletions.
12 changes: 8 additions & 4 deletions api/src/lib/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ process.on('unhandledRejection', (reason, promise) => {

export let version = new State('version', '')
export let updateChannel = new State('updateChannel', undefined, { persist: true })
export let updateStatus = new State('updateStatus', {})

export let app: Express = express()
app.use(express.json({ limit: '500mb' }))
Expand Down Expand Up @@ -100,13 +101,16 @@ async function initSever() {
// Electron Hook if present
let parentPort = process['parentPort']

parentPort?.on('message', (e) => {
parentPort?.on('message', (e: any) => {
console.log('[electron to api]', e)
if (e.data.event == 'version') version.set(e.data.body)
if (e.data.event == 'updateChannel') {
if (e.data.event == 'version') {
version.set(e.data.body)
} else if (e.data.event == 'updateChannel') {
if (e.data.body) updateChannel.set(e.data.body)
} else if (['update-available', 'download-progress', 'update-downloaded'].includes(e.data.event)) {
updateStatus.set(e.data)
}
wss?.send(e.data.event, e.data.body)
// wss?.send(e.data.event, e.data.body)
})

updateChannel.subscribe((v) => {
Expand Down
23 changes: 16 additions & 7 deletions electron/src/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@ import { autoUpdater } from 'electron-updater'
let apiProcess: Electron.UtilityProcess
let apiPort: any = 9999

/** asnyc needed for updateCheckLoop to allow electron to launch main window */
async function updateCheckLoop() {
console.log('[electron] Checking for updates on channel', autoUpdater.channel)
autoUpdater.checkForUpdates()
setInterval(() => {
autoUpdater.checkForUpdates()
}, 7.2e6)
// autoUpdater.checkForUpdatesAndNotify({ title: 'MeshSense', body: 'MeshSense has an update!' })
}

function createWindow(): void {
// Create the browser window.
const mainWindow = new BrowserWindow({
Expand All @@ -22,12 +32,6 @@ function createWindow(): void {
})

mainWindow.on('ready-to-show', () => {
console.log('[electron] Checking for updates on channel', autoUpdater.channel)
autoUpdater.checkForUpdates()
setInterval(() => {
autoUpdater.checkForUpdates()
}, 7.2e6)
// autoUpdater.checkForUpdatesAndNotify({ title: 'MeshSense', body: 'MeshSense has an update!' })
mainWindow.show()
})

Expand Down Expand Up @@ -73,7 +77,7 @@ app.whenReady().then(async () => {
}

console.log('[electron] Arguments', process.argv)
if (!process.argv.includes('--headless')) {
if (!process.env['HEADLESS']) {
apiProcess.stdout?.on('data', createWindowOnServerListening)
}
apiProcess.postMessage({ event: 'version', body: app.getVersion() })
Expand Down Expand Up @@ -129,6 +133,11 @@ app.whenReady().then(async () => {
// dock icon is clicked and there are no other windows open.
if (BrowserWindow.getAllWindows().length === 0) createWindow()
})

updateCheckLoop()
// setTimeout(() => {
// autoUpdater.quitAndInstall()
// }, 3000)
})

// Quit when all windows are closed, except on macOS. There, it's common
Expand Down
98 changes: 13 additions & 85 deletions ui/src/lib/UpdateStatus.svelte
Original file line number Diff line number Diff line change
@@ -1,99 +1,27 @@
<script lang="ts">
import axios from 'axios'
import { events } from './wsc'
import { hasAccess } from './util'
import { State } from 'api/src/lib/state'
export let updateStatus = new State<any>('updateStatus', {})
export let status = ''
export let progress = 0
export let version = ''
let exampleMessages = [
{
event: 'checking-for-update',
data: 'Checking for update'
},
{
event: 'update-available',
data: {
version: '0.3.14',
files: [
{
url: 'n3fjp-universal-x86_64.AppImage',
sha512: 'qBI3W7iHTU1g1cO09+quqzp5hDqVzCz3CXxXesfanKB7JdHnduUv9dv7dc3D89m+VTUQ0kLlM54/BU83en1K5g==',
size: 110380053,
blockMapSize: 115753
}
],
path: 'n3fjp-universal-x86_64.AppImage',
sha512: 'qBI3W7iHTU1g1cO09+quqzp5hDqVzCz3CXxXesfanKB7JdHnduUv9dv7dc3D89m+VTUQ0kLlM54/BU83en1K5g==',
releaseDate: '2023-10-18T19:28:02.980Z'
}
},
{
event: 'download-progress',
data: {
total: 110380053,
delta: 60436707,
transferred: 60436707,
percent: 54.75328680989128,
bytesPerSecond: 60195923
}
},
{
event: 'download-progress',
data: {
total: 110380053,
delta: 49943346,
transferred: 110380053,
percent: 100,
bytesPerSecond: 67059570
}
},
{
event: 'update-downloaded',
data: {
version: '0.3.14',
files: [
{
url: 'n3fjp-universal-x86_64.AppImage',
sha512: 'qBI3W7iHTU1g1cO09+quqzp5hDqVzCz3CXxXesfanKB7JdHnduUv9dv7dc3D89m+VTUQ0kLlM54/BU83en1K5g==',
size: 110380053,
blockMapSize: 115753
}
],
path: 'n3fjp-universal-x86_64.AppImage',
sha512: 'qBI3W7iHTU1g1cO09+quqzp5hDqVzCz3CXxXesfanKB7JdHnduUv9dv7dc3D89m+VTUQ0kLlM54/BU83en1K5g==',
releaseDate: '2023-10-18T19:28:02.980Z',
downloadedFile: '/home/chris/.cache/n3fjp-universal-updater/pending/n3fjp-universal-x86_64.AppImage'
}
}
]
// $: document.title = `MeshSense ${$version ?? 'Development'}`
function runExample() {
let msg = exampleMessages.shift()
if (msg) {
console.log(msg)
events.emit(msg.event, msg.data)
setTimeout(runExample, 2000)
}
export let friendlyMessages = {
'update-available': 'New update!',
'download-progress': 'Downloading Update',
'update-downloaded': 'Update Ready'
}
events.on('update-available', (e) => {
status = 'New update!'
version = e.version
})
events.on('download-progress', (e) => {
status = 'Downloading Update'
progress = e.percent
})
events.on('update-downloaded', (e) => {
status = 'Update Ready'
version = e.version
})
// $: document.title = `MeshSense ${$version ?? 'Development'}`
$: {
status = friendlyMessages[$updateStatus.event]
progress = $updateStatus.body?.percent
version = $updateStatus.body?.version
}
function installUpdate() {
axios.get('/installUpdate')
Expand Down

0 comments on commit f9e0153

Please sign in to comment.