settings optimalization

This commit is contained in:
Jan Prochazka
2021-04-29 11:28:32 +02:00
parent e647ab471e
commit 47ea474555
3 changed files with 15 additions and 9 deletions

View File

@@ -9,6 +9,16 @@ const currentVersion = require('../currentVersion');
const platformInfo = require('../utility/platformInfo'); const platformInfo = require('../utility/platformInfo');
module.exports = { module.exports = {
settingsValue: {},
async _init() {
try {
this.settingsValue = JSON.parse(await fs.readFile(path.join(datadir(), 'settings.json'), { encoding: 'utf-8' }));
} catch (err) {
this.settingsValue = {};
}
},
get_meta: 'get', get_meta: 'get',
async get() { async get() {
// const toolbarButtons = process.env.TOOLBAR; // const toolbarButtons = process.env.TOOLBAR;
@@ -47,23 +57,19 @@ module.exports = {
getSettings_meta: 'get', getSettings_meta: 'get',
async getSettings() { async getSettings() {
try { return this.settingsValue;
return JSON.parse(await fs.readFile(path.join(datadir(), 'settings.json'), { encoding: 'utf-8' }));
} catch (err) {
return {};
}
}, },
updateSettings_meta: 'post', updateSettings_meta: 'post',
async updateSettings(values) { async updateSettings(values) {
if (!hasPermission(`settings/change`)) return false; if (!hasPermission(`settings/change`)) return false;
const oldSettings = await this.getSettings();
try { try {
const updated = { const updated = {
...oldSettings, ...this.settingsValue,
...values, ...values,
}; };
await fs.writeFile(path.join(datadir(), 'settings.json'), JSON.stringify(updated, undefined, 2)); await fs.writeFile(path.join(datadir(), 'settings.json'), JSON.stringify(updated, undefined, 2));
this.settingsValue = updated;
socket.emitChanged(`settings-changed`); socket.emitChanged(`settings-changed`);
return updated; return updated;
} catch (err) { } catch (err) {

View File

@@ -80,7 +80,7 @@ module.exports = {
msgtype: 'connect', msgtype: 'connect',
connection: { ...connection, database }, connection: { ...connection, database },
structure: lastClosed ? lastClosed.structure : null, structure: lastClosed ? lastClosed.structure : null,
globalSettings: await config.getSettings() globalSettings: config.settingsValue
}); });
return newOpened; return newOpened;
}, },

View File

@@ -66,7 +66,7 @@ module.exports = {
if (newOpened.disconnected) return; if (newOpened.disconnected) return;
this.close(conid, false); this.close(conid, false);
}); });
subprocess.send({ msgtype: 'connect', ...connection, globalSettings: await config.getSettings() }); subprocess.send({ msgtype: 'connect', ...connection, globalSettings: config.settingsValue });
return newOpened; return newOpened;
}); });
return res; return res;