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() ) $('#add-admin-json').click(async () => await add_server_to_config() )
try { try {
let config = await ipcRenderer.invoke('config-request') let config = await auth.init()
await auth.init(config)
for(const server_cfg of config['servers']) { for(const server_cfg of config.data['servers']) {
const remote = server_cfg['server'] const remote = server_cfg['server']
const user = server_cfg['user'] const user = server_cfg['user']

View File

@ -1,37 +1,51 @@
const { ipcRenderer } = require('electron')
const { ArgumentParser } = require('argparse') 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 * 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 * @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 args = parser.parse_args()
const defaultcfg = `${process.env.HOME || process.env.USERPROFILE}/.config/freechat/config.json` 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 // We'll just assume that the config is there as per the installation process
const path = (a['config']) ? a['config'] : defaultcfg const path = (args['config']) ? args['config'] : defaultpath
let ret = { path: path } let ret = new Config(path, {})
fs.readFile(path, (err, data) => { // Allow errors from here to bubble up instead of handling them
if(err) {
console.log('No valid config found') fs.readFile(path).then(file => {
process.exit() const json = JSON.parse(file)
}
const file_json = JSON.parse(data) for(const key of Object.keys(json)) {
for(const key of Object.keys(file_json)) { ret.data[key] = json[key]
if(key == 'path') { continue } // don't overwrite the old path
else {
ret[key] = file_json[key]
}
} }
}) })
.catch(err => console.error(err))
// now we'll just blindly construct the config object from here on out
return ret 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('') $('#admin-json-err').text('')
try { try {
const data = JSON.parse($('#admin-json').val()) const data = JSON.parse($('#admin-json').val())
const config = await ipcRenderer.invoke('config-request') let config = await ipcRenderer.invoke('config-request')
if(!config['servers']) { if(!config['servers']) {
config['servers'] = [] config['servers'] = []
} }
config['servers'].push(data) config['servers'].push(data)
await ipcRenderer.invoke('config-update', config, config['path']) await ipcRenderer.invoke('config-update', config)
try { try {
await auth.login( await auth.login(