Changeset View
Changeset View
Standalone View
Standalone View
desktop/src/auto-update.js
// @flow | // @flow | ||||
// eslint-disable-next-line import/extensions | // eslint-disable-next-line import/extensions | ||||
import { app, ipcMain, autoUpdater } from 'electron/main'; | import { app, ipcMain, autoUpdater } from 'electron/main'; | ||||
const getUpdateUrl = version => | const getUpdateUrl = version => | ||||
`https://electron-update.commtechnologies.org/update/${process.platform}/${version}`; | `https://electron-update.commtechnologies.org/update/${process.platform}/${version}`; | ||||
export function initAutoUpdate(): void { | export function initAutoUpdate(): void { | ||||
autoUpdater.setFeedURL({ url: getUpdateUrl(app.getVersion()) }); | autoUpdater.setFeedURL({ url: getUpdateUrl(app.getVersion()) }); | ||||
// Check for new updates every 10 minutes | // Check for new updates every 10 minutes | ||||
const updateIntervalMs = 10 * 60_000; | const updateIntervalMs = 10 * 60_000; | ||||
let currentTimeout = null; | |||||
const scheduleCheckForUpdates = () => { | const scheduleCheckForUpdates = () => { | ||||
setTimeout(() => autoUpdater.checkForUpdates(), updateIntervalMs); | if (!currentTimeout) { | ||||
currentTimeout = setTimeout(() => { | |||||
currentTimeout = null; | |||||
autoUpdater.checkForUpdates(); | |||||
}, updateIntervalMs); | |||||
} | |||||
}; | }; | ||||
scheduleCheckForUpdates(); | scheduleCheckForUpdates(); | ||||
autoUpdater.on('update-not-available', scheduleCheckForUpdates); | autoUpdater.on('update-not-available', scheduleCheckForUpdates); | ||||
autoUpdater.on('update-downloaded', (event, releaseNotes, releaseName) => { | autoUpdater.on('update-downloaded', (event, releaseNotes, releaseName) => { | ||||
autoUpdater.setFeedURL({ url: getUpdateUrl(releaseName) }); | autoUpdater.setFeedURL({ url: getUpdateUrl(releaseName) }); | ||||
scheduleCheckForUpdates(); | scheduleCheckForUpdates(); | ||||
}); | }); | ||||
autoUpdater.on('error', error => { | |||||
console.error(error); | |||||
scheduleCheckForUpdates(); | |||||
}); | |||||
ipcMain.on('update-to-new-version', () => autoUpdater.quitAndInstall()); | ipcMain.on('update-to-new-version', () => autoUpdater.quitAndInstall()); | ||||
} | } |