feat(auto-updater, menu): implement update state management and enhance menu options for updates

This commit is contained in:
Anish Sarkar 2026-05-26 03:11:06 +05:30
parent 6cf0f07366
commit 98697f1dd6
2 changed files with 88 additions and 25 deletions

View file

@ -17,8 +17,32 @@ type UpdateInfo = {
version: string; version: string;
}; };
type UpdateMenuState =
| { status: 'idle' }
| { status: 'downloading'; version: string }
| { status: 'ready'; version: string };
let listenersRegistered = false; let listenersRegistered = false;
let manualUpdateCheckInProgress = false; let updateMenuState: UpdateMenuState = { status: 'idle' };
const updateMenuStateListeners = new Set<(state: UpdateMenuState) => void>();
export function getUpdateMenuState(): UpdateMenuState {
return updateMenuState;
}
export function onUpdateMenuStateChange(listener: (state: UpdateMenuState) => void): () => void {
updateMenuStateListeners.add(listener);
return () => {
updateMenuStateListeners.delete(listener);
};
}
function setUpdateMenuState(state: UpdateMenuState): void {
updateMenuState = state;
for (const listener of updateMenuStateListeners) {
listener(state);
}
}
function getAutoUpdater(): AutoUpdater { function getAutoUpdater(): AutoUpdater {
const { autoUpdater } = require('electron-updater'); const { autoUpdater } = require('electron-updater');
@ -35,6 +59,7 @@ function configureAutoUpdater(autoUpdater: AutoUpdater): void {
autoUpdater.on('update-available', (info: UpdateInfo) => { autoUpdater.on('update-available', (info: UpdateInfo) => {
console.log(`Update available: ${info.version}`); console.log(`Update available: ${info.version}`);
setUpdateMenuState({ status: 'downloading', version: info.version });
trackEvent('desktop_update_available', { trackEvent('desktop_update_available', {
current_version: version, current_version: version,
new_version: info.version, new_version: info.version,
@ -43,16 +68,20 @@ function configureAutoUpdater(autoUpdater: AutoUpdater): void {
autoUpdater.on('update-downloaded', (info: UpdateInfo) => { autoUpdater.on('update-downloaded', (info: UpdateInfo) => {
console.log(`Update downloaded: ${info.version}`); console.log(`Update downloaded: ${info.version}`);
setUpdateMenuState({ status: 'ready', version: info.version });
trackEvent('desktop_update_downloaded', { trackEvent('desktop_update_downloaded', {
current_version: version, current_version: version,
new_version: info.version, new_version: info.version,
}); });
if (!manualUpdateCheckInProgress) {
notifyRenderersUpdateDownloaded(info); notifyRenderersUpdateDownloaded(info);
} });
autoUpdater.on('update-not-available', () => {
setUpdateMenuState({ status: 'idle' });
}); });
autoUpdater.on('error', (err: Error) => { autoUpdater.on('error', (err: Error) => {
setUpdateMenuState({ status: 'idle' });
console.log('Auto-updater: update check skipped —', err.message?.split('\n')[0]); console.log('Auto-updater: update check skipped —', err.message?.split('\n')[0]);
trackEvent('desktop_update_error', { trackEvent('desktop_update_error', {
message: err.message?.split('\n')[0], message: err.message?.split('\n')[0],
@ -92,6 +121,13 @@ export function setupAutoUpdater(): void {
} }
export async function checkForUpdatesManually(): Promise<void> { export async function checkForUpdatesManually(): Promise<void> {
const currentState = getUpdateMenuState();
if (currentState.status === 'ready') {
installDownloadedUpdate();
return;
}
if (currentState.status === 'downloading') return;
if (!app.isPackaged) { if (!app.isPackaged) {
await dialog.showMessageBox({ await dialog.showMessageBox({
type: 'info', type: 'info',
@ -115,29 +151,20 @@ export async function checkForUpdatesManually(): Promise<void> {
configureAutoUpdater(autoUpdater); configureAutoUpdater(autoUpdater);
try { try {
manualUpdateCheckInProgress = true;
const result = await new Promise<'not-available' | 'downloaded'>((resolve, reject) => { const result = await new Promise<'not-available' | 'downloaded'>((resolve, reject) => {
const cleanup = () => { const cleanup = () => {
manualUpdateCheckInProgress = false;
autoUpdater.removeListener('update-available', onAvailable); autoUpdater.removeListener('update-available', onAvailable);
autoUpdater.removeListener('update-not-available', onNotAvailable); autoUpdater.removeListener('update-not-available', onNotAvailable);
autoUpdater.removeListener('update-downloaded', onDownloaded); autoUpdater.removeListener('update-downloaded', onDownloaded);
autoUpdater.removeListener('error', onError); autoUpdater.removeListener('error', onError);
}; };
const onAvailable = (info: UpdateInfo) => { const onAvailable = () => {};
void dialog.showMessageBox({
type: 'info',
title: 'Update Available',
message: `Version ${info.version} is available and will download in the background.`,
});
};
const onNotAvailable = () => { const onNotAvailable = () => {
cleanup(); cleanup();
resolve('not-available'); resolve('not-available');
}; };
const onDownloaded = (info: UpdateInfo) => { const onDownloaded = () => {
cleanup(); cleanup();
notifyRenderersUpdateDownloaded(info);
resolve('downloaded'); resolve('downloaded');
}; };
const onError = (err: Error) => { const onError = (err: Error) => {
@ -151,6 +178,7 @@ export async function checkForUpdatesManually(): Promise<void> {
autoUpdater.once('error', onError); autoUpdater.once('error', onError);
autoUpdater.checkForUpdates().catch((err: Error) => { autoUpdater.checkForUpdates().catch((err: Error) => {
cleanup(); cleanup();
setUpdateMenuState({ status: 'idle' });
reject(err); reject(err);
}); });
}); });
@ -163,7 +191,7 @@ export async function checkForUpdatesManually(): Promise<void> {
}); });
} }
} catch (err) { } catch (err) {
manualUpdateCheckInProgress = false; setUpdateMenuState({ status: 'idle' });
await dialog.showMessageBox({ await dialog.showMessageBox({
type: 'error', type: 'error',
title: 'Update Check Failed', title: 'Update Check Failed',

View file

@ -1,12 +1,39 @@
import { app, Menu, shell } from 'electron'; import { app, Menu, shell } from 'electron';
import { checkForUpdatesManually } from './auto-updater'; import {
checkForUpdatesManually,
getUpdateMenuState,
installDownloadedUpdate,
onUpdateMenuStateChange,
} from './auto-updater';
const checkForUpdatesItem: Electron.MenuItemConstructorOptions = { let updateMenuListenerRegistered = false;
function getUpdateMenuItem(): Electron.MenuItemConstructorOptions {
const state = getUpdateMenuState();
if (state.status === 'downloading') {
return {
label: 'Downloading...',
enabled: false,
};
}
if (state.status === 'ready') {
return {
label: 'Install and Restart',
click: () => {
installDownloadedUpdate();
},
};
}
return {
label: 'Check for Updates...', label: 'Check for Updates...',
click: () => { click: () => {
void checkForUpdatesManually(); void checkForUpdatesManually();
}, },
}; };
}
const privacyPolicyItem: Electron.MenuItemConstructorOptions = { const privacyPolicyItem: Electron.MenuItemConstructorOptions = {
label: 'Privacy Policy', label: 'Privacy Policy',
@ -23,14 +50,22 @@ const termsOfServiceItem: Electron.MenuItemConstructorOptions = {
}; };
export function setupMenu(): void { export function setupMenu(): void {
if (!updateMenuListenerRegistered) {
updateMenuListenerRegistered = true;
onUpdateMenuStateChange(() => {
setupMenu();
});
}
const isMac = process.platform === 'darwin'; const isMac = process.platform === 'darwin';
const updateMenuItem = getUpdateMenuItem();
const template: Electron.MenuItemConstructorOptions[] = [ const template: Electron.MenuItemConstructorOptions[] = [
...(isMac ...(isMac
? [{ ? [{
label: app.name, label: app.name,
submenu: [ submenu: [
{ role: 'about' as const }, { role: 'about' as const },
checkForUpdatesItem, updateMenuItem,
{ type: 'separator' as const }, { type: 'separator' as const },
{ role: 'services' as const }, { role: 'services' as const },
{ type: 'separator' as const }, { type: 'separator' as const },
@ -51,7 +86,7 @@ export function setupMenu(): void {
submenu: [ submenu: [
...(!isMac ...(!isMac
? [ ? [
checkForUpdatesItem, updateMenuItem,
{ type: 'separator' as const }, { type: 'separator' as const },
] ]
: []), : []),