SDA-4247 (Update user config once on before-quit) (#1918)

* SDA-4347 - Add Semaphore for updating user config file

Signed-off-by: Kiran Niranjan <kiran.niranjan@symphony.com>

* SDA-4347 - Update user config once on before-quit

Signed-off-by: Kiran Niranjan <kiran.niranjan@symphony.com>

* SDA-4347 - Handle application exit

Signed-off-by: Kiran Niranjan <kiran.niranjan@symphony.com>

* SDA-4347 - Fix uts

Signed-off-by: Kiran Niranjan <kiran.niranjan@symphony.com>

---------

Signed-off-by: Kiran Niranjan <kiran.niranjan@symphony.com>
This commit is contained in:
Kiran Niranjan 2023-07-28 18:51:09 +05:30 committed by GitHub
parent 090c8e52b1
commit b41e07db88
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 39 additions and 15 deletions

View File

@ -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('app cache handler', () => {
describe('check app cache file', () => { describe('check app cache file', () => {
const cachePathExpected = path.join(app.getPath('userData'), 'CacheCheck'); const cachePathExpected = path.join(app.getPath('userData'), 'CacheCheck');

View File

@ -11,6 +11,7 @@ import * as rimraf from 'rimraf';
import { i18n } from '../common/i18n'; import { i18n } from '../common/i18n';
import { logger } from '../common/logger'; import { logger } from '../common/logger';
import { windowHandler } from './window-handler';
// Cache check file path // Cache check file path
const userDataPath: string = app.getPath('userData'); const userDataPath: string = app.getPath('userData');
@ -123,8 +124,7 @@ export const cleanAppCacheOnCrash = (window: BrowserWindow): void => {
if (response === 0) { if (response === 0) {
cleanOldCache(); cleanOldCache();
app.relaunch(); await windowHandler.exitApplication(true);
app.exit();
} }
}); });
}); });

View File

@ -1,5 +1,6 @@
import { app, powerSaveBlocker } from 'electron'; import { powerSaveBlocker } from 'electron';
import { logger } from '../common/logger'; import { logger } from '../common/logger';
import { windowHandler } from './window-handler';
class AppStateHandler { class AppStateHandler {
private id?: number; private id?: number;
@ -9,8 +10,7 @@ class AppStateHandler {
*/ */
public restart() { public restart() {
logger.info(`Restarting app as per instruction from SFE`); logger.info(`Restarting app as per instruction from SFE`);
app.relaunch(); windowHandler.exitApplication(true);
app.exit();
} }
/** /**

View File

@ -219,6 +219,8 @@ class Config {
this.readGlobalConfig(); this.readGlobalConfig();
this.readInstallVariant(); this.readInstallVariant();
this.readCloudConfig(); this.readCloudConfig();
app.on('before-quit', this.writeUserConfig);
} }
/** /**
@ -320,6 +322,13 @@ class Config {
JSON.stringify(data), JSON.stringify(data),
); );
this.userConfig = { ...this.userConfig, ...data }; this.userConfig = { ...this.userConfig, ...data };
}
/**
* Writes the config data into the user config file
*/
public writeUserConfig = async (): Promise<void> => {
logger.info(`config-handler: Updating user config file`);
try { try {
await writeFile( await writeFile(
this.userConfigPath, this.userConfigPath,
@ -328,13 +337,13 @@ class Config {
); );
logger.info( logger.info(
`config-handler: updated user config values with the data ${JSON.stringify( `config-handler: updated user config values with the data ${JSON.stringify(
data, this.userConfig,
)}`, )}`,
); );
} catch (error) { } catch (error) {
logger.error( logger.error(
`config-handler: failed to update user config file with ${JSON.stringify( `config-handler: failed to update user config file with ${JSON.stringify(
data, this.userConfig,
)}`, )}`,
error, error,
); );
@ -343,7 +352,7 @@ class Config {
`Failed to update user config due to error: ${error}`, `Failed to update user config due to error: ${error}`,
); );
} }
} };
/** /**
* updates new data to the cloud config * updates new data to the cloud config

View File

@ -194,8 +194,7 @@ export const titleBarChangeDialog = async (
if (response === 0) { if (response === 0) {
logger.error(`test`, isNativeStyle); logger.error(`test`, isNativeStyle);
await config.updateUserConfig({ isCustomTitleBar: isNativeStyle }); await config.updateUserConfig({ isCustomTitleBar: isNativeStyle });
app.relaunch(); await windowHandler.exitApplication(true);
app.exit();
} }
}; };
@ -220,7 +219,6 @@ export const restartDialog = async (configFields: any) => {
const { response } = await dialog.showMessageBox(focusedWindow, options); const { response } = await dialog.showMessageBox(focusedWindow, options);
await config.updateUserConfig(configFields); await config.updateUserConfig(configFields);
if (response === 0) { if (response === 0) {
app.relaunch(); await windowHandler.exitApplication(true);
app.exit();
} }
}; };

View File

@ -1188,8 +1188,7 @@ export class WindowHandler {
logger.info('window-handler: user updated pod url', hostname); logger.info('window-handler: user updated pod url', hostname);
const url = new URL(`https://${hostname}`).toString(); const url = new URL(`https://${hostname}`).toString();
await config.updateUserConfig({ url }); await config.updateUserConfig({ url });
app.relaunch(); await windowHandler.exitApplication(true);
app.exit();
}; };
await versionHandler.getClientVersion(true, this.url); 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 * Listens for app load timeouts and reloads if required
*/ */
@ -2374,7 +2381,7 @@ export class WindowHandler {
const { response } = await dialog.showMessageBox(browserWindow, options); const { response } = await dialog.showMessageBox(browserWindow, options);
if (response === 0) { if (response === 0) {
app.exit(); await this.exitApplication(false);
} }
} }