Skip to content

Commit

Permalink
Emit stream events in stream_connection.ts
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
CJxD authored Nov 25, 2024
1 parent f1d6c28 commit bf6ac0c
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/managedwriter/stream_connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
}

Expand Down Expand Up @@ -364,6 +367,7 @@ export class StreamConnection extends EventEmitter {
);
this.close();
this.open();
this.emit('reconnect');
}

/**
Expand All @@ -375,6 +379,7 @@ export class StreamConnection extends EventEmitter {
}
this._connection.end();
this._connection.removeAllListeners();
this.emit('close');
this._connection.destroy();
this._connection = null;
}
Expand Down

0 comments on commit bf6ac0c

Please sign in to comment.