Fix hard coded config.hjson event in ConfigLoader

* ConfigLoader init can now take onReload handler
* Use onReload for config.hjson/system config event
This commit is contained in:
Bryan Ashby 2020-06-15 21:21:26 -06:00
parent 5b7c83cdc1
commit 8808447343
No known key found for this signature in database
GPG Key ID: B49EB437951D2542
2 changed files with 22 additions and 8 deletions

View File

@ -37,6 +37,12 @@ exports.Config = class Config extends ConfigLoader {
} }
} }
}, },
onReload : err => {
if (!err) {
const Events = require('./events.js');
Events.emit(Events.getSystemEvents().ConfigChanged);
}
},
}; };
systemConfigInstance = new Config(options); systemConfigInstance = new Config(options);

View File

@ -8,13 +8,26 @@ const mapValuesDeep = require('deepdash/getMapValuesDeep')(_);
module.exports = class ConfigLoader { module.exports = class ConfigLoader {
constructor( constructor(
{ hotReload = true, defaultConfig = {}, defaultsCustomizer = null } = { hotReload : true, defaultConfig : {}, defaultsCustomizer : null } ) {
hotReload = true,
defaultConfig = {},
defaultsCustomizer = null,
onReload = null,
} =
{
hotReload : true,
defaultConfig : {},
defaultsCustomizer : null,
onReload : null,
}
)
{ {
this.current = {}; this.current = {};
this.hotReload = hotReload; this.hotReload = hotReload;
this.defaultConfig = defaultConfig; this.defaultConfig = defaultConfig;
this.defaultsCustomizer = defaultsCustomizer; this.defaultsCustomizer = defaultsCustomizer;
this.onReload = onReload;
} }
init(baseConfigPath, cb) { init(baseConfigPath, cb) {
@ -135,10 +148,6 @@ module.exports = class ConfigLoader {
} }
} }
break; break;
case 'regex' :
// :TODO: What flags to use, etc.?
break;
} }
return value; return value;
@ -182,9 +191,8 @@ module.exports = class ConfigLoader {
const reCachedPath = paths.join(fileRoot, fileName); const reCachedPath = paths.join(fileRoot, fileName);
if (this.configPaths.includes(reCachedPath)) { if (this.configPaths.includes(reCachedPath)) {
this._reload(this.baseConfigPath, err => { this._reload(this.baseConfigPath, err => {
if (!err) { if (_.isFunction(this.onReload)) {
const Events = require('./events.js'); this.onReload(err, reCachedPath);
Events.emit(Events.getSystemEvents().ConfigChanged);
} }
}); });
} }