From b7e540d8da2bd435a3af93f2c686b0cd4d62634a Mon Sep 17 00:00:00 2001 From: Julian Waller Date: Wed, 16 Feb 2022 19:14:19 +0000 Subject: [PATCH] fix: suppress errors when connection is inactive --- src/__mocks__/net.ts | 3 ++- src/hyperdeck.ts | 11 +++++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/__mocks__/net.ts b/src/__mocks__/net.ts index 08e50b1..ca51127 100644 --- a/src/__mocks__/net.ts +++ b/src/__mocks__/net.ts @@ -53,10 +53,11 @@ export class Socket extends EventEmitter { }, 3) } public write(buff: Buffer, encoding = 'utf8'): void { - expect(this.expectedWrites).not.toHaveLength(0) + if (this.expectedWrites.length === 0) throw new Error('Got unexpected write') const w = this.expectedWrites.shift() if (w) { + // eslint-disable-next-line jest/no-standalone-expect expect(buff).toEqual(w.call) this.emit('data', w.response) } diff --git a/src/hyperdeck.ts b/src/hyperdeck.ts index f68e8ea..b6fefd4 100644 --- a/src/hyperdeck.ts +++ b/src/hyperdeck.ts @@ -184,7 +184,12 @@ export class Hyperdeck extends EventEmitter { .then(() => { this._logDebug('ping: setting up') this._pingInterval = setInterval(() => { - if (this.connected) this._performPing().catch((e) => this.emit('error', e)) + if (this.connected) + this._performPing().catch((e) => { + if (this._connectionActive) { + this.emit('error', e) + } + }) }, this._pingPeriod) }) .then(() => c) @@ -199,7 +204,9 @@ export class Hyperdeck extends EventEmitter { .catch((e) => { this._connected = false this.socket.destroy() - this.emit('error', 'connection failed', e) + if (this._connectionActive) { + this.emit('error', 'connection failed', e) + } this._log('connection failed', e) this._triggerRetryConnection()