fix(base-driver): allow subclass to define shape of settings object

The mixin approach prevented subclasses of BaseDriver from narrowing the type of `this.settings`.  Now, that's possible.

Also:
- removed an unused type argument from BD constructor
- and did some typedef aliasing
- remove unneeded type assertion from log mixin
This commit is contained in:
Christopher Hiller
2023-06-19 17:16:16 -07:00
parent ee9b2a30de
commit 3d614d6d41
5 changed files with 25 additions and 35 deletions

View File

@@ -1,6 +1,5 @@
import './event';
import './find';
import './log';
import './settings';
import './timeout';
import './execute';

View File

@@ -1,6 +1,6 @@
import {Constraints, Driver, ILogCommands, LogDefRecord} from '@appium/types';
import type {Constraints, Driver, ILogCommands} from '@appium/types';
import _ from 'lodash';
import {BaseDriver} from '../driver';
import type {BaseDriver} from '../driver';
import {mixin} from './mixin';
declare module '../driver' {
@@ -9,7 +9,7 @@ declare module '../driver' {
}
const LogCommands: ILogCommands = {
supportedLogTypes: <LogDefRecord>{},
supportedLogTypes: {},
async getLogTypes<C extends Constraints>(this: BaseDriver<C>) {
this.log.debug('Retrieving supported log types');

View File

@@ -1,26 +0,0 @@
import {BaseDriver} from '../driver';
import {Constraints, ISettingsCommands, StringRecord} from '@appium/types';
import {mixin} from './mixin';
declare module '../driver' {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
interface BaseDriver<C extends Constraints> extends ISettingsCommands {}
}
const SettingsCommands: ISettingsCommands = {
async updateSettings<C extends Constraints>(this: BaseDriver<C>, newSettings: StringRecord) {
if (!this.settings) {
this.log.errorAndThrow('Cannot update settings; settings object not found');
}
return await this.settings.update(newSettings);
},
async getSettings<C extends Constraints>(this: BaseDriver<C>) {
if (!this.settings) {
this.log.errorAndThrow('Cannot get settings; settings object not found');
}
return this.settings.getSettings();
},
};
mixin(SettingsCommands);

View File

@@ -9,7 +9,7 @@ import {errors} from '../protocol/errors';
export const MAX_SETTINGS_SIZE = 20 * 1024 * 1024; // 20 MB
/**
* @template {import('@appium/types').StringRecord} T
* @template {StringRecord} T
* @implements {IDeviceSettings<T>}
*/
export class DeviceSettings {
@@ -74,6 +74,10 @@ export class DeviceSettings {
export default DeviceSettings;
/**
* @template {import('@appium/types').StringRecord} [T=import('@appium/types').StringRecord]
* @typedef {import('@appium/types').StringRecord} StringRecord
*/
/**
* @template {StringRecord} [T=StringRecord]
* @typedef {import('@appium/types').IDeviceSettings<T>} IDeviceSettings
*/

View File

@@ -33,11 +33,10 @@ const ON_UNEXPECTED_SHUTDOWN_EVENT = 'onUnexpectedShutdown';
export class BaseDriver<
C extends Constraints,
CArgs extends StringRecord = StringRecord,
Settings extends StringRecord = StringRecord,
SessionData extends StringRecord = StringRecord
Settings extends StringRecord = StringRecord
>
extends DriverCore<C, Settings>
implements Driver<C, CArgs>
implements Driver<C, CArgs, Settings>
{
cliArgs: CArgs & ServerArgs;
@@ -380,6 +379,20 @@ export class BaseDriver<
return true;
}
async updateSettings(newSettings: Settings) {
if (!this.settings) {
this.log.errorAndThrow('Cannot update settings; settings object not found');
}
return await this.settings.update(newSettings);
}
async getSettings() {
if (!this.settings) {
this.log.errorAndThrow('Cannot get settings; settings object not found');
}
return this.settings.getSettings();
}
}
export * from './commands';