Skip to content

Commit

Permalink
fix(#100): update closure check logic to avoid error when closed corr…
Browse files Browse the repository at this point in the history
…ectly
  • Loading branch information
Seán Bryceland committed Jun 25, 2024
1 parent 2af260f commit 8409326
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/client/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,13 @@ export class Connection extends EventEmitter implements IConnection {
this._socket?.destroy()
this._socket?.end()

this.emit('close')

// Ensure no further reconnections are attempted
clearTimeout(this._connectionTimer)

// Emit close event on the socket
this.emit('close')

// Set closure state
this._readyState = ReadyState.CLOSED
}

Expand Down Expand Up @@ -334,9 +337,9 @@ export class Connection extends EventEmitter implements IConnection {
})

socket.on('close', () => {
if (this._readyState === ReadyState.CLOSING || (this._connectionTimer == null)) {
if (this._readyState === ReadyState.CLOSING) {
this._readyState = ReadyState.CLOSED
} else {
} else if (!(this._readyState === ReadyState.CLOSED && this._connectionTimer != null && (this._connectionTimer as unknown as { _destroyed: boolean })._destroyed)) {
connectionError = (connectionError != null) ? connectionError : new HL7FatalError('Socket closed unexpectedly by server.')
if (this._readyState === ReadyState.OPEN) {
this._onConnect = createDeferred(true)
Expand Down

0 comments on commit 8409326

Please sign in to comment.