diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index ac6a4af..cd80227 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -7,6 +7,7 @@ on: schedule: - cron: '0 22 * * 3' workflow_call: + workflow_dispatch: permissions: contents: read diff --git a/src/index.ts b/src/index.ts index c378898..5dd8f1a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -115,6 +115,13 @@ export interface IUpdateElectronAppOptions { readonly onNotifyUser?: (info: IUpdateInfo) => void; } +export interface IUpdater { + readonly isSupported: boolean; + readonly isLookingForUpdates: boolean; + readonly stopLookingForUpdates: () => void; + readonly startLookingForUpdates: () => void; +} + // eslint-disable-next-line @typescript-eslint/no-require-imports const pkg = require('../package.json'); const userAgent = format('%s/%s (%s: %s)', pkg.name, pkg.version, os.platform(), os.arch()); @@ -128,7 +135,7 @@ const isHttpsUrl = (maybeURL: string) => { } }; -export function updateElectronApp(opts: IUpdateElectronAppOptions = {}) { +export function updateElectronApp(opts: IUpdateElectronAppOptions = {}): IUpdater { // check for bad input early, so it will be logged during development const safeOpts = validateInput(opts); @@ -141,17 +148,23 @@ export function updateElectronApp(opts: IUpdateElectronAppOptions = {}) { } else { console.log(message); } - return; + + //Return Updater but does not start + return makeUpdater(safeOpts); } + const updater = makeUpdater(safeOpts); + if (app.isReady()) { - initUpdater(safeOpts); + updater.startLookingForUpdates(); } else { - app.on('ready', () => initUpdater(safeOpts)); + app.on('ready', () => updater.startLookingForUpdates()); } + + return updater; } -function initUpdater(opts: ReturnType) { +function makeUpdater(opts: ReturnType): IUpdater { const { updateSource, updateInterval, logger } = opts; // exit early on unsupported platforms, e.g. `linux` @@ -159,7 +172,12 @@ function initUpdater(opts: ReturnType) { log( `Electron's autoUpdater does not support the '${process.platform}' platform. Ref: https://www.electronjs.org/docs/latest/api/auto-updater#platform-notices`, ); - return; + return { + isSupported: false, + isLookingForUpdates: false, + stopLookingForUpdates: () => {}, + startLookingForUpdates: () => {}, + }; } let feedURL: string; @@ -241,11 +259,29 @@ function initUpdater(opts: ReturnType) { ); } - // check for updates right away and keep checking later - autoUpdater.checkForUpdates(); - setInterval(() => { - autoUpdater.checkForUpdates(); - }, ms(updateInterval)); + let intervalID: ReturnType; + let isLookingForUpdates = false; + + return { + isSupported: true, + get isLookingForUpdates() { + return isLookingForUpdates; + }, + stopLookingForUpdates() { + if (isLookingForUpdates) { + clearInterval(intervalID); + isLookingForUpdates = false; + } + }, + startLookingForUpdates() { + if (!isLookingForUpdates) { + intervalID = setInterval(() => { + autoUpdater.checkForUpdates(); + }, ms(updateInterval)); + isLookingForUpdates = true; + } + }, + }; } /**