mirror of
https://github.com/finos/SymphonyElectron.git
synced 2025-02-25 18:55:29 -06:00
Electron-154 - Optimized reading config fields which fix the menu items issue on Windows
This commit is contained in:
parent
bb28b1e7d8
commit
4f4fc69f85
61
js/config.js
61
js/config.js
@ -6,6 +6,8 @@ const path = require('path');
|
||||
const fs = require('fs');
|
||||
const AppDirectory = require('appdirectory');
|
||||
const omit = require('lodash.omit');
|
||||
const pick = require('lodash.pick');
|
||||
const difference = require('lodash.difference');
|
||||
|
||||
const isDevEnv = require('./utils/misc.js').isDevEnv;
|
||||
const isMac = require('./utils/misc.js').isMac;
|
||||
@ -314,6 +316,64 @@ function updateUserConfigMac() {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Method that tries to grab multiple config field from user config
|
||||
* if field doesn't exist tries reading from global config
|
||||
*
|
||||
* @param {Array} fieldNames - array of config filed names
|
||||
* @returns {Promise} - object all the config data from user and global config
|
||||
*/
|
||||
function getMultipleConfigField(fieldNames) {
|
||||
return new Promise((resolve, reject) => {
|
||||
let userConfigData;
|
||||
|
||||
if (!fieldNames && fieldNames.length < 0) {
|
||||
reject('cannot read config file, invalid fields');
|
||||
return;
|
||||
}
|
||||
|
||||
// reads user config data
|
||||
readUserConfig().then((config) => {
|
||||
userConfigData = pick(config, fieldNames);
|
||||
let userConfigKeys = userConfigData ? Object.keys(userConfigData) : undefined;
|
||||
|
||||
/**
|
||||
* Condition to validate data from user config,
|
||||
* if all the required fields are not present
|
||||
* this tries to fetch the remaining fields from global config
|
||||
*/
|
||||
if (!userConfigKeys || userConfigKeys.length < fieldNames.length) {
|
||||
|
||||
// remainingConfig - config field that are not present in the user config
|
||||
let remainingConfig = difference(fieldNames, userConfigKeys);
|
||||
|
||||
if (remainingConfig && Object.keys(remainingConfig).length > 0) {
|
||||
readGlobalConfig().then((globalConfigData) => {
|
||||
// assigns the remaining fields from global config to the user config
|
||||
userConfigData = Object.assign(userConfigData, pick(globalConfigData, remainingConfig));
|
||||
resolve(userConfigData);
|
||||
}).catch((err) => {
|
||||
reject(err);
|
||||
});
|
||||
}
|
||||
|
||||
} else {
|
||||
resolve(userConfigData);
|
||||
}
|
||||
}).catch(() => {
|
||||
// This reads global config if there was any
|
||||
// error while reading user config
|
||||
readGlobalConfig().then((config) => {
|
||||
userConfigData = pick(config, fieldNames);
|
||||
resolve(userConfigData);
|
||||
}).catch((err) => {
|
||||
reject(err);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Clears the cached config
|
||||
*/
|
||||
@ -331,6 +391,7 @@ module.exports = {
|
||||
updateConfigField,
|
||||
updateUserConfigWin,
|
||||
updateUserConfigMac,
|
||||
getMultipleConfigField,
|
||||
|
||||
// items below here are only exported for testing, do NOT use!
|
||||
saveUserConfig,
|
||||
|
@ -1,7 +1,7 @@
|
||||
'use strict';
|
||||
|
||||
const electron = require('electron');
|
||||
const { getConfigField, updateConfigField } = require('../config.js');
|
||||
const { updateConfigField, getMultipleConfigField } = require('../config.js');
|
||||
const AutoLaunch = require('auto-launch');
|
||||
const isMac = require('../utils/misc.js').isMac;
|
||||
const log = require('../log.js');
|
||||
@ -266,39 +266,38 @@ function getTemplate(app) {
|
||||
* based on configuration
|
||||
*/
|
||||
function setCheckboxValues() {
|
||||
getConfigField('minimizeOnClose').then(function(mClose) {
|
||||
minimizeOnClose = mClose;
|
||||
}).catch(function(err) {
|
||||
let title = 'Error loading configuration';
|
||||
log.send(logLevels.ERROR, 'MenuTemplate: error getting config field minimizeOnClose, error: ' + err);
|
||||
electron.dialog.showErrorBox(title, title + ': ' + err);
|
||||
});
|
||||
|
||||
getConfigField('launchOnStartup').then(function(lStartup) {
|
||||
launchOnStartup = lStartup;
|
||||
}).catch(function(err) {
|
||||
let title = 'Error loading configuration';
|
||||
log.send(logLevels.ERROR, 'MenuTemplate: error getting config field launchOnStartup, error: ' + err);
|
||||
electron.dialog.showErrorBox(title, title + ': ' + err);
|
||||
});
|
||||
|
||||
getConfigField('alwaysOnTop').then(function(mAlwaysOnTop) {
|
||||
isAlwaysOnTop = mAlwaysOnTop;
|
||||
eventEmitter.emit('isAlwaysOnTop', isAlwaysOnTop);
|
||||
}).catch(function(err) {
|
||||
let title = 'Error loading configuration';
|
||||
log.send(logLevels.ERROR, 'MenuTemplate: error getting config field alwaysOnTop, error: ' + err);
|
||||
electron.dialog.showErrorBox(title, title + ': ' + err);
|
||||
});
|
||||
|
||||
getConfigField('notificationSettings').then(function(notfObject) {
|
||||
eventEmitter.emit('notificationSettings', notfObject);
|
||||
}).catch(function(err) {
|
||||
let title = 'Error loading configuration';
|
||||
log.send(logLevels.ERROR, 'MenuTemplate: error getting config field notificationSettings, error: ' + err);
|
||||
electron.dialog.showErrorBox(title, title + ': ' + err);
|
||||
});
|
||||
|
||||
/**
|
||||
* Method that reads multiple config fields
|
||||
*/
|
||||
getMultipleConfigField(['minimizeOnClose', 'launchOnStartup', 'alwaysOnTop', 'notificationSettings'])
|
||||
.then(function (configData) {
|
||||
for (let key in configData) {
|
||||
if (configData.hasOwnProperty(key)) { // eslint-disable-line no-prototype-builtins
|
||||
switch (key) {
|
||||
case 'minimizeOnClose':
|
||||
minimizeOnClose = configData[key];
|
||||
break;
|
||||
case 'launchOnStartup':
|
||||
launchOnStartup = configData[key];
|
||||
break;
|
||||
case 'alwaysOnTop':
|
||||
isAlwaysOnTop = configData[key];
|
||||
eventEmitter.emit('isAlwaysOnTop', configData[key]);
|
||||
break;
|
||||
case 'notificationSettings':
|
||||
eventEmitter.emit('notificationSettings', configData[key]);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
.catch((err) => {
|
||||
let title = 'Error loading configuration';
|
||||
log.send(logLevels.ERROR, 'MenuTemplate: error reading configuration fields, error: ' + err);
|
||||
electron.dialog.showErrorBox(title, title + ': ' + err);
|
||||
});
|
||||
}
|
||||
|
||||
function getMinimizeOnClose() {
|
||||
|
@ -104,6 +104,8 @@
|
||||
"filesize": "^3.5.10",
|
||||
"keymirror": "0.1.1",
|
||||
"lodash.omit": "^4.5.0",
|
||||
"lodash.pick": "^4.4.0",
|
||||
"lodash.difference": "^4.5.0",
|
||||
"winreg": "^1.2.3"
|
||||
},
|
||||
"optionalDependencies": {
|
||||
|
Loading…
Reference in New Issue
Block a user