Skip to content

Commit

Permalink
✨ Add setting actions & dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
skerit committed Jan 18, 2024
1 parent 0abd8e1 commit bd125f8
Show file tree
Hide file tree
Showing 5 changed files with 830 additions and 137 deletions.
83 changes: 77 additions & 6 deletions lib/app/model/alchemy_setting_model.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,57 @@ AlchemySetting.constitute(function chimeraConfig() {
edit.addField('configuration')
});

/**
* Apply the given changes
*
* @author Jelle De Loecker <[email protected]>
* @since 1.4.0
* @version 1.4.0
*
* @param {Object} changes
* @param {Conduit} permission_context
*/
AlchemySetting.setMethod(async function saveChanges(changes, permission_context) {

if (!changes) {
return;
}

let setting_id,
new_value,
setting,
doc;

for (setting_id in changes) {
new_value = changes[setting_id];
setting = Classes.Alchemy.Setting.SYSTEM.get(setting_id);

if (!setting) {
throw new Error('Unknown setting: ' + setting_id);
}

// We don't throw an error here:
// we assume the user should not have seen this setting anyway
if (!setting.canBeEditedBy(permission_context)) {
continue;
}

doc = await this.findByValues({
setting_id: setting_id
});

if (!doc) {
doc = this.createDocument({
setting_id
});
}

doc.configuration = setting.createConfigurationObject(new_value);

await doc.save();
}
});

/**
* Do something before saving the record
*
Expand All @@ -65,19 +116,39 @@ AlchemySetting.constitute(function chimeraConfig() {
* @param {Document.AlchemyTask} doc
*/
AlchemySetting.setMethod(function beforeSave(doc) {

return doc.applySetting();
});

/**
* Update the schedules after saving
* Apply this setting
*
* @author Jelle De Loecker <[email protected]>
* @since 1.4.0
* @version 1.4.0
*
* @param {Object} main
* @param {Object} info
* @param {boolean} do_actions Should the setting actions be executed
*/
AlchemySetting.setMethod(function afterSave(main, info) {
AlchemySetting.setDocumentMethod(function applySetting(do_actions = true) {

});
if (!this.setting_id) {
return;
}

let setting = Classes.Alchemy.Setting.SYSTEM.get(this.setting_id);

if (!setting) {
return;
}

let existing_value = alchemy.system_settings.getPath(this.setting_id);

if (!existing_value) {
existing_value = setting.generateValue();
}

if (do_actions) {
return existing_value.setValue(this.configuration.value);
} else {
return existing_value.setValueSilently(this.configuration.value);
}
});
80 changes: 38 additions & 42 deletions lib/core/alchemy.js
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ Alchemy.setMethod(function isProcessRunning(pid) {
*
* @author Jelle De Loecker <[email protected]>
* @since 0.5.0
* @version 1.3.1
* @version 1.4.0
*
* @param {Object} options
*/
Expand Down Expand Up @@ -527,41 +527,6 @@ Alchemy.setMethod(function startJaneway(options) {

this.Janeway.started = true;
this.Janeway.start(options);

if (this.settings.frontend.title) {
let title = this.settings.frontend.title;
this.Janeway.setTitle(title);
}

if (this.settings.sessions.janeway_menu) {

let session_menu = this.Janeway.addIndicator('⌨ ');

if (!session_menu.addItem) {
return session_menu.remove();
}

session_menu.addItem({
title : 'Current active browser sessions:',
weight : Infinity,
}, () => {
console.log('All sessions:', this.sessions);
});

this.Janeway.session_menu = session_menu;
}

if (this.settings.performance.janeway_lag_menu) {
let lag_menu = this.Janeway.addIndicator('0 ms');

setInterval(() => {
lag_menu.setIcon(this.lagInMs() + ' ms');
this.Janeway.redraw();
}, 900).unref();

this.Janeway.lag_menu = lag_menu;
}

});

/**
Expand Down Expand Up @@ -649,13 +614,44 @@ Alchemy.setMethod(function setSetting(path, value) {
*/
Alchemy.setMethod(function getSetting(path) {

let value = this.system_settings.getPath(path);
let value = this.getSettingValueInstance(path);

if (value) {
return value.get();
}
});

/**
* Get a setting value instance
*
* @author Jelle De Loecker <[email protected]>
* @since 1.4.0
* @version 1.4.0
*
* @param {string} path The path of the setting (without system prefix)
*/
Alchemy.setMethod(function getSettingValueInstance(path) {
return this.system_settings.getPath(path);
});

/**
* Execute the action linked to a setting
*
* @author Jelle De Loecker <[email protected]>
* @since 1.4.0
* @version 1.4.0
*
* @param {string} path The path of the setting (without system prefix)
*/
Alchemy.setMethod(function executeSetting(path) {

let value = this.getSettingValueInstance(path);

if (value) {
return value.executeAction();
}
});

/**
* Load the initial hard-coded settings
*
Expand Down Expand Up @@ -683,7 +679,7 @@ Alchemy.setMethod(function loadSettings() {
let value = require(default_path);
system.setDefaultValue(value);
} catch (err) {
this.setSetting('no_default_file', true);
this.setSetting('no_default_file', default_path);
}

// Generate the path to the local settings file
Expand All @@ -695,7 +691,7 @@ Alchemy.setMethod(function loadSettings() {
local = require(local_path);
} catch(err) {
local = {};
this.setSetting('no_local_file', true);
this.setSetting('no_local_file', local_path);
}

// Default to the 'dev' environment
Expand All @@ -721,13 +717,13 @@ Alchemy.setMethod(function loadSettings() {
// Get the config
try {
let value = require(env_path);
system.setValue(value);
system.setValueSilently(value);
} catch(err) {
this.setSetting('no_env_file', true);
this.setSetting('no_env_file', env_path);
}

// And now overlay the final local settings
system.setValue(local);
system.setValueSilently(local);

if (!settings.name) {
this.setSetting('name', this.package.name);
Expand Down
Loading

0 comments on commit bd125f8

Please sign in to comment.