diff --git a/src/TethrPTPUSB/TethrPTPUSB.ts b/src/TethrPTPUSB/TethrPTPUSB.ts index 4efee7d..0302705 100644 --- a/src/TethrPTPUSB/TethrPTPUSB.ts +++ b/src/TethrPTPUSB/TethrPTPUSB.ts @@ -85,7 +85,10 @@ export class TethrPTPUSB extends Tethr { EventCode.DevicePropChanged, this.onDevicePropChanged ) - this.device.on('disconnect', () => this.emit('disconnect')) + this.device.on('disconnect', () => { + this._opened = false + this.emit('disconnect') + }) window.addEventListener('beforeunload', async () => { await this.close() diff --git a/src/TethrPTPUSB/TethrSigma.ts b/src/TethrPTPUSB/TethrSigma.ts index 6aa7c2b..9b3060e 100644 --- a/src/TethrPTPUSB/TethrSigma.ts +++ b/src/TethrPTPUSB/TethrSigma.ts @@ -132,9 +132,8 @@ const SigmaExpirationMs = 16 const SigmaCheckConfigIntervalMs = 1000 export class TethrSigma extends TethrPTPUSB { - private liveviewEnabled = false - private isCapturing = false - private checkPropChangedTimerId!: ReturnType + #liveviewEnabled = false + #isCapturing = false async open() { await super.open() @@ -151,16 +150,22 @@ export class TethrSigma extends TethrPTPUSB { firmwareVersion: {tag: 3, type: IFDType.Ascii}, communiationVersion: {tag: 5, type: IFDType.Float}, }) - this.checkPropChangedTimerId = setInterval(() => { - if (this.isCapturing) return - this.checkConfigChanged() - }, SigmaCheckConfigIntervalMs) + + const checkPropChangedInterval = () => { + if (!this.opened) return + + if (!this.#isCapturing) { + this.checkConfigChanged() + } + + setTimeout(checkPropChangedInterval, SigmaCheckConfigIntervalMs) + } + + checkPropChangedInterval() } async close(): Promise { await this.stopLiveview() - clearInterval(this.checkPropChangedTimerId) - await super.close() } @@ -800,7 +805,7 @@ export class TethrSigma extends TethrPTPUSB { async getLiveviewEnabledDesc(): Promise> { return { writable: false, - value: this.liveviewEnabled, + value: this.#liveviewEnabled, } } @@ -959,7 +964,7 @@ export class TethrSigma extends TethrPTPUSB { async takePhoto({doDownload = true}: TakePhotoOption = {}): Promise< OperationResult > { - this.isCapturing = true + this.#isCapturing = true const captureId = await this.executeSnapCommand( SnapCaptureMode.NonAFCapture, @@ -1011,7 +1016,7 @@ export class TethrSigma extends TethrPTPUSB { await this.clearImageDBAll() - this.isCapturing = false + this.#isCapturing = false return {status: 'ok', value: picts} } @@ -1056,14 +1061,14 @@ export class TethrSigma extends TethrPTPUSB { const canvas = this.#ctx.canvas const ctx = this.#ctx - this.liveviewEnabled = true + this.#liveviewEnabled = true this.emit('liveviewEnabledChanged', await this.getDesc('liveviewEnabled')) const updateFrame = async () => { - if (!this.liveviewEnabled) return + if (!this.#liveviewEnabled || !this.opened) return try { - if (this.isCapturing) return + if (this.#isCapturing) return const lvImage = await this.getLiveViewImage() @@ -1093,7 +1098,7 @@ export class TethrSigma extends TethrPTPUSB { } async stopLiveview(): Promise { - this.liveviewEnabled = false + this.#liveviewEnabled = false this.emit('liveviewStreamUpdate', null) this.emit('liveviewEnabledChanged', await this.getDesc('liveviewEnabled'))