Reduce the number of writes to the config (#2498)

* Add some logging, refactor setting config items

* Move active team to setMultiple, reduce serverInfos calls if the data is already the same

* Fix some logging

* Lint fix
This commit is contained in:
Devin Binnie
2023-01-12 08:46:06 -05:00
committed by GitHub
parent 9f6893fdb2
commit db76a67fb5
7 changed files with 62 additions and 30 deletions

View File

@@ -102,7 +102,7 @@ export class Config extends EventEmitter {
ipcMain.handle(GET_CONFIGURATION, this.handleGetConfiguration); ipcMain.handle(GET_CONFIGURATION, this.handleGetConfiguration);
ipcMain.handle(GET_LOCAL_CONFIGURATION, this.handleGetLocalConfiguration); ipcMain.handle(GET_LOCAL_CONFIGURATION, this.handleGetLocalConfiguration);
ipcMain.handle(UPDATE_TEAMS, this.handleUpdateTeams); ipcMain.handle(UPDATE_TEAMS, this.handleUpdateTeams);
ipcMain.on(UPDATE_CONFIGURATION, this.setMultiple); ipcMain.on(UPDATE_CONFIGURATION, this.updateConfiguration);
if (process.platform === 'darwin' || process.platform === 'win32') { if (process.platform === 'darwin' || process.platform === 'win32') {
nativeTheme.on('updated', this.handleUpdateTheme); nativeTheme.on('updated', this.handleUpdateTheme);
} }
@@ -151,16 +151,22 @@ export class Config extends EventEmitter {
* @param {*} data value to save for provided key * @param {*} data value to save for provided key
*/ */
set = (key: keyof ConfigType, data: ConfigType[keyof ConfigType]): void => { set = (key: keyof ConfigType, data: ConfigType[keyof ConfigType]): void => {
if (key && this.localConfigData) { log.debug('Config.set');
if (key === 'teams') { this.setMultiple({[key]: data});
this.localConfigData.teams = this.filterOutPredefinedTeams(data as TeamWithTabs[]); }
this.predefinedTeams = this.filterInPredefinedTeams(data as TeamWithTabs[]);
} else { updateConfiguration = (event: Electron.IpcMainEvent, properties: Array<{key: keyof ConfigType; data: ConfigType[keyof ConfigType]}> = []): Partial<ConfigType> | undefined => {
this.localConfigData = Object.assign({}, this.localConfigData, {[key]: data}); log.debug('Config.updateConfiguration', properties);
}
this.regenerateCombinedConfigData(); if (properties.length) {
this.saveLocalConfigData(); const newData = properties.reduce((obj, data) => {
(obj as any)[data.key] = data.data;
return obj;
}, {} as Partial<ConfigType>);
this.setMultiple(newData);
} }
return this.localConfigData;
} }
/** /**
@@ -168,14 +174,16 @@ export class Config extends EventEmitter {
* *
* @param {array} properties an array of config properties to save * @param {array} properties an array of config properties to save
*/ */
setMultiple = (event: Electron.IpcMainEvent, properties: Array<{key: keyof ConfigType; data: ConfigType[keyof ConfigType]}> = []): Partial<ConfigType> | undefined => { setMultiple = (newData: Partial<ConfigType>) => {
log.debug('Config.setMultiple', properties); log.debug('Config.setMultiple', newData);
if (properties.length) { this.localConfigData = Object.assign({}, this.localConfigData, newData);
this.localConfigData = Object.assign({}, this.localConfigData, ...properties.map(({key, data}) => ({[key]: data}))); if (newData.teams && this.localConfigData) {
this.regenerateCombinedConfigData(); this.localConfigData.teams = this.filterOutPredefinedTeams(newData.teams as TeamWithTabs[]);
this.saveLocalConfigData(); this.predefinedTeams = this.filterInPredefinedTeams(newData.teams as TeamWithTabs[]);
} }
this.regenerateCombinedConfigData();
this.saveLocalConfigData();
return this.localConfigData; //this is the only part that changes return this.localConfigData; //this is the only part that changes
} }
@@ -211,6 +219,8 @@ export class Config extends EventEmitter {
return; return;
} }
log.info('Saving config data to file...');
try { try {
this.writeFile(this.configFilePath, this.localConfigData, (error: NodeJS.ErrnoException | null) => { this.writeFile(this.configFilePath, this.localConfigData, (error: NodeJS.ErrnoException | null) => {
if (error) { if (error) {

View File

@@ -24,6 +24,13 @@ let didCheckForAddServerModal = false;
// //
export function handleConfigUpdate(newConfig: CombinedConfig) { export function handleConfigUpdate(newConfig: CombinedConfig) {
if (log.transports.file.level !== newConfig.logLevel) {
log.error('Log level set to:', newConfig.logLevel);
}
if (newConfig.logLevel) {
setLoggingLevel(newConfig.logLevel as LogLevel);
}
log.debug('App.Config.handleConfigUpdate'); log.debug('App.Config.handleConfigUpdate');
log.silly('App.Config.handleConfigUpdate', newConfig); log.silly('App.Config.handleConfigUpdate', newConfig);
@@ -63,9 +70,6 @@ export function handleConfigUpdate(newConfig: CombinedConfig) {
handleMainWindowIsShown(); handleMainWindowIsShown();
} }
log.info('Log level set to:', newConfig.logLevel);
setLoggingLevel(newConfig.logLevel as LogLevel);
handleUpdateMenuEvent(); handleUpdateMenuEvent();
if (newConfig.trayIconTheme) { if (newConfig.trayIconTheme) {
refreshTrayImages(newConfig.trayIconTheme); refreshTrayImages(newConfig.trayIconTheme);

View File

@@ -321,8 +321,10 @@ export function handleUpdateLastActive(event: IpcMainEvent, serverName: string,
team.lastActiveTab = viewOrder; team.lastActiveTab = viewOrder;
} }
}); });
Config.set('teams', teams); Config.setMultiple({
Config.set('lastActiveTeam', teams.find((team) => team.name === serverName)?.order || 0); teams,
lastActiveTeam: teams.find((team) => team.name === serverName)?.order || 0,
});
} }
export function handlePingDomain(event: IpcMainInvokeEvent, url: string): Promise<string> { export function handlePingDomain(event: IpcMainInvokeEvent, url: string): Promise<string> {

View File

@@ -50,6 +50,7 @@ export function updateSpellCheckerLocales() {
} }
export function updateServerInfos(teams: TeamWithTabs[]) { export function updateServerInfos(teams: TeamWithTabs[]) {
log.silly('app.utils.updateServerInfos');
const serverInfos: Array<Promise<RemoteInfo | string | undefined>> = []; const serverInfos: Array<Promise<RemoteInfo | string | undefined>> = [];
teams.forEach((team) => { teams.forEach((team) => {
const serverInfo = new ServerInfo(new MattermostServer(team.name, team.url)); const serverInfo = new ServerInfo(new MattermostServer(team.name, team.url));
@@ -57,11 +58,14 @@ export function updateServerInfos(teams: TeamWithTabs[]) {
}); });
Promise.all(serverInfos).then((data: Array<RemoteInfo | string | undefined>) => { Promise.all(serverInfos).then((data: Array<RemoteInfo | string | undefined>) => {
const teams = Config.teams; const teams = Config.teams;
let hasUpdates = false;
teams.forEach((team) => { teams.forEach((team) => {
updateServerURL(data, team); hasUpdates = hasUpdates || updateServerURL(data, team);
openExtraTabs(data, team); hasUpdates = hasUpdates || openExtraTabs(data, team);
}); });
Config.set('teams', teams); if (hasUpdates) {
Config.set('teams', teams);
}
}).catch((reason: any) => { }).catch((reason: any) => {
log.error('Error getting server infos', reason); log.error('Error getting server infos', reason);
}); });
@@ -69,27 +73,33 @@ export function updateServerInfos(teams: TeamWithTabs[]) {
function updateServerURL(data: Array<RemoteInfo | string | undefined>, team: TeamWithTabs) { function updateServerURL(data: Array<RemoteInfo | string | undefined>, team: TeamWithTabs) {
const remoteInfo = data.find((info) => info && typeof info !== 'string' && info.name === team.name) as RemoteInfo; const remoteInfo = data.find((info) => info && typeof info !== 'string' && info.name === team.name) as RemoteInfo;
if (remoteInfo && remoteInfo.siteURL) { if (remoteInfo && remoteInfo.siteURL && team.url !== remoteInfo.siteURL) {
team.url = remoteInfo.siteURL; team.url = remoteInfo.siteURL;
return true;
} }
return false;
} }
function openExtraTabs(data: Array<RemoteInfo | string | undefined>, team: TeamWithTabs) { function openExtraTabs(data: Array<RemoteInfo | string | undefined>, team: TeamWithTabs) {
let hasUpdates = false;
const remoteInfo = data.find((info) => info && typeof info !== 'string' && info.name === team.name) as RemoteInfo; const remoteInfo = data.find((info) => info && typeof info !== 'string' && info.name === team.name) as RemoteInfo;
if (remoteInfo) { if (remoteInfo) {
team.tabs.forEach((tab) => { team.tabs.forEach((tab) => {
if (tab.name !== TAB_MESSAGING && remoteInfo.serverVersion && Utils.isVersionGreaterThanOrEqualTo(remoteInfo.serverVersion, '6.0.0')) { if (tab.name !== TAB_MESSAGING && remoteInfo.serverVersion && Utils.isVersionGreaterThanOrEqualTo(remoteInfo.serverVersion, '6.0.0')) {
if (tab.name === TAB_PLAYBOOKS && remoteInfo.hasPlaybooks && tab.isOpen !== false) { if (tab.name === TAB_PLAYBOOKS && remoteInfo.hasPlaybooks && typeof tab.isOpen === 'undefined') {
log.info(`opening ${team.name}___${tab.name} on hasPlaybooks`); log.info(`opening ${team.name}___${tab.name} on hasPlaybooks`);
tab.isOpen = true; tab.isOpen = true;
hasUpdates = true;
} }
if (tab.name === TAB_FOCALBOARD && remoteInfo.hasFocalboard && tab.isOpen !== false) { if (tab.name === TAB_FOCALBOARD && remoteInfo.hasFocalboard && typeof tab.isOpen === 'undefined') {
log.info(`opening ${team.name}___${tab.name} on hasFocalboard`); log.info(`opening ${team.name}___${tab.name} on hasFocalboard`);
tab.isOpen = true; tab.isOpen = true;
hasUpdates = true;
} }
} }
}); });
} }
return hasUpdates;
} }
export function handleUpdateMenuEvent() { export function handleUpdateMenuEvent() {

View File

@@ -85,7 +85,7 @@ export class MattermostView extends EventEmitter {
this.view = new BrowserView(this.options); this.view = new BrowserView(this.options);
this.resetLoadingStatus(); this.resetLoadingStatus();
log.info(`BrowserView created for server ${this.tab.name}`); log.verbose(`BrowserView created for server ${this.tab.name}`);
this.hasBeenShown = false; this.hasBeenShown = false;
@@ -164,7 +164,7 @@ export class MattermostView extends EventEmitter {
} else { } else {
loadURL = this.tab.url.toString(); loadURL = this.tab.url.toString();
} }
log.info(`[${Util.shorten(this.tab.name)}] Loading ${loadURL}`); log.verbose(`[${Util.shorten(this.tab.name)}] Loading ${loadURL}`);
const loading = this.view.webContents.loadURL(loadURL, {userAgent: composeUserAgent()}); const loading = this.view.webContents.loadURL(loadURL, {userAgent: composeUserAgent()});
loading.then(this.loadSuccess(loadURL)).catch((err) => { loading.then(this.loadSuccess(loadURL)).catch((err) => {
if (err.code && err.code.startsWith('ERR_CERT')) { if (err.code && err.code.startsWith('ERR_CERT')) {
@@ -220,7 +220,7 @@ export class MattermostView extends EventEmitter {
loadSuccess = (loadURL: string) => { loadSuccess = (loadURL: string) => {
return () => { return () => {
log.info(`[${Util.shorten(this.tab.name)}] finished loading ${loadURL}`); log.verbose(`[${Util.shorten(this.tab.name)}] finished loading ${loadURL}`);
WindowManager.sendToRenderer(LOAD_SUCCESS, this.tab.name); WindowManager.sendToRenderer(LOAD_SUCCESS, this.tab.name);
this.maxRetries = MAX_SERVER_RETRIES; this.maxRetries = MAX_SERVER_RETRIES;
if (this.status === Status.LOADING) { if (this.status === Status.LOADING) {

View File

@@ -127,6 +127,8 @@ export class ViewManager {
* close, open, or reload tabs, taking care to reuse tabs and * close, open, or reload tabs, taking care to reuse tabs and
* preserve focus on the currently selected tab. */ * preserve focus on the currently selected tab. */
reloadConfiguration = (configServers: TeamWithTabs[]) => { reloadConfiguration = (configServers: TeamWithTabs[]) => {
log.debug('viewManager.reloadConfiguration');
const focusedTuple: TabTuple | undefined = this.views.get(this.currentView as string)?.urlTypeTuple; const focusedTuple: TabTuple | undefined = this.views.get(this.currentView as string)?.urlTypeTuple;
const current: Map<TabTuple, MattermostView> = new Map(); const current: Map<TabTuple, MattermostView> = new Map();
@@ -199,6 +201,8 @@ export class ViewManager {
} }
showInitial = () => { showInitial = () => {
log.verbose('viewManager.showInitial');
const servers = this.getServers(); const servers = this.getServers();
if (servers.length) { if (servers.length) {
const element = servers.find((e) => e.order === this.lastActiveServer) || servers.find((e) => e.order === 0); const element = servers.find((e) => e.order === this.lastActiveServer) || servers.find((e) => e.order === 0);

View File

@@ -590,6 +590,7 @@ export class WindowManager {
} }
switchServer = (serverName: string, waitForViewToExist = false) => { switchServer = (serverName: string, waitForViewToExist = false) => {
log.debug('windowManager.switchServer');
this.showMainWindow(); this.showMainWindow();
const server = Config.teams.find((team) => team.name === serverName); const server = Config.teams.find((team) => team.name === serverName);
if (!server) { if (!server) {
@@ -617,6 +618,7 @@ export class WindowManager {
} }
switchTab = (serverName: string, tabName: string) => { switchTab = (serverName: string, tabName: string) => {
log.debug('windowManager.switchTab');
this.showMainWindow(); this.showMainWindow();
const tabViewName = getTabViewName(serverName, tabName); const tabViewName = getTabViewName(serverName, tabName);
this.viewManager?.showByName(tabViewName); this.viewManager?.showByName(tabViewName);