From b41e07db880f043685d02c5d8f88378fbe0ae66e Mon Sep 17 00:00:00 2001 From: Kiran Niranjan Date: Fri, 28 Jul 2023 18:51:09 +0530 Subject: [PATCH] SDA-4247 (Update user config once on before-quit) (#1918) * SDA-4347 - Add Semaphore for updating user config file Signed-off-by: Kiran Niranjan * SDA-4347 - Update user config once on before-quit Signed-off-by: Kiran Niranjan * SDA-4347 - Handle application exit Signed-off-by: Kiran Niranjan * SDA-4347 - Fix uts Signed-off-by: Kiran Niranjan --------- Signed-off-by: Kiran Niranjan --- spec/appCacheHandler.spec.ts | 10 ++++++++++ src/app/app-cache-handler.ts | 4 ++-- src/app/app-state-handler.ts | 6 +++--- src/app/config-handler.ts | 15 ++++++++++++--- src/app/dialog-handler.ts | 6 ++---- src/app/window-handler.ts | 13 ++++++++++--- 6 files changed, 39 insertions(+), 15 deletions(-) diff --git a/spec/appCacheHandler.spec.ts b/spec/appCacheHandler.spec.ts index 7c174ad0..4ff6ed48 100644 --- a/spec/appCacheHandler.spec.ts +++ b/spec/appCacheHandler.spec.ts @@ -42,6 +42,16 @@ jest.mock('../src/common/logger', () => { }; }); +jest.mock('../src/app/window-handler', () => { + return { + windowHandler: { + createMoreInfoWindow: jest.fn(), + getMainWindow: jest.fn(), + isMana: true, + }, + }; +}); + describe('app cache handler', () => { describe('check app cache file', () => { const cachePathExpected = path.join(app.getPath('userData'), 'CacheCheck'); diff --git a/src/app/app-cache-handler.ts b/src/app/app-cache-handler.ts index c625c1b1..80fdc1b9 100644 --- a/src/app/app-cache-handler.ts +++ b/src/app/app-cache-handler.ts @@ -11,6 +11,7 @@ import * as rimraf from 'rimraf'; import { i18n } from '../common/i18n'; import { logger } from '../common/logger'; +import { windowHandler } from './window-handler'; // Cache check file path const userDataPath: string = app.getPath('userData'); @@ -123,8 +124,7 @@ export const cleanAppCacheOnCrash = (window: BrowserWindow): void => { if (response === 0) { cleanOldCache(); - app.relaunch(); - app.exit(); + await windowHandler.exitApplication(true); } }); }); diff --git a/src/app/app-state-handler.ts b/src/app/app-state-handler.ts index fb6a48f0..60b30d35 100644 --- a/src/app/app-state-handler.ts +++ b/src/app/app-state-handler.ts @@ -1,5 +1,6 @@ -import { app, powerSaveBlocker } from 'electron'; +import { powerSaveBlocker } from 'electron'; import { logger } from '../common/logger'; +import { windowHandler } from './window-handler'; class AppStateHandler { private id?: number; @@ -9,8 +10,7 @@ class AppStateHandler { */ public restart() { logger.info(`Restarting app as per instruction from SFE`); - app.relaunch(); - app.exit(); + windowHandler.exitApplication(true); } /** diff --git a/src/app/config-handler.ts b/src/app/config-handler.ts index be37e230..2c83acb0 100644 --- a/src/app/config-handler.ts +++ b/src/app/config-handler.ts @@ -219,6 +219,8 @@ class Config { this.readGlobalConfig(); this.readInstallVariant(); this.readCloudConfig(); + + app.on('before-quit', this.writeUserConfig); } /** @@ -320,6 +322,13 @@ class Config { JSON.stringify(data), ); this.userConfig = { ...this.userConfig, ...data }; + } + + /** + * Writes the config data into the user config file + */ + public writeUserConfig = async (): Promise => { + logger.info(`config-handler: Updating user config file`); try { await writeFile( this.userConfigPath, @@ -328,13 +337,13 @@ class Config { ); logger.info( `config-handler: updated user config values with the data ${JSON.stringify( - data, + this.userConfig, )}`, ); } catch (error) { logger.error( `config-handler: failed to update user config file with ${JSON.stringify( - data, + this.userConfig, )}`, error, ); @@ -343,7 +352,7 @@ class Config { `Failed to update user config due to error: ${error}`, ); } - } + }; /** * updates new data to the cloud config diff --git a/src/app/dialog-handler.ts b/src/app/dialog-handler.ts index b8cab4d0..e39d8d45 100644 --- a/src/app/dialog-handler.ts +++ b/src/app/dialog-handler.ts @@ -194,8 +194,7 @@ export const titleBarChangeDialog = async ( if (response === 0) { logger.error(`test`, isNativeStyle); await config.updateUserConfig({ isCustomTitleBar: isNativeStyle }); - app.relaunch(); - app.exit(); + await windowHandler.exitApplication(true); } }; @@ -220,7 +219,6 @@ export const restartDialog = async (configFields: any) => { const { response } = await dialog.showMessageBox(focusedWindow, options); await config.updateUserConfig(configFields); if (response === 0) { - app.relaunch(); - app.exit(); + await windowHandler.exitApplication(true); } }; diff --git a/src/app/window-handler.ts b/src/app/window-handler.ts index 21d6547e..512d0992 100644 --- a/src/app/window-handler.ts +++ b/src/app/window-handler.ts @@ -1188,8 +1188,7 @@ export class WindowHandler { logger.info('window-handler: user updated pod url', hostname); const url = new URL(`https://${hostname}`).toString(); await config.updateUserConfig({ url }); - app.relaunch(); - app.exit(); + await windowHandler.exitApplication(true); }; await versionHandler.getClientVersion(true, this.url); @@ -2289,6 +2288,14 @@ export class WindowHandler { } } + public exitApplication = async (shouldRelaunch: boolean = true) => { + await config.writeUserConfig(); + if (shouldRelaunch) { + app.relaunch(); + } + app.exit(); + }; + /** * Listens for app load timeouts and reloads if required */ @@ -2374,7 +2381,7 @@ export class WindowHandler { const { response } = await dialog.showMessageBox(browserWindow, options); if (response === 0) { - app.exit(); + await this.exitApplication(false); } }