Skip to content

Commit

Permalink
Export readonly configs in Tethr.exportConfigs
Browse files Browse the repository at this point in the history
  • Loading branch information
baku89 committed Oct 27, 2023
1 parent fab8162 commit 6683f70
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 11 deletions.
17 changes: 7 additions & 10 deletions src/Tethr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
Aperture,
BatteryLevel,
ConfigName,
ConfigNameList,
ConfigType,
DriveMode,
ExposureMeteringMode,
Expand All @@ -18,10 +19,8 @@ import {
ISO,
ManualFocusOption,
WhiteBalance,
WritableConfigNameList,
} from './configs'
import {TethrObject} from './TethrObject'
import {isntNil} from './util'

export type OperationResultStatus =
| 'ok'
Expand Down Expand Up @@ -127,25 +126,23 @@ export abstract class Tethr
*/
async exportConfigs(): Promise<Partial<ConfigType>> {
const configs = await Promise.all(
WritableConfigNameList.map(async name => {
ConfigNameList.flatMap(async name => {
const desc = await this.getDesc(name)
if (!desc.writable || desc.value === null) return null
return [name, desc.value] as const
if (desc.value === null) return []
return [[name, desc.value] as const]
})
)

const entries = configs.filter(isntNil)

return Object.fromEntries(entries)
return Object.fromEntries(configs)
}

/**
* Apply all writable configs.
*/
async importConfigs(configs: Partial<ConfigType>) {
const sortedConfigs = Object.entries(configs).sort(([a], [b]) => {
const ai = WritableConfigNameList.indexOf(a as ConfigName)
const bi = WritableConfigNameList.indexOf(b as ConfigName)
const ai = ConfigNameList.indexOf(a as ConfigName)
const bi = ConfigNameList.indexOf(b as ConfigName)
return ai - bi
}) as [ConfigName, ConfigType[ConfigName]][]

Expand Down
2 changes: 1 addition & 1 deletion src/configs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ export type ConfigName = keyof ConfigType
/**
* All settable config list for `Tethr.{exportConfigs,importConfigs}()`. The list is sorted to ensure that all configs are correctly set. For example, `exposureMode` must be set before `shutterSpeed` and `aperture`.
*/
export const WritableConfigNameList: ConfigName[] = [
export const ConfigNameList: ConfigName[] = [
// Exposure settings
'exposureMode',
'aperture',
Expand Down

0 comments on commit 6683f70

Please sign in to comment.