From 4e47102f56e7724a854880fd0d2ce82f5f9900bb Mon Sep 17 00:00:00 2001 From: Mykola Mokhnach Date: Mon, 9 Dec 2019 15:58:41 +0100 Subject: [PATCH] refactor: Change the setting update validation, so we don't print unnecessary warnings (#372) --- lib/basedriver/device-settings.js | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/lib/basedriver/device-settings.js b/lib/basedriver/device-settings.js index 9d5e49e27..77fe361e9 100644 --- a/lib/basedriver/device-settings.js +++ b/lib/basedriver/device-settings.js @@ -73,23 +73,25 @@ class DeviceSettings { // 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'); + if (!_.isPlainObject(newSettings)) { + throw new Error(`Settings update should be called with valid JSON. Got ` + + `${JSON.stringify(newSettings)} instead`); } - 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. Valid settings: ${_.keys(this._settings)}`); - } - 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'); + for (const prop of _.keys(newSettings)) { + if (!_.isUndefined(this._settings[prop])) { + if (this._settings[prop] === newSettings[prop]) { + log.debug(`The value of '${prop}' setting did not change. Skipping the update for it`); + continue; } } + // update setting only when there is updateSettings defined. + if (_.isFunction(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 on '${this.constructor.name}'`); + } } }