From bf6ac0c02a798a2e3a70d4211f450ca69d27b44b Mon Sep 17 00:00:00 2001 From: Chris Watts Date: Mon, 25 Nov 2024 14:58:39 +0000 Subject: [PATCH] Emit stream events in stream_connection.ts When using the managedwriter, I found that the connection often needs to reconnect, but my calling code was unable to determine when this happens. My workaround hack was to do `(stream as any)._connection.on('close', ...` and to reregister the listener every time the connection changes, but it would be great to listen to an event directly. In this commit I have forwarded the various events from the underlying connection, except 'close' is only called when the stream_connection has been instructed to close, and a new event 'reconnect' is emitted when the stream has been closed and reopened. --- src/managedwriter/stream_connection.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/managedwriter/stream_connection.ts b/src/managedwriter/stream_connection.ts index da312f27..6d0e5de0 100644 --- a/src/managedwriter/stream_connection.ts +++ b/src/managedwriter/stream_connection.ts @@ -107,12 +107,15 @@ export class StreamConnection extends EventEmitter { }); this._connection.on('pause', () => { this.trace('connection paused'); + this.emit('pause'); }); this._connection.on('resume', () => { this.trace('connection resumed'); + this.emit('resume'); }); this._connection.on('end', () => { this.trace('connection ended'); + this.emit('end'); }); } @@ -364,6 +367,7 @@ export class StreamConnection extends EventEmitter { ); this.close(); this.open(); + this.emit('reconnect'); } /** @@ -375,6 +379,7 @@ export class StreamConnection extends EventEmitter { } this._connection.end(); this._connection.removeAllListeners(); + this.emit('close'); this._connection.destroy(); this._connection = null; }