Removing myself from callback hell slowly but surely

 Removing ipc calls reducing chance to shrek the drive
This commit is contained in:
shockrah 2021-03-08 19:53:14 -08:00
parent ef0bc70f90
commit d8244388c2
3 changed files with 40 additions and 38 deletions

View File

@ -129,10 +129,9 @@
$('#add-admin-json').click(async () => await add_server_to_config() )
try {
let config = await ipcRenderer.invoke('config-request')
await auth.init(config)
let config = await auth.init()
for(const server_cfg of config['servers']) {
for(const server_cfg of config.data['servers']) {
const remote = server_cfg['server']
const user = server_cfg['user']

View File

@ -1,37 +1,51 @@
const { ipcRenderer } = require('electron')
const { ArgumentParser } = require('argparse')
const fs = require('fs')
const fs = require('fs').promises
const syncFs = require('fs')
class Config {
/**
*
* @param {String} path path to config on startup
* @param {Object} data Contains actual config data
*/
constructor(path, data) {
this.path = path
this.data = data
}
// TODO: this function basically
async write_disk() {
const str = JSON.stringify(this.data, null, 4)
await fs.writeFile(this.path, str)
}
}
exports.Config = Config
/**
* Gets the config data off disk
* Because we're calling this from node's process we can actuall us the 'fs' module here
* @param {ArgumentParset} parser
* @returns {Config} { path }
* @returns {Config}
*/
exports.get_config = function(parser) {
exports.from_cli_parser = function(parser) {
const a = parser.parse_args()
const defaultcfg = `${process.env.HOME || process.env.USERPROFILE}/.config/freechat/config.json`
const args = parser.parse_args()
const defaultpath = `${process.env.HOME || process.env.USERPROFILE}/.config/freechat/config.json`
// we'll just assume that the config is there as per the installation process
const path = (a['config']) ? a['config'] : defaultcfg
let ret = { path: path }
// We'll just assume that the config is there as per the installation process
const path = (args['config']) ? args['config'] : defaultpath
let ret = new Config(path, {})
fs.readFile(path, (err, data) => {
if(err) {
console.log('No valid config found')
process.exit()
}
const file_json = JSON.parse(data)
for(const key of Object.keys(file_json)) {
if(key == 'path') { continue } // don't overwrite the old path
else {
ret[key] = file_json[key]
}
// Allow errors from here to bubble up instead of handling them
fs.readFile(path).then(file => {
const json = JSON.parse(file)
for(const key of Object.keys(json)) {
ret.data[key] = json[key]
}
})
.catch(err => console.error(err))
// now we'll just blindly construct the config object from here on out
return ret
}
@ -57,15 +71,4 @@ exports.update_file = function(config, path='default') {
})
}
/**
* Loads the parse with the various arguments that the program takes
* @param {ArgumentParser} parser
*/
exports.load_parser = function() {
const parser = new ArgumentParser({
description: 'Freechat Electron Client'
})
parser.add_argument('-c', '--config', {help: 'Specify config path'})
return parser
}

View File

@ -6,13 +6,13 @@ exports.add_server_to_config = async function() {
$('#admin-json-err').text('')
try {
const data = JSON.parse($('#admin-json').val())
const config = await ipcRenderer.invoke('config-request')
let config = await ipcRenderer.invoke('config-request')
if(!config['servers']) {
config['servers'] = []
}
config['servers'].push(data)
await ipcRenderer.invoke('config-update', config, config['path'])
await ipcRenderer.invoke('config-update', config)
try {
await auth.login(