|
| 1 | +import type { createContext } from '@moeru/eventa/adapters/electron/main' |
| 2 | +import type { BrowserWindow } from 'electron' |
| 3 | +import type { UpdateInfo } from 'electron-updater' |
| 4 | + |
| 5 | +import type { AutoUpdaterState } from '../../../shared/eventa' |
| 6 | + |
| 7 | +import electronUpdater from 'electron-updater' |
| 8 | + |
| 9 | +import { is } from '@electron-toolkit/utils' |
| 10 | +import { useLogg } from '@guiiai/logg' |
| 11 | +import { defineInvokeHandler } from '@moeru/eventa' |
| 12 | +import { errorMessageFrom } from '@moeru/std' |
| 13 | +import { committerDate } from '~build/git' |
| 14 | +import { app } from 'electron' |
| 15 | +import { Semaphore } from 'es-toolkit' |
| 16 | + |
| 17 | +import { |
| 18 | + autoUpdater as autoUpdaterEventa, |
| 19 | + electronAutoUpdaterStateChanged, |
| 20 | +} from '../../../shared/eventa' |
| 21 | +import { MockAutoUpdater } from './mock-auto-updater' |
| 22 | + |
| 23 | +export interface AppUpdaterLike { |
| 24 | + on: (event: string, listener: (...args: any[]) => void) => any |
| 25 | + checkForUpdates: () => Promise<any> |
| 26 | + downloadUpdate: () => Promise<any> |
| 27 | + quitAndInstall: () => Promise<void> |
| 28 | +} |
| 29 | + |
| 30 | +// NOTICE: this part of code is copied from https://www.electron.build/auto-update |
| 31 | +// Or https://github.com/electron-userland/electron-builder/blob/b866e99ccd3ea9f85bc1e840f0f6a6a162fca388/pages/auto-update.md?plain=1#L57-L66 |
| 32 | +export function fromImported(): AppUpdaterLike { |
| 33 | + if (is.dev) { |
| 34 | + return new MockAutoUpdater() |
| 35 | + } |
| 36 | + |
| 37 | + // Using destructuring to access autoUpdater due to the CommonJS module of 'electron-updater'. |
| 38 | + // It is a workaround for ESM compatibility issues, see https://github.com/electron-userland/electron-builder/issues/7976. |
| 39 | + const { autoUpdater } = electronUpdater |
| 40 | + return autoUpdater as unknown as AppUpdaterLike |
| 41 | +} |
| 42 | + |
| 43 | +type MainContext = ReturnType<typeof createContext>['context'] |
| 44 | + |
| 45 | +export interface AutoUpdater { |
| 46 | + state: AutoUpdaterState |
| 47 | + checkForUpdates: () => Promise<void> |
| 48 | + downloadUpdate: () => Promise<void> |
| 49 | + quitAndInstall: () => void |
| 50 | + subscribe: (callback: (state: AutoUpdaterState) => void) => () => void |
| 51 | +} |
| 52 | + |
| 53 | +export function setupAutoUpdater(): AutoUpdater { |
| 54 | + const semaphore = new Semaphore(1) |
| 55 | + |
| 56 | + const log = useLogg('auto-updater').useGlobalConfig() |
| 57 | + const autoUpdater = fromImported() |
| 58 | + |
| 59 | + let state: AutoUpdaterState = { status: 'idle' } |
| 60 | + const hooks = new Set<(state: AutoUpdaterState) => void>() |
| 61 | + |
| 62 | + function broadcast(next: AutoUpdaterState) { |
| 63 | + state = next |
| 64 | + |
| 65 | + for (const listener of hooks) { |
| 66 | + try { |
| 67 | + listener(next) |
| 68 | + } |
| 69 | + catch (error) { |
| 70 | + log.withError(error).error('Failed to notify listener') |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + autoUpdater.on('error', error => broadcast({ status: 'error', error: { message: errorMessageFrom(error) || String(error) } })) |
| 76 | + autoUpdater.on('checking-for-update', () => broadcast({ status: 'checking' })) |
| 77 | + autoUpdater.on('update-available', (info: UpdateInfo) => broadcast({ status: 'available', info })) |
| 78 | + autoUpdater.on('update-downloaded', (info: UpdateInfo) => broadcast({ status: 'downloaded', info })) |
| 79 | + autoUpdater.on('update-not-available', () => broadcast({ status: 'not-available', info: { version: app.getVersion(), files: [], releaseDate: committerDate } })) |
| 80 | + autoUpdater.on('download-progress', progress => broadcast({ |
| 81 | + ...state, |
| 82 | + status: 'downloading', |
| 83 | + progress: { |
| 84 | + percent: progress.percent, |
| 85 | + bytesPerSecond: progress.bytesPerSecond, |
| 86 | + transferred: progress.transferred, |
| 87 | + total: progress.total, |
| 88 | + }, |
| 89 | + })) |
| 90 | + |
| 91 | + autoUpdater.checkForUpdates().catch(error => log.withError(error).error('checkForUpdates() failed')) |
| 92 | + |
| 93 | + return { |
| 94 | + get state() { |
| 95 | + return state |
| 96 | + }, |
| 97 | + async checkForUpdates() { |
| 98 | + broadcast({ status: 'checking' }) |
| 99 | + await autoUpdater.checkForUpdates() |
| 100 | + }, |
| 101 | + async downloadUpdate() { |
| 102 | + if (state.status === 'downloading' || state.status === 'downloaded') |
| 103 | + return |
| 104 | + |
| 105 | + await semaphore.acquire() |
| 106 | + |
| 107 | + try { |
| 108 | + await autoUpdater.downloadUpdate() |
| 109 | + } |
| 110 | + finally { |
| 111 | + semaphore.release() |
| 112 | + } |
| 113 | + }, |
| 114 | + async quitAndInstall() { |
| 115 | + await semaphore.acquire() |
| 116 | + |
| 117 | + try { |
| 118 | + autoUpdater.quitAndInstall() |
| 119 | + } |
| 120 | + finally { |
| 121 | + semaphore.release() |
| 122 | + } |
| 123 | + }, |
| 124 | + subscribe(callback) { |
| 125 | + hooks.add(callback) |
| 126 | + // Send current state immediately |
| 127 | + try { |
| 128 | + callback(state) |
| 129 | + } |
| 130 | + catch {} |
| 131 | + |
| 132 | + return () => { |
| 133 | + hooks.delete(callback) |
| 134 | + } |
| 135 | + }, |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +export function createAutoUpdaterService(params: { context: MainContext, window: BrowserWindow, service: AutoUpdater }) { |
| 140 | + const { context, window, service } = params |
| 141 | + |
| 142 | + // Subscribe to state changes and forward to the context |
| 143 | + const unsubscribe = service.subscribe((state) => { |
| 144 | + if (window.isDestroyed()) |
| 145 | + return |
| 146 | + |
| 147 | + try { |
| 148 | + context.emit(electronAutoUpdaterStateChanged, state) |
| 149 | + } |
| 150 | + catch {} |
| 151 | + }) |
| 152 | + |
| 153 | + const cleanups: Array<() => void> = [unsubscribe] |
| 154 | + |
| 155 | + cleanups.push( |
| 156 | + defineInvokeHandler(context, autoUpdaterEventa.getState, () => service.state), |
| 157 | + ) |
| 158 | + |
| 159 | + cleanups.push( |
| 160 | + defineInvokeHandler(context, autoUpdaterEventa.checkForUpdates, async () => { |
| 161 | + await service.checkForUpdates() |
| 162 | + return service.state |
| 163 | + }), |
| 164 | + ) |
| 165 | + |
| 166 | + cleanups.push( |
| 167 | + defineInvokeHandler(context, autoUpdaterEventa.downloadUpdate, async () => { |
| 168 | + await service.downloadUpdate() |
| 169 | + return service.state |
| 170 | + }), |
| 171 | + ) |
| 172 | + |
| 173 | + cleanups.push( |
| 174 | + defineInvokeHandler(context, autoUpdaterEventa.quitAndInstall, () => { |
| 175 | + service.quitAndInstall() |
| 176 | + }), |
| 177 | + ) |
| 178 | + |
| 179 | + const cleanup = () => { |
| 180 | + for (const fn of cleanups) |
| 181 | + fn() |
| 182 | + } |
| 183 | + |
| 184 | + window.on('closed', cleanup) |
| 185 | + return cleanup |
| 186 | +} |
0 commit comments