Settings management library for AirDC++ JavaScript extensions
npm install airdcpp-extension-settingsSettingsManager(socket, options)
socket (object, required)
options (object, required)
load method for information about possible migration handling. |
load(dataMigrationCallback)
dataMigrationCallback (function, optional)
js
// Settings migration callback
const migrate = (loadedConfigVersion, loadedData) => {
if (loadedConfigVersion <= 1) {
throw Migration for settings version ${loadedConfigVersion} is not supported;
}
if (loadedConfigVersion === 2) {
// Perform the required conversions
return Object.keys(loadedData).reduce((reduced, key) => {
if (key === 'message_type' && loadedData[key] === 'private') {
// The value 'private' has been renamed to 'private_chat'
reduced[key] = 'private_chat';
} else {
reduced[key] = loadedData[key];
}
return reduced;
}, {})
}
// Return as it is
return loadedData;
};
// Load
await settings.load(migrate);
`
Return value
Promise that will return after all tasks have been completed.
$3
getValue(key)
Returns the current setting value.
Usage example
`js
const showSpam = settings.getValue('show_spam');
`
$3
setValue(key, value)
Update value of the setting.
Return value
Promise that will be resolved after the value has been updated in the API.
Usage example
`js
try {
await settings.setValue('show_spam', false);
} catch (err) {
socket.logger.error(Failed to update settings value: ${err.message});
}
`
$3
onValuesUpdated(callback)
Set listener for updated setting values.
The callback function will receive the updated settings value object (settingKey -> newValue) an argument (it won't contain unchanged values). The listener will also be fired during initial loading with all loaded settings.
Usage example
`js
settings.onValuesUpdated = updatedValues => {
if (updatedValues.hasOwnProperty('search_interval')) {
resetSearchInterval();
}
};
``