Files
appium/lib/basedriver/device-settings.js
T
Jonathan Lipps e2032ebeba warn when a setting does not exist (#127)
* commit npm 5 package-lock

* warn if someone attempts to set a setting which has no default (fix appium/appium#3585)
2017-07-19 07:36:16 -04:00

39 lines
1.2 KiB
JavaScript

import _ from 'lodash';
import log from './logger';
class DeviceSettings {
constructor (defaultSettings = {}, onSettingsUpdate = null) {
this._settings = Object.assign({}, defaultSettings);
this.onSettingsUpdate = onSettingsUpdate;
}
// calls updateSettings from implementing driver every time a setting is changed.
async update (newSettings) {
if (!_.isObject(newSettings)) {
throw new Error('Settings update should be called with valid JSON');
}
for (let prop of _.keys(newSettings)) {
if (_.isUndefined(this._settings[prop])) {
log.warn(`Didn't know about setting '${prop}'. Are you sure you ` +
`spelled it correctly? Proceeding anyway.`);
}
if (this._settings[prop] !== newSettings[prop]) {
// update setting only when there is updateSettings defined.
if (this.onSettingsUpdate) {
await this.onSettingsUpdate(prop, newSettings[prop], this._settings[prop]);
this._settings[prop] = newSettings[prop];
} else {
log.errorAndThrow('Unable to update settings; onSettingsUpdate method not found');
}
}
}
}
getSettings () {
return this._settings;
}
}
export default DeviceSettings;