@appium/base-driver
Version:
Base driver class for Appium drivers
61 lines • 2.73 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DeviceSettings = exports.MAX_SETTINGS_SIZE = void 0;
const lodash_1 = __importDefault(require("lodash"));
const logger_1 = require("./logger");
const support_1 = require("@appium/support");
const errors_1 = require("../protocol/errors");
/**
* Maximum size (in bytes) of a given driver's settings object (which is internal to {@linkcode DeviceSettings}).
*/
exports.MAX_SETTINGS_SIZE = 20 * 1024 * 1024; // 20 MB
/**
* @template T - Settings object shape (string-keyed record)
*/
class DeviceSettings {
_settings;
_onSettingsUpdate;
/**
* Creates a _shallow copy_ of the `defaultSettings` parameter!
*
* @param defaultSettings - Initial settings (shallow-copied).
* @param onSettingsUpdate - Called when a setting is changed; receives (prop, newValue, curValue).
*/
constructor(defaultSettings = {}, onSettingsUpdate = async () => { }) {
this._settings = { ...defaultSettings };
this._onSettingsUpdate = onSettingsUpdate;
}
/**
* Calls updateSettings from implementing driver every time a setting is changed.
*
* @param newSettings - New settings to merge (must be plain object; total size remains bounded).
*/
async update(newSettings) {
if (!lodash_1.default.isPlainObject(newSettings)) {
throw new errors_1.errors.InvalidArgumentError(`Settings update should be called with valid JSON. Got ` +
`${JSON.stringify(newSettings)} instead`);
}
if (support_1.node.getObjectSize({ ...this._settings, ...newSettings }) >= exports.MAX_SETTINGS_SIZE) {
throw new errors_1.errors.InvalidArgumentError(`New settings cannot be applied, because the overall ` +
`object size exceeds the allowed limit of ${support_1.util.toReadableSizeString(exports.MAX_SETTINGS_SIZE)}`);
}
for (const prop in newSettings) {
if (!lodash_1.default.isUndefined(this._settings[prop])) {
if (this._settings[prop] === newSettings[prop]) {
logger_1.log.debug(`The value of '${prop}' setting did not change. Skipping the update for it`);
continue;
}
}
await this._onSettingsUpdate(prop, newSettings[prop], this._settings[prop]);
this._settings[prop] = newSettings[prop];
}
}
getSettings() {
return this._settings;
}
}
exports.DeviceSettings = DeviceSettings;
//# sourceMappingURL=device-settings.js.map