From f59caa3d6fce82c556ff9ab9f48014b0cfc3fe0a Mon Sep 17 00:00:00 2001 From: Samuel Elliott Date: Sun, 17 Aug 2025 20:52:04 +0100 Subject: [PATCH] Increase update interval after errors --- src/app/main/index.ts | 4 +--- src/app/main/ipc.ts | 5 ++--- src/app/main/monitor.ts | 5 +++-- src/app/main/util.ts | 9 +-------- src/app/preload/index.ts | 4 ++-- src/util/loop.ts | 17 +++++++++++++++++ 6 files changed, 26 insertions(+), 18 deletions(-) diff --git a/src/app/main/index.ts b/src/app/main/index.ts index cc0182a..b28ee84 100644 --- a/src/app/main/index.ts +++ b/src/app/main/index.ts @@ -586,7 +586,6 @@ export class Store extends EventEmitter { error: err, buttons: ['OK', 'Retry'], defaultId: 1, - app: this.app, }); if (response === 1) { @@ -616,7 +615,6 @@ export class Store extends EventEmitter { error: err, buttons: ['OK', 'Retry'], defaultId: 1, - app: this.app, }); if (response === 1) { @@ -624,4 +622,4 @@ export class Store extends EventEmitter { } } } -} \ No newline at end of file +} diff --git a/src/app/main/ipc.ts b/src/app/main/ipc.ts index 69267e5..9713b11 100644 --- a/src/app/main/ipc.ts +++ b/src/app/main/ipc.ts @@ -90,9 +90,8 @@ export function setupIpc(appinstance: App, ipcMain: IpcMain) { handle('systemPreferences:getloginitem', () => appinstance.store.getLoginItem()); handle('systemPreferences:setloginitem', (e, settings: LoginItemOptions) => appinstance.store.setLoginItem(settings)); - handle('preferences:showerroralerts', () => storage.getItem('preferences.showerroralerts')); - handle('preferences:setshowerroralerts', (e, enabled: boolean) => - storage.setItem('preferences.showerroralerts', enabled)); + handle('preferences:getshowerroralerts', () => storage.getItem('ShowErrorAlertsPreference').then(s => s ?? false)); + handle('preferences:setshowerroralerts', (e, show: boolean) => storage.setItem('ShowErrorAlertsPreference', show)); handle('update:get', () => appinstance.updater.cache ?? appinstance.updater.check()); handle('update:check', () => appinstance.updater.check()); diff --git a/src/app/main/monitor.ts b/src/app/main/monitor.ts index 63c1ef2..a81a683 100644 --- a/src/app/main/monitor.ts +++ b/src/app/main/monitor.ts @@ -397,12 +397,14 @@ export class PresenceMonitorManager { return LoopResult.OK; } + const show_error_alerts: boolean = await this.app.store.storage.getItem('ShowErrorAlertsPreference') ?? false; + if (!show_error_alerts) return LoopResult.DEFER_NEXT_UPDATE; + const {response} = await showErrorDialog({ message: err.name + ' updating presence monitor', error: err, buttons: ['OK', 'Retry'], defaultId: 0, - app: this.app, }); if (response === 1) { @@ -432,7 +434,6 @@ export class PresenceMonitorManager { await showErrorDialog({ message: error.name + ' updating presence monitor', error, - app: this.app, }); } } diff --git a/src/app/main/util.ts b/src/app/main/util.ts index 2061726..c2187de 100644 --- a/src/app/main/util.ts +++ b/src/app/main/util.ts @@ -77,17 +77,10 @@ interface ErrorBoxOptions extends MessageBoxOptions { window?: BrowserWindow; } -export async function showErrorDialog(options: ErrorBoxOptions) { +export function showErrorDialog(options: ErrorBoxOptions) { const {error, app, window, ...message_box_options} = options; const detail = ErrorDescription.getErrorDescription(error); - if (app) { - const showErrorAlerts = await app.store.storage.getItem('preferences.showerroralerts') as boolean - if (showErrorAlerts === false) { - return Promise.resolve({ response: 0, checkboxChecked: false }); - } - } - message_box_options.detail = message_box_options.detail ? detail + '\n\n' + message_box_options.detail : detail; diff --git a/src/app/preload/index.ts b/src/app/preload/index.ts index f645830..472332b 100644 --- a/src/app/preload/index.ts +++ b/src/app/preload/index.ts @@ -50,8 +50,8 @@ const ipc = { getLoginItemSettings: () => inv('systemPreferences:getloginitem'), setLoginItemSettings: (settings: LoginItemOptions) => inv('systemPreferences:setloginitem', settings), - getShowErrorAlerts: () => inv('preferences:showerroralerts'), - setShowErrorAlerts: (enabled: boolean) => inv('preferences:setshowerroralerts', enabled), + getShowErrorAlerts: () => inv('preferences:getshowerroralerts'), + setShowErrorAlerts: (show: boolean) => inv('preferences:setshowerroralerts', show), getUpdateData: () => inv('update:get'), checkUpdates: () => inv('update:check'), diff --git a/src/util/loop.ts b/src/util/loop.ts index 6084488..dd0a110 100644 --- a/src/util/loop.ts +++ b/src/util/loop.ts @@ -4,6 +4,7 @@ const debug = createDebug('nxapi:util:loop'); export default abstract class Loop { update_interval = 60; + errors = 0; init(): void | Promise {} @@ -13,8 +14,11 @@ export default abstract class Loop { try { const result = init ? await this.init() : await this.update(); + this.errors = 0; + return result ?? (init ? LoopResult.OK_SKIP_INTERVAL : LoopResult.OK); } catch (err) { + this.errors++; return this.handleError(err as any); } } @@ -23,6 +27,10 @@ export default abstract class Loop { throw err; } + get next_update_interval() { + return this.update_interval * Math.min(this.errors / 2, 20); + } + private is_loop_active = 0; async loop(init = false) { @@ -38,6 +46,13 @@ export default abstract class Loop { await new Promise(rs => setTimeout(this.timeout_resolve = rs, this.update_interval * 1000)); } } + if (result === LoopResult.DEFER_NEXT_UPDATE) { + if (this.skip_interval_once) { + this.skip_interval_once = false; + } else { + await new Promise(rs => setTimeout(this.timeout_resolve = rs, this.next_update_interval * 1000)); + } + } if (result === LoopResult.STOP) { return LoopResult.STOP; } @@ -62,11 +77,13 @@ export default abstract class Loop { const LoopRunOk = Symbol('LoopRunOk'); const LoopRunOkSkipInterval = Symbol('LoopRunOkSkipInterval'); +const LoopRunIncrementInterval = Symbol('LoopRunIncrementInterval'); const LoopRunStop = Symbol('LoopRunStopNow'); export enum LoopResult { OK = LoopRunOk as any, OK_SKIP_INTERVAL = LoopRunOkSkipInterval as any, + DEFER_NEXT_UPDATE = LoopRunIncrementInterval as any, STOP = LoopRunStop as any, }