Skip to content

Commit

Permalink
Rename *Changed event with *Change
Browse files Browse the repository at this point in the history
  • Loading branch information
baku89 committed Oct 9, 2023
1 parent c32800f commit b0fa8e0
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 21 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,21 +179,21 @@ interface ConfigDesc<ConfigType> {

### Watching Config Changes

Whenever a config is changed, a correspoinding `configNameChanged` event will be fired. Since Tethr class inherits from [EventEmitter](https://github.com/primus/eventemitter3), you can monitor the value change using the following pattern:
Whenever a config is changed, a correspoinding `configNameChange` event will be fired. Since Tethr class inherits from [EventEmitter](https://github.com/primus/eventemitter3), you can monitor the value change using the following pattern:

```ts
tethr.on('apertureChanged', (newValue: Aperture) => {
tethr.on('apertureChange', (newValue: Aperture) => {
// Handle the value change here
})

// Or watch once
tethr.once('shutterSpeedChanged', callback)
tethr.once('shutterSpeedChange', callback)

// Delete event listener
tethr.off('apertureChanged', callback)
tethr.off('apertureChange', callback)
```

An event `configNameChanged` is triggered in the the following:
An event `configNameChange` is triggered in the the following:

- When you manually set the value of the `configName`.
- When you modify other configs that have a side effect on the value of `configName`. For example, setting the `whiteBalance` to `'auto'` will make the `colorTemperature` read-only.
Expand Down
6 changes: 3 additions & 3 deletions src/Tethr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export interface TakePhotoOption {
}

type EventTypes = {
[N in ConfigName as `${N}Changed`]: ConfigDesc<ConfigType[N]>
[N in ConfigName as `${N}Change`]: ConfigDesc<ConfigType[N]>
} & {
change: [name: ConfigName, value: ConfigType[ConfigName]]
disconnect: void
Expand Down Expand Up @@ -106,8 +106,8 @@ export abstract class Tethr
emit(eventName: keyof EventTypes, ...args: any[]) {
const ret = super.emit(eventName, ...args)

if (eventName.endsWith('Changed')) {
this.emit('change', eventName.slice(0, -7) as ConfigName, args[0])
if (eventName.endsWith('Change')) {
this.emit('change', eventName.slice(0, -6) as ConfigName, args[0])
}

return ret
Expand Down
2 changes: 1 addition & 1 deletion src/TethrPTPUSB/TethrPTPUSB.ts
Original file line number Diff line number Diff line change
Expand Up @@ -706,7 +706,7 @@ export class TethrPTPUSB extends Tethr {
if (!name) return

const desc = await this.getDesc(name)
this.emit(`${name}Changed`, desc)
this.emit(`${name}Change`, desc)
}

protected getConfigNameByCode(code: number) {
Expand Down
6 changes: 3 additions & 3 deletions src/TethrPTPUSB/TethrPanasonic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,7 @@ export class TethrPanasonic extends TethrPTPUSB {
})

this.liveviewEnabled = true
this.emit('liveviewEnabledChanged', await this.getDesc('liveviewEnabled'))
this.emit('liveviewEnabledChange', await this.getDesc('liveviewEnabled'))

const updateFrame = async () => {
if (!this.liveviewEnabled) return
Expand Down Expand Up @@ -629,7 +629,7 @@ export class TethrPanasonic extends TethrPTPUSB {
})

this.liveviewEnabled = false
this.emit('liveviewEnabledChanged', await this.getDesc('liveviewEnabled'))
this.emit('liveviewEnabledChange', await this.getDesc('liveviewEnabled'))

return {status: 'ok'}
}
Expand Down Expand Up @@ -883,7 +883,7 @@ export class TethrPanasonic extends TethrPTPUSB {

for (const config of configs) {
const desc = await this.getDesc(config)
this.emit(`${config}Changed`, desc)
this.emit(`${config}Change`, desc)
}
}

Expand Down
18 changes: 9 additions & 9 deletions src/TethrPTPUSB/TethrSigma.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,17 +161,17 @@ export class TethrSigma extends TethrPTPUSB {
})
*/

const checkPropChangedInterval = () => {
const checkPropChangeInterval = () => {
if (!this.opened) return

if (!this.#isCapturing) {
this.checkConfigChanged()
this.checkConfigChange()
}

setTimeout(checkPropChangedInterval, SigmaCheckConfigIntervalMs)
setTimeout(checkPropChangeInterval, SigmaCheckConfigIntervalMs)
}

checkPropChangedInterval()
checkPropChangeInterval()
}

async close(): Promise<void> {
Expand Down Expand Up @@ -1266,7 +1266,7 @@ export class TethrSigma extends TethrPTPUSB {
const ctx = this.#ctx

this.#liveviewEnabled = true
this.emit('liveviewEnabledChanged', await this.getDesc('liveviewEnabled'))
this.emit('liveviewEnabledChange', await this.getDesc('liveviewEnabled'))

const updateFrame = async () => {
if (!this.#liveviewEnabled || !this.opened) return
Expand Down Expand Up @@ -1304,7 +1304,7 @@ export class TethrSigma extends TethrPTPUSB {
async stopLiveview(): Promise<OperationResult> {
this.#liveviewEnabled = false
this.emit('liveviewStreamUpdate', null)
this.emit('liveviewEnabledChanged', await this.getDesc('liveviewEnabled'))
this.emit('liveviewEnabledChange', await this.getDesc('liveviewEnabled'))

return {status: 'ok'}
}
Expand Down Expand Up @@ -1623,7 +1623,7 @@ export class TethrSigma extends TethrPTPUSB {
await sleep(25)
}

await this.checkConfigChanged()
await this.checkConfigChange()

return {status: 'ok'}
}
Expand Down Expand Up @@ -1721,14 +1721,14 @@ export class TethrSigma extends TethrPTPUSB {

#prevConfigValue = new Map<ConfigName, ConfigDesc<any>>()

private async checkConfigChanged() {
private async checkConfigChange() {
for (const name of ConfigListSigma) {
const desc = await this.getDesc(name)

const prev = this.#prevConfigValue.get(name)

if (prev !== undefined && !isEqual(prev, desc)) {
this.emit(`${name}Changed`, desc)
this.emit(`${name}Change`, desc)
}

this.#prevConfigValue.set(name, desc)
Expand Down

0 comments on commit b0fa8e0

Please sign in to comment.